Search in sources :

Example 46 with Elements

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
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 47 with Elements

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();
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Example 48 with Elements

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);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 49 with Elements

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);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Example 50 with Elements

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;
}
Also used : MoreTypes(com.google.auto.common.MoreTypes) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements) MoreElements(com.google.auto.common.MoreElements)

Aggregations

Elements (javax.lang.model.util.Elements)146 TypeElement (javax.lang.model.element.TypeElement)90 TypeMirror (javax.lang.model.type.TypeMirror)52 Element (javax.lang.model.element.Element)46 Types (javax.lang.model.util.Types)40 VariableElement (javax.lang.model.element.VariableElement)35 ExecutableElement (javax.lang.model.element.ExecutableElement)32 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)17 PackageElement (javax.lang.model.element.PackageElement)17 LinkedHashSet (java.util.LinkedHashSet)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 Test (org.junit.Test)15 List (java.util.List)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Set (java.util.Set)12 DeclaredType (javax.lang.model.type.DeclaredType)12 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12