@Test public void getAnnotationShouldFindTheAnnotationOfAnAnnotation() { Rule ruleAnnotation = getAnnotation(Rule.class, SubRuleWithResult.class); SubRuleAnnotation subRuleAnnotation = getAnnotation(SubRuleAnnotation.class, SubRuleWithResult.class); Assert.assertNotNull(ruleAnnotation); Assert.assertNotNull(subRuleAnnotation); }
@SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class<A> annotation, Class<?> clazz) { return Optional.ofNullable(clazz.getAnnotation(annotation)).orElse((A) Arrays.stream(clazz.getDeclaredAnnotations()) .flatMap(anno -> Arrays.stream(anno.getClass().getInterfaces()) .flatMap(iface -> Arrays.stream(iface.getDeclaredAnnotations()))) .filter(annotation::isInstance) .findFirst().orElse(null) ); }
AnnotationUtils { @SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class<A> annotation, Class<?> clazz) { return Optional.ofNullable(clazz.getAnnotation(annotation)).orElse((A) Arrays.stream(clazz.getDeclaredAnnotations()) .flatMap(anno -> Arrays.stream(anno.getClass().getInterfaces()) .flatMap(iface -> Arrays.stream(iface.getDeclaredAnnotations()))) .filter(annotation::isInstance) .findFirst().orElse(null) ); } }
AnnotationUtils { @SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class<A> annotation, Class<?> clazz) { return Optional.ofNullable(clazz.getAnnotation(annotation)).orElse((A) Arrays.stream(clazz.getDeclaredAnnotations()) .flatMap(anno -> Arrays.stream(anno.getClass().getInterfaces()) .flatMap(iface -> Arrays.stream(iface.getDeclaredAnnotations()))) .filter(annotation::isInstance) .findFirst().orElse(null) ); } private  AnnotationUtils(); }
AnnotationUtils { @SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class<A> annotation, Class<?> clazz) { return Optional.ofNullable(clazz.getAnnotation(annotation)).orElse((A) Arrays.stream(clazz.getDeclaredAnnotations()) .flatMap(anno -> Arrays.stream(anno.getClass().getInterfaces()) .flatMap(iface -> Arrays.stream(iface.getDeclaredAnnotations()))) .filter(annotation::isInstance) .findFirst().orElse(null) ); } 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 <A extends Annotation> A getAnnotation(Class<A> annotation, Class<?> clazz) { return Optional.ofNullable(clazz.getAnnotation(annotation)).orElse((A) Arrays.stream(clazz.getDeclaredAnnotations()) .flatMap(anno -> Arrays.stream(anno.getClass().getInterfaces()) .flatMap(iface -> Arrays.stream(iface.getDeclaredAnnotations()))) .filter(annotation::isInstance) .findFirst().orElse(null) ); } 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);  }