aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/eclipse/handlers/HandleCleanup.java
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-07-01 15:58:24 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-07-01 15:58:24 +0200
commit34780e3ab5c549dd83085fe0893e015d3ef4d211 (patch)
treea65018fff52d22f225ef8175c07adda87d22048d /src/lombok/eclipse/handlers/HandleCleanup.java
parentdb4819183f698acea6221656098e6e42a6e9e6fd (diff)
downloadlombok-34780e3ab5c549dd83085fe0893e015d3ef4d211.tar.gz
lombok-34780e3ab5c549dd83085fe0893e015d3ef4d211.tar.bz2
lombok-34780e3ab5c549dd83085fe0893e015d3ef4d211.zip
Added a bonus feature to @Cleanup: Assigning a @Cleanup marked variable to anything else generates a warning.
Diffstat (limited to 'src/lombok/eclipse/handlers/HandleCleanup.java')
-rw-r--r--src/lombok/eclipse/handlers/HandleCleanup.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lombok/eclipse/handlers/HandleCleanup.java b/src/lombok/eclipse/handlers/HandleCleanup.java
index a28c6c6a..fa97e76d 100644
--- a/src/lombok/eclipse/handlers/HandleCleanup.java
+++ b/src/lombok/eclipse/handlers/HandleCleanup.java
@@ -1,5 +1,7 @@
package lombok.eclipse.handlers;
+import java.util.Arrays;
+
import lombok.Cleanup;
import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
@@ -9,8 +11,10 @@ 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.Assignment;
import org.eclipse.jdt.internal.compiler.ast.Block;
import org.eclipse.jdt.internal.compiler.ast.CaseStatement;
+import org.eclipse.jdt.internal.compiler.ast.CastExpression;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
@@ -97,6 +101,8 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> {
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.
+ doAssignmentCheck(annotationNode, tryBlock, decl.name);
+
TryStatement tryStatement = new TryStatement();
tryStatement.tryBlock = new Block(0);
tryStatement.tryBlock.statements = tryBlock;
@@ -126,4 +132,24 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> {
return true;
}
+
+ private void doAssignmentCheck(Node node, Statement[] tryBlock, char[] varName) {
+ for ( Statement statement : tryBlock ) doAssignmentCheck0(node, statement, varName);
+ }
+
+ private void doAssignmentCheck0(Node node, Statement statement, char[] varName) {
+ if ( statement instanceof Assignment )
+ doAssignmentCheck0(node, ((Assignment)statement).expression, varName);
+ else if ( statement instanceof LocalDeclaration )
+ doAssignmentCheck0(node, ((LocalDeclaration)statement).initialization, varName);
+ else if ( statement instanceof CastExpression )
+ doAssignmentCheck0(node, ((CastExpression)statement).expression, varName);
+ else if ( statement instanceof SingleNameReference ) {
+ if ( Arrays.equals(((SingleNameReference)statement).token, varName) ) {
+ Node problemNode = node.getNodeFor(statement);
+ if ( problemNode != null ) problemNode.addWarning(
+ "You're assigning a guarded variable to something else. This is a bad idea.");
+ }
+ }
+ }
}