Search in sources :

Example 36 with AnnotationValue

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

the class AnnotationMirrorsTest method testGetValueEntry.

@Test
public void testGetValueEntry() {
    Map.Entry<ExecutableElement, AnnotationValue> elementValue = AnnotationMirrors.getAnnotationElementAndValue(annotationOn(TestClassBlah.class), "value");
    assertThat(elementValue.getKey().getSimpleName().toString()).isEqualTo("value");
    assertThat(elementValue.getValue().getValue()).isInstanceOf(VariableElement.class);
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationOn(TestClassBlah.class), "value");
    assertThat(value.getValue()).isInstanceOf(VariableElement.class);
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) Map(java.util.Map) Test(org.junit.Test)

Example 37 with AnnotationValue

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

the class AutoAnnotationProcessor method processMethod.

private void processMethod(ExecutableElement method) {
    if (!method.getModifiers().contains(Modifier.STATIC)) {
        throw abortWithError("@AutoAnnotation method must be static", method);
    }
    TypeElement annotationElement = getAnnotationReturnType(method);
    TypeMirror annotationTypeMirror = annotationElement.asType();
    Set<Class<?>> wrapperTypesUsedInCollections = wrapperTypesUsedInCollections(method);
    ImmutableMap<String, ExecutableElement> memberMethods = getMemberMethods(annotationElement);
    Set<TypeMirror> memberTypes = getMemberTypes(memberMethods.values());
    Set<TypeMirror> referencedTypes = getReferencedTypes(annotationTypeMirror, method, memberTypes, wrapperTypesUsedInCollections);
    TypeElement methodClass = (TypeElement) method.getEnclosingElement();
    String pkg = TypeSimplifier.packageNameOf(methodClass);
    TypeSimplifier typeSimplifier = new TypeSimplifier(typeUtils, pkg, referencedTypes, annotationTypeMirror);
    AnnotationOutput annotationOutput = new AnnotationOutput(typeSimplifier);
    ImmutableMap<String, AnnotationValue> defaultValues = getDefaultValues(annotationElement);
    ImmutableMap<String, Member> members = getMembers(method, memberMethods, typeSimplifier, annotationOutput);
    ImmutableMap<String, Parameter> parameters = getParameters(annotationElement, method, members, typeSimplifier);
    validateParameters(annotationElement, method, members, parameters, defaultValues);
    String generatedClassName = generatedClassName(method);
    AutoAnnotationTemplateVars vars = new AutoAnnotationTemplateVars();
    vars.annotationFullName = annotationElement.toString();
    vars.annotationName = typeSimplifier.simplify(annotationElement.asType());
    vars.className = generatedClassName;
    vars.imports = typeSimplifier.typesToImport();
    vars.generated = typeSimplifier.simplify(getTypeMirror(Generated.class));
    vars.arrays = typeSimplifier.simplify(getTypeMirror(Arrays.class));
    vars.members = members;
    vars.params = parameters;
    vars.pkg = pkg;
    vars.wrapperTypesUsedInCollections = wrapperTypesUsedInCollections;
    vars.gwtCompatible = isGwtCompatible(annotationElement);
    ImmutableMap<String, Integer> invariableHashes = invariableHashes(members, parameters.keySet());
    vars.invariableHashSum = 0;
    for (int h : invariableHashes.values()) {
        vars.invariableHashSum += h;
    }
    vars.invariableHashes = invariableHashes.keySet();
    String text = vars.toText();
    text = Reformatter.fixup(text);
    String fullName = fullyQualifiedName(pkg, generatedClassName);
    writeSourceFile(fullName, text, methodClass);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 38 with AnnotationValue

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

the class AutoAnnotationProcessor method invariableHashes.

/**
   * Returns a map from the names of members with invariable hashCodes to the values of those
   * hashCodes.
   */
private static ImmutableMap<String, Integer> invariableHashes(ImmutableMap<String, Member> members, ImmutableSet<String> parameters) {
    ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
    for (String element : members.keySet()) {
        if (!parameters.contains(element)) {
            Member member = members.get(element);
            AnnotationValue annotationValue = member.method.getDefaultValue();
            Optional<Integer> invariableHash = invariableHash(annotationValue);
            if (invariableHash.isPresent()) {
                builder.put(element, (element.hashCode() * 127) ^ invariableHash.get());
            }
        }
    }
    return builder.build();
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 39 with AnnotationValue

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

the class AutoServiceProcessor method getProviderInterface.

private DeclaredType getProviderInterface(AnnotationMirror providerAnnotation) {
    // The very simplest of way of doing this, is also unfortunately unworkable.
    // We'd like to do:
    //    ServiceProvider provider = e.getAnnotation(ServiceProvider.class);
    //    Class<?> providerInterface = provider.value();
    //
    // but unfortunately we can't load the arbitrary class at annotation
    // processing time. So, instead, we have to use the mirror to get at the
    // value (much more painful).
    Map<? extends ExecutableElement, ? extends AnnotationValue> valueIndex = providerAnnotation.getElementValues();
    log("annotation values: " + valueIndex);
    AnnotationValue value = valueIndex.values().iterator().next();
    return (DeclaredType) value.getValue();
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) DeclaredType(javax.lang.model.type.DeclaredType)

Example 40 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project neo4j-mobile-android by neo4j-contrib.

the class ServiceProcessor method process.

@SuppressWarnings("unchecked")
@Override
protected void process(TypeElement annotationType, Element annotated, AnnotationMirror annotation, Map<? extends ExecutableElement, ? extends AnnotationValue> values) throws IOException {
    for (AnnotationValue o : (List<? extends AnnotationValue>) values.values().iterator().next().getValue()) {
        TypeMirror service = (TypeMirror) o.getValue();
        addTo(((TypeElement) annotated).getQualifiedName().toString(), "META-INF", "services", service.toString());
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List)

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