Search in sources :

Example 1 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project buck by facebook.

the class MoreElements method findAnnotation.

@Nullable
private static AnnotationMirror findAnnotation(CharSequence name, Element element) {
    for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        DeclaredType annotationType = annotationMirror.getAnnotationType();
        TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
        if (annotationTypeElement.getQualifiedName().contentEquals(name)) {
            return annotationMirror;
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Nullable(javax.annotation.Nullable)

Example 2 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project buck by facebook.

the class MoreElements method isSourceRetention.

public static boolean isSourceRetention(AnnotationMirror annotation) {
    DeclaredType annotationType = annotation.getAnnotationType();
    TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
    AnnotationMirror retentionAnnotation = findAnnotation("java.lang.annotation.Retention", annotationTypeElement);
    if (retentionAnnotation == null) {
        return false;
    }
    VariableElement retentionPolicy = (VariableElement) Preconditions.checkNotNull(findAnnotationValue(retentionAnnotation, "value"));
    return retentionPolicy.getSimpleName().contentEquals("SOURCE");
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.

the class Annotations method getAnnotationLines.

static List<CharSequence> getAnnotationLines(Element element, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, ElementType elementType, Function<String, String> importsResolver) {
    List<CharSequence> lines = Lists.newArrayList();
    Set<String> seenAnnotations = new HashSet<>();
    for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
        TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
        if (annotationTypeMatches(element, annotationElement, includeAnnotations, includeAllAnnotations, includeJacksonAnnotations, seenAnnotations) && annotationMatchesTarget(annotationElement, elementType)) {
            lines.add(AnnotationMirrors.toCharSequence(annotation, importsResolver));
        }
    }
    return lines;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 4 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.

the class Annotations method annotationTypeMatches.

private static boolean annotationTypeMatches(Element element, TypeElement annotationElement, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, Set<String> seenAnnotations) {
    String qualifiedName = annotationElement.getQualifiedName().toString();
    if (seenAnnotations.contains(qualifiedName) || qualifiedName.startsWith(PREFIX_IMMUTABLES) || qualifiedName.startsWith(PREFIX_JAVA_LANG)) {
        // also skip any we've already seen, since we're recursing.
        return false;
    }
    seenAnnotations.add(qualifiedName);
    if (annotationElement.getSimpleName().contentEquals(NULLABLE_SIMPLE_NAME)) {
        // we expect to propagate nullability separately
        return false;
    }
    if (includeAllAnnotations) {
        return true;
    }
    if (qualifiedName.equals(PREFIX_JACKSON_IGNORE_PROPERTIES)) {
        // but preferred way is to use additionalJsonAnnotations style attribute.
        return true;
    }
    if (includeJacksonAnnotations) {
        if (element.getKind() != ElementKind.METHOD) {
            if (qualifiedName.equals(Proto.JACKSON_DESERIALIZE) || qualifiedName.equals(Proto.JACKSON_SERIALIZE)) {
                return false;
            }
        }
        if (qualifiedName.startsWith(PREFIX_JACKSON) || qualifiedName.startsWith(PREFIX_JACKSON_DATABIND)) {
            return true;
        }
    }
    if (includeAnnotations.contains(qualifiedName)) {
        return true;
    }
    // This block of code can include annotation if it's parent annotation is included
    if (includeJacksonAnnotations || !includeAnnotations.isEmpty()) {
        for (AnnotationMirror parentAnnotation : annotationElement.getAnnotationMirrors()) {
            TypeElement parentElement = (TypeElement) parentAnnotation.getAnnotationType().asElement();
            if (annotationTypeMatches(element, parentElement, includeAnnotations, false, includeJacksonAnnotations, seenAnnotations)) {
                return true;
            }
        }
    }
    return false;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement)

Example 5 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.

the class TypeStringProvider method typeAnnotationsToBuffer.

private StringBuilder typeAnnotationsToBuffer(List<? extends AnnotationMirror> annotations) {
    StringBuilder annotationBuffer = new StringBuilder(100);
    for (AnnotationMirror annotationMirror : annotations) {
        CharSequence sequence = AnnotationMirrors.toCharSequence(annotationMirror, importsResolver);
        if (!nullableTypeAnnotation && sequence.toString().endsWith(EPHEMERAL_ANNOTATION_NULLABLE)) {
            this.nullableTypeAnnotation = true;
        }
        annotationBuffer.append(sequence).append(' ');
    }
    return annotationBuffer;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29