@Test public void getAnnotatedMethodsShouldFindAnnotatedMethods() { List<Method> thenMethods = getAnnotatedMethods(Then.class, SampleRuleWithResult.class); List<Method> whenMethods = getAnnotatedMethods(When.class, SampleRuleWithResult.class); Assert.assertEquals(1, thenMethods.size()); Assert.assertEquals(1, whenMethods.size()); }
@SuppressWarnings("unchecked") public static List<Method> getAnnotatedMethods(Class annotation, Class clazz) { List<Method> methods = new ArrayList<>(); if (clazz == Object.class) { return methods; } methods.addAll((List<Method>) Arrays.stream(clazz.getDeclaredMethods()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList())); methods.addAll(getAnnotatedMethods(annotation, clazz.getSuperclass())); return methods; }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Method> getAnnotatedMethods(Class annotation, Class clazz) { List<Method> methods = new ArrayList<>(); if (clazz == Object.class) { return methods; } methods.addAll((List<Method>) Arrays.stream(clazz.getDeclaredMethods()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList())); methods.addAll(getAnnotatedMethods(annotation, clazz.getSuperclass())); return methods; } }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Method> getAnnotatedMethods(Class annotation, Class clazz) { List<Method> methods = new ArrayList<>(); if (clazz == Object.class) { return methods; } methods.addAll((List<Method>) Arrays.stream(clazz.getDeclaredMethods()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList())); methods.addAll(getAnnotatedMethods(annotation, clazz.getSuperclass())); return methods; } private  AnnotationUtils(); }
AnnotationUtils { @SuppressWarnings("unchecked") public static List<Method> getAnnotatedMethods(Class annotation, Class clazz) { List<Method> methods = new ArrayList<>(); if (clazz == Object.class) { return methods; } methods.addAll((List<Method>) Arrays.stream(clazz.getDeclaredMethods()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList())); methods.addAll(getAnnotatedMethods(annotation, clazz.getSuperclass())); return methods; } 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<Method> getAnnotatedMethods(Class annotation, Class clazz) { List<Method> methods = new ArrayList<>(); if (clazz == Object.class) { return methods; } methods.addAll((List<Method>) Arrays.stream(clazz.getDeclaredMethods()) .filter(field -> field.getAnnotation(annotation) != null) .collect(Collectors.toList())); methods.addAll(getAnnotatedMethods(annotation, clazz.getSuperclass())); return methods; } 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);  }