Search in sources :

Example 1 with JavaPositionsRetriever

use of org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSourceWithLines.

protected void compareWithJavaSourceWithLines(String name) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    CeyloncTaskImpl task = getCompilerTask(new String[] { name + ".ceylon" });
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        private JavaPositionsRetriever javaPositionsRetriever = new JavaPositionsRetriever();

        @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?
                    javaPositionsRetriever.retrieve(compilationUnit);
                    compilerSrc = normalizeLineEndings(javaPositionsRetriever.getJavaSourceCodeWithCeylonLines());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", exitState.ceylonState, CeylonState.OK);
    // now look at what we expected
    String expectedSrc = normalizeLineEndings(readFile(new File(getPackagePath(), name + ".src"))).trim();
    String compiledSrc = listener.compilerSrc.trim();
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) DiagnosticListener(org.eclipse.ceylon.javax.tools.DiagnosticListener) ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) JavaPositionsRetriever(org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with JavaPositionsRetriever

use of org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSourceWithPositions.

protected void compareWithJavaSourceWithPositions(String name) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    CeyloncTaskImpl task = getCompilerTask(new String[] { name + ".ceylon" });
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        private JavaPositionsRetriever javaPositionsRetriever = new JavaPositionsRetriever();

        @Override
        public void started(TaskEvent e) {
            AbstractTransformer.trackNodePositions(javaPositionsRetriever);
        }

        @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?
                    javaPositionsRetriever.retrieve(compilationUnit);
                    compilerSrc = normalizeLineEndings(javaPositionsRetriever.getJavaSourceCodeWithCeylonPositions());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", CeylonState.OK, exitState.ceylonState);
    // now look at what we expected
    String expectedSrc = normalizeLineEndings(readFile(new File(getPackagePath(), name + ".src"))).trim();
    String compiledSrc = listener.compilerSrc.trim();
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) DiagnosticListener(org.eclipse.ceylon.javax.tools.DiagnosticListener) ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) JavaPositionsRetriever(org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)2 ZipFile (java.util.zip.ZipFile)2 JavaPositionsRetriever (org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever)2 ExitState (org.eclipse.ceylon.compiler.java.launcher.Main.ExitState)2 CeyloncTaskImpl (org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl)2 DiagnosticListener (org.eclipse.ceylon.javax.tools.DiagnosticListener)2 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)2 TaskListener (org.eclipse.ceylon.langtools.source.util.TaskListener)2 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)2