Search in sources :

Example 21 with TypeElement

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

the class DescriptorAndSignatureFactoryTestBase method findErrors.

private void findErrors(TypeElement typeElement, Function<FieldNode, String> fieldNodeExpectedValueGetter, Function<MethodNode, String> methodNodeExpectedValueGetter, Function<ClassNode, String> classNodeExpectedValueGetter, Function<Element, String> elementActualValueGetter) throws IOException {
    ClassNode typeNode = getClassNode(elements.getBinaryName(typeElement).toString());
    for (Element enclosedElement : typeElement.getEnclosedElements()) {
        Name elementName = enclosedElement.getSimpleName();
        String actual = elementActualValueGetter.apply(enclosedElement);
        switch(enclosedElement.getKind()) {
            case FIELD:
                checkValue("Field", elementName, fieldNodeExpectedValueGetter.apply(getFieldNode(typeNode, elementName)), actual);
                break;
            case CONSTRUCTOR:
            case METHOD:
                checkValue("Method", elementName, methodNodeExpectedValueGetter.apply(getMethodNode(typeNode, elementName)), actual);
                break;
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
                ClassNode innerTypeNode = getClassNode(elements.getBinaryName((TypeElement) enclosedElement).toString());
                checkValue("Class", elementName, classNodeExpectedValueGetter.apply(innerTypeNode), actual);
                findErrors((TypeElement) enclosedElement, fieldNodeExpectedValueGetter, methodNodeExpectedValueGetter, classNodeExpectedValueGetter, elementActualValueGetter);
                break;
            // $CASES-OMITTED$
            default:
                fail(String.format("Didn't implement testing for element kind %s", enclosedElement.getKind()));
                continue;
        }
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Name(javax.lang.model.element.Name)

Example 22 with TypeElement

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

the class TreeBackedPackageElementTest method testCanExtendPackageFromDependencies.

@Test
public void testCanExtendPackageFromDependencies() throws IOException {
    compile(Joiner.on('\n').join("package java.util;", "class Foo { }"));
    PackageElement javaUtilPackage = elements.getPackageElement("java.util");
    TypeElement listType = elements.getTypeElement("java.util.List");
    TypeElement fooType = elements.getTypeElement("java.util.Foo");
    assertPackageContains(javaUtilPackage, listType);
    assertPackageContains(javaUtilPackage, fooType);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Test(org.junit.Test)

Example 23 with TypeElement

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

the class TreeBackedTreesTest method testGetTreeGetPathRoundtripTypeElement.

@Test
public void testGetTreeGetPathRoundtripTypeElement() throws IOException {
    compile("class Foo<T, U> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    Tree fooTree = trees.getTree(fooElement);
    TreePath fooPath = trees.getPath(fooElement);
    assertSame(fooPath.getLeaf(), fooTree);
    assertSame(fooElement, trees.getElement(fooPath));
}
Also used : TreePath(com.sun.source.util.TreePath) TypeElement(javax.lang.model.element.TypeElement) Tree(com.sun.source.tree.Tree) Test(org.junit.Test)

Example 24 with TypeElement

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

the class StandaloneDeclaredTypeTest method testToStringWithGenerics.

@Test
public void testToStringWithGenerics() throws IOException {
    initCompiler();
    TypeElement mapElement = elements.getTypeElement("java.util.Map");
    TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
    TypeMirror integerType = elements.getTypeElement("java.lang.Integer").asType();
    DeclaredType mapStringIntType = types.getDeclaredType(mapElement, stringType, integerType);
    assertEquals("java.util.Map<java.lang.String,java.lang.Integer>", mapStringIntType.toString());
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 25 with TypeElement

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

the class TypeResolverTest method testMultiDimArrayTypeResolves.

@Test
public void testMultiDimArrayTypeResolves() throws IOException {
    compile("abstract class Foo extends java.util.ArrayList<java.lang.String[][]> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement listElement = elements.getTypeElement("java.util.ArrayList");
    TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
    ArrayType stringArrayType = types.getArrayType(stringType);
    ArrayType stringArrayArrayType = types.getArrayType(stringArrayType);
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, stringArrayArrayType);
    assertSameType(expectedSuperclass, fooElement.getSuperclass());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) 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