Search in sources :

Example 1 with TaskListener

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));
}
Also used : TaskListener(com.sun.source.util.TaskListener) JavacTask(com.sun.source.util.JavacTask)

Example 2 with TaskListener

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);
    }
}
Also used : TaskListener(com.sun.source.util.TaskListener)

Example 3 with TaskListener

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);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTool(com.sun.tools.javac.api.JavacTool) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) TaskListener(com.sun.source.util.TaskListener) TaskEvent(com.sun.source.util.TaskEvent) JavacTask(com.sun.source.util.JavacTask) Pair(com.sun.tools.javac.util.Pair)

Example 4 with TaskListener

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);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTool(com.sun.tools.javac.api.JavacTool) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) TaskListener(com.sun.source.util.TaskListener) TaskEvent(com.sun.source.util.TaskEvent) JavacTask(com.sun.source.util.JavacTask) Pair(com.sun.tools.javac.util.Pair)

Example 5 with TaskListener

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);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(com.sun.source.util.TaskListener) DiagnosticListener(javax.tools.DiagnosticListener) TaskEvent(com.sun.source.util.TaskEvent) TaskListener(com.sun.source.util.TaskListener) CeyloncTaskImpl(com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl) File(java.io.File)

Aggregations

TaskListener (com.sun.source.util.TaskListener)10 TaskEvent (com.sun.source.util.TaskEvent)8 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 CeyloncTaskImpl (com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl)4 JavacTask (com.sun.source.util.JavacTask)3 File (java.io.File)3 DiagnosticListener (javax.tools.DiagnosticListener)3 JavaPositionsRetriever (com.redhat.ceylon.compiler.java.codegen.JavaPositionsRetriever)2 ExitState (com.redhat.ceylon.compiler.java.launcher.Main.ExitState)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)2 JavacTool (com.sun.tools.javac.api.JavacTool)2 JCTree (com.sun.tools.javac.tree.JCTree)2 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)2 Context (com.sun.tools.javac.util.Context)2 Pair (com.sun.tools.javac.util.Pair)2 ArrayList (java.util.ArrayList)2 Element (javax.lang.model.element.Element)2 CeylonModelLoader (com.redhat.ceylon.compiler.java.loader.CeylonModelLoader)1 RuntimeModelLoader (com.redhat.ceylon.compiler.java.runtime.model.RuntimeModelLoader)1