Search in sources :

Example 1 with JavacElements

use of com.sun.tools.javac.model.JavacElements in project ceylon-compiler by ceylon.

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) {
            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 {
        if (oldSource != null)
            log.useSource(oldSource);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavacElements(com.sun.tools.javac.model.JavacElements) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

JavacElements (com.sun.tools.javac.model.JavacElements)1 JCTree (com.sun.tools.javac.tree.JCTree)1 JavaFileObject (javax.tools.JavaFileObject)1