mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 20:15:09 +00:00
Made lombok more robust by catching exceptions near the top level and turning them into eclipse-wide errors in the worst case, but usually in an error in the problems dialog.
This commit is contained in:
1 parent
65cd03b6c5
commit
65de2407ae
2 files changed
+51
-40
No files matched your search
@@ -25,7 +25,6 @@ import org.eclipse.jdt.internal.compiler.ast.Initializer;
|
||||
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Statement;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
|
||||
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
|
||||
import org.eclipse.jdt.internal.compiler.util.Util;
|
||||
|
||||
@@ -81,7 +80,7 @@ public class EclipseAST {
|
||||
return nodeMap.get(node);
|
||||
}
|
||||
|
||||
private static class ParseProblem {
|
||||
private class ParseProblem {
|
||||
final boolean isWarning;
|
||||
final String message;
|
||||
final Node node;
|
||||
@@ -95,13 +94,18 @@ public class EclipseAST {
|
||||
this.sourceStart = sourceStart;
|
||||
this.sourceEnd = sourceEnd;
|
||||
}
|
||||
|
||||
void addToCompilationResult() {
|
||||
addProblemToCompilationResult(getFileName(), (CompilationUnitDeclaration) top().getEclipseNode(),
|
||||
isWarning, message, node.getEclipseNode(), sourceStart, sourceEnd);
|
||||
}
|
||||
}
|
||||
|
||||
public void propagateProblems() {
|
||||
if ( queuedProblems.isEmpty() ) return;
|
||||
CompilationUnitDeclaration cud = (CompilationUnitDeclaration) top().getEclipseNode();
|
||||
if ( cud.compilationResult == null ) return;
|
||||
for ( ParseProblem problem : queuedProblems ) addProblemToCompilationResult(problem);
|
||||
for ( ParseProblem problem : queuedProblems ) problem.addToCompilationResult();
|
||||
queuedProblems.clear();
|
||||
}
|
||||
|
||||
@@ -112,33 +116,25 @@ public class EclipseAST {
|
||||
propagateProblems();
|
||||
}
|
||||
|
||||
private void addProblemToCompilationResult(ParseProblem problem) {
|
||||
Node referenceContextNode = problem.node;
|
||||
while ( !(referenceContextNode.getEclipseNode() instanceof ReferenceContext) ) {
|
||||
referenceContextNode = referenceContextNode.up();
|
||||
}
|
||||
ReferenceContext referenceContext = (ReferenceContext)referenceContextNode.getEclipseNode();
|
||||
char[] fileName = getFileName().toCharArray();
|
||||
String message = problem.message;
|
||||
static void addProblemToCompilationResult(String fileName, CompilationUnitDeclaration ast,
|
||||
boolean isWarning, String message, ASTNode node, int sourceStart, int sourceEnd) {
|
||||
char[] fileNameArray = fileName.toCharArray();
|
||||
int lineNumber = 0;
|
||||
int columnNumber = 1;
|
||||
int startPosition = problem.sourceStart;
|
||||
int endPosition = problem.sourceEnd;
|
||||
if (referenceContext != null) {
|
||||
CompilationResult result = referenceContext.compilationResult();
|
||||
int[] lineEnds = null;
|
||||
lineNumber = startPosition >= 0
|
||||
? Util.getLineNumber(startPosition, lineEnds = result.getLineSeparatorPositions(), 0, lineEnds.length-1)
|
||||
: 0;
|
||||
columnNumber = startPosition >= 0
|
||||
? Util.searchColumnNumber(result.getLineSeparatorPositions(), lineNumber,startPosition)
|
||||
: 0;
|
||||
}
|
||||
CategorizedProblem ecProblem = new AptProblem(referenceContext,
|
||||
fileName, message, 0, new String[0],
|
||||
problem.isWarning ? ProblemSeverities.Warning : ProblemSeverities.Error,
|
||||
startPosition, endPosition, lineNumber, columnNumber);
|
||||
((CompilationUnitDeclaration)top().getEclipseNode()).compilationResult.record(ecProblem, referenceContext);
|
||||
CompilationResult result = ast.compilationResult;
|
||||
int[] lineEnds = null;
|
||||
lineNumber = sourceStart >= 0
|
||||
? Util.getLineNumber(sourceStart, lineEnds = result.getLineSeparatorPositions(), 0, lineEnds.length-1)
|
||||
: 0;
|
||||
columnNumber = sourceStart >= 0
|
||||
? Util.searchColumnNumber(result.getLineSeparatorPositions(), lineNumber,sourceStart)
|
||||
: 0;
|
||||
|
||||
CategorizedProblem ecProblem = new AptProblem(null,
|
||||
fileNameArray, message, 0, new String[0],
|
||||
isWarning ? ProblemSeverities.Warning : ProblemSeverities.Error,
|
||||
sourceStart, sourceEnd, lineNumber, columnNumber);
|
||||
ast.compilationResult.record(ecProblem, null);
|
||||
}
|
||||
|
||||
public final class Node {
|
||||
|
||||
@@ -31,18 +31,17 @@ public class TransformEclipseAST {
|
||||
private static final Field astCacheField;
|
||||
private static final HandlerLibrary handlers;
|
||||
|
||||
private static boolean disableLombok = false;
|
||||
|
||||
static {
|
||||
Field f = null;
|
||||
HandlerLibrary l = null;
|
||||
try {
|
||||
l = HandlerLibrary.load();
|
||||
} catch ( Exception e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
f = CompilationUnitDeclaration.class.getDeclaredField("$lombokAST");
|
||||
} catch ( NoSuchFieldException ignore ) {
|
||||
ignore.printStackTrace();
|
||||
} catch ( Throwable t ) {
|
||||
Eclipse.error("Problem initializing lombok", t);
|
||||
disableLombok = true;
|
||||
}
|
||||
astCacheField = f;
|
||||
handlers = l;
|
||||
@@ -60,12 +59,28 @@ public class TransformEclipseAST {
|
||||
* @param ast The AST node belonging to the compilation unit (java speak for a single source file).
|
||||
*/
|
||||
public static void transform(Parser parser, CompilationUnitDeclaration ast) {
|
||||
EclipseAST existing = getCache(ast);
|
||||
if ( existing == null ) {
|
||||
existing = new EclipseAST(ast);
|
||||
setCache(ast, existing);
|
||||
} else existing.reparse();
|
||||
new TransformEclipseAST(existing).go();
|
||||
if ( disableLombok ) return;
|
||||
try {
|
||||
EclipseAST existing = getCache(ast);
|
||||
if ( existing == null ) {
|
||||
existing = new EclipseAST(ast);
|
||||
setCache(ast, existing);
|
||||
} else existing.reparse();
|
||||
new TransformEclipseAST(existing).go();
|
||||
} catch ( Throwable t ) {
|
||||
try {
|
||||
String fileName = "(unknown)";
|
||||
if ( ast.compilationResult != null && ast.compilationResult.fileName != null ) {
|
||||
fileName = new String(ast.compilationResult.fileName);
|
||||
}
|
||||
|
||||
String message = "Lombok can't parse this source: " + t.toString();
|
||||
|
||||
EclipseAST.addProblemToCompilationResult(fileName, ast, false, message, ast, 0, 0);
|
||||
} catch ( Throwable t2 ) {
|
||||
Eclipse.error("Can't create an error in the problems dialog while adding: " + t.toString(), t2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static EclipseAST getCache(CompilationUnitDeclaration ast) {
|
||||
|
||||
Reference in new issue
Block a user