Search in sources :

Example 66 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class AnnotationFileElementTypes method getJarURLConnectionToJdk.

/**
 * Returns a JarURLConnection to "/jdk*".
 *
 * @return a JarURLConnection to "/jdk*"
 */
private JarURLConnection getJarURLConnectionToJdk() {
    URL resourceURL = factory.getClass().getResource("/annotated-jdk");
    JarURLConnection connection;
    try {
        connection = (JarURLConnection) resourceURL.openConnection();
        // disable caching / connection sharing of the low level URLConnection to the Jarfile
        connection.setDefaultUseCaches(false);
        connection.setUseCaches(false);
        connection.connect();
    } catch (IOException e) {
        throw new BugInCF("cannot open a connection to the Jar file " + resourceURL.getFile(), e);
    }
    return connection;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) URL(java.net.URL)

Example 67 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class AnnotationFileElementTypes method injectRecordComponentType.

/**
 * Adds annotations from stub files for the corresponding record components (if the given
 * constructor/method is the canonical constructor or a record accessor). Such transfer is
 * automatically done by javac usually, but not from stubs.
 *
 * @param types a Types instance used for checking type equivalence
 * @param elt a member. This method does nothing if it's not a method or constructor.
 * @param memberType the type corresponding to the element elt; side-effected by this method
 */
public void injectRecordComponentType(Types types, Element elt, AnnotatedExecutableType memberType) {
    if (parsing) {
        throw new BugInCF("parsing while calling injectRecordComponentType");
    }
    if (elt.getKind() == ElementKind.METHOD) {
        if (((ExecutableElement) elt).getParameters().isEmpty()) {
            String recordName = ElementUtils.getQualifiedName(elt.getEnclosingElement());
            AnnotationFileParser.RecordStub recordComponentType = annotationFileAnnos.records.get(recordName);
            if (recordComponentType != null) {
                // If the record component has an annotation in the stub, the component annotation
                // replaces any from the same hierarchy on the accessor method, unless there is an
                // accessor in the stubs file (which may or may not have an annotation in the same
                // hierarchy;
                // the user may want to specify the annotation or deliberately not annotate the accessor).
                // We thus only replace the method annotation with the component annotation
                // if there is no accessor in the stubs file:
                RecordComponentStub recordComponentStub = recordComponentType.componentsByName.get(elt.getSimpleName().toString());
                if (recordComponentStub != null && !recordComponentStub.hasAccessorInStubs()) {
                    replaceAnnotations(memberType.getReturnType(), recordComponentStub.type);
                }
            }
        }
    } else if (elt.getKind() == ElementKind.CONSTRUCTOR) {
        if (AnnotationFileUtil.isCanonicalConstructor((ExecutableElement) elt, types)) {
            TypeElement enclosing = (TypeElement) elt.getEnclosingElement();
            AnnotationFileParser.RecordStub recordComponentType = annotationFileAnnos.records.get(enclosing.getQualifiedName().toString());
            if (recordComponentType != null) {
                List<AnnotatedTypeMirror> componentsInCanonicalConstructor = recordComponentType.getComponentsInCanonicalConstructor();
                if (componentsInCanonicalConstructor != null) {
                    for (int i = 0; i < componentsInCanonicalConstructor.size(); i++) {
                        replaceAnnotations(memberType.getParameterTypes().get(i), componentsInCanonicalConstructor.get(i));
                    }
                }
            }
        }
    }
}
Also used : RecordComponentStub(org.checkerframework.framework.stub.AnnotationFileParser.RecordComponentStub) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) List(java.util.List) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 68 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class AnnotationFileStore method addFileOrDirectory.

/**
 * If {@code location} is a file, stores it in this as an annotation file. If {@code location} is
 * a directory, stores all annotation files contained in it.
 *
 * @param location an annotation file or a directory containing annotation files
 */
