Search in sources :

Example 46 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 47 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project antlr4 by tunnelvisionlabs.

the class RuleDependencyProcessor method findRuleDependencyProperty.

@Nullable
private Tuple2<AnnotationMirror, AnnotationValue> findRuleDependencyProperty(@NotNull Tuple2<RuleDependency, Element> dependency, @NotNull RuleDependencyProperty property) {
    TypeElement ruleDependencyTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependencyClassName);
    TypeElement ruleDependenciesTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependenciesClassName);
    List<? extends AnnotationMirror> mirrors = dependency.getItem2().getAnnotationMirrors();
    for (AnnotationMirror annotationMirror : mirrors) {
        if (processingEnv.getTypeUtils().isSameType(ruleDependencyTypeElement.asType(), annotationMirror.getAnnotationType())) {
            AnnotationValue element = findRuleDependencyProperty(dependency, annotationMirror, property);
            if (element != null) {
                return Tuple.create(annotationMirror, element);
            }
        } else if (processingEnv.getTypeUtils().isSameType(ruleDependenciesTypeElement.asType(), annotationMirror.getAnnotationType())) {
            Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationMirror.getElementValues();
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> value : values.entrySet()) {
                if ("value()".equals(value.getKey().toString())) {
                    AnnotationValue annotationValue = value.getValue();
                    if (!(annotationValue.getValue() instanceof List)) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected array of RuleDependency annotations for annotation property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
                        break;
                    }
                    List<?> annotationValueList = (List<?>) annotationValue.getValue();
                    for (Object obj : annotationValueList) {
                        if (!(obj instanceof AnnotationMirror)) {
                            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected RuleDependency annotation mirror for element of property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
                            break;
                        }
                        AnnotationValue element = findRuleDependencyProperty(dependency, (AnnotationMirror) obj, property);
                        if (element != null) {
                            return Tuple.create((AnnotationMirror) obj, element);
                        }
                    }
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("Unexpected annotation property %s.", value.getKey().toString()), dependency.getItem2(), annotationMirror, value.getValue());
                }
            }
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project robolectric by robolectric.

the class Helpers method getImplementedClassName.

private TypeMirror getImplementedClassName(AnnotationMirror am) {
    AnnotationValue className = Helpers.getAnnotationTypeMirrorValue(am, "className");
    if (className == null) {
        return null;
    }
    String classNameString = Helpers.getAnnotationStringValue(className);
    if (classNameString == null) {
        return null;
    }
    TypeElement impElement = elements.getTypeElement(classNameString.replace('$', '.'));
    if (impElement == null) {
        return null;
    }
    return impElement.asType();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 49 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project robolectric by robolectric.

the class Helpers method getImplementedClass.

public TypeMirror getImplementedClass(AnnotationMirror am) {
    if (am == null) {
        return null;
    }
    // RobolectricWiringTest prefers className (if provided) to value, so we do the same here.
    TypeMirror impType = getImplementedClassName(am);
    if (impType != null) {
        return impType;
    }
    AnnotationValue av = Helpers.getAnnotationTypeMirrorValue(am, "value");
    if (av == null) {
        return null;
    }
    TypeMirror type = Helpers.getAnnotationTypeMirrorValue(av);
    if (type == null) {
        return null;
    }
    return type;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

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

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)182 AnnotationMirror (javax.lang.model.element.AnnotationMirror)81 TypeElement (javax.lang.model.element.TypeElement)62 ArrayList (java.util.ArrayList)54 ExecutableElement (javax.lang.model.element.ExecutableElement)53 TypeMirror (javax.lang.model.type.TypeMirror)48 List (java.util.List)45 Test (org.junit.Test)30 DeclaredType (javax.lang.model.type.DeclaredType)27 Map (java.util.Map)20 HashSet (java.util.HashSet)19 HashMap (java.util.HashMap)18 VariableElement (javax.lang.model.element.VariableElement)15 Element (javax.lang.model.element.Element)14 ElementUtils.getAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValue)10 ElementUtils.fromTypeMirror (com.oracle.truffle.dsl.processor.java.ElementUtils.fromTypeMirror)9 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)8 ElementUtils.resolveAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.resolveAnnotationValue)7 ElementUtils.unboxAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.unboxAnnotationValue)7 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)7