检索带注释的字段
/*n o w j a v a . c o m 提 供*/ //package com.nowjava; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class Main { public static List<Field> retrieveAnnotatedFields( final Class<?> objectClass, final Class<? extends Annotation> annotationClass) { final List<Field> fields = new ArrayList<>();/**时代Java - N o w J a v a . c o m**/ for (final Field field : retrieveAllFields(objectClass)) { final Annotation annotation = field .getAnnotation(annotationClass); if (annotation != null && annotation.annotationType().equals(annotationClass)) { fields.add(field); } } return fields; } public static List<Field> retrieveAllFields(final Class<?> clazz) { final List<Field> fields = new ArrayList<Field>(); Class<?> currentClass = clazz; while (!Object.