Search in sources :

Example 11 with JavaFileObject

use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.

the class RegularFileObject method getCharContent.

@Override
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
    CharBuffer cb = fileManager.getCachedContent(this);
    if (cb == null) {
        InputStream in = new FileInputStream(file);
        try {
            ByteBuffer bb = fileManager.makeByteBuffer(in);
            JavaFileObject prev = fileManager.log.useSource(this);
            try {
                cb = fileManager.decode(bb, ignoreEncodingErrors);
            } finally {
                fileManager.log.useSource(prev);
            }
            fileManager.recycleByteBuffer(bb);
            if (!ignoreEncodingErrors) {
                fileManager.cache(this, cb);
            }
        } finally {
            in.close();
        }
    }
    return cb;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 12 with JavaFileObject

use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.

the class ZipFileIndexArchive method getFileObject.

public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
    RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
    ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
    JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
    return ret;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) RelativeFile(org.eclipse.ceylon.langtools.tools.javac.file.RelativePath.RelativeFile)

Example 13 with JavaFileObject

use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.

the class JavacTrees method printMessage.

private void printMessage(Diagnostic.Kind kind, CharSequence msg, JCDiagnostic.DiagnosticPosition pos, org.eclipse.ceylon.langtools.source.tree.CompilationUnitTree root) {
    JavaFileObject oldSource = null;
    JavaFileObject newSource = null;
    newSource = root.getSourceFile();
    if (newSource == null) {
        pos = null;
    } else {
        oldSource = log.useSource(newSource);
    }
    try {
        switch(kind) {
            case ERROR:
                boolean prev = log.multipleErrors;
                try {
                    log.error(pos, "proc.messager", msg.toString());
                } finally {
                    log.multipleErrors = prev;
                }
                break;
            case WARNING:
                log.warning(pos, "proc.messager", msg.toString());
                break;
            case MANDATORY_WARNING:
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
                break;
            default:
                log.note(pos, "proc.messager", msg.toString());
        }
    } finally {
        if (oldSource != null)
            log.useSource(oldSource);
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject)

Example 14 with JavaFileObject

use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.

the class JavaCompiler method desugar.

/**
 * Prepare attributed parse trees, in conjunction with their attribution contexts,
 * for source or code generation. If the file was not listed on the command line,
 * the current implicitSourcePolicy is taken into account.
 * The preparation stops as soon as an error is found.
 */
protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
    if (shouldStop(CompileState.TRANSTYPES))
        return;
    if (implicitSourcePolicy == ImplicitSourcePolicy.NONE && !inputFiles.contains(env.toplevel.sourcefile)) {
        return;
    }
    if (compileStates.isDone(env, CompileState.LOWER)) {
        results.addAll(desugaredEnvs.get(env));
        return;
    }
    /**
     * Ensure that superclasses of C are desugared before C itself. This is
     * required for two reasons: (i) as erasure (TransTypes) destroys
     * information needed in flow analysis and (ii) as some checks carried
     * out during lowering require that all synthetic fields/methods have
     * already been added to C and its superclasses.
     */
    class ScanNested extends TreeScanner {

        Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>();

        protected boolean hasLambdas;

        @Override
        public void visitClassDef(JCClassDecl node) {
            Type st = types.supertype(node.sym.type);
            boolean envForSuperTypeFound = false;
            while (!envForSuperTypeFound && st.hasTag(CLASS)) {
                ClassSymbol c = st.tsym.outermostClass();
                Env<AttrContext> stEnv = enter.getEnv(c);
                if (stEnv != null && env != stEnv) {
                    if (dependencies.add(stEnv)) {
                        boolean prevHasLambdas = hasLambdas;
                        try {
                            scan(stEnv.tree);
                        } finally {
                            /*
                                 * ignore any updates to hasLambdas made during
                                 * the nested scan, this ensures an initalized
                                 * LambdaToMethod is available only to those
                                 * classes that contain lambdas
                                 */
                            hasLambdas = prevHasLambdas;
                        }
                    }
                    envForSuperTypeFound = true;
                }
                st = types.supertype(st);
            }
            super.visitClassDef(node);
        }

        @Override
        public void visitLambda(JCLambda tree) {
            hasLambdas = true;
            super.visitLambda(tree);
        }

        @Override
        public void visitReference(JCMemberReference tree) {
            hasLambdas = true;
            super.visitReference(tree);
        }
    }
    ScanNested scanner = new ScanNested();
    scanner.scan(env.tree);
    for (Env<AttrContext> dep : scanner.dependencies) {
        if (!compileStates.isDone(dep, CompileState.FLOW))
            desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
    }
    // have been attributed and analyzed at this stage
    if (shouldStop(CompileState.TRANSTYPES))
        return;
    if (verboseCompilePolicy)
        printNote("[desugar " + env.enclClass.sym + "]");
    JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
    try {
        // save tree prior to rewriting
        JCTree untranslated = env.tree;
        make.at(Position.FIRSTPOS);
        TreeMaker localMake = make.forToplevel(env.toplevel);
        if (env.tree instanceof JCCompilationUnit) {
            if (!(stubOutput || sourceOutput || printFlat)) {
                if (shouldStop(CompileState.LOWER))
                    return;
                List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake);
                if (pdef.head != null) {
                    Assert.check(pdef.tail.isEmpty());
                    results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, (JCClassDecl) pdef.head));
                }
            }
            return;
        }
        if (stubOutput) {
            // emit stub Java source file, only for compilation
            // units enumerated explicitly on the command line
            JCClassDecl cdef = (JCClassDecl) env.tree;
            if (untranslated instanceof JCClassDecl && rootClasses.contains((JCClassDecl) untranslated) && ((cdef.mods.flags & (Flags.PROTECTED | Flags.PUBLIC)) != 0 || cdef.sym.packge().getQualifiedName() == names.java_lang)) {
                results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, removeMethodBodies(cdef)));
            }
            return;
        }
        if (shouldStop(CompileState.TRANSTYPES))
            return;
        env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
        compileStates.put(env, CompileState.TRANSTYPES);
        if (source.allowLambda() && scanner.hasLambdas) {
            if (shouldStop(CompileState.UNLAMBDA))
                return;
            env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
            compileStates.put(env, CompileState.UNLAMBDA);
        }
        if (shouldStop(CompileState.LOWER))
            return;
        if (sourceOutput) {
            // emit standard Java source file, only for compilation
            // units enumerated explicitly on the command line
            JCClassDecl cdef = (JCClassDecl) env.tree;
            if (untranslated instanceof JCClassDecl && rootClasses.contains((JCClassDecl) untranslated)) {
                results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef));
            }
            return;
        }
        // translate out inner classes
        List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
        compileStates.put(env, CompileState.LOWER);
        if (shouldStop(CompileState.LOWER))
            return;
        // generate code for each class
        for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
            JCClassDecl cdef = (JCClassDecl) l.head;
            results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef));
        }
    } finally {
        log.useSource(prev);
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject)

