Search in sources :

Example 1 with StubResource

use of org.checkerframework.framework.stub.StubResource in project checker-framework by typetools.

the class AnnotatedTypeFactory method parseStubFiles.

/**
 * Parses the stub files in the following order:
 *
 * <ol>
 *   <li>jdk.astub in the same directory as the checker, if it exists and ignorejdkastub option
 *       is not supplied <br>
 *   <li>flow.astub in the same directory as BaseTypeChecker <br>
 *   <li>Stub files listed in @Stubfiles annotation on the checker; must be in same directory as
 *       the checker<br>
 *   <li>Stub files provide via stubs system property <br>
 *   <li>Stub files provide via stubs environment variable <br>
 *   <li>Stub files provide via stubs compiler option
 * </ol>
 *
 * <p>If a type is annotated with a qualifier from the same hierarchy in more than one stub
 * file, the qualifier in the last stub file is applied.
 *
 * <p>Sets typesFromStubFiles and declAnnosFromStubFiles by side effect, just before returning.
 */
protected void parseStubFiles() {
    if (this.typesFromStubFiles != null || this.declAnnosFromStubFiles != null) {
        ErrorReporter.errorAbort("AnnotatedTypeFactory.parseStubFiles called more than once");
    }
    Map<Element, AnnotatedTypeMirror> typesFromStubFiles = new HashMap<>();
    Map<String, Set<AnnotationMirror>> declAnnosFromStubFiles = new HashMap<>();
    // 1. jdk.astub
    if (!checker.hasOption("ignorejdkastub")) {
        InputStream in = null;
        in = checker.getClass().getResourceAsStream("jdk.astub");
        if (in != null) {
            StubParser.parse("jdk.astub", in, this, processingEnv, typesFromStubFiles, declAnnosFromStubFiles);
        }
    }
    // 2. flow.astub
    // stub file for type-system independent annotations
    InputStream input = BaseTypeChecker.class.getResourceAsStream("flow.astub");
    if (input != null) {
        StubParser.parse("flow.astub", input, this, processingEnv, typesFromStubFiles, declAnnosFromStubFiles);
    }
    // Stub files specified via stubs compiler option, stubs system property,
    // stubs env. variable, or @Stubfiles
    List<String> allStubFiles = new ArrayList<>();
    // 3. Stub files listed in @Stubfiles annotation on the checker
    StubFiles stubFilesAnnotation = checker.getClass().getAnnotation(StubFiles.class);
    if (stubFilesAnnotation != null) {
        Collections.addAll(allStubFiles, stubFilesAnnotation.value());
    }
    // 4. Stub files provide via stubs system property
    String stubsProperty = System.getProperty("stubs");
    if (stubsProperty != null) {
        Collections.addAll(allStubFiles, stubsProperty.split(File.pathSeparator));
    }
    // 5. Stub files provide via stubs environment variable
    String stubEnvVar = System.getenv("stubs");
    if (stubEnvVar != null) {
        Collections.addAll(allStubFiles, stubEnvVar.split(File.pathSeparator));
    }
    // 6. Stub files provide via stubs option
    String stubsOption = checker.getOption("stubs");
    if (stubsOption != null) {
        Collections.addAll(allStubFiles, stubsOption.split(File.pathSeparator));
    }
    if (allStubFiles.isEmpty()) {
        this.typesFromStubFiles = typesFromStubFiles;
        this.declAnnosFromStubFiles = declAnnosFromStubFiles;
        return;
    }
    // stubs env. variable, or @Stubfiles
    for (String stubPath : allStubFiles) {
        if (stubPath == null || stubPath.isEmpty()) {
            continue;
        }
        // Handle case when running in jtreg
        String base = System.getProperty("test.src");
        String stubPathFull = stubPath;
        if (base != null) {
            stubPathFull = base + "/" + stubPath;
        }
        List<StubResource> stubs = StubUtil.allStubFiles(stubPathFull);
        if (stubs.size() == 0) {
            InputStream in = null;
            in = checker.getClass().getResourceAsStream(stubPath);
            if (in != null) {
                StubParser.parse(stubPath, in, this, processingEnv, typesFromStubFiles, declAnnosFromStubFiles);
                // We could handle the stubPath -> continue.
                continue;
            }
            // We couldn't handle the stubPath -> error message.
            checker.message(Kind.NOTE, "Did not find stub file or files within directory: " + stubPath + " " + new File(stubPath).getAbsolutePath() + " " + stubPathFull);
        }
        for (StubResource resource : stubs) {
            InputStream stubStream;
            try {
                stubStream = resource.getInputStream();
            } catch (IOException e) {
                checker.message(Kind.NOTE, "Could not read stub resource: " + resource.getDescription());
                continue;
            }
            StubParser.parse(resource.getDescription(), stubStream, this, processingEnv, typesFromStubFiles, declAnnosFromStubFiles);
        }
    }
    this.typesFromStubFiles = typesFromStubFiles;
    this.declAnnosFromStubFiles = declAnnosFromStubFiles;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) InputStream(java.io.InputStream) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StubFiles(org.checkerframework.framework.qual.StubFiles) File(java.io.File) FromStubFile(org.checkerframework.framework.qual.FromStubFile) StubResource(org.checkerframework.framework.stub.StubResource)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 VariableElement (javax.lang.model.element.VariableElement)1 FromStubFile (org.checkerframework.framework.qual.FromStubFile)1 StubFiles (org.checkerframework.framework.qual.StubFiles)1 StubResource (org.checkerframework.framework.stub.StubResource)1