ReflectionUtils { @SuppressWarnings("unchecked") public static <T> T findAnnotation(Annotation[] annotations, Class<T> annotationType) { for (Annotation annotation : annotations) { if (annotation.annotationType().equals(annotationType)) { return (T) annotation; } } return null; } private  ReflectionUtils(); static Class getWrapperClassForPrimitive(Class primitiveClass); static Class getPrimitiveForWrapper(Class wrapperClass); static Type[] getParameterTypes(Class implementation, Class parametrizedInterface); static T chooseBestClassStrategy(Map<Class, T> strategies, Class requiredType); @SuppressWarnings("unchecked") static T findAnnotation(Annotation[] annotations, Class<T> annotationType); static final int COLLECTION_ELEMENT_PARAMETER_INDEX; static final int MAP_ELEMENT_PARAMETER_INDEX; }
@Test public void shouldFindIdenticalAnnotation() throws NoSuchMethodException { Method method = MockClientActionClass.class.getMethod(MockClientActionClass.SIMPLE_ACTION_NAME, String.class, VerySimpleMockBean.class, UserContext.class); Annotation[] annotations = method.getParameterAnnotations()[2]; Ignore annotationToFind = ReflectionUtils.findAnnotation(annotations, Ignore.class); assertNotNull(annotationToFind); }
@Test public void shouldNotFindIdenticalAnnotation() throws NoSuchMethodException { Method method = MockClientActionClass.class.getMethod(MockClientActionClass.SIMPLE_ACTION_NAME, String.class, VerySimpleMockBean.class, UserContext.class); Annotation[] annotations = method.getParameterAnnotations()[1]; Ignore annotationToFind = ReflectionUtils.findAnnotation(annotations, Ignore.class); assertNull(annotationToFind); }