mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 14:16:11 +00:00
Warnings and errors on specific positions works, sort of. Still missing something crucial to properly make javac generate the line + the ^^^^ indicators.
This commit is contained in:
1 parent
ccf0e5da41
commit
0c9897303f
2 files changed
+74
-14
No files matched your search
@@ -22,9 +22,6 @@ import lombok.core.TypeLibrary;
|
||||
import lombok.core.TypeResolver;
|
||||
import lombok.core.AnnotationValues.AnnotationValue;
|
||||
import lombok.core.AnnotationValues.AnnotationValueDecodeFail;
|
||||
import lombok.eclipse.Eclipse;
|
||||
import lombok.eclipse.EclipseAST;
|
||||
import lombok.eclipse.EclipseASTVisitor;
|
||||
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
import com.sun.tools.javac.tree.JCTree.JCAssign;
|
||||
@@ -34,6 +31,8 @@ import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
|
||||
import com.sun.tools.javac.tree.JCTree.JCIdent;
|
||||
import com.sun.tools.javac.tree.JCTree.JCLiteral;
|
||||
import com.sun.tools.javac.tree.JCTree.JCNewArray;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
|
||||
|
||||
|
||||
public class HandlerLibrary {
|
||||
@@ -69,7 +68,7 @@ public class HandlerLibrary {
|
||||
} else return null;
|
||||
}
|
||||
|
||||
public void handle(JavacAST.Node node) {
|
||||
public void handle(final JavacAST.Node node) {
|
||||
Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
|
||||
JCAnnotation anno = (JCAnnotation) node.get();
|
||||
List<JCExpression> arguments = anno.getArguments();
|
||||
@@ -78,6 +77,7 @@ public class HandlerLibrary {
|
||||
String name = m.getName();
|
||||
List<String> raws = new ArrayList<String>();
|
||||
List<Object> guesses = new ArrayList<Object>();
|
||||
final List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>();
|
||||
|
||||
for ( JCExpression arg : arguments ) {
|
||||
JCAssign assign = (JCAssign) arg;
|
||||
@@ -89,17 +89,18 @@ public class HandlerLibrary {
|
||||
for ( JCExpression inner : elems ) {
|
||||
raws.add(inner.toString());
|
||||
guesses.add(calculateGuess(inner));
|
||||
positions.add(new SimpleDiagnosticPosition(inner.pos));
|
||||
}
|
||||
} else {
|
||||
raws.add(rhs.toString());
|
||||
guesses.add(calculateGuess(rhs));
|
||||
positions.add(new SimpleDiagnosticPosition(rhs.pos));
|
||||
}
|
||||
}
|
||||
|
||||
values.put(name, new AnnotationValue(node, raws, guesses) {
|
||||
@Override public void setError(String message, int valueIdx) {
|
||||
//TODO
|
||||
super.setError(message, valueIdx);
|
||||
node.addError(message, positions.get(valueIdx));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package lombok.javac;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import lombok.core.AST;
|
||||
|
||||
@@ -22,21 +25,21 @@ import com.sun.tools.javac.tree.JCTree.JCImport;
|
||||
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.Log;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
|
||||
|
||||
public class JavacAST extends AST<JCTree> {
|
||||
private final Trees trees;
|
||||
private final JavacProcessingEnvironment env;
|
||||
private final Messager messager;
|
||||
private final Name.Table nameTable;
|
||||
private final TreeMaker treeMaker;
|
||||
private final Log log;
|
||||
|
||||
public JavacAST(Trees trees, JavacProcessingEnvironment env, JCCompilationUnit top) {
|
||||
super(top.sourcefile == null ? null : top.sourcefile.toString());
|
||||
setTop(buildCompilationUnit(top));
|
||||
this.trees = trees;
|
||||
this.env = env;
|
||||
this.messager = env.getMessager();
|
||||
this.log = Log.instance(env.getContext());
|
||||
this.nameTable = Name.Table.instance(env.getContext());
|
||||
this.treeMaker = TreeMaker.instance(env.getContext());
|
||||
}
|
||||
@@ -313,13 +316,69 @@ public class JavacAST extends AST<JCTree> {
|
||||
}
|
||||
|
||||
public void addError(String message) {
|
||||
System.err.println("ERR: " + message);
|
||||
//TODO
|
||||
printMessage(Diagnostic.Kind.ERROR, message, this, null);
|
||||
}
|
||||
|
||||
public void addError(String message, DiagnosticPosition pos) {
|
||||
printMessage(Diagnostic.Kind.ERROR, message, null, pos);
|
||||
}
|
||||
|
||||
public void addWarning(String message) {
|
||||
System.err.println("WARN: " + message);
|
||||
//TODO
|
||||
printMessage(Diagnostic.Kind.WARNING, message, this, null);
|
||||
}
|
||||
|
||||
public void addWarning(String message, DiagnosticPosition pos) {
|
||||
printMessage(Diagnostic.Kind.WARNING, message, null, pos);
|
||||
}
|
||||
}
|
||||
|
||||
/** Supply either a position or a node (in that case, position of the node is used) */
|
||||
private void printMessage(Diagnostic.Kind kind, String message, Node node, DiagnosticPosition pos) {
|
||||
JavaFileObject oldSource = null;
|
||||
JavaFileObject newSource = null;
|
||||
JCTree astObject = node == null ? null : node.get();
|
||||
JCCompilationUnit top = (JCCompilationUnit) top().get();
|
||||
if (node != null) {
|
||||
newSource = top.sourcefile;
|
||||
if (newSource != null) {
|
||||
oldSource = log.useSource(newSource);
|
||||
pos = astObject.pos();
|
||||
}
|
||||
}
|
||||
try {
|
||||
switch (kind) {
|
||||
case ERROR:
|
||||
increaseErrorCount(messager);
|
||||
boolean prev = log.multipleErrors;
|
||||
log.multipleErrors = true;
|
||||
try {
|
||||
log.error(pos, "proc.messager", message);
|
||||
} finally {
|
||||
log.multipleErrors = prev;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
case WARNING:
|
||||
log.warning(pos, "proc.messager", message);
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
if (oldSource != null)
|
||||
log.useSource(oldSource);
|
||||
}
|
||||
}
|
||||
|
||||
private void increaseErrorCount(Messager messager) {
|
||||
try {
|
||||
Field f = messager.getClass().getDeclaredField("errorCount");
|
||||
f.setAccessible(true);
|
||||
if ( f.getType() == int.class ) {
|
||||
int val = ((Number)f.get(messager)).intValue();
|
||||
val++;
|
||||
f.set(messager, val);
|
||||
}
|
||||
} catch ( Throwable t ) {
|
||||
//Very unfortunate, but in most cases it still works fine, so we'll silently swallow it.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user