Author: tchemit Date: 2011-01-25 12:34:29 +0100 (Tue, 25 Jan 2011) New Revision: 2047 Url: http://nuiton.org/repositories/revision/nuiton-utils/2047 Log: Evolution #1235: Add a new method in ReflectUtil to find a given annotation on all fields of a class Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ReflectUtil.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ReflectUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ReflectUtil.java 2011-01-25 07:05:55 UTC (rev 2046) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ReflectUtil.java 2011-01-25 11:34:29 UTC (rev 2047) @@ -28,13 +28,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import static org.nuiton.i18n.I18n._; @@ -269,7 +272,27 @@ // not a primitive type return type; + } + /** + * Obtain all the fields with the given annotation type. + * + * @param objectClass the type to scan + * @param annotationClass the type of annotation to scan + * @param <A> type of annotation to scan + * @return the dictionnary of fields with the given annotation + * @since 2.0 + */ + public static <A extends Annotation> Map<Field, A> getFieldAnnotation(Class<?> objectClass, + Class<A> annotationClass) { + Map<Field, A> result = new HashMap<Field, A>(); + for (Field field : objectClass.getDeclaredFields()) { + A annotation = field.getAnnotation(annotationClass); + if (annotation != null) { + result.put(field, annotation); + } + } + return result; } protected static Method getDeclaredMethod(Class<?> klass,