Make extract method work with @Cleanup and @Synchronized

This commit is contained in:
Roel Spilker committed 2011-08-12 01:15:34 +02:00
1 parent c996e8749d
commit 55cb9f8dac
3 files changed
+47 -3

No files matched your search

@@ -127,7 +127,7 @@ public class HandleCleanup extends EclipseAnnotationHandler<Cleanup> {
//At this point:
// start-1 = Local Declaration marked with @Cleanup
// start = first instruction that needs to be wrapped into a try block
// end = last intruction of the scope -OR- last instruction before the next case label in switch statements.
// end = last instruction of the scope -OR- last instruction before the next case label in switch statements.
// hence:
// [start, end) = statements for the try block.
@@ -146,6 +146,20 @@ public class HandleCleanup extends EclipseAnnotationHandler<Cleanup> {
Eclipse.setGeneratedBy(tryStatement, ast);
tryStatement.tryBlock = new Block(0);
tryStatement.tryBlock.statements = tryBlock;
Eclipse.setGeneratedBy(tryStatement.tryBlock, ast);
// Positions for in-method generated nodes are special
int ss = decl.declarationSourceEnd + 1;
int se = ss;
if (tryBlock.length > 0) {
se = tryBlock[tryBlock.length - 1].sourceEnd + 1; //+1 for the closing semicolon. Yes, there could be spaces. Bummer.
tryStatement.sourceStart = ss;
tryStatement.sourceEnd = se;
tryStatement.tryBlock.sourceStart = ss;
tryStatement.tryBlock.sourceEnd = se;
}
newStatements[start] = tryStatement;
Statement[] finallyBlock = new Statement[1];
@@ -190,6 +204,12 @@ public class HandleCleanup extends EclipseAnnotationHandler<Cleanup> {
finallyBlock[0] = ifStatement;
tryStatement.finallyBlock = new Block(0);
// Positions for in-method generated nodes are special
if (!isSwitch) {
tryStatement.finallyBlock.sourceStart = blockNode.sourceEnd;
tryStatement.finallyBlock.sourceEnd = blockNode.sourceEnd;
}
Eclipse.setGeneratedBy(tryStatement.finallyBlock, ast);
tryStatement.finallyBlock.statements = finallyBlock;
@@ -155,21 +155,25 @@ public class HandleSneakyThrows extends EclipseAnnotationHandler<SneakyThrows> {
Statement[] contents = method.statements;
for (DeclaredException exception : exceptions) {
contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node) };
contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node, method) };
}
method.statements = contents;
annotation.up().rebuild();
}
private Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source) {
private Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source, AbstractMethodDeclaration method) {
long p = exception.getPos();
int pS = (int)(p >> 32), pE = (int)p;
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;
Eclipse.setGeneratedBy(tryStatement.tryBlock, source);
tryStatement.tryBlock.statements = contents;
TypeReference typeReference;
@@ -212,13 +216,23 @@ public class HandleSneakyThrows extends EclipseAnnotationHandler<SneakyThrows> {
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;
Eclipse.setGeneratedBy(rethrowStatement, source);
Block block = new Block(0);
block.sourceStart = pS;
block.sourceEnd = pE;
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;
return tryStatement;
@@ -129,6 +129,11 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> {
Block block = new Block(0);
Eclipse.setGeneratedBy(block, source);
block.statements = method.statements;
// Positions for in-method generated nodes are special
block.sourceEnd = method.bodyEnd;
block.sourceStart = method.bodyStart;
Expression lockVariable;
if (method.isStatic()) lockVariable = new QualifiedNameReference(new char[][] {
methodNode.up().getName().toCharArray(), lockName }, new long[] { pos, pos }, p1, p2);
@@ -143,6 +148,11 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> {
method.statements = new Statement[] {
new SynchronizedStatement(lockVariable, block, 0, 0)
};
// Positions for in-method generated nodes are special
method.statements[0].sourceEnd = method.bodyEnd;
method.statements[0].sourceStart = method.bodyStart;
Eclipse.setGeneratedBy(method.statements[0], source);
methodNode.rebuild();