Search in sources :

Example 16 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project auto by google.

the class AutoAnnotationProcessor method getDefaultValues.

private ImmutableMap<String, AnnotationValue> getDefaultValues(TypeElement annotationElement) {
    ImmutableMap.Builder<String, AnnotationValue> defaultValues = ImmutableMap.builder();
    for (ExecutableElement member : ElementFilter.methodsIn(annotationElement.getEnclosedElements())) {
        String name = member.getSimpleName().toString();
        AnnotationValue defaultValue = member.getDefaultValue();
        if (defaultValue != null) {
            defaultValues.put(name, defaultValue);
        }
    }
    return defaultValues.build();
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 17 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project auto by google.

the class AnnotationMirrors method getAnnotationValuesWithDefaults.

/**
   * Returns the {@link AnnotationMirror}'s map of {@link AnnotationValue} indexed by
   * {@link ExecutableElement}, supplying default values from the annotation if the
   * annotation property has not been set.  This is equivalent to
   * {@link Elements#getElementValuesWithDefaults(AnnotationMirror)} but can be called
   * statically without an {@link Elements} instance.
   */
public static Map<ExecutableElement, AnnotationValue> getAnnotationValuesWithDefaults(AnnotationMirror annotation) {
    Map<ExecutableElement, AnnotationValue> values = Maps.newLinkedHashMap();
    Map<? extends ExecutableElement, ? extends AnnotationValue> declaredValues = annotation.getElementValues();
    for (ExecutableElement method : ElementFilter.methodsIn(annotation.getAnnotationType().asElement().getEnclosedElements())) {
        // Must iterate and put in this order, to ensure consistency in generated code.
        if (declaredValues.containsKey(method)) {
            values.put(method, declaredValues.get(method));
        } else if (method.getDefaultValue() != null) {
            values.put(method, method.getDefaultValue());
        } else {
            throw new IllegalStateException("Unset annotation value without default should never happen: " + MoreElements.asType(method.getEnclosingElement()).getQualifiedName() + '.' + method.getSimpleName() + "()");
        }
    }
    return values;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 18 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project j2objc by google.

the class TranslationUtil method createAnnotationValue.

public Expression createAnnotationValue(TypeMirror type, AnnotationValue aValue) {
    Object value = aValue.getValue();
    if (value == null) {
        return new NullLiteral(typeUtil.getNull());
    } else if (value instanceof VariableElement) {
        return new SimpleName((VariableElement) value);
    } else if (TypeUtil.isArray(type)) {
        assert value instanceof List;
        ArrayType arrayType = (ArrayType) type;
        @SuppressWarnings("unchecked") List<? extends AnnotationValue> list = (List<? extends AnnotationValue>) value;
        List<Expression> generatedValues = new ArrayList<>();
        for (AnnotationValue elem : list) {
            generatedValues.add(createAnnotationValue(arrayType.getComponentType(), elem));
        }
        return createObjectArray(generatedValues, arrayType);
    } else if (TypeUtil.isAnnotation(type)) {
        assert value instanceof AnnotationMirror;
        return createAnnotation((AnnotationMirror) value);
    } else if (value instanceof TypeMirror) {
        return new TypeLiteral((TypeMirror) value, typeUtil);
    } else {
        // Boolean, Character, Number, String
        return TreeUtil.newLiteral(value, typeUtil);
    }
}
Also used : SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) ArrayType(javax.lang.model.type.ArrayType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) NullLiteral(com.google.devtools.j2objc.ast.NullLiteral)

Example 19 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project j2objc by google.

the class AnnotationRewriter method addDefaultAccessors.

// Create accessors for properties that have default values.
private void addDefaultAccessors(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
    TypeElement type = node.getTypeElement();
    for (AnnotationTypeMemberDeclaration member : members) {
        ExecutableElement memberElement = member.getExecutableElement();
        AnnotationValue defaultValue = memberElement.getDefaultValue();
        if (defaultValue == null || defaultValue.getValue() == null) {
            continue;
        }
        TypeMirror memberType = memberElement.getReturnType();
        String propName = NameTable.getAnnotationPropertyName(memberElement);
        ExecutableElement defaultGetterElement = GeneratedExecutableElement.newMethodWithSelector(propName + "Default", memberType, type).addModifiers(Modifier.STATIC);
        MethodDeclaration defaultGetter = new MethodDeclaration(defaultGetterElement);
        defaultGetter.setHasDeclaration(false);
        Block defaultGetterBody = new Block();
        defaultGetter.setBody(defaultGetterBody);
        defaultGetterBody.addStatement(new ReturnStatement(translationUtil.createAnnotationValue(memberType, defaultValue)));
        node.addBodyDeclaration(defaultGetter);
    }
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) AnnotationValue(javax.lang.model.element.AnnotationValue) Block(com.google.devtools.j2objc.ast.Block)

Example 20 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project error-prone by google.

the class RequiredAnnotationProcessor method validateElement.

private void validateElement(final Element element) {
    TypeMirror requiredAnnotationTypeMirror = processingEnv.getElementUtils().getTypeElement(RequiredAnnotation.class.getName()).asType();
    for (final AnnotationMirror annotation : processingEnv.getElementUtils().getAllAnnotationMirrors(element)) {
        AnnotationMirror requiredAnnotationMirror = getAnnotationMirror(annotation.getAnnotationType().asElement(), requiredAnnotationTypeMirror);
        if (requiredAnnotationMirror == null) {
            continue;
        }
        AnnotationValue value = getAnnotationValue(requiredAnnotationMirror, "value");
        if (value == null) {
            continue;
        }
        new SimpleAnnotationValueVisitor7<Void, Void>() {

            @Override
            public Void visitType(TypeMirror t, Void p) {
                if (getAnnotationMirror(element, t) == null) {
                    printError(element, annotation, "Annotation %s on %s also requires %s", annotation, element, t);
                }
                return null;
            }

            @Override
            public Void visitArray(List<? extends AnnotationValue> vals, Void p) {
                for (AnnotationValue val : vals) {
                    visit(val);
                }
                return null;
            }
        }.visit(value);
    }
    validateElements(element.getEnclosedElements());
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)41 AnnotationMirror (javax.lang.model.element.AnnotationMirror)21 TypeElement (javax.lang.model.element.TypeElement)20 List (java.util.List)15 ArrayList (java.util.ArrayList)12 ExecutableElement (javax.lang.model.element.ExecutableElement)12 TypeMirror (javax.lang.model.type.TypeMirror)11 HashSet (java.util.HashSet)8 Map (java.util.Map)8 DeclaredType (javax.lang.model.type.DeclaredType)8 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (javax.lang.model.element.Element)3 ClassName (com.squareup.javapoet.ClassName)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 HashMap (java.util.HashMap)2 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1