use of com.sun.tools.javac.file.BaseFileManager in project lombok by rzwitserloot.
the class Delombok method delombok.
public boolean delombok() throws IOException {
LombokOptions options = LombokOptionsFactory.getDelombokOptions(context);
options.deleteLombokAnnotations();
options.putJavacOption("ENCODING", charset.name());
if (classpath != null)
options.putJavacOption("CLASSPATH", unpackClasspath(classpath));
if (sourcepath != null)
options.putJavacOption("SOURCEPATH", sourcepath);
if (bootclasspath != null)
options.putJavacOption("BOOTCLASSPATH", unpackClasspath(bootclasspath));
options.setFormatPreferences(new FormatPreferences(formatPrefs));
options.put("compilePolicy", "check");
if (Javac.getJavaCompilerVersion() >= 9) {
Arguments args = Arguments.instance(context);
List<String> argsList = new ArrayList<String>();
if (classpath != null) {
argsList.add("--class-path");
argsList.add(options.get("--class-path"));
}
if (sourcepath != null) {
argsList.add("--source-path");
argsList.add(options.get("--source-path"));
}
if (bootclasspath != null) {
argsList.add("--boot-class-path");
argsList.add(options.get("--boot-class-path"));
}
if (charset != null) {
argsList.add("-encoding");
argsList.add(charset.name());
}
String pathToSelfJar = getPathOfSelf();
if (pathToSelfJar != null) {
argsList.add("--module-path");
argsList.add((modulepath == null || modulepath.isEmpty()) ? pathToSelfJar : (pathToSelfJar + File.pathSeparator + modulepath));
} else if (modulepath != null && !modulepath.isEmpty()) {
argsList.add("--module-path");
argsList.add(modulepath);
}
if (!disablePreview && Javac.getJavaCompilerVersion() >= 11)
argsList.add("--enable-preview");
if (Javac.getJavaCompilerVersion() < 15) {
String[] argv = argsList.toArray(new String[0]);
args.init("javac", argv);
} else {
args.init("javac", argsList);
}
options.put("diags.legacy", "TRUE");
options.put("allowStringFolding", "FALSE");
} else {
if (modulepath != null && !modulepath.isEmpty())
throw new IllegalStateException("DELOMBOK: Option --module-path requires usage of JDK9 or higher.");
}
CommentCatcher catcher = CommentCatcher.create(context, Javac.getJavaCompilerVersion() >= 13);
JavaCompiler compiler = catcher.getCompiler();
List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>();
Map<JCCompilationUnit, File> baseMap = new IdentityHashMap<JCCompilationUnit, File>();
Set<AbstractProcessor> processors = new LinkedHashSet<AbstractProcessor>();
processors.add(new lombok.javac.apt.LombokProcessor());
processors.addAll(additionalAnnotationProcessors);
if (Javac.getJavaCompilerVersion() >= 9) {
JavaFileManager jfm_ = context.get(JavaFileManager.class);
if (jfm_ instanceof BaseFileManager) {
Arguments args = Arguments.instance(context);
// reinit with options
((BaseFileManager) jfm_).setContext(context);
((BaseFileManager) jfm_).handleOptions(args.getDeferredFileManagerOptions());
}
}
if (Javac.getJavaCompilerVersion() < 9) {
compiler.initProcessAnnotations(processors);
} else {
compiler.initProcessAnnotations(processors, Collections.<JavaFileObject>emptySet(), Collections.<String>emptySet());
}
Object unnamedModule = null;
if (Javac.getJavaCompilerVersion() >= 9)
unnamedModule = Symtab.instance(context).unnamedModule;
for (File fileToParse : filesToParse) {
JCCompilationUnit unit = compiler.parse(fileToParse.getAbsolutePath());
if (Javac.getJavaCompilerVersion() >= 9)
try {
MODULE_FIELD.set(unit, unnamedModule);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
baseMap.put(unit, fileToBase.get(fileToParse));
roots.add(unit);
}
if (compiler.errorCount() > 0) {
// At least one parse error. No point continuing (a real javac run doesn't either).
return false;
}
for (JCCompilationUnit unit : roots) {
catcher.setComments(unit, new DocCommentIntegrator().integrate(catcher.getComments(unit), unit));
}
if (Javac.getJavaCompilerVersion() >= 9) {
compiler.initModules(com.sun.tools.javac.util.List.from(roots.toArray(new JCCompilationUnit[0])));
}
com.sun.tools.javac.util.List<JCCompilationUnit> trees = compiler.enterTrees(toJavacList(roots));
JavaCompiler delegate;
if (Javac.getJavaCompilerVersion() < 9) {
delegate = compiler.processAnnotations(trees, com.sun.tools.javac.util.List.<String>nil());
} else {
delegate = compiler;
Collection<String> c = com.sun.tools.javac.util.List.nil();
compiler.processAnnotations(trees, c);
}
Object care = callAttributeMethodOnJavaCompiler(delegate, delegate.todo);
callFlowMethodOnJavaCompiler(delegate, care);
FormatPreferences fps = new FormatPreferences(formatPrefs);
for (JCCompilationUnit unit : roots) {
DelombokResult result = new DelombokResult(catcher.getComments(unit), catcher.getTextBlockStarts(unit), unit, force || options.isChanged(unit), fps);
if (onlyChanged && !result.isChanged() && !options.isChanged(unit)) {
if (verbose)
feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), "unchanged (skipped)");
continue;
}
ListBuffer<JCTree> newDefs = new ListBuffer<JCTree>();
for (JCTree def : unit.defs) {
if (def instanceof JCImport) {
Boolean b = JavacAugments.JCImport_deletable.get((JCImport) def);
if (b == null || !b.booleanValue())
newDefs.append(def);
} else {
newDefs.append(def);
}
}
unit.defs = newDefs.toList();
if (verbose)
feedback.printf("File: %s [%s%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged", force && !options.isChanged(unit) ? " (forced)" : "");
Writer rawWriter;
if (presetWriter != null)
rawWriter = createUnicodeEscapeWriter(presetWriter);
else if (output == null)
rawWriter = createStandardOutWriter();
else
rawWriter = createFileWriter(output, baseMap.get(unit), unit.sourcefile.toUri());
BufferedWriter writer = new BufferedWriter(rawWriter);
try {
result.print(writer);
} finally {
if (output != null) {
writer.close();
} else {
writer.flush();
}
}
}
delegate.close();
return true;
}
Aggregations