Search in sources :

Example 21 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project dagger by square.

the class ValidationProcessor method validateScoping.

private void validateScoping(Element element) {
    boolean suppressWarnings = element.getAnnotation(SuppressWarnings.class) != null && Arrays.asList(element.getAnnotation(SuppressWarnings.class).value()).contains("scoping");
    int numberOfScopingAnnotationsOnElement = 0;
    for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
        if (annotation.getAnnotationType().asElement().getAnnotation(Scope.class) == null) {
            continue;
        }
        switch(element.getKind()) {
            case METHOD:
                numberOfScopingAnnotationsOnElement++;
                if (!isProvidesMethod(element) && !suppressWarnings) {
                    warning("Dagger will ignore scoping annotations on methods that are not " + "@Provides methods: " + elementToString(element), element);
                }
                break;
            case CLASS:
                if (!element.getModifiers().contains(ABSTRACT)) {
                    numberOfScopingAnnotationsOnElement++;
                    break;
                }
            // fall through if abstract
            default:
                error("Scoping annotations are only allowed on concrete types and @Provides methods: " + elementToString(element), element);
        }
    }
    if (numberOfScopingAnnotationsOnElement > 1) {
        error("Only one scoping annotation is allowed per element: " + elementToString(element), element);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Scope(javax.inject.Scope)

Example 22 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project dagger by square.

the class ValidationProcessor method validateQualifiers.

private void validateQualifiers(Element element, Map<Element, Element> parametersToTheirMethods) {
    boolean suppressWarnings = element.getAnnotation(SuppressWarnings.class) != null && Arrays.asList(element.getAnnotation(SuppressWarnings.class).value()).contains("qualifiers");
    int numberOfQualifiersOnElement = 0;
    for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
        if (annotation.getAnnotationType().asElement().getAnnotation(Qualifier.class) == null) {
            continue;
        }
        switch(element.getKind()) {
            case FIELD:
                numberOfQualifiersOnElement++;
                if (element.getAnnotation(Inject.class) == null && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on fields that are not " + "annotated with @Inject: " + elementToString(element), element);
                }
                break;
            case METHOD:
                numberOfQualifiersOnElement++;
                if (!isProvidesMethod(element) && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on methods that are not " + "@Provides methods: " + elementToString(element), element);
                }
                break;
            case PARAMETER:
                numberOfQualifiersOnElement++;
                if (!isInjectableConstructorParameter(element, parametersToTheirMethods) && !isProvidesMethodParameter(element, parametersToTheirMethods) && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on parameters that are not " + "@Inject constructor parameters or @Provides method parameters: " + elementToString(element), element);
                }
                break;
            default:
                error("Qualifier annotations are only allowed on fields, methods, and parameters: " + elementToString(element), element);
        }
    }
    if (numberOfQualifiersOnElement > 1) {
        error("Only one qualifier annotation is allowed per element: " + elementToString(element), element);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Qualifier(javax.inject.Qualifier)

Example 23 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project commons by twitter.

the class CmdLineProcessor method processVerifiers.

private void processVerifiers(Set<? extends Element> elements) {
    TypeElement verifierType = typeElement(Verifier.class);
    TypeElement verifierForType = typeElement(VerifierFor.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found a @VerifierFor annotation on a non-class %s", element);
        } else {
            TypeElement verifier = (TypeElement) element;
            if (!isAssignable(verifier, Verifier.class)) {
                error("Found a @Verifier annotation on a non-Verifier %s", element);
                return;
            }
            String verifierClassName = getBinaryName(verifier);
            @Nullable AnnotationMirror verifierFor = getAnnotationMirror(verifier, verifierForType);
            if (verifierFor != null) {
                @Nullable TypeElement verifyAnnotationType = getClassType(verifierFor, "value", null);
                if (verifyAnnotationType != null) {
                    @Nullable String verifiedType = getTypeArgument(verifier, verifierType);
                    if (verifiedType != null) {
                        String verifyAnnotationClassName = elementUtils.getBinaryName(verifyAnnotationType).toString();
                        getBuilder(verifierClassName).addVerifier(verifiedType, verifyAnnotationClassName, verifierClassName);
                    }
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Verifier(com.twitter.common.args.Verifier) Nullable(javax.annotation.Nullable)

Example 24 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project classindex by atteo.

the class ClassIndexProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    try {
        for (Element element : roundEnv.getRootElements()) {
            if (!(element instanceof TypeElement)) {
                continue;
            }
            final PackageElement packageElement = getPackage(element);
            element.accept(new ElementScanner6<Void, Void>() {

                @Override
                public Void visitType(TypeElement typeElement, Void o) {
                    try {
                        for (AnnotationMirror mirror : typeElement.getAnnotationMirrors()) {
                            final TypeElement annotationElement = (TypeElement) mirror.getAnnotationType().asElement();
                            storeAnnotation(annotationElement, typeElement);
                        }
                        indexSupertypes(typeElement, typeElement);
                        if (packageElement != null) {
                            storeClassFromPackage(packageElement, typeElement);
                        }
                    } catch (IOException e) {
                        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] " + e.getMessage());
                    }
                    return super.visitType(typeElement, o);
                }
            }, null);
        }
        if (!roundEnv.processingOver()) {
            return false;
        }
        writeIndexFiles(ClassIndex.SUBCLASS_INDEX_PREFIX, subclassMap);
        writeIndexFiles(ClassIndex.ANNOTATED_INDEX_PREFIX, annotatedMap);
        for (Map.Entry<String, Set<String>> entry : packageMap.entrySet()) {
            writeSimpleNameIndexFile(entry.getValue(), entry.getKey().replace(".", "/") + "/" + ClassIndex.PACKAGE_INDEX_NAME);
        }
    } catch (IOException e) {
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Can't write index file: " + e.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Internal error: " + e.getMessage());
    }
    return false;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) PackageElement(javax.lang.model.element.PackageElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project bazel by bazelbuild.

the class PurityUtils method getPurityKinds.

/**
     * @return The types of purity of the method {@code methodElement}.
     */
public static List<Pure.Kind> getPurityKinds(AnnotationProvider provider, Element methodElement) {
    AnnotationMirror pureAnnotation = provider.getDeclAnnotation(methodElement, Pure.class);
    AnnotationMirror sefAnnotation = provider.getDeclAnnotation(methodElement, SideEffectFree.class);
    AnnotationMirror detAnnotation = provider.getDeclAnnotation(methodElement, Deterministic.class);
    List<Pure.Kind> kinds = new ArrayList<>();
    if (pureAnnotation != null) {
        kinds.add(Kind.DETERMINISTIC);
        kinds.add(Kind.SIDE_EFFECT_FREE);
    }
    if (sefAnnotation != null) {
        kinds.add(Kind.SIDE_EFFECT_FREE);
    }
    if (detAnnotation != null) {
        kinds.add(Kind.DETERMINISTIC);
    }
    return kinds;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Kind(org.checkerframework.dataflow.qual.Pure.Kind) ArrayList(java.util.ArrayList)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)88 TypeElement (javax.lang.model.element.TypeElement)46 AnnotationValue (javax.lang.model.element.AnnotationValue)21 Element (javax.lang.model.element.Element)19 DeclaredType (javax.lang.model.type.DeclaredType)19 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableElement (javax.lang.model.element.VariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)16 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)13 List (java.util.List)12 Map (java.util.Map)10 PackageElement (javax.lang.model.element.PackageElement)7 HashMap (java.util.HashMap)5 Nullable (javax.annotation.Nullable)5 IOException (java.io.IOException)4 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ClassName (com.squareup.javapoet.ClassName)3