Search in sources :

Example 66 with AnnotationValue

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

the class AnnotationValuesTest method getTypeMirrors.

@Test
public void getTypeMirrors() {
    TypeMirror insideClassA = getTypeElement(InsideClassA.class).asType();
    TypeMirror insideClassB = getTypeElement(InsideClassB.class).asType();
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "classValues");
    ImmutableList<DeclaredType> valueElements = AnnotationValues.getTypeMirrors(value);
    assertThat(valueElements).comparingElementsUsing(Correspondence.from(types::isSameType, "has Same Type")).containsExactly(insideClassA, insideClassB).inOrder();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 67 with AnnotationValue

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

the class AnnotationValuesTest method getShorts.

@Test
public void getShorts() {
    AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationMirror, "shortValues");
    assertThat(AnnotationValues.getShorts(value)).containsExactly((short) 11, (short) 12).inOrder();
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 68 with AnnotationValue

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

the class SimpleTypeAnnotationValueTest method primitiveClass.

@Test
public void primitiveClass() {
    AnnotationValue annotationValue = SimpleTypeAnnotationValue.of(primitiveType);
    assertThat(annotationValue.getValue()).isEqualTo(primitiveType);
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 69 with AnnotationValue

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

the class SimpleAnnotationMirrorTest method multipleValues.

@Test
public void multipleValues() {
    TypeElement multipleValues = getTypeElement(MultipleValues.class);
    Map<String, AnnotationValue> values = new HashMap<>();
    values.put("value1", intValue(1));
    values.put("value2", intValue(2));
    assertThat(SimpleAnnotationMirror.of(multipleValues, values).getElementValues()).hasSize(2);
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) Test(org.junit.Test)

Example 70 with AnnotationValue

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

the class AutoValueishProcessor method warnAboutPrimitiveArrays.

private void warnAboutPrimitiveArrays(TypeElement autoValueClass, ExecutableElement getter) {
    boolean suppressed = false;
    Optional<AnnotationMirror> maybeAnnotation = getAnnotationMirror(getter, "java.lang.SuppressWarnings");
    if (maybeAnnotation.isPresent()) {
        AnnotationValue listValue = getAnnotationValue(maybeAnnotation.get(), "value");
        suppressed = listValue.accept(new ContainsMutableVisitor(), null);
    }
    if (!suppressed) {
        // If the primitive-array property method is defined directly inside the @AutoValue class,
        // then our error message should point directly to it. But if it is inherited, we don't
        // want to try to make the error message point to the inherited definition, since that would
        // be confusing (there is nothing wrong with the definition itself), and won't work if the
        // inherited class is not being recompiled. Instead, in this case we point to the @AutoValue
        // class itself, and we include extra text in the error message that shows the full name of
        // the inherited method.
        boolean sameClass = getter.getEnclosingElement().equals(autoValueClass);
        Element element = sameClass ? getter : autoValueClass;
        String context = sameClass ? "" : (" Method: " + getter.getEnclosingElement() + "." + getter);
        errorReporter.reportWarning(element, "[AutoValueMutable] An @%s property that is a primitive array returns the original" + " array, which can therefore be modified by the caller. If this is OK, you can" + " suppress this warning with @SuppressWarnings(\"mutable\"). Otherwise, you should" + " replace the property with an immutable type, perhaps a simple wrapper around the" + " original array.%s", simpleAnnotationName, context);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) AnnotationMirrors.getAnnotationValue(com.google.auto.common.AnnotationMirrors.getAnnotationValue) 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