use of org.checkerframework.framework.qual.StubFiles 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;
}
use of org.checkerframework.framework.qual.StubFiles in project checker-framework by typetools.
the class AnnotationFileElementTypes 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
* <li>If parsing annotated JDK as stub files, all package-info.java files under the jdk/
* directory
* <li>Stub files listed in @StubFiles annotation on the checker; must be in same directory as
* the checker
* <li>Stub files returned by {@link BaseTypeChecker#getExtraStubFiles} (treated like those
* listed in @StubFiles annotation)
* <li>Stub files provided via {@code -Astubs} 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>If using JDK 11, then the JDK stub files are only parsed if a type or declaration annotation
* is requested from a class in that file.
*/
public void parseStubFiles() {
parsing = true;
BaseTypeChecker checker = factory.getChecker();
ProcessingEnvironment processingEnv = factory.getProcessingEnv();
// Only look in .jar files, and parse it right away.
if (!checker.hasOption("ignorejdkastub")) {
InputStream jdkStubIn = checker.getClass().getResourceAsStream("jdk.astub");
if (jdkStubIn != null) {
AnnotationFileParser.parseStubFile(checker.getClass().getResource("jdk.astub").toString(), jdkStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
}
String jdkVersionStub = "jdk" + annotatedJdkVersion + ".astub";
InputStream jdkVersionStubIn = checker.getClass().getResourceAsStream(jdkVersionStub);
if (jdkVersionStubIn != null) {
AnnotationFileParser.parseStubFile(checker.getClass().getResource(jdkVersionStub).toString(), jdkVersionStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
}
// 2. Annotated JDK
// This preps but does not parse the JDK files (except package-info.java files).
// The JDK source code files will be parsed later, on demand.
prepJdkStubs();
// prepping the JDK parses all package-info.java files, which sets the `parsing` field to
// false, so re-set it to true.
parsing = true;
}
// 3. Stub files listed in @StubFiles annotation on the checker
StubFiles stubFilesAnnotation = checker.getClass().getAnnotation(StubFiles.class);
if (stubFilesAnnotation != null) {
parseAnnotationFiles(Arrays.asList(stubFilesAnnotation.value()), AnnotationFileType.BUILTIN_STUB);
}
// 4. Stub files returned by the `getExtraStubFiles()` method
parseAnnotationFiles(checker.getExtraStubFiles(), AnnotationFileType.BUILTIN_STUB);
// 5. Stub files provided via -Astubs command-line option
String stubsOption = checker.getOption("stubs");
if (stubsOption != null) {
parseAnnotationFiles(Arrays.asList(stubsOption.split(File.pathSeparator)), AnnotationFileType.COMMAND_LINE_STUB);
}
parsing = false;
}
Aggregations