@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { analyzeQualifiers(roundEnv); analyzeAttributes(roundEnv); analyzeAliases(roundEnv); return false; // let other processors work too }
private void analyzeAttributes(RoundEnvironment re) { analyzeAnnotations(re, Attribute.class, "attribute"); }
private void analyzeAnnotations(RoundEnvironment re, Class<? extends Annotation> annotation, String annotationName){ Set<? extends Element> elts = re.getElementsAnnotatedWith(annotation); for (Element elt: elts) { if (elt.getAnnotation(Documented.class) == null) { warning(elt, String.format("%s annotation should be @Documented", annotationName)); } Retention ret = elt.getAnnotation(Retention.class); if (ret == null || !ret.value().equals(RetentionPolicy.RUNTIME)) { error(elt, String.format("%s annotation must have RUNTIME retention", annotationName)); } } }
private void analyzeAliases(RoundEnvironment re) { Set<? extends Element> elts = re.getElementsAnnotatedWith(AliasFor.class); for (Element elt : elts) { if (elt.getAnnotation(Qualifier.class) == null) { error(elt, "alias annotation must also be a qualifier"); } if (elt.getAnnotation(AllowUnqualifiedMatch.class) != null) { error(elt, "alias annotation has @AllowUnqualifiedMatch (should be on alias target)"); } for (AnnotationMirror mirror: elt.getAnnotationMirrors()) { Element element = mirror.getAnnotationType().asElement(); if (element instanceof TypeElement) { TypeElement type = (TypeElement) element; Name name = type.getQualifiedName(); if (DEFAULT_PATTERN.matcher(name).matches()) { warning(elt, "alias annotation has %s, defaults should be on alias target", type.getSimpleName()); } } } } } }
private void analyzeQualifiers(RoundEnvironment re) { analyzeAnnotations(re, Qualifier.class, "qualifier"); }