Search in sources :

Example 71 with AnnotationValue

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

the class AutoBuilderProcessor method propertyInitializers.

private ImmutableMap<String, String> propertyInitializers(TypeElement autoBuilderType, ExecutableElement executable) {
    boolean autoAnnotation = MoreElements.getAnnotationMirror(executable, AUTO_ANNOTATION_NAME).isPresent();
    if (!autoAnnotation) {
        return ImmutableMap.of();
    }
    // We expect the return type of an @AutoAnnotation method to be an annotation type. If it isn't,
    // AutoAnnotation will presumably complain, so we don't need to complain further.
    TypeMirror returnType = executable.getReturnType();
    if (!returnType.getKind().equals(TypeKind.DECLARED)) {
        return ImmutableMap.of();
    }
    // This might not actually be an annotation (if the code is wrong), but if that's the case we
    // just won't see any contained ExecutableElement where getDefaultValue() returns something.
    TypeElement annotation = MoreTypes.asTypeElement(returnType);
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (ExecutableElement method : methodsIn(annotation.getEnclosedElements())) {
        AnnotationValue defaultValue = method.getDefaultValue();
        if (defaultValue != null) {
            String memberName = method.getSimpleName().toString();
            builder.put(memberName, AnnotationOutput.sourceFormForInitializer(defaultValue, processingEnv, memberName, autoBuilderType));
        }
    }
    return builder.build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 72 with AnnotationValue

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

the class AutoBuilderProcessor method findOfClassValue.

private TypeElement findOfClassValue(AnnotationMirror autoBuilderAnnotation) {
    AnnotationValue ofClassValue = AnnotationMirrors.getAnnotationValue(autoBuilderAnnotation, "ofClass");
    Object value = ofClassValue.getValue();
    if (value instanceof TypeMirror) {
        TypeMirror ofClassType = (TypeMirror) value;
        switch(ofClassType.getKind()) {
            case DECLARED:
                return MoreTypes.asTypeElement(ofClassType);
            case ERROR:
                throw new MissingTypeException(MoreTypes.asError(ofClassType));
            default:
                break;
        }
    }
    throw new MissingTypeException(null);
}
Also used : MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 73 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 74 with AnnotationValue

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

the class AutoAnnotationProcessor method invariableHash.

private static Optional<Integer> invariableHash(List<? extends AnnotationValue> annotationValues) {
    int h = 1;
    for (AnnotationValue annotationValue : annotationValues) {
        Optional<Integer> maybeHash = invariableHash(annotationValue);
        if (!maybeHash.isPresent()) {
            return Optional.empty();
        }
        h = h * 31 + maybeHash.get();
    }
    return Optional.of(h);
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 75 with AnnotationValue

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

the class GeneralInjectorGeneratorHubClone method generateMapContributors.

protected final void generateMapContributors(BindingKey key, ParameterizedTypeName returnType, MethodSpec.Builder methodSpecBuilder) {
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    // TODO: remove this hack
    if (dependencyInfos == null) {
        dependencyInfos = new HashSet<>();
        logger.w("no dI for key: " + key);
    }
    Preconditions.checkNotNull(dependencyInfos, String.format("dependencyInfo not found for key: %s", key));
    TypeName mapKeyType = returnType.typeArguments.get(0);
    TypeName mapValueType = returnType.typeArguments.get(1);
    BindingKey mapValueKey = BindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            logger.e("unwrapValue = false not supported yet. Consider using set binding.");
            return;
        }
        AnnotationValue mapKey = Utils.getAnnotationValue(elements, mapKeyMirror, "value");
        logger.l(Kind.NOTE, "mapKey: %s", mapKey.toString());
        methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
        if (utils.isMapWithBuiltinValueType(key)) {
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(mapValueKey, di));
        } else {
            addNewStatementToMethodSpec(methodSpecBuilder, di, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) AnnotationValue(javax.lang.model.element.AnnotationValue)

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