Search in sources :

Example 6 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project checker-framework by typetools.

the class SourceChecker method logCheckerError.

private void logCheckerError(CheckerError ce) {
    if (ce.getMessage() == null) {
        final String stackTrace = formatStackTrace(ce.getStackTrace());
        ErrorReporter.errorAbort("Null error message while logging Checker error.\nStack Trace:\n" + stackTrace);
    }
    StringBuilder msg = new StringBuilder(ce.getMessage());
    if ((processingEnv == null || processingEnv.getOptions() == null || processingEnv.getOptions().containsKey("printErrorStack")) && ce.getCause() != null) {
        if (this.currentRoot != null && this.currentRoot.getSourceFile() != null) {
            msg.append("\nCompilation unit: " + this.currentRoot.getSourceFile().getName());
        }
        if (this.visitor != null) {
            DiagnosticPosition pos = (DiagnosticPosition) this.visitor.lastVisited;
            DiagnosticSource source = new DiagnosticSource(this.currentRoot.getSourceFile(), null);
            int linenr = source.getLineNumber(pos.getStartPosition());
            int col = source.getColumnNumber(pos.getStartPosition(), true);
            String line = source.getLine(pos.getStartPosition());
            msg.append("\nLast visited tree at line " + linenr + " column " + col + ":\n" + line);
        }
        msg.append("\nException: " + ce.getCause().toString() + "; " + formatStackTrace(ce.getCause().getStackTrace()));
        Throwable cause = ce.getCause().getCause();
        while (cause != null) {
            msg.append("\nUnderlying Exception: " + (cause.toString() + "; " + formatStackTrace(cause.getStackTrace())));
            cause = cause.getCause();
        }
    } else {
        if (ce.userError) {
            msg.append('.');
        } else {
            msg.append("; The Checker Framework crashed.  Please report the crash.  To see " + "the full stack trace invoke the compiler with -AprintErrorStack");
        }
    }
    if (this.messager == null) {
        messager = processingEnv.getMessager();
    }
    this.messager.printMessage(javax.tools.Diagnostic.Kind.ERROR, msg);
}
Also used : DiagnosticSource(com.sun.tools.javac.util.DiagnosticSource) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)

Example 7 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project lombok by rzwitserloot.

the class JavacHandlerUtil method createAnnotation.

/**
 * Creates an instance of {@code AnnotationValues} for the provided AST Node.
 *
 * @param type An annotation class type, such as {@code lombok.Getter.class}.
 * @param node A Lombok AST node representing an annotation in source code.
 */
public static <A extends Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final JavacNode node) {
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
    JCAnnotation anno = (JCAnnotation) node.get();
    List<JCExpression> arguments = anno.getArguments();
    for (JCExpression arg : arguments) {
        String mName;
        JCExpression rhs;
        java.util.List<String> raws = new ArrayList<String>();
        java.util.List<Object> guesses = new ArrayList<Object>();
        java.util.List<Object> expressions = new ArrayList<Object>();
        final java.util.List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>();
        if (arg instanceof JCAssign) {
            JCAssign assign = (JCAssign) arg;
            mName = assign.lhs.toString();
            rhs = assign.rhs;
        } else {
            rhs = arg;
            mName = "value";
        }
        if (rhs instanceof JCNewArray) {
            List<JCExpression> elems = ((JCNewArray) rhs).elems;
            for (JCExpression inner : elems) {
                raws.add(inner.toString());
                expressions.add(inner);
                guesses.add(calculateGuess(inner));
                positions.add(inner.pos());
            }
        } else {
            raws.add(rhs.toString());
            expressions.add(rhs);
            guesses.add(calculateGuess(rhs));
            positions.add(rhs.pos());
        }
        values.put(mName, new AnnotationValue(node, raws, expressions, guesses, true) {

            @Override
            public void setError(String message, int valueIdx) {
                if (valueIdx < 0)
                    node.addError(message);
                else
                    node.addError(message, positions.get(valueIdx));
            }

            @Override
            public void setWarning(String message, int valueIdx) {
                if (valueIdx < 0)
                    node.addWarning(message);
                else
                    node.addWarning(message, positions.get(valueIdx));
            }
        });
    }
    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(node, new ArrayList<String>(), new ArrayList<Object>(), new ArrayList<Object>(), false) {

                @Override
                public void setError(String message, int valueIdx) {
                    node.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    node.addWarning(message);
                }
            });
        }
    }
    return new AnnotationValues<A>(type, values, node);
}
Also used : HashMap(java.util.HashMap) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) AnnotationValues(lombok.core.AnnotationValues) AnnotationValue(lombok.core.AnnotationValues.AnnotationValue) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 8 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project ceylon-compiler by ceylon.

the class Lower method visitAssert.

/**
 * Visitor method for assert statements. Translate them away.
 */
public void visitAssert(JCAssert tree) {
    DiagnosticPosition detailPos = (tree.detail == null) ? tree.pos() : tree.detail.pos();
    tree.cond = translate(tree.cond, syms.booleanType);
    if (!tree.cond.type.isTrue()) {
        JCExpression cond = assertFlagTest(tree.pos());
        List<JCExpression> exnArgs = (tree.detail == null) ? List.<JCExpression>nil() : List.of(translate(tree.detail));
        if (!tree.cond.type.isFalse()) {
            cond = makeBinary(JCTree.AND, cond, makeUnary(JCTree.NOT, tree.cond));
        }
        result = make.If(cond, make_at(detailPos).Throw(makeNewClass(syms.assertionErrorType, exnArgs)), null);
    } else {
        result = make.Skip();
    }
}
Also used : DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)

Example 9 with DiagnosticPosition

use of com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition in project lombok by rzwitserloot.

the class JavacAST method removeDeferredErrors.

/**
 * Attempts to remove any compiler errors generated by java whose reporting position is located anywhere between the start and end of the supplied node.
 */
void removeDeferredErrors(JavacNode node) {
    DiagnosticPosition pos = node.get().pos();
    JCCompilationUnit top = (JCCompilationUnit) top().get();
    removeFromDeferredDiagnostics(pos.getStartPosition(), Javac.getEndPosition(pos, top));
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)

Aggregations

DiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)9 Symbol (com.sun.tools.javac.code.Symbol)2 Type (com.sun.tools.javac.code.Type)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)2 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)2 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)2 DiagnosticSource (com.sun.tools.javac.util.DiagnosticSource)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AnnotationValues (lombok.core.AnnotationValues)2 AnnotationValue (lombok.core.AnnotationValues.AnnotationValue)2 LocalizedString (com.sun.tools.javac.api.Formattable.LocalizedString)1 Kinds.kindName (com.sun.tools.javac.code.Kinds.kindName)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)1 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)1