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;
}
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));
}
}
}
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
Aggregations