@SuppressWarnings("unchecked") private static <A extends Annotation> void findRepeatableAnnotations( Annotation[] candidates, Class<A> annotationType, Class<? extends Annotation> containerType, boolean inherited, Set<A> found, Set<Annotation> visited) { for (Annotation candidate : candidates) { if (!isInJavaLangAnnotationPackage(candidate) && visited.add(candidate)) { // Exact match? if (candidate.annotationType().equals(annotationType)) { found.add(annotationType.cast(candidate)); } // Container? else if (candidate.annotationType().equals(containerType)) { // Note: it's not a legitimate container annotation if it doesn't declare // a 'value' attribute that returns an array of the contained annotation type. // Thus we proceed without verifying this assumption. Method method = ReflectionUtils.getMethod(containerType, "value").get(); Annotation[] containedAnnotations = (Annotation[]) ReflectionUtils.invokeMethod(method, candidate); found.addAll((Collection<? extends A>) asList(containedAnnotations)); } // Otherwise search recursively through the meta-annotation hierarchy... else { findRepeatableAnnotations( candidate.annotationType(), annotationType, containerType, inherited, found, visited); } } } }
@Test public void invokeMethod() throws Exception { String rob = "Rob Harrop"; TestObject bean = new TestObject(); bean.setName(rob); Method getName = TestObject.class.getMethod("getName", (Class[]) null); Method setName = TestObject.class.getMethod("setName", String.class); Object name = ReflectionUtils.invokeMethod(getName, bean); assertEquals("Incorrect name returned", rob, name); String juergen = "Juergen Hoeller"; ReflectionUtils.invokeMethod(setName, bean, juergen); assertEquals("Incorrect name set", juergen, bean.getName()); }
protected static String doGetPath(Object resource) { return (String) ReflectionUtils.invokeMethod(VIRTUAL_FILE_METHOD_GET_PATH_NAME, resource); }