@Test public void testOf_WrongTarget() { try { MetaAnnotation.of(MethodAnnotation.class); fail("expected a " + IllegalArgumentException.class.getSimpleName()); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString(MethodAnnotation.class.getSimpleName())); assertThat(e.getMessage(), containsString(Target.class.getSimpleName())); assertThat(e.getMessage(), containsString(ElementType.METHOD.toString())); assertThat(e.getMessage(), containsString(ElementType.ANNOTATION_TYPE.toString())); } }
public static <META extends Annotation> MetaAnnotation<META> of(Class<META> metaAnnotationClass) { Target target = metaAnnotationClass.getAnnotation(Target.class); if (target == null) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " has no @" + Target.class.getSimpleName() + " annotation"); } if (Arrays.stream(target.value()).noneMatch(t -> t == ElementType.ANNOTATION_TYPE || t == ElementType.TYPE)) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " is no meta-annotation. @" + Target.class.getSimpleName() + " is " + Arrays.toString(target.value()) + " but should be " + ElementType.ANNOTATION_TYPE); } return new MetaAnnotation<>(metaAnnotationClass); }
MetaAnnotation { public static <META extends Annotation> MetaAnnotation<META> of(Class<META> metaAnnotationClass) { Target target = metaAnnotationClass.getAnnotation(Target.class); if (target == null) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " has no @" + Target.class.getSimpleName() + " annotation"); } if (Arrays.stream(target.value()).noneMatch(t -> t == ElementType.ANNOTATION_TYPE || t == ElementType.TYPE)) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " is no meta-annotation. @" + Target.class.getSimpleName() + " is " + Arrays.toString(target.value()) + " but should be " + ElementType.ANNOTATION_TYPE); } return new MetaAnnotation<>(metaAnnotationClass); } }
MetaAnnotation { public static <META extends Annotation> MetaAnnotation<META> of(Class<META> metaAnnotationClass) { Target target = metaAnnotationClass.getAnnotation(Target.class); if (target == null) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " has no @" + Target.class.getSimpleName() + " annotation"); } if (Arrays.stream(target.value()).noneMatch(t -> t == ElementType.ANNOTATION_TYPE || t == ElementType.TYPE)) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " is no meta-annotation. @" + Target.class.getSimpleName() + " is " + Arrays.toString(target.value()) + " but should be " + ElementType.ANNOTATION_TYPE); } return new MetaAnnotation<>(metaAnnotationClass); } private  MetaAnnotation(Class<META> metaAnnotationClass); }
MetaAnnotation { public static <META extends Annotation> MetaAnnotation<META> of(Class<META> metaAnnotationClass) { Target target = metaAnnotationClass.getAnnotation(Target.class); if (target == null) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " has no @" + Target.class.getSimpleName() + " annotation"); } if (Arrays.stream(target.value()).noneMatch(t -> t == ElementType.ANNOTATION_TYPE || t == ElementType.TYPE)) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " is no meta-annotation. @" + Target.class.getSimpleName() + " is " + Arrays.toString(target.value()) + " but should be " + ElementType.ANNOTATION_TYPE); } return new MetaAnnotation<>(metaAnnotationClass); } private  MetaAnnotation(Class<META> metaAnnotationClass); static MetaAnnotation<META> of(Class<META> metaAnnotationClass); boolean isRepeatable(); boolean isPresentOnAnyAnnotationOn(AnnotatedElement annotatedElement); boolean isPresentOn(@CheckForNull Annotation annotation); Optional<META> findOn(Annotation annotation); Stream<META> findAllOn(Annotation annotation); Stream<Annotation> findAnnotatedAnnotationsOn(AnnotatedElement annotatedElement); BinaryOperator<A> onlyOneOn(AnnotatedElement annotatedElement); Supplier<? extends IllegalArgumentException> missingAnnotation(Annotation annotation,
            AnnotatedElement annotatedElement,
            String checkerMethod); }
MetaAnnotation { public static <META extends Annotation> MetaAnnotation<META> of(Class<META> metaAnnotationClass) { Target target = metaAnnotationClass.getAnnotation(Target.class); if (target == null) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " has no @" + Target.class.getSimpleName() + " annotation"); } if (Arrays.stream(target.value()).noneMatch(t -> t == ElementType.ANNOTATION_TYPE || t == ElementType.TYPE)) { throw new IllegalArgumentException( metaAnnotationClass.getSimpleName() + " is no meta-annotation. @" + Target.class.getSimpleName() + " is " + Arrays.toString(target.value()) + " but should be " + ElementType.ANNOTATION_TYPE); } return new MetaAnnotation<>(metaAnnotationClass); } private  MetaAnnotation(Class<META> metaAnnotationClass); static MetaAnnotation<META> of(Class<META> metaAnnotationClass); boolean isRepeatable(); boolean isPresentOnAnyAnnotationOn(AnnotatedElement annotatedElement); boolean isPresentOn(@CheckForNull Annotation annotation); Optional<META> findOn(Annotation annotation); Stream<META> findAllOn(Annotation annotation); Stream<Annotation> findAnnotatedAnnotationsOn(AnnotatedElement annotatedElement); BinaryOperator<A> onlyOneOn(AnnotatedElement annotatedElement); Supplier<? extends IllegalArgumentException> missingAnnotation(Annotation annotation,
            AnnotatedElement annotatedElement,
            String checkerMethod);  }