@Test public void getAnnotatedFieldsShouldFindAnnotatedFieldsInParentClasses() { List<Field> givenFields = getAnnotatedFields(Given.class, SubRuleWithResult.class); List<Field> resultFields = getAnnotatedFields(Result.class, SubRuleWithResult.class); Assert.assertEquals(12, givenFields.size()); Assert.assertEquals(1, resultFields.size()); }
@SuppressWarnings("unchecked") public static List<Field> getAnnotatedFields(Class annotation, Class clazz) { if (clazz == Object.class) { return new ArrayList<>(); } List<Field> fields = (List<Field>)Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList()); if (clazz.getSuperclass() != null) { fields.addAll(getAnnotatedFields(annotation, clazz.getSuperclass())); } return fields; }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Field> getAnnotatedFields(Class annotation, Class clazz) { if (clazz == Object.class) { return new ArrayList<>(); } List<Field> fields = (List<Field>)Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList()); if (clazz.getSuperclass() != null) { fields.addAll(getAnnotatedFields(annotation, clazz.getSuperclass())); } return fields; } }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Field> getAnnotatedFields(Class annotation, Class clazz) { if (clazz == Object.class) { return new ArrayList<>(); } List<Field> fields = (List<Field>)Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList()); if (clazz.getSuperclass() != null) { fields.addAll(getAnnotatedFields(annotation, clazz.getSuperclass())); } return fields; } private  AnnotationUtils(); }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Field> getAnnotatedFields(Class annotation, Class clazz) { if (clazz == Object.class) { return new ArrayList<>(); } List<Field> fields = (List<Field>)Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList()); if (clazz.getSuperclass() != null) { fields.addAll(getAnnotatedFields(annotation, clazz.getSuperclass())); } return fields; } private  AnnotationUtils(); @SuppressWarnings("unchecked") static List<Field> getAnnotatedFields(Class annotation, Class clazz); static Optional<Field> getAnnotatedField(Class annotation, Class clazz); @SuppressWarnings("unchecked") static List<Method> getAnnotatedMethods(Class annotation, Class clazz); static Optional<Method> getAnnotatedMethod(Class annotation, Class clazz); @SuppressWarnings("unchecked") static A getAnnotation(Class<A> annotation, Class<?> clazz); }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Field> getAnnotatedFields(Class annotation, Class clazz) { if (clazz == Object.class) { return new ArrayList<>(); } List<Field> fields = (List<Field>)Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList()); if (clazz.getSuperclass() != null) { fields.addAll(getAnnotatedFields(annotation, clazz.getSuperclass())); } return fields; } private  AnnotationUtils(); @SuppressWarnings("unchecked") static List<Field> getAnnotatedFields(Class annotation, Class clazz); static Optional<Field> getAnnotatedField(Class annotation, Class clazz); @SuppressWarnings("unchecked") static List<Method> getAnnotatedMethods(Class annotation, Class clazz); static Optional<Method> getAnnotatedMethod(Class annotation, Class clazz); @SuppressWarnings("unchecked") static A getAnnotation(Class<A> annotation, Class<?> clazz);  }