Search in sources :

Example 11 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 12 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project javapoet by square.

the class AnnotationSpec method get.

public static AnnotationSpec get(AnnotationMirror annotation) {
    TypeElement element = (TypeElement) annotation.getAnnotationType().asElement();
    AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element));
    Visitor visitor = new Visitor(builder);
    for (ExecutableElement executableElement : annotation.getElementValues().keySet()) {
        String name = executableElement.getSimpleName().toString();
        AnnotationValue value = annotation.getElementValues().get(executableElement);
        value.accept(visitor, name);
    }
    return builder.build();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 13 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project realm-java by realm.

the class ModuleMetaData method getClassMetaDataFromModule.

// Detour needed to access the class elements in the array
// See http://blog.retep.org/2009/02/13/getting-class-values-from-annotations-in-an-annotationprocessor/
@SuppressWarnings("unchecked")
private Set<String> getClassMetaDataFromModule(Element classElement) {
    AnnotationMirror annotationMirror = getAnnotationMirror(classElement);
    AnnotationValue annotationValue = getAnnotationValue(annotationMirror);
    Set<String> classes = new HashSet<String>();
    List<? extends AnnotationValue> moduleClasses = (List<? extends AnnotationValue>) annotationValue.getValue();
    for (AnnotationValue classMirror : moduleClasses) {
        String fullyQualifiedClassName = classMirror.getValue().toString();
        classes.add(fullyQualifiedClassName);
    }
    return classes;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) HashSet(java.util.HashSet)

Example 14 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project realm-java by realm.

the class ModuleMetaData method hasCustomClassList.

// Work-around for asking for a Class primitive array which would otherwise throw a TypeMirrorException
// https://community.oracle.com/thread/1184190
@SuppressWarnings("unchecked")
private boolean hasCustomClassList(Element classElement) {
    AnnotationMirror annotationMirror = getAnnotationMirror(classElement);
    AnnotationValue annotationValue = getAnnotationValue(annotationMirror);
    if (annotationValue == null) {
        return false;
    } else {
        List<? extends AnnotationValue> moduleClasses = (List<? extends AnnotationValue>) annotationValue.getValue();
        return moduleClasses.size() > 0;
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List)

Example 15 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)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)43 AnnotationMirror (javax.lang.model.element.AnnotationMirror)23 TypeElement (javax.lang.model.element.TypeElement)20 List (java.util.List)16 ArrayList (java.util.ArrayList)13 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 ImmutableList (com.google.common.collect.ImmutableList)2 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 ImmutableSet (com.google.common.collect.ImmutableSet)1