diff options
Diffstat (limited to 'src/core/lombok/javac/JavacAST.java')
-rw-r--r-- | src/core/lombok/javac/JavacAST.java | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index c2c55f1d..71c17538 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 The Project Lombok Authors. + * Copyright (C) 2009-2013 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,6 +28,7 @@ import java.util.List; import javax.annotation.processing.Messager; import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; import javax.tools.JavaFileObject; import lombok.core.AST; @@ -50,6 +51,8 @@ import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.JCDiagnostic; +import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; @@ -287,8 +290,28 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { if (node != null) nodes.add(node); } + private static final Field JAVAC7_DEFERRED_DIAGNOSTICS; + + static { + Field f = null; + try { + f = Log.class.getField("deferredDiagnostics"); + if (!ListBuffer.class.isAssignableFrom(f.getType())) throw new NoSuchFieldException("deferredDiagnostics does not have the expected type."); + } catch (NoSuchFieldException e) {} + JAVAC7_DEFERRED_DIAGNOSTICS = f; + } + + /** + * 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(), pos.getEndPosition(top.endPositions)); + } + /** Supply either a position or a node (in that case, position of the node is used) */ - void printMessage(Diagnostic.Kind kind, String message, JavacNode node, DiagnosticPosition pos) { + void printMessage(Diagnostic.Kind kind, String message, JavacNode node, DiagnosticPosition pos, boolean attemptToRemoveErrorsInRange) { JavaFileObject oldSource = null; JavaFileObject newSource = null; JCTree astObject = node == null ? null : node.get(); @@ -298,6 +321,9 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { oldSource = log.useSource(newSource); if (pos == null) pos = astObject.pos(); } + if (pos != null && attemptToRemoveErrorsInRange) { + removeFromDeferredDiagnostics(pos.getStartPosition(), pos.getEndPosition(top.endPositions)); + } try { switch (kind) { case ERROR: @@ -319,6 +345,35 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { if (oldSource != null) log.useSource(oldSource); } } + + private void removeFromDeferredDiagnostics(int startPos, int endPos) { + DiagnosticListener<?> listener = getContext().get(DiagnosticListener.class); + if (listener instanceof CapturingDiagnosticListener) { + ((CapturingDiagnosticListener) listener).suppress(startPos, endPos); + } + try { + if (JAVAC7_DEFERRED_DIAGNOSTICS != null) { + ListBuffer<?> deferredDiagnostics = (ListBuffer<?>) JAVAC7_DEFERRED_DIAGNOSTICS.get(log); + ListBuffer<Object> newDeferredDiagnostics = ListBuffer.lb(); + for (Object diag : deferredDiagnostics) { + if (!(diag instanceof JCDiagnostic)) { + newDeferredDiagnostics.add(diag); + continue; + } + long here = ((JCDiagnostic) diag).getStartPosition(); + if (here >= startPos && here < endPos) { + // We eliminate it + } else { + newDeferredDiagnostics.add(diag); + } + } + JAVAC7_DEFERRED_DIAGNOSTICS.set(log, newDeferredDiagnostics); + } + } catch (Exception e) { + // We do not expect failure here; if failure does occur, the best course of action is to silently continue; the result will be that the error output of + // javac will contain rather a lot of messages, but this is a lot better than just crashing during compilation! + } + } /** {@inheritDoc} */ @Override protected void setElementInASTCollection(Field field, Object refField, List<Collection<?>> chain, Collection<?> collection, int idx, JCTree newN) throws IllegalAccessException { |