Skip to content
Snippets Groups Projects
Commit 07a46c73 authored by brinn's avatar brinn
Browse files

add: method BeanUtils.fillBean(), unit tests and annotations

SVN: 664
parent ec520524
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation that allows to override the name mapping of beans, i.e. that a setter requires a getter on the source
* bean with same name.
*
* @author Bernd Rinn
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface BeanNameMapping
{
/**
* The names to look for among the getters instead of the setter name.
*/
String[] names();
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
/**
* An annotation that defines the type of collection and the type of elements to use when creating the collection in a
* bean context.
*
* @author Bernd Rinn
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface CollectionMapping
{
/**
* The concrete class to use as a collection.
*/
Class<? extends Collection> collectionClass();
/**
* The class to use as the elements of the collection (since the generics type isn't known at run time).
*/
Class<?> elementClass();
}
...@@ -18,9 +18,7 @@ package ch.systemsx.cisd.common.utilities; ...@@ -18,9 +18,7 @@ package ch.systemsx.cisd.common.utilities;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -186,81 +184,4 @@ public final class ClassUtils ...@@ -186,81 +184,4 @@ public final class ClassUtils
.size()]), new ProxyingInvocationHandler(objectToProxy)); .size()]), new ProxyingInvocationHandler(objectToProxy));
} }
private static boolean isNull(Object objectToCheck)
{
return (objectToCheck instanceof Number) && ((Number) objectToCheck).longValue() == 0;
}
/**
* Checks the list of bean objects item by item for public getters which return <code>null</code> or 0.
*
* @param beanListToCheck The list of beans to check. Can be <code>null</code>.
* @return <var>beanListToCheck</var> (the parameter itself)
* @see #checkGettersNotNull(Object)
* @throws IllegalStateException If at least one of the public getters returns <code>null</code> or 0.
*/
public final static <T> List<T> checkGettersNotNull(List<T> beanListToCheck)
{
if (beanListToCheck == null)
{
return beanListToCheck;
}
for (Object bean : beanListToCheck)
{
checkGettersNotNull(bean);
}
return beanListToCheck;
}
/**
* Checks bean object for public getters which return <code>null</code> or 0.
*
* @param beanToCheck The bean to check. Can be <code>null</code>. Must not be an array type.
* @return <var>beanToCheck</var> (the parameter itself)
* @throws IllegalArgumentException If the <var>beanToCheck</var> is an array type.
* @throws IllegalStateException If at least one of the public getters returns <code>null</code> or 0.
*/
public final static <T> T checkGettersNotNull(T beanToCheck)
{
if (beanToCheck == null)
{
return beanToCheck;
}
if (beanToCheck.getClass().isArray())
{
throw new IllegalArgumentException("Arrays are not supported.");
}
for (Method method : beanToCheck.getClass().getMethods())
{
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0
&& Modifier.isPublic(method.getModifiers()))
{
try
{
final Object result = method.invoke(beanToCheck, new Object[0]);
if (result == null)
{
throw new IllegalStateException("Method '" + method.getName() + "' returns null.");
} else if (isNull(result))
{
throw new IllegalStateException("Method '" + method.getName() + "' returns 0.");
}
} catch (InvocationTargetException ex)
{
final Throwable cause = ex.getCause();
if (cause instanceof Error)
{
throw (Error) cause;
}
throw CheckedExceptionTunnel.wrapIfNecessary((Exception) cause);
} catch (IllegalAccessException ex)
{
// Can't happen since we checked for isAccessible()
throw new Error("Cannot call method '" + method.getName() + "'.");
}
}
}
return beanToCheck;
}
} }
...@@ -16,12 +16,8 @@ ...@@ -16,12 +16,8 @@
package ch.systemsx.cisd.common.utilities; package ch.systemsx.cisd.common.utilities;
import static org.testng.AssertJUnit.*; import static org.testng.AssertJUnit.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.testng.AssertJUnit;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
...@@ -59,83 +55,4 @@ public final class ClassUtilsTest ...@@ -59,83 +55,4 @@ public final class ClassUtilsTest
} }
} }
private static class SimpleBean
{
private final int number;
private final String string;
SimpleBean(int number, String string)
{
this.number = number;
this.string = string;
}
public int getNumber()
{
return number;
}
public String getString()
{
return string;
}
String getIgnoreThisBecauseItIsNotPublic()
{
AssertJUnit.fail("Should be ignore because not public");
return null;
}
}
@Test
public void testCheckGettersForNullOK()
{
final SimpleBean bean = new SimpleBean(1, "");
assert ClassUtils.checkGettersNotNull(bean) == bean;
}
@Test
public void testCheckGettersForNullOKNullBean()
{
assertNull(ClassUtils.checkGettersNotNull(null));
}
@Test(expectedExceptions = IllegalStateException.class)
public void testCheckGettersForNullStringNull()
{
final SimpleBean bean = new SimpleBean(1, null);
ClassUtils.checkGettersNotNull(bean);
}
@Test(expectedExceptions = IllegalStateException.class)
public void testCheckGettersForNullInt0()
{
final SimpleBean bean = new SimpleBean(0, "test");
ClassUtils.checkGettersNotNull(bean);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testCheckGettersForNullForbiddenArray()
{
ClassUtils.checkGettersNotNull(new Object[0]);
}
@Test
public void testCheckGettersForNullListOK()
{
final SimpleBean bean1 = new SimpleBean(1, "test");
final SimpleBean bean2 = new SimpleBean(5, "test2");
final List<SimpleBean> beanList = Arrays.asList(bean1, bean2);
assert ClassUtils.checkGettersNotNull(beanList) == beanList;
}
@Test(expectedExceptions = IllegalStateException.class)
public void testCheckGettersForNullListInt0()
{
final SimpleBean bean1 = new SimpleBean(1, "test");
final SimpleBean bean2 = new SimpleBean(0, "test2");
ClassUtils.checkGettersNotNull(Arrays.asList(bean1, bean2));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment