Search in sources :

Example 26 with TypeElement

use of javax.lang.model.element.TypeElement in project buck by facebook.

the class TreeBackedTreesTest method testNoTreeOrPathForPrecompiledCode.

@Test
public void testNoTreeOrPathForPrecompiledCode() throws IOException {
    compile("class Foo { }");
    TypeElement stringElement = elements.getTypeElement("java.lang.String");
    assertNull(trees.getTree(stringElement));
    assertNull(trees.getPath(stringElement));
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Test(org.junit.Test)

Example 27 with TypeElement

use of javax.lang.model.element.TypeElement in project buck by facebook.

the class TreeBackedTypeElementTest method testGetQualifiedNameInnerClass.

@Test
public void testGetQualifiedNameInnerClass() throws IOException {
    compile(Joiner.on('\n').join("package com.facebook.buck;", "public class Foo {", "  public class Bar { }", "}"));
    TypeElement element = elements.getTypeElement("com.facebook.buck.Foo.Bar");
    assertNameEquals("com.facebook.buck.Foo.Bar", element.getQualifiedName());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Test(org.junit.Test)

Example 28 with TypeElement

use of javax.lang.model.element.TypeElement in project buck by facebook.

the class TreeBackedTypeElementTest method testGetEnclosingElementForTopLevelClasses.

@Test
public void testGetEnclosingElementForTopLevelClasses() throws IOException {
    compile("class Foo { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    PackageElement unnamedPackage = elements.getPackageElement("");
    assertSame(unnamedPackage, fooElement.getEnclosingElement());
    assertTrue(unnamedPackage.getEnclosedElements().contains(fooElement));
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Test(org.junit.Test)

Example 29 with TypeElement

use of javax.lang.model.element.TypeElement in project buck by facebook.

the class TreeBackedTypeElementTest method testEnclosedClasses.

@Test
public void testEnclosedClasses() throws IOException {
    compile(Joiner.on('\n').join("class Foo {", "  class Bar { }", "}"));
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement barElement = elements.getTypeElement("Foo.Bar");
    List<Element> enclosedElements = new ArrayList<>(fooElement.getEnclosedElements());
    assertThat(enclosedElements, Matchers.hasItems(barElement));
    assertSame(fooElement, barElement.getEnclosingElement());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 30 with TypeElement

use of javax.lang.model.element.TypeElement in project buck by facebook.

the class TreeBackedTypeElementTest method testGetSuperclassOtherSuperclass.

@Test
public void testGetSuperclassOtherSuperclass() throws IOException {
    compile(ImmutableMap.of("Foo.java", Joiner.on('\n').join("package com.facebook.foo;", "public class Foo extends com.facebook.bar.Bar { }"), "Bar.java", Joiner.on('\n').join("package com.facebook.bar;", "public class Bar { }")));
    TypeElement fooElement = elements.getTypeElement("com.facebook.foo.Foo");
    TypeElement barElement = elements.getTypeElement("com.facebook.bar.Bar");
    DeclaredType superclass = (DeclaredType) fooElement.getSuperclass();
    assertSame(barElement, superclass.asElement());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)1564 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)504 TypeMirror (javax.lang.model.type.TypeMirror)421 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)202 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63