Search in sources :

Example 1 with JCCompilationUnit

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon by eclipse.

the class CeylonTransformer method makeJCCompilationUnitPlaceholder.

/**
 * In this pass we only make an empty placeholder which we'll fill in the
 * EnterCeylon phase later on
 */
public JCCompilationUnit makeJCCompilationUnitPlaceholder(Tree.CompilationUnit t, JavaFileObject file, String pkgName, PhasedUnit phasedUnit) {
    JCExpression pkg = pkgName != null ? getPackage(pkgName) : null;
    at(t);
    List<JCTree> defs = makeDefs(t);
    JCCompilationUnit topLev = new CeylonCompilationUnit(List.<JCTree.JCAnnotation>nil(), pkg, defs, null, null, null, null, t, phasedUnit);
    topLev.lineMap = getMap();
    topLev.sourcefile = file;
    topLev.isCeylonProgram = true;
    return topLev;
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 2 with JCCompilationUnit

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon by eclipse.

the class CeylonTransformer method transformPackageInfo.

public List<JCCompilationUnit> transformPackageInfo(CeylonCompilationUnit ccu) {
    final CeylonFileObject fo = (CeylonFileObject) ((CeylonPhasedUnit) ccu.phasedUnit).getFileObject();
    ListBuffer<JCCompilationUnit> packageInfos = new ListBuffer<JCCompilationUnit>();
    for (Tree.PackageDescriptor pack : ccu.ceylonTree.getPackageDescriptors()) {
        List<JCAnnotation> packageAnnotations = expressionGen().transformAnnotations(OutputElement.PACKAGE, pack);
        if (packageAnnotations.isEmpty()) {
            continue;
        }
        JCCompilationUnit packageInfo = make().TopLevel(packageAnnotations, naming.makeQuotedFQIdent(pack.getScope().getQualifiedNameString()), List.<JCTree>nil());
        // Enter.visitTopLevel(JCCompilationUnit) uses the tree.sourceFile
        // to decide whether it's seeing a package-info.java
        // So set up a fake one...
        packageInfo.sourcefile = new JavaFileObject() {

            @Override
            public boolean isNameCompatible(String simpleName, Kind kind) {
                return "package-info".equals(simpleName) && JavaFileObject.Kind.SOURCE == kind;
            }

            @Override
            public URI toUri() {
                return fo.toUri();
            }

            @Override
            public String getName() {
                return fo.getName();
            }

            @Override
            public InputStream openInputStream() throws IOException {
                return fo.openInputStream();
            }

            @Override
            public OutputStream openOutputStream() throws IOException {
                return fo.openOutputStream();
            }

            @Override
            public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
                return fo.openReader(ignoreEncodingErrors);
            }

            @Override
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                return fo.getCharContent(ignoreEncodingErrors);
            }

            @Override
            public Writer openWriter() throws IOException {
                return fo.openWriter();
            }

            @Override
            public long getLastModified() {
                return fo.getLastModified();
            }

            @Override
            public boolean delete() {
                return fo.delete();
            }

            @Override
            public Kind getKind() {
                return fo.getKind();
            }

            @Override
            public NestingKind getNestingKind() {
                return fo.getNestingKind();
            }

            @Override
            public Modifier getAccessLevel() {
                return fo.getAccessLevel();
            }
        };
        packageInfos.add(packageInfo);
    }
    return packageInfos.toList();
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) InputStream(java.io.InputStream) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) OutputStream(java.io.OutputStream) Reader(java.io.Reader) IOException(java.io.IOException) URI(java.net.URI) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) NestingKind(org.eclipse.ceylon.javax.lang.model.element.NestingKind) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) PackageDescriptor(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) Modifier(org.eclipse.ceylon.javax.lang.model.element.Modifier) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Writer(java.io.Writer) NestingKind(org.eclipse.ceylon.javax.lang.model.element.NestingKind)

Example 3 with JCCompilationUnit

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit 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 4 with JCCompilationUnit

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSource.

protected void compareWithJavaSource(List<String> options, final int index, 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;

        int count = 0;

        @Override
        public void started(TaskEvent e) {
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null && index == count++) {
                    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(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) DiagnosticListener(org.eclipse.ceylon.javax.tools.DiagnosticListener) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 5 with JCCompilationUnit

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit 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

JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)13 IOException (java.io.IOException)5 CeylonCompilationUnit (org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit)5 File (java.io.File)4 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)4 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)4 ZipFile (java.util.zip.ZipFile)3 CeyloncTaskImpl (org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl)3 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)3 DiagnosticListener (org.eclipse.ceylon.javax.tools.DiagnosticListener)3 TaskListener (org.eclipse.ceylon.langtools.source.util.TaskListener)3 Package (org.eclipse.ceylon.model.typechecker.model.Package)3 JavaPositionsRetriever (org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever)2 ExitState (org.eclipse.ceylon.compiler.java.launcher.Main.ExitState)2 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)2 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)2 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)2 JCClassDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl)2 JCMethodDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl)2 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)2