use of javax.lang.model.util.Elements in project auto by google.
the class MoreTypesTest method asElement.
@Test
public void asElement() {
Elements elements = compilationRule.getElements();
TypeElement stringElement = elements.getTypeElement("java.lang.String");
assertThat(MoreTypes.asElement(stringElement.asType())).isEqualTo(stringElement);
TypeParameterElement setParameterElement = Iterables.getOnlyElement(compilationRule.getElements().getTypeElement("java.util.Set").getTypeParameters());
assertThat(MoreTypes.asElement(setParameterElement.asType())).isEqualTo(setParameterElement);
// we don't test error types because those are very hard to get predictably
}
use of javax.lang.model.util.Elements in project auto by google.
the class MoreTypesTest method testIsConversionFromObjectUnchecked_no.
@Test
public void testIsConversionFromObjectUnchecked_no() {
Elements elements = compilationRule.getElements();
TypeElement notUnchecked = elements.getTypeElement(NotUnchecked.class.getCanonicalName());
for (VariableElement field : ElementFilter.fieldsIn(notUnchecked.getEnclosedElements())) {
TypeMirror type = field.asType();
expect.withMessage("Casting to %s is not unchecked", type).that(MoreTypes.isConversionFromObjectUnchecked(type)).isFalse();
}
}
use of javax.lang.model.util.Elements in project auto by google.
the class MoreTypesTest method testReferencedTypes.
@Test
public void testReferencedTypes() {
Elements elements = compilationRule.getElements();
TypeElement testDataElement = elements.getTypeElement(ReferencedTypesTestData.class.getCanonicalName());
ImmutableMap<String, VariableElement> fieldIndex = FluentIterable.from(ElementFilter.fieldsIn(testDataElement.getEnclosedElements())).uniqueIndex(input -> input.getSimpleName().toString());
TypeElement objectElement = elements.getTypeElement(Object.class.getCanonicalName());
TypeElement stringElement = elements.getTypeElement(String.class.getCanonicalName());
TypeElement integerElement = elements.getTypeElement(Integer.class.getCanonicalName());
TypeElement setElement = elements.getTypeElement(Set.class.getCanonicalName());
TypeElement mapElement = elements.getTypeElement(Map.class.getCanonicalName());
TypeElement charSequenceElement = elements.getTypeElement(CharSequence.class.getCanonicalName());
assertThat(referencedTypes(fieldIndex, "f1")).containsExactly(objectElement);
assertThat(referencedTypes(fieldIndex, "f2")).containsExactly(setElement, stringElement);
assertThat(referencedTypes(fieldIndex, "f3")).containsExactly(mapElement, stringElement, objectElement);
assertThat(referencedTypes(fieldIndex, "f4")).containsExactly(integerElement);
assertThat(referencedTypes(fieldIndex, "f5")).containsExactly(setElement);
assertThat(referencedTypes(fieldIndex, "f6")).containsExactly(setElement, charSequenceElement);
assertThat(referencedTypes(fieldIndex, "f7")).containsExactly(mapElement, stringElement, setElement, charSequenceElement);
assertThat(referencedTypes(fieldIndex, "f8")).containsExactly(stringElement);
assertThat(referencedTypes(fieldIndex, "f9")).containsExactly(stringElement);
assertThat(referencedTypes(fieldIndex, "f10")).isEmpty();
assertThat(referencedTypes(fieldIndex, "f11")).isEmpty();
assertThat(referencedTypes(fieldIndex, "f12")).containsExactly(setElement, stringElement);
}
use of javax.lang.model.util.Elements in project auto by google.
the class BasicAnnotationProcessorTest method processingStepAsStepProcessElementsMatchClasses.
/**
* Tests that a {@link ProcessingStep} passed to {@link
* BasicAnnotationProcessor#asStep(ProcessingStep)} still gets passed the correct arguments to
* {@link Step#process(ImmutableSetMultimap)}.
*/
@Test
public void processingStepAsStepProcessElementsMatchClasses() {
Elements elements = compilation.getElements();
String anAnnotationName = AnAnnotation.class.getCanonicalName();
String referencesAClassName = ReferencesAClass.class.getCanonicalName();
TypeElement anAnnotationElement = elements.getTypeElement(anAnnotationName);
TypeElement referencesAClassElement = elements.getTypeElement(referencesAClassName);
MultiAnnotationProcessingStep processingStep = new MultiAnnotationProcessingStep();
BasicAnnotationProcessor.asStep(processingStep).process(ImmutableSetMultimap.of(anAnnotationName, anAnnotationElement, referencesAClassName, referencesAClassElement));
assertThat(processingStep.getElementsByAnnotation()).containsExactly(AnAnnotation.class, anAnnotationElement, ReferencesAClass.class, referencesAClassElement);
}
use of javax.lang.model.util.Elements in project auto by google.
the class ToPrettyStringValidator method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
Types types = processingEnv.getTypeUtils();
Elements elements = processingEnv.getElementUtils();
TypeElement toPrettyString = elements.getTypeElement(TO_PRETTY_STRING_NAME);
Set<ExecutableElement> annotatedMethods = methodsIn(roundEnvironment.getElementsAnnotatedWith(toPrettyString));
for (ExecutableElement method : annotatedMethods) {
validateMethod(method, elements);
}
validateSingleToPrettyStringMethod(annotatedMethods, types, elements);
return false;
}
Aggregations