Java examples for java.lang.annotation:Field Annotation
find Field Annotation from class and field name
//package com.java2s; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class Main { public static Annotation[] findFieldAnnotation(Class<?> clazz, String fieldName) {/*from w ww . j a v a2 s. c o m*/ Annotation[] annotations = null; try { List<Field> fields = getAllFields(clazz); Field field = null; for (Field fld : fields) { if (fld.getName().equals(fieldName)) { field = fld; break; } } if (field != null) { annotations = field.getAnnotations(); } } catch (SecurityException e) { } return annotations; } public static List<Field> getAllFields(Class<?> type) { return getAllFields(null, type); } private static List<Field> getAllFields(List<Field> fields, Class<?> type) { if (fields == null) fields = new ArrayList<Field>(); for (Field field : type.getDeclaredFields()) { fields.add(field); } if (type.getSuperclass() != null) { fields = getAllFields(fields, type.getSuperclass()); } return fields; } }