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();
}
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);
}
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();
}
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();
}
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);
}
Aggregations