use of com.sun.source.util.TaskListener in project buck by facebook.
the class TracingTaskListener method setupTracing.
public static void setupTracing(JavaCompiler.CompilationTask task, JavacPhaseTracer tracing, Object next) {
if (!(task instanceof JavacTask)) {
return;
}
final JavacTask javacTask = (JavacTask) task;
// The main part of Buck doesn't have access to `TaskListener`, since that's defined in the
// compiler jar. So we had to pass it as an object and downcast.
final TaskListener nextListener = (TaskListener) next;
javacTask.setTaskListener(new TracingTaskListener(tracing, nextListener));
}
use of com.sun.source.util.TaskListener in project btrace by btraceio.
the class Verifier method prepareContext.
/**
* adds a listener for attribution.
*/
private void prepareContext(Context context) {
TaskListener otherListener = context.get(TaskListener.class);
if (otherListener == null) {
context.put(TaskListener.class, listener);
} else {
// handle cases of multiple listeners
context.put(TaskListener.class, (TaskListener) null);
TaskListeners listeners = new TaskListeners();
listeners.add(otherListener);
listeners.add(listener);
context.put(TaskListener.class, listeners);
}
}
use of com.sun.source.util.TaskListener in project ceylon by eclipse.
the class CheckAttributedTree method read.
/**
* Read a file.
* @param file the file to be read
* @return the tree for the content of the file
* @throws IOException if any IO errors occur
* @throws TreePosTest.ParseException if any errors occur while parsing the file
*/
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
final List<Element> analyzedElems = new ArrayList<>();
task.setTaskListener(new TaskListener() {
public void started(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE)
analyzedElems.add(e.getTypeElement());
}
public void finished(TaskEvent e) {
}
});
try {
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
// System.out.println("Try to add pairs. Elems are " + analyzedElems);
for (CompilationUnitTree t : trees) {
JCCompilationUnit cu = (JCCompilationUnit) t;
for (JCTree def : cu.defs) {
if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
// System.out.println("Adding pair...");
res.add(new Pair<>(cu, def));
}
}
}
return res;
} catch (Throwable t) {
throw new AttributionException("Exception while attributing file: " + file);
}
}
use of com.sun.source.util.TaskListener in project ceylon-compiler by ceylon.
the class CheckAttributedTree method read.
/**
* Read a file.
* @param file the file to be read
* @return the tree for the content of the file
* @throws IOException if any IO errors occur
* @throws TreePosTest.ParseException if any errors occur while parsing the file
*/
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
final List<Element> analyzedElems = new ArrayList<>();
task.setTaskListener(new TaskListener() {
public void started(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE)
analyzedElems.add(e.getTypeElement());
}
public void finished(TaskEvent e) {
}
});
try {
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
// System.out.println("Try to add pairs. Elems are " + analyzedElems);
for (CompilationUnitTree t : trees) {
JCCompilationUnit cu = (JCCompilationUnit) t;
for (JCTree def : cu.defs) {
if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
// System.out.println("Adding pair...");
res.add(new Pair<>(cu, def));
}
}
}
return res;
} catch (Throwable t) {
throw new AttributionException("Exception while attributing file: " + file);
}
}
use of com.sun.source.util.TaskListener in project ceylon-compiler by ceylon.
the class CompilerTests method compareWithJavaSource.
protected void compareWithJavaSource(List<String> options, String java, String... ceylon) {
// make a compiler task
// FIXME: runFileManager.setSourcePath(dir);
ErrorCollector collector = new ErrorCollector();
CeyloncTaskImpl task = getCompilerTask(options, collector, ceylon);
// grab the CU after we've completed it
class Listener implements TaskListener {
JCCompilationUnit compilationUnit;
private String compilerSrc;
@Override
public void started(TaskEvent e) {
}
@Override
public void finished(TaskEvent e) {
if (e.getKind() == Kind.ENTER) {
if (compilationUnit == null) {
compilationUnit = (JCCompilationUnit) e.getCompilationUnit();
// for some reason compilationUnit is full here in the listener, but empty as soon
// as the compile task is done. probably to clean up for the gc?
compilerSrc = normalizeLineEndings(compilationUnit.toString());
}
}
}
}
Listener listener = new Listener();
task.setTaskListener(listener);
// now compile it all the way
assertCompilesOk(collector, task.call2());
// now look at what we expected
File expectedSrcFile = new File(getPackagePath(), java);
String expectedSrc = normalizeLineEndings(readFile(expectedSrcFile)).trim();
String compiledSrc = listener.compilerSrc.trim();
// THIS IS FOR INTERNAL USE ONLY!!!
// Can be used to do batch updating of known correct tests
// Uncomment only when you know what you're doing!
// if (expectedSrc != null && compiledSrc != null && !expectedSrc.equals(compiledSrc)) {
// writeFile(expectedSrcFile, compiledSrc);
// expectedSrc = compiledSrc;
// }
Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Aggregations