public void addFileOrDirectory(File location) {
    if (location.isDirectory()) {
        for (File child : location.listFiles()) {
            addFileOrDirectory(child);
        }
        return;
    }
    if (location.isFile() && location.getName().endsWith(".ajava")) {
        try {
            CompilationUnit root = JavaParserUtil.parseCompilationUnit(location);
            for (TypeDeclaration<?> type : root.getTypes()) {
                String name = JavaParserUtil.getFullyQualifiedName(type, root);
                if (!annotationFiles.containsKey(name)) {
                    annotationFiles.put(name, new ArrayList<>());
                }
                annotationFiles.get(name).add(location.getPath());
            }
        } catch (FileNotFoundException e) {
            throw new BugInCF("Unable to open annotation file: " + location.getPath(), e);
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) FileNotFoundException(java.io.FileNotFoundException) BugInCF(org.checkerframework.javacutil.BugInCF) File(java.io.File)

Example 69 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class RemoveAnnotationsForInference method process.

/**
 * Process each file in the given directory; see the {@link RemoveAnnotationsForInference class
 * documentation} for details.
 *
 * @param dir directory to process
 */
private static void process(String dir) {
    Path root = JavaStubifier.dirnameToPath(dir);
    RemoveAnnotationsCallback rac = new RemoveAnnotationsCallback();
    CollectionStrategy strategy = new ParserCollectionStrategy();
    // Required to include directories that contain a module-info.java, which don't parse by
    // default.
    strategy.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
    ProjectRoot projectRoot = strategy.collect(root);
    for (SourceRoot sourceRoot : projectRoot.getSourceRoots()) {
        try {
            sourceRoot.parse("", rac);
        } catch (IOException e) {
            throw new BugInCF(e);
        }
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) CollectionStrategy(com.github.javaparser.utils.CollectionStrategy) ParserCollectionStrategy(com.github.javaparser.utils.ParserCollectionStrategy) ParserCollectionStrategy(com.github.javaparser.utils.ParserCollectionStrategy) ProjectRoot(com.github.javaparser.utils.ProjectRoot) IOException(java.io.IOException) SourceRoot(com.github.javaparser.utils.SourceRoot) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 70 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class ToIndexFileConverter method convert.

/**
 * Augment given scene with information from stubfile, reading stubs from input stream and writing
 * JAIF to output stream.
 *
 * @param scene the initial scene
 * @param in stubfile contents
 * @param out JAIF representing augmented scene
 * @throws ParseException if the stub file cannot be parsed
 * @throws DefException if two different definitions of the same annotation cannot be unified
 * @throws IOException if there is trouble with file reading or writing
 */
private static void convert(AScene scene, InputStream in, OutputStream out) throws IOException, DefException, ParseException {
    StubUnit iu;
    try {
        iu = JavaParserUtil.parseStubUnit(in);
    } catch (ParseProblemException e) {
        iu = null;
        throw new BugInCF("ToIndexFileConverter: exception from JavaParser.parseStubUnit for InputStream." + System.lineSeparator() + "Problem message with problems encountered: " + e.getMessage());
    }
    extractScene(iu, scene);
    try (Writer w = new BufferedWriter(new OutputStreamWriter(out))) {
        IndexFileWriter.write(scene, w);
    }
}
Also used : StubUnit(com.github.javaparser.ast.StubUnit) OutputStreamWriter(java.io.OutputStreamWriter) BugInCF(org.checkerframework.javacutil.BugInCF) Writer(java.io.Writer) IndexFileWriter(scenelib.annotations.io.IndexFileWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) ParseProblemException(com.github.javaparser.ParseProblemException) BufferedWriter(java.io.BufferedWriter)

Aggregations

BugInCF (org.checkerframework.javacutil.BugInCF)127 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)29 ArrayList (java.util.ArrayList)28 AnnotationMirror (javax.lang.model.element.AnnotationMirror)26 TypeElement (javax.lang.model.element.TypeElement)26 TypeMirror (javax.lang.model.type.TypeMirror)25 ExecutableElement (javax.lang.model.element.ExecutableElement)24 MethodTree (com.sun.source.tree.MethodTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)18 VariableTree (com.sun.source.tree.VariableTree)18 Element (javax.lang.model.element.Element)18 ClassTree (com.sun.source.tree.ClassTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 NewClassTree (com.sun.source.tree.NewClassTree)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 IOException (java.io.IOException)16 Tree (com.sun.source.tree.Tree)15 Map (java.util.Map)15 List (java.util.List)14 VariableElement (javax.lang.model.element.VariableElement)14