Skip to content
Snippets Groups Projects
Commit d64df1d2 authored by ribeaudc's avatar ribeaudc
Browse files

[LMS-486] add: - 'AnnotationUtils.getAnnotatedParameters' and its test.

SVN: 7188
parent 2107ffaa
No related branches found
No related tags found
No related merge requests found
......@@ -116,4 +116,87 @@ public final class AnnotationUtils
}
return list;
}
/**
* From the list of given annotations tries to return the one of given type.
*
* @return <code>null</code> if not found.
*/
public final static <A extends Annotation> A tryGetAnnotation(final Annotation[] annotations,
final Class<A> annotationType)
{
assert annotations != null : "Unspecified annotations";
assert annotationType != null : "Unspecified annotation type";
for (final Annotation annotation : annotations)
{
if (annotation.annotationType().equals(annotationType))
{
return annotationType.cast(annotation);
}
}
return null;
}
/**
* Returns a list of method parameters where given <var>annotation</var> could be found.
*
* @return never <code>null</code> but could return an empty list.
*/
public final static <A extends Annotation> List<Parameter<A>> getAnnotatedParameters(
final Method method, final Class<A> annotationType)
{
assert method != null : "Unspecified method";
assert annotationType != null : "Unspecified annotation type";
final Annotation[][] annotations = method.getParameterAnnotations();
final Class<?>[] types = method.getParameterTypes();
final List<Parameter<A>> list = new ArrayList<Parameter<A>>();
for (int i = 0; i < types.length; i++)
{
final Class<?> type = types[i];
final A annotationOrNull = tryGetAnnotation(annotations[i], annotationType);
if (annotationOrNull != null)
{
list.add(new Parameter<A>(i, type, annotationOrNull));
}
}
return list;
}
//
// Helper classes
//
public final static class Parameter<A extends Annotation>
{
/**
* This parameter index in the list of method arguments.
*/
private final int index;
private final Class<?> type;
private final A annotation;
Parameter(final int index, final Class<?> type, final A annotation)
{
this.index = index;
this.type = type;
this.annotation = annotation;
}
public final int getIndex()
{
return index;
}
public final Class<?> getType()
{
return type;
}
public final A getAnnotation()
{
return annotation;
}
}
}
......@@ -17,6 +17,7 @@
package ch.systemsx.cisd.common.utilities;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.fail;
import java.lang.reflect.Field;
......@@ -26,6 +27,7 @@ import java.util.List;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.annotation.BeanProperty;
import ch.systemsx.cisd.common.utilities.AnnotationUtils.Parameter;
/**
* Test cases for the {@link AnnotationUtils} class.
......@@ -35,6 +37,20 @@ import ch.systemsx.cisd.common.annotation.BeanProperty;
public final class AnnotationUtilsTest
{
private final static Method getDoSomethingMethod()
{
final Method[] methods = A.class.getDeclaredMethods();
for (final Method method : methods)
{
if (method.getName().equals("doSomething"))
{
return method;
}
}
fail();
return null;
}
@Test
public final void testGetAnnotatedFieldList()
{
......@@ -55,14 +71,15 @@ public final class AnnotationUtilsTest
@Test
public final void testGetAnnotatedMethodList()
{
boolean fail = true;
try
{
AnnotationUtils.getAnnotatedMethodList(null, null);
fail("Null value not accepted.");
} catch (final AssertionError e)
{
// Nothing to do here.
fail = false;
}
assertFalse(fail);
List<Method> methods = AnnotationUtils.getAnnotatedMethodList(A.class, BeanProperty.class);
assertEquals(1, methods.size());
methods = AnnotationUtils.getAnnotatedMethodList(B.class, BeanProperty.class);
......@@ -73,6 +90,31 @@ public final class AnnotationUtilsTest
}
}
@SuppressWarnings("unchecked")
@Test
public final void testGetAnnotatedParameters()
{
boolean fail = true;
try
{
AnnotationUtils.getAnnotatedParameters(null, null);
} catch (final AssertionError e)
{
fail = false;
}
assertFalse(fail);
final Method method = getDoSomethingMethod();
List<?> annotatedParameters =
AnnotationUtils.getAnnotatedParameters(method, BeanProperty.class);
assertEquals(0, annotatedParameters.size());
annotatedParameters = AnnotationUtils.getAnnotatedParameters(method, Deprecated.class);
assertEquals(1, annotatedParameters.size());
final Parameter<Deprecated> parameter = (Parameter<Deprecated>) annotatedParameters.get(0);
assertEquals(Deprecated.class, parameter.getAnnotation().annotationType());
assertEquals(Object.class, parameter.getType());
assertEquals(2, parameter.getIndex());
}
//
// Helper classes
//
......@@ -91,6 +133,10 @@ public final class AnnotationUtilsTest
this.a = a;
}
void doSomething(final Object c, final String d, @Deprecated
final Object e)
{
}
}
private final static class B extends A
......@@ -110,4 +156,4 @@ public final class AnnotationUtilsTest
this.a = a;
}
}
}
}
\ No newline at end of file
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