Example 15 with JavaFileObject

use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.

the class JavaCompiler method flow.

/**
 * Perform dataflow checks on an attributed parse tree.
 */
protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
    if (compileStates.isDone(env, CompileState.FLOW)) {
        results.add(env);
        return;
    }
    try {
        if (shouldStop(CompileState.FLOW))
            return;
        if (relax) {
            results.add(env);
            return;
        }
        if (verboseCompilePolicy)
            printNote("[flow " + env.enclClass.sym + "]");
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
        try {
            make.at(Position.FIRSTPOS);
            TreeMaker localMake = make.forToplevel(env.toplevel);
            flow.analyzeTree(env, localMake);
            compileStates.put(env, CompileState.FLOW);
            if (shouldStop(CompileState.FLOW))
                return;
            results.add(env);
        } finally {
            log.useSource(prev);
        }
    } finally {
        if (!taskListener.isEmpty()) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
            taskListener.finished(e);
        }
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent)

Aggregations

JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)53 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)9 File (java.io.File)8 IOException (java.io.IOException)8 HashSet (java.util.HashSet)8 CeylonFileObject (org.eclipse.ceylon.compiler.java.codegen.CeylonFileObject)5 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)5 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)5 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)5 InputStream (java.io.InputStream)4 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)4 LinkedHashSet (java.util.LinkedHashSet)3 VirtualFile (org.eclipse.ceylon.compiler.typechecker.io.VirtualFile)3 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)3 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)3 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 ByteBuffer (java.nio.ByteBuffer)2 CharBuffer (java.nio.CharBuffer)2