Search in sources :

Example 1 with BinaryName

use of org.checkerframework.checker.signature.qual.BinaryName 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 2 with BinaryName

use of org.checkerframework.checker.signature.qual.BinaryName in project checker-framework by typetools.

the class ValueTreeAnnotator method visitFieldAccess.

/**
 * Visit a tree that might be a field access.
 *
 * @param tree a tree that might be a field access. It is either a MemberSelectTree or an
 *     IdentifierTree (if the programmer omitted the leading `this.`).
 * @param type its type
 */
private void visitFieldAccess(ExpressionTree tree, AnnotatedTypeMirror type) {
    if (!TreeUtils.isFieldAccess(tree) || !handledByValueChecker(type)) {
        return;
    }
    VariableElement fieldElement = (VariableElement) TreeUtils.elementFromTree(tree);
    Object value = fieldElement.getConstantValue();
    if (value != null) {
        // The field is a compile-time constant.
        type.replaceAnnotation(atypeFactory.createResultingAnnotation(type.getUnderlyingType(), value));
        return;
    }
    if (ElementUtils.isStatic(fieldElement) && ElementUtils.isFinal(fieldElement)) {
        // The field is static and final, but its declaration does not initialize it to a
        // compile-time constant.  Obtain its value reflectively.
        Element classElement = fieldElement.getEnclosingElement();
        if (classElement != null) {
            @// TODO: bug in ValueAnnotatedTypeFactory.
            SuppressWarnings(// TODO: bug in ValueAnnotatedTypeFactory.
            "signature") @BinaryName String classname = ElementUtils.getQualifiedClassName(classElement).toString();
            // https://tinyurl.com/cfissue/658 for Name.toString()
            @SuppressWarnings("signature") @Identifier String fieldName = fieldElement.getSimpleName().toString();
            value = atypeFactory.evaluator.evaluateStaticFieldAccess(classname, fieldName, tree);
            if (value != null) {
                type.replaceAnnotation(atypeFactory.createResultingAnnotation(type.getUnderlyingType(), value));
            }
            return;
        }
    }
    return;
}
Also used : Identifier(org.checkerframework.checker.signature.qual.Identifier) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) VariableElement(javax.lang.model.element.VariableElement)

Example 3 with BinaryName

use of org.checkerframework.checker.signature.qual.BinaryName 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 4 with BinaryName

use of org.checkerframework.checker.signature.qual.BinaryName in project checker-framework by typetools.

the class AnnotationClassLoader method getAnnotationNamesFromDirectory.

/**
 * Retrieves all annotation names from the current directory, and recursively descends and
 * retrieves annotation names from sub-directories.
 *
 * @param packageName the name of the package that contains the qual package, or null
 * @param rootDirectory a {@link File} object representing the root directory of a set of
 *     annotations, which is subtracted from class names to retrieve each class's fully qualified
 *     class names
 * @param currentDirectory a {@link File} object representing the current sub-directory of the
 *     root directory
 * @return a set fully qualified annotation class name, for annotations in the root directory or
 *     its sub-directories
 */
// TODO: reduce use of string manipulation
@SuppressWarnings("signature")
private final Set<@BinaryName String> getAnnotationNamesFromDirectory(@Nullable @DotSeparatedIdentifiers final String packageName, final File rootDirectory, final File currentDirectory) {
    Set<@BinaryName String> results = new LinkedHashSet<>();
    // Full path to root directory
    String rootPath = rootDirectory.getAbsolutePath();
    // check every file and directory within the current directory
    File[] directoryContents = currentDirectory.listFiles();
    Arrays.sort(directoryContents, new Comparator<File>() {

        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (File file : directoryContents) {
        if (file.isFile()) {
            // TODO: simplify all this string manipulation.
            // Full file name, including path to file
            String fullFileName = file.getAbsolutePath();
            // Simple file name
            String fileName = fullFileName.substring(fullFileName.lastIndexOf(File.separator) + 1, fullFileName.length());
            // Path to file
            String filePath = fullFileName.substring(0, fullFileName.lastIndexOf(File.separator));
            // Package name beginning with "qual"
            String qualPackage = null;
            if (!filePath.equals(rootPath)) {
                qualPackage = filePath.substring(rootPath.length() + 1, filePath.length()).replace(SLASH, DOT);
            }
            // Simple annotation name, which is the same as the file name (without directory)
            // but with file extension removed.
            @BinaryName String annotationName = fileName;
            if (fileName.lastIndexOf(DOT) != -1) {
                annotationName = fileName.substring(0, fileName.lastIndexOf(DOT));
            }
            // Fully qualified annotation class name (a @BinaryName, not a @FullyQualifiedName)
            @BinaryName String fullyQualifiedAnnoName = Signatures.addPackage(packageName, Signatures.addPackage(qualPackage, annotationName));
            if (fileName.endsWith(CLASS_SUFFIX)) {
                // add the fully qualified annotation class name to the set
                results.add(fullyQualifiedAnnoName);
            }
        } else if (file.isDirectory()) {
            // recursively add all sub directories's fully qualified annotation class name
            results.addAll(getAnnotationNamesFromDirectory(packageName, rootDirectory, file));
        }
    }
    return results;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 5 with BinaryName

use of org.checkerframework.checker.signature.qual.BinaryName in project checker-framework by typetools.

the class WholeProgramInferenceScenesStorage method getFieldAnnotations.

@Override
public ATypeElement getFieldAnnotations(Element element, String fieldName, AnnotatedTypeMirror lhsATM, AnnotatedTypeFactory atypeFactory) {
    ClassSymbol enclosingClass = ((VarSymbol) element).enclClass();
    String file = getFileForElement(element);
    // https://tinyurl.com/cfissue/3094
    @SuppressWarnings("signature") @BinaryName String className = enclosingClass.flatname.toString();
    AClass classAnnos = getClassAnnos(className, file, enclosingClass);
    AField field = classAnnos.fields.getVivify(fieldName);
    field.setTypeMirror(lhsATM.getUnderlyingType());
    return field.type;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) AClass(scenelib.annotations.el.AClass) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) AField(scenelib.annotations.el.AField)

Aggregations

BinaryName (org.checkerframework.checker.signature.qual.BinaryName)7 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3 Element (javax.lang.model.element.Element)3 TypeElement (javax.lang.model.element.TypeElement)3 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)2 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 PackageElement (javax.lang.model.element.PackageElement)2 VariableElement (javax.lang.model.element.VariableElement)2 IOException (java.io.IOException)1 JarURLConnection (java.net.JarURLConnection)1 LinkedHashSet (java.util.LinkedHashSet)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 Identifier (org.checkerframework.checker.signature.qual.Identifier)1 BugInCF (org.checkerframework.javacutil.BugInCF)1 AClass (scenelib.annotations.el.AClass)1 AField (scenelib.annotations.el.AField)1