use of com.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.
the class SuggestedFixes method compilesWithFix.
/**
* Returns true if the current compilation would succeed with the given fix applied. Note that
* calling this method is very expensive as it requires rerunning the entire compile, so it should
* be used with restraint.
*/
public static boolean compilesWithFix(Fix fix, VisitorState state) {
if (fix.isEmpty()) {
return true;
}
JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
JavaFileObject modifiedFile = compilationUnit.getSourceFile();
JavacTaskImpl javacTask = (JavacTaskImpl) state.context.get(JavacTask.class);
if (javacTask == null) {
throw new IllegalArgumentException("No JavacTask in context.");
}
Arguments arguments = Arguments.instance(javacTask.getContext());
List<JavaFileObject> fileObjects = new ArrayList<>(arguments.getFileObjects());
for (int i = 0; i < fileObjects.size(); i++) {
final JavaFileObject oldFile = fileObjects.get(i);
if (modifiedFile.toUri().equals(oldFile.toUri())) {
DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER);
diff.handleFix(fix);
SourceFile fixSource;
try {
fixSource = new SourceFile(modifiedFile.getName(), modifiedFile.getCharContent(false));
} catch (IOException e) {
return false;
}
diff.applyDifferences(fixSource);
fileObjects.set(i, new SimpleJavaFileObject(modifiedFile.toUri(), Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return fixSource.getAsSequence();
}
});
break;
}
}
DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<>();
Context context = new Context();
Options.instance(context).putAll(Options.instance(javacTask.getContext()));
context.put(Arguments.class, arguments);
JavacTask newTask = JavacTool.create().getTask(CharStreams.nullWriter(), state.context.get(JavaFileManager.class), diagnosticListener, ImmutableList.of(), arguments.getClassNames(), fileObjects, context);
try {
newTask.analyze();
} catch (Throwable e) {
e.printStackTrace();
}
return countErrors(diagnosticListener) == 0;
}
use of com.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.
the class BaseErrorProneJavaCompiler method getTask.
@Override
public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
ErrorProneOptions errorProneOptions = ErrorProneOptions.processArgs(options);
List<String> remainingOptions = Arrays.asList(errorProneOptions.getRemainingArgs());
ImmutableList<String> javacOpts = ImmutableList.copyOf(remainingOptions);
javacOpts = defaultToLatestSupportedLanguageLevel(javacOpts);
javacOpts = setCompilePolicyToByFile(javacOpts);
final JavacTaskImpl task = (JavacTaskImpl) javacTool.getTask(out, fileManager, diagnosticListener, javacOpts, classes, compilationUnits);
setupMessageBundle(task.getContext());
RefactoringCollection[] refactoringCollection = { null };
task.addTaskListener(createAnalyzer(scannerSupplier, errorProneOptions, task.getContext(), refactoringCollection));
if (refactoringCollection[0] != null) {
task.addTaskListener(new RefactoringTask(task.getContext(), refactoringCollection[0]));
}
task.addTaskListener(new CFCacheClearingListener());
return task;
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.
the class T6457284 method main.
public static void main(String[] args) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
MyMessages.preRegister(task.getContext());
task.parse();
for (Element e : task.analyze()) {
if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))
throw new AssertionError(e.getEnclosingElement());
System.out.println("OK: " + e.getEnclosingElement());
return;
}
throw new AssertionError("No top-level classes!");
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.
the class T6330997 method main.
public static void main(String... args) {
increaseMajor("T1.class", 1);
increaseMajor("T2.class", 2);
javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
JavaCompiler compiler = JavaCompiler.instance(task.getContext());
try {
compiler.resolveIdent("T1").complete();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed: unexpected exception while reading class T1");
}
try {
compiler.resolveIdent("T2").complete();
} catch (BadClassFile e) {
System.err.println("Passed: expected completion failure " + e.getClass().getName());
return;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed: unexpected exception while reading class T2");
}
throw new RuntimeException("Failed: no error reported");
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon-compiler by ceylon.
the class TestJavacTask method getTask.
static JavacTaskImpl getTask(JavaCompiler compiler, File... file) {
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
return (JavacTaskImpl) compiler.getTask(null, fm, null, null, null, files);
}
Aggregations