Search in sources :

Example 51 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.
 *
 * <p>The iteration order of elements of the returned map will be the order in which the {@link
 * ExecutableElement}s are defined in {@code annotation}'s {@linkplain
 * AnnotationMirror#getAnnotationType() type}.
 */
public static ImmutableMap<ExecutableElement, AnnotationValue> getAnnotationValuesWithDefaults(AnnotationMirror annotation) {
    ImmutableMap.Builder<ExecutableElement, AnnotationValue> values = ImmutableMap.builder();
    // Use unmodifiableMap to eliminate wildcards, which cause issues for our nullness checker.
    @SuppressWarnings("GetElementValues") Map<ExecutableElement, AnnotationValue> declaredValues = unmodifiableMap(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.build();
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 52 with AnnotationValue

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

the class AnnotationValuesTest method getAnnotationMirror.

@Test
public void getAnnotationMirror() {
    TypeElement insideAnnotation = getTypeElement(InsideAnnotation.class);
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "insideAnnotationValue");
    AnnotationMirror annotationMirror = AnnotationValues.getAnnotationMirror(value);
    assertThat(annotationMirror.getAnnotationType().asElement()).isEqualTo(insideAnnotation);
    assertThat(AnnotationMirrors.getAnnotationValue(annotationMirror, "value").getValue()).isEqualTo(19);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 53 with AnnotationValue

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

the class AnnotationValuesTest method getDoubles.

@Test
public void getDoubles() {
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "doubleValues");
    assertThat(AnnotationValues.getDoubles(value)).containsExactly(17D, 18D).inOrder();
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 54 with AnnotationValue

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

the class AnnotationValuesTest method getAnnotationMirrors.

@Test
public void getAnnotationMirrors() {
    TypeElement insideAnnotation = getTypeElement(InsideAnnotation.class);
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "insideAnnotationValues");
    ImmutableList<AnnotationMirror> annotationMirrors = AnnotationValues.getAnnotationMirrors(value);
    ImmutableList<Element> valueElements = annotationMirrors.stream().map(AnnotationMirror::getAnnotationType).map(DeclaredType::asElement).collect(toImmutableList());
    assertThat(valueElements).containsExactly(insideAnnotation, insideAnnotation);
    ImmutableList<Object> valuesStoredInAnnotation = annotationMirrors.stream().map(annotationMirror -> AnnotationMirrors.getAnnotationValue(annotationMirror, "value").getValue()).collect(toImmutableList());
    assertThat(valuesStoredInAnnotation).containsExactly(20, 21).inOrder();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) VariableElement(javax.lang.model.element.VariableElement) RunWith(org.junit.runner.RunWith) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Types(javax.lang.model.util.Types) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Elements(javax.lang.model.util.Elements) Collectors.joining(java.util.stream.Collectors.joining) Correspondence(com.google.common.truth.Correspondence) TypeMirror(javax.lang.model.type.TypeMirror) Rule(org.junit.Rule) ImmutableList(com.google.common.collect.ImmutableList) DeclaredType(javax.lang.model.type.DeclaredType) CompilationRule(com.google.testing.compile.CompilationRule) AnnotationValue(javax.lang.model.element.AnnotationValue) Name(javax.lang.model.element.Name) Before(org.junit.Before) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 55 with AnnotationValue

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

the class AnnotationValuesTest method getShort.

@Test
public void getShort() {
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "shortValue");
    assertThat(AnnotationValues.getShort(value)).isEqualTo((short) 10);
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

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