Search in sources :

Example 56 with PackageElement

use of javax.lang.model.element.PackageElement in project checker-framework by typetools.

the class ElementUtils method getBinaryName.

/**
 * Returns the binary name of the given type.
 *
 * @param te a type
 * @return the binary name of the type
 */
// string manipulation
@SuppressWarnings("signature:return")
@BinaryName
public static String getBinaryName(TypeElement te) {
    Element enclosing = te.getEnclosingElement();
    String simpleName = te.getSimpleName().toString();
    if (enclosing == null) {
        // is this possible?
        return simpleName;
    }
    if (ElementUtils.isTypeElement(enclosing)) {
        return getBinaryName((TypeElement) enclosing) + "$" + simpleName;
    } else if (enclosing.getKind() == ElementKind.PACKAGE) {
        PackageElement pe = (PackageElement) enclosing;
        if (pe.isUnnamed()) {
            return simpleName;
        } else {
            return pe.getQualifiedName() + "." + simpleName;
        }
    } else {
        // This case occurs for anonymous inner classes. Fall back to the flatname method.
        return ((ClassSymbol) te).flatName().toString();
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) BinaryName(org.checkerframework.checker.signature.qual.BinaryName)

Example 57 with PackageElement

use of javax.lang.model.element.PackageElement in project checker-framework by typetools.

the class ReportVisitor method checkReportUse.

/**
 * Check for uses of the {@link ReportUse} annotation. This method has to be called for every
 * explicit or implicit use of a type, most cases are simply covered by the type validator.
 *
 * @param node the tree for error reporting only
 * @param member the element from which to start looking
 */
private void checkReportUse(Tree node, Element member) {
    Element loop = member;
    while (loop != null) {
        boolean report = this.atypeFactory.getDeclAnnotation(loop, ReportUse.class) != null;
        if (report) {
            checker.reportError(node, "usage", node, ElementUtils.getQualifiedName(loop), loop.getKind(), ElementUtils.getQualifiedName(member), member.getKind());
            break;
        } else {
            if (loop.getKind() == ElementKind.PACKAGE) {
                loop = ElementUtils.parentPackage((PackageElement) loop, elements);
                continue;
            }
        }
        // Package will always be the last iteration.
        loop = loop.getEnclosingElement();
    }
}
Also used : ReportUse(org.checkerframework.common.util.report.qual.ReportUse) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement)

Example 58 with PackageElement

use of javax.lang.model.element.PackageElement in project checker-framework by typetools.

the class AnnotationClassLoader method loadBundledAnnotationClasses.

/**
 * Loads the set of annotation classes in the qual directory of a checker shipped with the Checker
 * Framework.
 */
private void loadBundledAnnotationClasses() {
    // retrieve the fully qualified class names of the annotations
    Set<@BinaryName String> annotationNames;
    // see whether the resource URL has a protocol of jar or file
    if (resourceURL != null && resourceURL.getProtocol().contentEquals("jar")) {
        // if the checker class file is contained within a jar, then the resource URL for the qual
        // directory will have the protocol "jar". This means the whole checker is loaded as a jar
        // file.
        JarURLConnection connection;
        // create a connection to the jar file
        try {
            connection = (JarURLConnection) resourceURL.openConnection();
            // disable caching / connection sharing of the low level URLConnection to the Jar file
            connection.setDefaultUseCaches(false);
            connection.setUseCaches(false);
            // connect to the Jar file
            connection.connect();
        } catch (IOException e) {
            throw new BugInCF("AnnotationClassLoader: cannot open a connection to the Jar file " + resourceURL.getFile());
        }
        // open up that jar file and extract annotation class names
        try (JarFile jarFile = connection.getJarFile()) {
            // get class names inside the jar file within the particular package
            annotationNames = getBundledAnnotationNamesFromJar(jarFile);
        } catch (IOException e) {
            throw new BugInCF("AnnotationClassLoader: cannot open the Jar file " + resourceURL.getFile());
        }
    } else if (resourceURL != null && resourceURL.getProtocol().contentEquals("file")) {
        // If the checker class file is found within the file system itself within some directory
        // (usually development build directories), then process the package as a file directory in
        // the file system and load the annotations contained in the qual directory.
        // open up the directory
        File packageDir = new File(resourceURL.getFile());
        annotationNames = getAnnotationNamesFromDirectory(packageName, packageDir, packageDir);
    } else {
        // We do not support a resource URL with any other protocols, so create an empty set.
        annotationNames = Collections.emptySet();
    }
    if (annotationNames.isEmpty()) {
        PackageElement pkgEle = checker.getElementUtils().getPackageElement(packageName);
        if (pkgEle != null) {
            for (Element e : pkgEle.getEnclosedElements()) {
                if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
                    // Elements needs to be annotated.
                    @SuppressWarnings("signature:assignment") @BinaryName String annoBinName = checker.getElementUtils().getBinaryName((TypeElement) e).toString();
                    annotationNames.add(annoBinName);
                }
            }
        }
    }
    supportedBundledAnnotationClasses.addAll(loadAnnotationClasses(annotationNames));
}
Also used : JarURLConnection(java.net.JarURLConnection) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) IOException(java.io.IOException) PackageElement(javax.lang.model.element.PackageElement) BugInCF(org.checkerframework.javacutil.BugInCF) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 59 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class SVGShapeProcessorTest method makeTypeElement.

