Search in sources :

Example 16 with JavaFileObject

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

the class JavaCompiler method generate.

public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
    if (shouldStop(CompileState.GENERATE))
        return;
    boolean usePrintSource = (stubOutput || sourceOutput || printFlat);
    for (Pair<Env<AttrContext>, JCClassDecl> x : queue) {
        Env<AttrContext> env = x.fst;
        JCClassDecl cdef = x.snd;
        if (verboseCompilePolicy) {
            printNote("[generate " + (usePrintSource ? " source" : "code") + " " + cdef.sym + "]");
        }
        if (!taskListener.isEmpty()) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
            taskListener.started(e);
        }
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
        try {
            JavaFileObject file;
            if (usePrintSource)
                file = printSource(env, cdef);
            else {
                if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT) && jniWriter.needsHeader(cdef.sym)) {
                    jniWriter.write(cdef.sym);
                }
                file = genCode(env, cdef);
            }
            if (results != null && file != null)
                results.add(file);
        } catch (IOException ex) {
            log.error(cdef.pos(), "class.cant.write", cdef.sym, ex.getMessage());
            return;
        } finally {
            log.useSource(prev);
        }
        if (!taskListener.isEmpty()) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
            taskListener.finished(e);
        }
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent)

Example 17 with JavaFileObject

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

the class JavaCompiler method parseFiles.

/**
 * Parses a list of files.
 */
public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
    if (shouldStop(CompileState.PARSE))
        return List.nil();
    // parse all files
    ListBuffer<JCCompilationUnit> trees = new ListBuffer<>();
    Set<JavaFileObject> filesSoFar = new HashSet<JavaFileObject>();
    for (JavaFileObject fileObject : fileObjects) {
        if (!filesSoFar.contains(fileObject)) {
            filesSoFar.add(fileObject);
            trees.append(parse(fileObject));
        }
    }
    return trees.toList();
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 18 with JavaFileObject

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

the class JavaCompiler method resolveIdent.

/**
 * Resolve an identifier.
 * @param name      The identifier to resolve
 */
public Symbol resolveIdent(String name) {
    if (name.equals(""))
        return syms.errSymbol;
    JavaFileObject prev = log.useSource(null);
    try {
        JCExpression tree = null;
        for (String s : name.split("\\.", -1)) {
            if (// TODO: check for keywords
            !SourceVersion.isIdentifier(s))
                return syms.errSymbol;
            tree = (tree == null) ? make.Ident(names.fromString(s)) : make.Select(tree, names.fromString(s));
        }
        JCCompilationUnit toplevel = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
        toplevel.packge = syms.unnamedPackage;
        return attr.attribIdent(tree, toplevel);
    } finally {
        log.useSource(prev);
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 19 with JavaFileObject

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

the class JavaCompiler method attribute.

/**
 * Attribute a parse tree.
 * @returns the attributed parse tree
 */
public Env<AttrContext> attribute(Env<AttrContext> env) {
    if (compileStates.isDone(env, CompileState.ATTR))
        return env;
    if (verboseCompilePolicy)
        printNote("[attribute " + env.enclClass.sym + "]");
    if (verbose)
        log.printVerbose("checking.attribution", env.enclClass.sym);
    if (!taskListener.isEmpty()) {
        TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
        taskListener.started(e);
    }
    JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
    try {
        attr.attrib(env);
        if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
            // if in fail-over mode, ensure that AST expression nodes
            // are correctly initialized (e.g. they have a type/symbol)
            attr.postAttr(env.tree);
        }
        compileStates.put(env, CompileState.ATTR);
        if (rootClasses != null && rootClasses.contains(env.enclClass)) {
            // This was a class that was explicitly supplied for compilation.
            // If we want to capture the public api of this class,
            // then now is a good time to do it.
            reportPublicApi(env.enclClass.sym);
        }
    } finally {
        log.useSource(prev);
    }
    return env;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent)

Example 20 with JavaFileObject

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

the class JavaCompiler method complete.

/**
 * Complete compiling a source file that has been accessed
 *  by the class file reader.
 *  @param c          The class the source file of which needs to be compiled.
 */
public void complete(ClassSymbol c) throws CompletionFailure {
    // System.err.println("completing " + c);//DEBUG
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    JCCompilationUnit tree;
    JavaFileObject filename = c.classfile;
    JavaFileObject prev = log.useSource(filename);
    try {
        tree = parse(filename, filename.getCharContent(false));
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
    } finally {
        log.useSource(prev);
    }
    if (!taskListener.isEmpty()) {
        TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
        taskListener.started(e);
    }
    enter.complete(List.of(tree), c);
    if (!taskListener.isEmpty()) {
        TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
        taskListener.finished(e);
    }
    if (enter.getEnv(c) == null) {
        boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
        if (isPkgInfo) {
            if (enter.getEnv(tree.packge) == null) {
                JCDiagnostic diag = diagFactory.fragment("file.does.not.contain.package", c.location());
                throw reader.new BadClassFile(c, filename, diag);
            }
        } else {
            JCDiagnostic diag = diagFactory.fragment("file.doesnt.contain.class", c.getQualifiedName());
            throw reader.new BadClassFile(c, filename, diag);
        }
    }
    implicitSourceFilesRead = true;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

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