Search in sources :

Example 71 with BugInCF

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

the class AnnotationFileElementTypes method parseJdkJarEntry.

/**
 * Parses the stub file in the given jar entry.
 *
 * @param jarEntryName name of the jar entry to parse
 */
private void parseJdkJarEntry(String jarEntryName) {
    JarURLConnection connection = getJarURLConnectionToJdk();
    parsing = true;
    try (JarFile jarFile = connection.getJarFile()) {
        InputStream jdkStub;
        try {
            jdkStub = jarFile.getInputStream(jarFile.getJarEntry(jarEntryName));
        } catch (IOException e) {
            throw new BugInCF("cannot open the jdk stub file " + jarEntryName, e);
        }
        AnnotationFileParser.parseJdkFileAsStub(jarEntryName, jdkStub, factory, factory.getProcessingEnv(), annotationFileAnnos);
    } catch (IOException e) {
        throw new BugInCF("cannot open the Jar file " + connection.getEntryName(), e);
    } catch (BugInCF e) {
        throw new BugInCF("Exception while parsing " + jarEntryName + ": " + e.getMessage(), e);
    } finally {
        parsing = false;
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 72 with BugInCF

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

the class SourceChecker method typeProcess.

/**
 * Type-check the code using this checker's visitor.
 *
 * @see Processor#process(Set, RoundEnvironment)
 */
@Override
public void typeProcess(TypeElement e, TreePath p) {
    if (javacErrored) {
        reportJavacError(p);
        return;
    }
    // Cannot use BugInCF here because it is outside of the try/catch for BugInCF.
    if (e == null) {
        messager.printMessage(Kind.ERROR, "Refusing to process empty TypeElement");
        return;
    }
    if (p == null) {
        messager.printMessage(Kind.ERROR, "Refusing to process empty TreePath in TypeElement: " + e);
        return;
    }
    if (!warnedAboutGarbageCollection) {
        String gcUsageMessage = SystemPlume.gcUsageMessage(.25, 60);
        if (gcUsageMessage != null) {
            messager.printMessage(Kind.WARNING, gcUsageMessage);
            warnedAboutGarbageCollection = true;
        }
    }
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Source source = Source.instance(context);
    // Also the enum constant Source.JDK1_8 was renamed at some point...
    if (!warnedAboutSourceLevel && source.compareTo(Source.lookup("8")) < 0) {
        messager.printMessage(Kind.WARNING, "-source " + source.name + " does not support type annotations");
        warnedAboutSourceLevel = true;
    }
    Log log = Log.instance(context);
    if (log.nerrors > this.errsOnLastExit) {
        this.errsOnLastExit = log.nerrors;
        javacErrored = true;
        reportJavacError(p);
        return;
    }
    if (visitor == null) {
        // there. Don't also cause a NPE here.
        return;
    }
    if (p.getCompilationUnit() != currentRoot) {
        setRoot(p.getCompilationUnit());
        if (hasOption("filenames")) {
            // TODO: Have a command-line option to turn the timestamps on/off too, because
            // they are nondeterministic across runs.
            // Add timestamp to indicate how long operations are taking.
            // Duplicate messages are suppressed, so this might not appear in front of every "
            // is type-checking " message (when a file takes less than a second to type-check).
            message(Kind.NOTE, Instant.now().toString());
            message(Kind.NOTE, "%s is type-checking %s", (Object) this.getClass().getSimpleName(), currentRoot.getSourceFile().getName());
        }
    }
    // Visit the attributed tree.
    try {
        visitor.visit(p);
        warnUnneededSuppressions();
    } catch (UserError ce) {
        logUserError(ce);
    } catch (TypeSystemError ce) {
        logTypeSystemError(ce);
    } catch (BugInCF ce) {
        logBugInCF(ce);
    } catch (Throwable t) {
        logBugInCF(wrapThrowableAsBugInCF("SourceChecker.typeProcess", t, p));
    } finally {
        // Also add possibly deferred diagnostics, which will get published back in
        // AbstractTypeProcessor.
        this.errsOnLastExit = log.nerrors;
    }
}
Also used : Context(com.sun.tools.javac.util.Context) UserError(org.checkerframework.javacutil.UserError) Log(com.sun.tools.javac.util.Log) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) TypeSystemError(org.checkerframework.javacutil.TypeSystemError) BugInCF(org.checkerframework.javacutil.BugInCF) Source(com.sun.tools.javac.code.Source) DiagnosticSource(com.sun.tools.javac.util.DiagnosticSource)

Example 73 with BugInCF

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

the class SourceChecker method getProperties.

// /////////////////////////////////////////////////////////////////////////
// / Miscellaneous
// /
/**
 * A helper function to parse a Properties file.
 *
 * @param cls the class whose location is the base of the file path
 * @param filePath the name/path of the file to be read
 * @param permitNonExisting if true, return an empty Properties if the file does not exist or
 *     cannot be parsed; if false, issue an error
 * @return the properties
 */
protected Properties getProperties(Class<?> cls, String filePath, boolean permitNonExisting) {
    Properties prop = new Properties();
    try {
        InputStream base = cls.getResourceAsStream(filePath);
        if (base == null) {
            // The property file was not found.
            if (permitNonExisting) {
                return prop;
            } else {
                throw new BugInCF("Couldn't locate properties file " + filePath);
            }
        }
        prop.load(base);
    } catch (IOException e) {
        throw new BugInCF("Couldn't parse properties file: " + filePath, e);
    }
    return prop;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 74 with BugInCF

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

the class SourceChecker method shouldSkipUses.

// /////////////////////////////////////////////////////////////////////////
// / Skipping uses and defs
// /
/**
 * Tests whether the class owner of the passed element is an unannotated class and matches the
 * pattern specified in the {@code checker.skipUses} property.
 *
 * @param element an element
 * @return true iff the enclosing class of element should be skipped
 */
public final boolean shouldSkipUses(Element element) {
    if (element == null) {
        return false;
    }
    TypeElement typeElement = ElementUtils.enclosingTypeElement(element);
    if (typeElement == null) {
        throw new BugInCF("enclosingTypeElement(%s [%s]) => null%n", element, element.getClass());
    }
    // TypeElement.toString(): @FullyQualifiedName
    @SuppressWarnings("signature:assignment") @FullyQualifiedName String name = typeElement.toString();
    return shouldSkipUses(name);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 75 with BugInCF

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

the class SourceChecker method getCheckerVersion.

/**
 * Returns the version of the Checker Framework.
 *
 * @return the Checker Framework version
 */
private String getCheckerVersion() {
    Properties gitProperties = getProperties(getClass(), "/git.properties", false);
    String version = gitProperties.getProperty("git.build.version");
    if (version != null) {
        return version;
    }
    throw new BugInCF("Could not find the version in git.properties");
}
Also used : Properties(java.util.Properties) BugInCF(org.checkerframework.javacutil.BugInCF)

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