aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2011-08-17 00:26:56 +0200
committerRoel Spilker <r.spilker@gmail.com>2011-08-17 00:26:56 +0200
commitb94911431f60e7c02707dc0160dffcbdb28fce47 (patch)
tree03f982e33245d8255f44ff0256d71b60e9afb10e /src
parent14efdb09ad5ab153d2c1a1019d5f5a97c25f8249 (diff)
downloadlombok-b94911431f60e7c02707dc0160dffcbdb28fce47.tar.gz
lombok-b94911431f60e7c02707dc0160dffcbdb28fce47.tar.bz2
lombok-b94911431f60e7c02707dc0160dffcbdb28fce47.zip
Make Extract Method work without affecting the rendering of the java file.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSneakyThrows.java69
1 files changed, 32 insertions, 37 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSneakyThrows.java b/src/core/lombok/eclipse/handlers/HandleSneakyThrows.java
index 6c00258e..95c11c2d 100644
--- a/src/core/lombok/eclipse/handlers/HandleSneakyThrows.java
+++ b/src/core/lombok/eclipse/handlers/HandleSneakyThrows.java
@@ -23,6 +23,7 @@ package lombok.eclipse.handlers;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import lombok.SneakyThrows;
@@ -55,6 +56,7 @@ import org.mangosdk.spi.ProviderFor;
*/
@ProviderFor(EclipseAnnotationHandler.class)
public class HandleSneakyThrows extends EclipseAnnotationHandler<SneakyThrows> {
+
private static class DeclaredException {
final String exceptionName;
final ASTNode node;
@@ -63,10 +65,6 @@ public class HandleSneakyThrows extends EclipseAnnotationHandler<SneakyThrows> {
this.exceptionName = exceptionName;
this.node = node;
}
-
- public long getPos() {
- return (long)node.sourceStart << 32 | node.sourceEnd;
- }
}
@Override public boolean deferUntilPostDiet() {
@@ -163,79 +161,76 @@ public class HandleSneakyThrows extends EclipseAnnotationHandler<SneakyThrows> {
}
private Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source, AbstractMethodDeclaration method) {
- long p = exception.getPos();
- int pS = (int)(p >> 32), pE = (int)p;
+ int methodStart = method.bodyStart;
+ int methodEnd = method.bodyEnd;
+ long methodPosEnd = methodEnd << 32 | (methodEnd & 0xFFFFFFFFL);
TryStatement tryStatement = new TryStatement();
Eclipse.setGeneratedBy(tryStatement, source);
tryStatement.tryBlock = new Block(0);
-
+
// Positions for in-method generated nodes are special
-// tryStatement.tryBlock.sourceStart = method.bodyStart; tryStatement.tryBlock.sourceEnd = method.bodyEnd;
- tryStatement.tryBlock.sourceStart = pS; tryStatement.tryBlock.sourceEnd = pE;
+ tryStatement.tryBlock.sourceStart = methodStart; tryStatement.tryBlock.sourceEnd = methodEnd;
Eclipse.setGeneratedBy(tryStatement.tryBlock, source);
tryStatement.tryBlock.statements = contents;
TypeReference typeReference;
if (exception.exceptionName.indexOf('.') == -1) {
- typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), p);
- typeReference.statementEnd = pE;
+ typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), methodPosEnd);
+ typeReference.statementEnd = methodEnd;
} else {
String[] x = exception.exceptionName.split("\\.");
char[][] elems = new char[x.length][];
long[] poss = new long[x.length];
- int start = pS;
+ Arrays.fill(poss, methodPosEnd);
for (int i = 0; i < x.length; i++) {
elems[i] = x[i].trim().toCharArray();
- int end = start + x[i].length();
- poss[i] = (long)start << 32 | end;
- start = end + 1;
}
typeReference = new QualifiedTypeReference(elems, poss);
}
Eclipse.setGeneratedBy(typeReference, source);
- Argument catchArg = new Argument("$ex".toCharArray(), p, typeReference, Modifier.FINAL);
+ Argument catchArg = new Argument("$ex".toCharArray(), methodPosEnd, typeReference, Modifier.FINAL);
Eclipse.setGeneratedBy(catchArg, source);
- catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = pE;
- catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = pS;
+ catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = methodEnd;
+ catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = methodEnd;
tryStatement.catchArguments = new Argument[] { catchArg };
MessageSend sneakyThrowStatement = new MessageSend();
Eclipse.setGeneratedBy(sneakyThrowStatement, source);
- sneakyThrowStatement.receiver = new QualifiedNameReference(new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[] { p, p }, pS, pE);
+ sneakyThrowStatement.receiver = new QualifiedNameReference(new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[2], methodEnd, methodEnd);
Eclipse.setGeneratedBy(sneakyThrowStatement.receiver, source);
- sneakyThrowStatement.receiver.statementEnd = pE;
+ sneakyThrowStatement.receiver.statementEnd = methodEnd;
sneakyThrowStatement.selector = "sneakyThrow".toCharArray();
- SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), p);
+ SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), methodPosEnd);
Eclipse.setGeneratedBy(exRef, source);
- exRef.statementEnd = pE;
+ exRef.statementEnd = methodEnd;
sneakyThrowStatement.arguments = new Expression[] { exRef };
- sneakyThrowStatement.nameSourcePosition = p;
- sneakyThrowStatement.sourceStart = pS;
- sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = pE;
- Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, pS, pE);
- // Positions for in-method generated nodes are special
- rethrowStatement.sourceStart = method.bodyEnd;
- rethrowStatement.sourceEnd = method.bodyEnd;
-
+ // This is the magic fix for rendering issues
+ // In org.eclipse.jdt.core.dom.ASTConverter#convert(org.eclipse.jdt.internal.compiler.ast.MessageSend)
+ // a new SimpleName is created and the setSourceRange should receive -1, 0. That's why we provide -2L :-)
+ sneakyThrowStatement.nameSourcePosition = -2L;
+
+ sneakyThrowStatement.sourceStart = methodEnd;
+ sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = methodEnd;
+
+ Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, methodEnd, methodEnd);
Eclipse.setGeneratedBy(rethrowStatement, source);
+
Block block = new Block(0);
- block.sourceStart = pS;
- block.sourceEnd = pE;
+ block.sourceStart = methodEnd;
+ block.sourceEnd = methodEnd;
Eclipse.setGeneratedBy(block, source);
block.statements = new Statement[] { rethrowStatement };
tryStatement.catchBlocks = new Block[] { block };
// Positions for in-method generated nodes are special
-// tryStatement.sourceStart = method.bodyStart;
-// tryStatement.sourceEnd = method.bodyEnd;
- tryStatement.sourceStart = pS;
- tryStatement.sourceEnd = pE;
-
+ tryStatement.sourceStart = method.bodyStart;
+ tryStatement.sourceEnd = method.bodyEnd;
+
return tryStatement;
}
}