aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/eclipse/handlers
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-06-30 18:22:38 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-06-30 18:22:38 +0200
commit37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7 (patch)
treede6f4a782753fe81c8809771fdae638f2bd7d8e0 /src/lombok/eclipse/handlers
parente0b623a3b85c89028bfc8c52578232b1c539723c (diff)
downloadlombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.tar.gz
lombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.tar.bz2
lombok-37006cb2954b6fcb23d2dcfaedbc3abac3fbb5d7.zip
After a great many iterations, Cleanup has now been reduced in functionality (exceptions from the cleanup call WILL mask exceptions from the body - this isn't intended, but it's just not possible to fix this without java 7 features or requiring a rewrite of the class file data.
Tried tactics, and why they won't work: - Replace every 'return', 'break', and 'continue' statement (for the latter 2, only if they break/continue out of the try block) with a block that first sets a uniquely named flag before doing the operation. Then, check that flag in the finally block to see if the cleanup call should be guarded by a try/catchThrowable. This doesn't work, because its not possible to instrument the 4th way out of a try block without throwing an exception: Just letting it run its course. Tossing a "#flag = true;" at the end may cause a compile time error if the code is not reachable, but figuring that out requires resolution and quite a bit of analysis. - Put catch blocks in for all relevant exceptions (RuntimeException, Error, all exceptions declared as thrown by the method, and all types of exceptions of the catch blocks of encapsulating try blocks. This doesn't work, partly because it'll fail for sneakily thrown exceptions, but mostly because you can't just catch an exception listed in the 'throws' clause of the method body; catching an exception that no statement in the try block can throw is a compile time error, but it is perfectly allright to declare these as 'thrown'. - Put in a blanket catch Throwable to set the flag. Two problems with this: First, sneaky throw can't be done. Thread.stop invokes a security manager and triggers a warning, Calling a sneakyThrow method creates a runtime dependency on lombok, constructing a sneakyThrow in-class creates visible methods or at least visible class files, and creating a new class via Class.loadClass would be very slow without caching - which gets you the same issues. Secondly, this would mean that any statements in the try body that throw an exception aren't flagged to the user as needing to be handled. The Cleanup annotation now also calls the cleanup method for you, and will call it at the END of the current scope. The following plans have been tried and abandoned: - Cleanup right after the final mention. This doesn't work, because the final mention may not be the final use-place. Example: @Cleanup InputStream in = new FileInputStream(someFile); InputStreamReader reader = new InputStreamReader(in); reader.read(); //oops - in is already closed by now. - Require an explicit var.cleanup() call and consider that the cue to close off the try block. This doesn't work either, because now variables set in between the @Cleanup declaration and the var.cleanup() call become invisible to following statements. Example: @Cleanup InputStream in = new FileInputStream(someFile); int i = in.read(); in.close(); System.out.println(i); //fail - i is inside the generated try block but this isn't, so 'i' is not visible from here. By running to the end of visible scope, all these problems are avoided. This does remove the flexibility of declaring where you want a close call to be executed, but there are two mitigating factors available: 1) Create an explicit scope block. You can just stick { code } in any place where you can legally write a statement, in java. This is relatively unknown, so I expect most users will go for: 2) Just call close explicitly. I've yet to see a cleanup method which isn't idempotent anyway (calling it multiple times is no different than calling it once). During the course of investigating these options, the AST code has been extended to support live replacement of any child node, including updating the actual underlying system AST as well as our own. Unfortunately, this code has NOT been tested. It was rather a lot of work so I'm leaving it in, and at least for eclipse it even seemed to work.
Diffstat (limited to 'src/lombok/eclipse/handlers')
-rw-r--r--src/lombok/eclipse/handlers/HandleCleanup.java133
1 files changed, 27 insertions, 106 deletions
diff --git a/src/lombok/eclipse/handlers/HandleCleanup.java b/src/lombok/eclipse/handlers/HandleCleanup.java
index f381f25e..867bd0e5 100644
--- a/src/lombok/eclipse/handlers/HandleCleanup.java
+++ b/src/lombok/eclipse/handlers/HandleCleanup.java
@@ -1,9 +1,5 @@
package lombok.eclipse.handlers;
-import static lombok.eclipse.Eclipse.copyType;
-
-import java.util.Arrays;
-
import lombok.Cleanup;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
@@ -13,24 +9,14 @@ import lombok.eclipse.EclipseAST.Node;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
-import org.eclipse.jdt.internal.compiler.ast.Argument;
-import org.eclipse.jdt.internal.compiler.ast.Assignment;
import org.eclipse.jdt.internal.compiler.ast.Block;
import org.eclipse.jdt.internal.compiler.ast.CaseStatement;
-import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
-import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
-import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.SwitchStatement;
-import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
-import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
-import org.eclipse.jdt.internal.compiler.ast.TypeReference;
-import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
-import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.mangosdk.spi.ProviderFor;
@ProviderFor(EclipseAnnotationHandler.class)
@@ -69,7 +55,7 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> {
}
if ( statements == null ) {
- annotationNode.addError("Parent block does not contain any statements. This is a lombok bug.");
+ annotationNode.addError("LOMBOK BUG: Parent block does not contain any statements.");
return true;
}
@@ -79,119 +65,53 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> {
}
if ( start == statements.length ) {
- annotationNode.addError("Can't find this local variable declaration inside its parent. This is a lombok bug.");
+ annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
return true;
}
start++;
- int end = start + 1;
- for ( ; end < statements.length ; end++ ) {
- if ( isSwitch && statements[end] instanceof CaseStatement ) {
- annotationNode.addError("The cleanup method must be called before the next case/default statement.");
- return true;
- }
- if ( statements[end] instanceof MessageSend ) {
- MessageSend ms = (MessageSend)statements[end];
- //The method name is the same as the 'cleanupName = ' field of the @Cleanup annotation...
- if ( ms.selector == null || !cleanupName.equals(new String(ms.selector)) ) continue;
- //The call is of the form 'foo.cleanup(anything)', where foo is a simple reference and not an expression...
- if ( !(ms.receiver instanceof SingleNameReference) ) continue;
- //And the reference is the same name as the local variable annotated with @Cleanup...
- if ( !Arrays.equals(((SingleNameReference)ms.receiver).token, decl.name) ) continue;
- //Then we found it!
- if ( ms.arguments != null && ms.arguments.length > 0 ) {
- //As we'll be moving the close() call around, any references to local vars may not be valid in the new scope.
- //Technically we could throw scope markers around the whole shebang and split local var declarations into a separate
- //declaration (in the newly created top scope) and an initialization, but, then there's 'final' and 'definite assignment'
- //rules to worry about. So, let's make this easy on ourselves and allow no arguments, for now.
- annotationNode.addError("The cleanup method cannot have any arguments.");
- return true;
+ int end;
+ if ( isSwitch ) {
+ end = start + 1;
+ for ( ; end < statements.length ; end++ ) {
+ if ( statements[end] instanceof CaseStatement ) {
+ break;
}
- break;
}
- }
+ } else end = statements.length;
- if ( end == statements.length ) {
- annotationNode.addError("You need to include a " + new String(decl.name) + "." + cleanupName + "() call at the same scope level.");
- return true;
- }
-
- //At this point, at start-1, there's the local declaration, and at end, there's the close call.
- //Thus, we need to move [start, end) into a try block, and move the close call to its own scope.
+ //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.
+ // hence:
+ // [start, end) = statements for the try block.
Statement[] tryBlock = new Statement[end - start];
System.arraycopy(statements, start, tryBlock, 0, end-start);
- //Remove the stuff we just dumped into the tryBlock, AND the close() call, and then leave room for the try node and the unique name.
- Statement[] newStatements = new Statement[statements.length - (end-start) +1];
- System.arraycopy(statements, 0, newStatements, 0, start);
- System.arraycopy(statements, end+1, newStatements, start+2, statements.length - end -1);
+ //Remove the stuff we just dumped into the tryBlock, and then leave room for the try node.
+ int newStatementsLength = statements.length - (end-start); //Remove room for every statement moved into try block...
+ newStatementsLength += 1; //But add room for the TryStatement node itself.
+ Statement[] newStatements = new Statement[newStatementsLength];
+ System.arraycopy(statements, 0, newStatements, 0, start); //copy all statements before the try block verbatim.
+ System.arraycopy(statements, end, newStatements, start+1, statements.length - end); //For switch statements.
+
TryStatement tryStatement = new TryStatement();
- newStatements[start+1] = tryStatement;
- LocalDeclaration tempVar = new LocalDeclaration(("$lombok$cleanup$" + new String(decl.name)).toCharArray(), 0, 0);
- tempVar.type = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
- tempVar.initialization = new FalseLiteral(0, 0);
- newStatements[start] = tempVar;
tryStatement.tryBlock = new Block(0);
tryStatement.tryBlock.statements = tryBlock;
+ newStatements[start] = tryStatement;
- char[] exName = ("$lombok$cleanup$ex$" + new String(decl.name)).toCharArray();
Statement[] finallyBlock = new Statement[1];
- TryStatement safeClose = new TryStatement();
- safeClose.tryBlock = new Block(0);
- safeClose.tryBlock.statements = new Statement[1];
- MessageSend newCloseCall = new MessageSend();
- newCloseCall.receiver = new SingleNameReference(decl.name, 0);
- newCloseCall.selector = cleanupName.toCharArray();
- safeClose.tryBlock.statements[0] = newCloseCall;
- safeClose.catchArguments = new Argument[1];
- safeClose.catchArguments[0] = new Argument(exName, 0,
- new QualifiedTypeReference(TypeConstants.JAVA_LANG_THROWABLE, new long[] { 0, 0, 0}), 0);
- safeClose.catchBlocks = new Block[1];
- safeClose.catchBlocks[0] = new Block(0);
- safeClose.catchBlocks[0].sourceEnd = safeClose.catchBlocks[0].sourceStart = -2;
MessageSend unsafeClose = new MessageSend();
unsafeClose.receiver = new SingleNameReference(decl.name, 0);
unsafeClose.selector = cleanupName.toCharArray();
- finallyBlock[0] = new IfStatement(new SingleNameReference(tempVar.name, 0), safeClose, unsafeClose, 0, 0);
+ finallyBlock[0] = unsafeClose;
tryStatement.finallyBlock = new Block(0);
tryStatement.finallyBlock.statements = finallyBlock;
- Node containingMethodNode = annotationNode;
- TypeReference[] thrownExceptions = null;
- findThrownExceptions:
- while ( containingMethodNode != null ) {
- switch ( containingMethodNode.getKind() ) {
- case INITIALIZER:
- break findThrownExceptions;
- case METHOD:
- thrownExceptions = ((AbstractMethodDeclaration)containingMethodNode.get()).thrownExceptions;
- break findThrownExceptions;
- default:
- containingMethodNode = containingMethodNode.up();
- }
- }
-
- if ( thrownExceptions == null ) thrownExceptions = new TypeReference[0];
- tryStatement.catchArguments = new Argument[thrownExceptions.length + 2];
- tryStatement.catchBlocks = new Block[thrownExceptions.length + 2];
- int idx = 0;
- tryStatement.catchArguments[idx++] = new Argument(exName, 0,
- new QualifiedTypeReference(TypeConstants.JAVA_LANG_RUNTIMEEXCEPTION, new long[] { 0, 0, 0 }), 0);
- tryStatement.catchArguments[idx++] = new Argument(exName, 0,
- new QualifiedTypeReference(TypeConstants.JAVA_LANG_ERROR, new long[] { 0, 0, 0 }), 0);
- for ( ; idx < tryStatement.catchArguments.length ; idx++ ) {
- tryStatement.catchArguments[idx] = new Argument(exName, 0, copyType(thrownExceptions[idx-2]), 0);
- }
-
- for ( idx = 0 ; idx < tryStatement.catchBlocks.length ; idx++ ) {
- Block b = new Block(0);
- tryStatement.catchBlocks[idx] = b;
- b.statements = new Statement[2];
- b.statements[0] = new Assignment(new SingleNameReference(tempVar.name, 0), new TrueLiteral(0, 0), 0);
- b.statements[1] = new ThrowStatement(new SingleNameReference(exName, 0), 0, 0);
- b.sourceEnd = b.sourceStart = -2;
- }
+ tryStatement.catchArguments = null;
+ tryStatement.catchBlocks = null;
if ( blockNode instanceof AbstractMethodDeclaration ) {
((AbstractMethodDeclaration)blockNode).statements = newStatements;
@@ -203,6 +123,7 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> {
ancestor.rebuild();
+
return true;
}
}