Search in sources :

Example 31 with AnnotationMirror

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

the class GwtCompatibility method gwtCompatibleAnnotationString.

String gwtCompatibleAnnotationString() {
    if (gwtCompatibleAnnotation.isPresent()) {
        AnnotationMirror annotation = gwtCompatibleAnnotation.get();
        TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
        String annotationArguments;
        if (annotation.getElementValues().isEmpty()) {
            annotationArguments = "";
        } else {
            List<String> elements = Lists.newArrayList();
            for (Map.Entry<ExecutableElement, AnnotationValue> entry : getElementValues(annotation).entrySet()) {
                elements.add(entry.getKey().getSimpleName() + " = " + entry.getValue());
            }
            annotationArguments = "(" + Joiner.on(", ").join(elements) + ")";
        }
        return "@" + annotationElement.getQualifiedName() + annotationArguments;
    } else {
        return "";
    }
}
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) Map(java.util.Map)

Example 32 with AnnotationMirror

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

the class AutoServiceProcessor method processAnnotations.

private void processAnnotations(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(AutoService.class);
    log(annotations.toString());
    log(elements.toString());
    for (Element e : elements) {
        // TODO(gak): check for error trees?
        TypeElement providerImplementer = (TypeElement) e;
        AnnotationMirror providerAnnotation = getAnnotationMirror(e, AutoService.class).get();
        DeclaredType providerInterface = getProviderInterface(providerAnnotation);
        TypeElement providerType = (TypeElement) providerInterface.asElement();
        log("provider interface: " + providerType.getQualifiedName());
        log("provider implementer: " + providerImplementer.getQualifiedName());
        if (!checkImplementer(providerImplementer, providerType)) {
            String message = "ServiceProviders must implement their service provider interface. " + providerImplementer.getQualifiedName() + " does not implement " + providerType.getQualifiedName();
            error(message, e, providerAnnotation);
        }
        String providerTypeName = getBinaryName(providerType);
        String providerImplementerName = getBinaryName(providerImplementer);
        log("provider interface binary name: " + providerTypeName);
        log("provider implementer binary name: " + providerImplementerName);
        providers.put(providerTypeName, providerImplementerName);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MoreElements.getAnnotationMirror(com.google.auto.common.MoreElements.getAnnotationMirror) AutoService(com.google.auto.service.AutoService) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 33 with AnnotationMirror

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

the class Parameter method forVariableElement.

static Parameter forVariableElement(VariableElement variable, TypeMirror type, Types types) {
    Optional<AnnotationMirror> nullable = Optional.absent();
    Iterable<? extends AnnotationMirror> annotations = variable.getAnnotationMirrors();
    for (AnnotationMirror annotation : annotations) {
        if (annotation.getAnnotationType().asElement().getSimpleName().contentEquals("Nullable")) {
            nullable = Optional.of(annotation);
            break;
        }
    }
    Key key = Key.create(type, annotations, types);
    return new AutoValue_Parameter(type, variable.getSimpleName().toString(), key, wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), nullable));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 34 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror 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 35 with AnnotationMirror

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

the class NameTable method getFullNameImpl.

private String getFullNameImpl(TypeElement element) {
    // have a dash character.
    if (ElementUtil.isPackageInfo(element)) {
        return camelCaseQualifiedName(ElementUtil.getName(ElementUtil.getPackage(element))) + PACKAGE_INFO_OBJC_NAME;
    }
    // Use ObjectiveCName annotation, if it exists.
    AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
    if (annotation != null) {
        return (String) ElementUtil.getAnnotationValue(annotation, "value");
    }
    TypeElement outerClass = ElementUtil.getDeclaringClass(element);
    if (outerClass != null) {
        return getFullName(outerClass) + '_' + getTypeSubName(element);
    }
    // Use mapping file entry, if it exists.
    String mappedName = classMappings.get(ElementUtil.getQualifiedName(element));
    if (mappedName != null) {
        return mappedName;
    }
    // Use camel-cased package+class name.
    return getPrefix(ElementUtil.getPackage(element)) + getTypeSubName(element);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement)

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