Get list of fields with annotation, by using reflection
You need to mark the annotation as being available at runtime. Add the following to your annotation code.
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
/**
* @return null safe set
*/
public static Set<Field> findFields(Class<?> classs, Class<? extends Annotation> ann) {
Set<Field> set = new HashSet<>();
Class<?> c = classs;
while (c != null) {
for (Field field : c.getDeclaredFields()) {
if (field.isAnnotationPresent(ann)) {
set.add(field);
}
}
c = c.getSuperclass();
}
return set;
}