Search in sources :

Example 46 with JavaFileObject

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

the class PathFileObject method getCharContent.

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    CharBuffer cb = fileManager.getCachedContent(this);
    if (cb == null) {
        InputStream in = openInputStream();
        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) InputStream(java.io.InputStream) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 47 with JavaFileObject

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

the class JavacMessager method printMessage.

/**
 * Prints a message of the specified kind at the location of the
 * annotation value inside the annotation mirror of the annotated
 * element.
 *
 * @param kind the kind of message
 * @param msg  the message, or an empty string if none
 * @param e    the annotated element
 * @param a    the annotation containing the annotaiton value
 * @param v    the annotation value to use as a position hint
 */
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
    JavaFileObject oldSource = null;
    JavaFileObject newSource = null;
    JCDiagnostic.DiagnosticPosition pos = null;
    JavacElements elemUtils = processingEnv.getElementUtils();
    Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
    if (treeTop != null) {
        newSource = treeTop.snd.sourcefile;
        if (newSource != null) {
            // save the old version and reinstate it later
            oldSource = log.useSource(newSource);
            pos = treeTop.fst.pos();
        }
    }
    try {
        switch(kind) {
            case ERROR:
                errorCount++;
                boolean prev = log.multipleErrors;
                log.multipleErrors = true;
                try {
                    log.error(pos, "proc.messager", msg.toString());
                } finally {
                    log.multipleErrors = prev;
                }
                break;
            case WARNING:
                warningCount++;
                log.warning(pos, "proc.messager", msg.toString());
                break;
            case MANDATORY_WARNING:
                warningCount++;
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
                break;
            default:
                log.note(pos, "proc.messager", msg.toString());
                break;
        }
    } finally {
        // reinstate the saved version, only if it was saved earlier
        if (newSource != null)
            log.useSource(oldSource);
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) JavacElements(org.eclipse.ceylon.langtools.tools.javac.model.JavacElements) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 48 with JavaFileObject

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

the class JavacProcessingEnvironment method doProcessing.

// TODO: internal catch clauses?; catch and rethrow an annotation
// processing error
public JavaCompiler doProcessing(Context context, List<JCCompilationUnit> roots, List<ClassSymbol> classSymbols, Iterable<? extends PackageSymbol> pckSymbols, Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
    log = Log.instance(context);
    Set<PackageSymbol> specifiedPackages = new LinkedHashSet<PackageSymbol>();
    for (PackageSymbol psym : pckSymbols) specifiedPackages.add(psym);
    this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages);
    Round round = new Round(context, roots, classSymbols, deferredDiagnosticHandler);
    boolean errorStatus;
    boolean moreToDo;
    do {
        // Run processors for round n
        round.run(false, false);
        // Processors for round n have run to completion.
        // Check for errors and whether there is more work to do.
        errorStatus = round.unrecoverableError();
        moreToDo = moreToDo();
        round.showDiagnostics(errorStatus || showResolveErrors);
        // Set up next round.
        // Copy mutable collections returned from filer.
        round = round.next(new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects()), new LinkedHashMap<String, JavaFileObject>(filer.getGeneratedClasses()));
        // Check for errors during setup.
        if (round.unrecoverableError())
            errorStatus = true;
    } while (moreToDo && !errorStatus);
    // run last round
    round.run(true, errorStatus);
    round.showDiagnostics(true);
    filer.warnIfUnclosedFiles();
    warnIfUnmatchedOptions();
    /*
         * If an annotation processor raises an error in a round,
         * that round runs to completion and one last round occurs.
         * The last round may also occur because no more source or
         * class files have been generated.  Therefore, if an error
         * was raised on either of the last *two* rounds, the compile
         * should exit with a nonzero exit code.  The current value of
         * errorStatus holds whether or not an error was raised on the
         * second to last round; errorRaised() gives the error status
         * of the last round.
         */
    if (messager.errorRaised() || werror && round.warningCount() > 0 && round.errorCount() > 0)
        errorStatus = true;
    Set<JavaFileObject> newSourceFiles = new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects());
    roots = cleanTrees(round.roots);
    JavaCompiler compiler = round.finalCompiler();
    // Ceylon: we need to call parseFiles even if we did not add anything, to reset
    // module stuff
    // if (newSourceFiles.size() > 0)
    roots = roots.appendList(compiler.parseFiles(newSourceFiles));
    errorStatus = errorStatus || (compiler.errorCount() > 0);
    // Free resources
    this.close();
    if (!taskListener.isEmpty())
        taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
    if (errorStatus) {
        if (compiler.errorCount() == 0)
            compiler.log.nerrors++;
        return compiler;
    }
    compiler.enterTreesIfNeeded(roots);
    return compiler;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) JavaCompiler(org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)

Example 49 with JavaFileObject

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

the class MandatoryWarningHandler method report.

/**
 * Report a mandatory warning.
 */
public void report(DiagnosticPosition pos, String msg, Object... args) {
    JavaFileObject currentSource = log.currentSourceFile();
    if (verbose) {
        if (sourcesWithReportedWarnings == null)
            sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
        if (log.nwarnings < log.MaxWarnings) {
            // generate message and remember the source file
            logMandatoryWarning(pos, msg, args);
            sourcesWithReportedWarnings.add(currentSource);
        } else if (deferredDiagnosticKind == null) {
            // set up deferred message
            if (sourcesWithReportedWarnings.contains(currentSource)) {
                // more errors in a file that already has reported warnings
                deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
            } else {
                // warnings in a new source file
                deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
            }
            deferredDiagnosticSource = currentSource;
            deferredDiagnosticArg = currentSource;
        } else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE) && !equal(deferredDiagnosticSource, currentSource)) {
            // additional errors in more than one source file
            deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
            deferredDiagnosticArg = null;
        }
    } else {
        if (deferredDiagnosticKind == null) {
            // warnings in a single source
            deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
            deferredDiagnosticSource = currentSource;
            deferredDiagnosticArg = currentSource;
        } else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE && !equal(deferredDiagnosticSource, currentSource)) {
            // warnings in multiple source files
            deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
            deferredDiagnosticArg = null;
        }
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) HashSet(java.util.HashSet)

Example 50 with JavaFileObject

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

the class LanguageCompiler method addModuleDescriptors.

// This is a bit of a hack, but if we got passed a list of resources
// without any accompaning source files we'll not be able to determine
// the module to which the resource files belong. So to try to fix that
// we see if a module file exists in the source folders and add it to
// the list of source files
private List<JavaFileObject> addModuleDescriptors(List<JavaFileObject> sourceFiles, List<JavaFileObject> resourceFiles) {
    List<JavaFileObject> result = sourceFiles;
    JavacFileManager dfm = (JavacFileManager) fileManager;
    for (JavaFileObject fo : resourceFiles) {
        String resName = JarUtils.toPlatformIndependentPath(dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
        JavaFileObject moduleFile = findModuleDescriptorForFile(new File(resName));
        if (moduleFile != null && !result.contains(moduleFile)) {
            result = result.append(moduleFile);
        }
    }
    return result;
}
Also used : JavacFileManager(org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

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