private TypeElement makeTypeElement(final String name) {
    final Name typeElementName = mock(Name.class);
    final Name packageElementName = mock(Name.class);
    final TypeElement typeElement = mock(TypeElement.class);
    final PackageElement packageElement = mock(PackageElement.class);
    when(typeElementName.toString()).thenReturn(name);
    when(packageElementName.toString()).thenReturn(this.getClass().getPackage().getName());
    when(typeElement.getKind()).thenReturn(ElementKind.INTERFACE);
    when(typeElement.getEnclosingElement()).thenReturn(packageElement);
    when(typeElement.getSimpleName()).thenReturn(typeElementName);
    when(typeElement.getAnnotation(SVGViewFactory.class)).thenReturn(svgViewFactory);
    when(packageElement.getQualifiedName()).thenReturn(packageElementName);
    return typeElement;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Name(javax.lang.model.element.Name)

Example 60 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processDefinitionSets.

private boolean processDefinitionSets(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered definition set class [" + classElement.getSimpleName() + "]");
        final String packageName = packageElement.getQualifiedName().toString();
        final String className = classElement.getSimpleName().toString();
        processingContext.setDefinitionSet(packageName, className);
        String defSetClassName = packageName + "." + className;
        // Description fields.
        processFieldName(classElement, defSetClassName, ANNOTATION_DESCRIPTION, processingContext.getDefSetAnnotations().getDescriptionFieldNames(), false);
        // Definitions identifiers.
        DefinitionSet definitionSetAnn = e.getAnnotation(DefinitionSet.class);
        List<? extends TypeMirror> mirrors = null;
        try {
            definitionSetAnn.definitions();
        } catch (MirroredTypesException mte) {
            mirrors = mte.getTypeMirrors();
        }
        if (null == mirrors) {
            throw new RuntimeException("No graph class class declared for the @DefinitionSet.");
        }
        Set<String> defIds = new LinkedHashSet<>();
        for (TypeMirror mirror : mirrors) {
            if (mirror.getKind().equals(TypeKind.DECLARED)) {
                final TypeElement t = (TypeElement) ((DeclaredType) mirror).asElement();
                processingContext.getDefinitionElements().add(t);
            }
            String fqcn = mirror.toString();
            defIds.add(fqcn);
        }
        processingContext.getDefSetAnnotations().getDefinitionIds().put(defSetClassName, defIds);
        // Builder class.
        processDefinitionSetModelBuilder(e, defSetClassName, processingContext.getDefSetAnnotations().getBuilderFieldNames());
        // Graph factory type.
        TypeMirror mirror = null;
        try {
            definitionSetAnn.graphFactory();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No graph factory class declared for the @DefinitionSet.");
        }
        String fqcn = mirror.toString();
        processingContext.getDefSetAnnotations().getGraphFactoryTypes().put(defSetClassName, fqcn);
        // Definition Set's qualifier.
        try {
            definitionSetAnn.qualifier();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No qualifier class declared for the @DefinitionSet.");
        }
        processingContext.getDefSetAnnotations().getQualifiers().put(defSetClassName, mirror.toString());
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MirroredTypesException(javax.lang.model.type.MirroredTypesException) Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) PackageElement(javax.lang.model.element.PackageElement) DefinitionSet(org.kie.workbench.common.stunner.core.definition.annotation.DefinitionSet)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)134 TypeElement (javax.lang.model.element.TypeElement)93 Element (javax.lang.model.element.Element)52 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