From 65de2407ae2b5fac6244d2ca670ea5b458992221 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 14 Jun 2009 23:55:39 +0200 Subject: 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. --- src/lombok/eclipse/EclipseAST.java | 52 +++++++++++++---------------- src/lombok/eclipse/TransformEclipseAST.java | 39 +++++++++++++++------- 2 files changed, 51 insertions(+), 40 deletions(-) (limited to 'src/lombok/eclipse') diff --git a/src/lombok/eclipse/EclipseAST.java b/src/lombok/eclipse/EclipseAST.java index edf70d70..8730849b 100644 --- a/src/lombok/eclipse/EclipseAST.java +++ b/src/lombok/eclipse/EclipseAST.java @@ -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 { diff --git a/src/lombok/eclipse/TransformEclipseAST.java b/src/lombok/eclipse/TransformEclipseAST.java index 34350597..c9690953 100644 --- a/src/lombok/eclipse/TransformEclipseAST.java +++ b/src/lombok/eclipse/TransformEclipseAST.java @@ -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) { -- cgit