Search in sources :

Example 1 with PackageElement

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

the class InterfaceValidator method validate.

public void validate(List<? extends CompilationUnitTree> compilationUnits) {
    try (BuckTracing.TraceSection trace = BUCK_TRACING.traceSection("buck.abi.validate")) {
        new InterfaceTypeAndConstantReferenceFinder(trees, new InterfaceTypeAndConstantReferenceFinder.Listener() {

            private final Set<Element> importedTypes = new HashSet<>();

            @Override
            public void onTypeImported(TypeElement type) {
                importedTypes.add(type);
            }

            @Override
            public void onTypeReferenceFound(TypeElement referencedType, TreePath path, Element enclosingElement) {
                PackageElement enclosingPackage = getPackageElement(enclosingElement);
                if (typeWillBeAvailable(referencedType) || referenceIsLegalForMissingTypes(path, enclosingPackage, referencedType)) {
                    // All good!
                    return;
                }
                String minimalQualifiedName = findMinimalQualifiedName(path, enclosingPackage, referencedType);
                // TODO(jkeljo): Clearer message
                trees.printMessage(messageKind, String.format("Must qualify the name: %s", minimalQualifiedName), path.getLeaf(), path.getCompilationUnit());
            }

            @Override
            public void onConstantReferenceFound(VariableElement constant, TreePath path, Element enclosingElement) {
                TypeElement constantEnclosingType = (TypeElement) constant.getEnclosingElement();
                if (typeWillBeAvailable(constantEnclosingType)) {
                    // All good!
                    return;
                }
                // TODO(jkeljo): Clearer message
                trees.printMessage(messageKind, String.format("Must inline the constant value: %s", constant.getConstantValue()), path.getLeaf(), path.getCompilationUnit());
            }

            private boolean typeWillBeAvailable(TypeElement type) {
                return isCompiledInCurrentRun(type) || isOnBootClasspath(type);
            }

            private boolean isCompiledInCurrentRun(TypeElement typeElement) {
                return trees.getPath(typeElement) != null;
            }

            private boolean isOnBootClasspath(TypeElement typeElement) {
                return bootClasspathOracle.isOnBootClasspath(elements.getBinaryName(typeElement).toString());
            }

            private boolean referenceIsLegalForMissingTypes(TreePath path, PackageElement enclosingPackage, TypeElement referencedTypeElement) {
                return isImported(referencedTypeElement) || isTopLevelTypeInPackage(referencedTypeElement, enclosingPackage) || isFullyQualified(path, referencedTypeElement);
            }

            private boolean isImported(TypeElement referencedTypeElement) {
                return importedTypes.contains(referencedTypeElement);
            }

            private boolean isTopLevelTypeInPackage(TypeElement referencedTypeElement, PackageElement enclosingPackage) {
                return enclosingPackage == referencedTypeElement.getEnclosingElement();
            }

            private boolean isFullyQualified(TreePath path, TypeElement referencedTypeElement) {
                return referencedTypeElement.getQualifiedName().contentEquals(TreeBackedTrees.treeToName(path.getLeaf()));
            }

            private String findMinimalQualifiedName(TreePath path, PackageElement enclosingPackage, TypeElement typeElement) {
                List<QualifiedNameable> enclosingElements = new ArrayList<>();
                QualifiedNameable walker = typeElement;
                while (walker.getKind() != ElementKind.PACKAGE && !referenceIsLegalForMissingTypes(path, enclosingPackage, (TypeElement) walker)) {
                    enclosingElements.add(walker);
                    walker = (QualifiedNameable) walker.getEnclosingElement();
                }
                enclosingElements.add(walker);
                StringBuilder resultBuilder = new StringBuilder();
                for (int i = enclosingElements.size() - 1; i >= 0; i--) {
                    QualifiedNameable element = enclosingElements.get(i);
                    if (element.getKind() == ElementKind.PACKAGE) {
                        resultBuilder.append(element.getQualifiedName());
                    } else {
                        resultBuilder.append(element.getSimpleName());
                    }
                    if (i > 0) {
                        resultBuilder.append(".");
                    }
                }
                return resultBuilder.toString();
            }
        }).findReferences(compilationUnits);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) BuckTracing(com.facebook.buck.event.api.BuckTracing) TreePath(com.sun.source.util.TreePath) QualifiedNameable(javax.lang.model.element.QualifiedNameable) PackageElement(javax.lang.model.element.PackageElement)

Example 2 with PackageElement

use of javax.lang.model.element.PackageElement 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 3 with PackageElement

use of javax.lang.model.element.PackageElement 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 4 with PackageElement

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

the class TreeBackedTypeElementTest method testGetEnclosingElementForBuiltInTopLevelClasses.

@Ignore("TODO(jkeljo): Need to wrap elements coming out of javac to ensure callers always get" + "our PackageElement impl instead of javac's.")
@Test
public void testGetEnclosingElementForBuiltInTopLevelClasses() throws IOException {
    initCompiler();
    TypeElement stringElement = elements.getTypeElement("java.lang.String");
    PackageElement javaLangElement = elements.getPackageElement("java.lang");
    assertSame(javaLangElement, stringElement.getEnclosingElement());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with PackageElement

use of javax.lang.model.element.PackageElement in project RoboBinding by RoboBinding.

the class ElementWrapperTest method unsupportedElements.

@DataPoints("unsupportedElements")
public static Element[] unsupportedElements() {
    Elements elements = compilation.getElements();
    PackageElement packageElement = elements.getPackageElement("java.lang");
    TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
    VariableElement variableElement = ElementFilter.fieldsIn(typeElement.getEnclosedElements()).get(0);
    return new Element[] { packageElement, variableElement };
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)133 TypeElement (javax.lang.model.element.TypeElement)92 Element (javax.lang.model.element.Element)51 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8