mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 06:17:44 +00:00
Added a bonus feature to @Cleanup: Assigning a @Cleanup marked variable to anything else generates a warning.
This commit is contained in:
1 parent
db4819183f
commit
34780e3ab5
2 files changed
+52
No files matched your search
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,15 +11,20 @@ import org.mangosdk.spi.ProviderFor;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.TreeMaker;
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
import com.sun.tools.javac.tree.JCTree.JCAssign;
|
||||
import com.sun.tools.javac.tree.JCTree.JCBlock;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCase;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCatch;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
|
||||
import com.sun.tools.javac.tree.JCTree.JCIdent;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
|
||||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
|
||||
@@ -67,6 +72,8 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
|
||||
return true;
|
||||
}
|
||||
|
||||
doAssignmentCheck(annotationNode, tryBlock, decl.name);
|
||||
|
||||
TreeMaker maker = annotationNode.getTreeMaker();
|
||||
JCFieldAccess cleanupCall = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
|
||||
List<JCStatement> finalizerBlock = List.<JCStatement>of(maker.Exec(
|
||||
@@ -87,4 +94,23 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void doAssignmentCheck(Node node, List<JCStatement> statements, Name name) {
|
||||
for ( JCStatement statement : statements ) doAssignmentCheck0(node, statement, name);
|
||||
}
|
||||
|
||||
private void doAssignmentCheck0(Node node, JCTree statement, Name name) {
|
||||
if ( statement instanceof JCAssign ) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name);
|
||||
if ( statement instanceof JCExpressionStatement ) doAssignmentCheck0(node,
|
||||
((JCExpressionStatement)statement).expr, name);
|
||||
if ( statement instanceof JCVariableDecl ) doAssignmentCheck0(node, ((JCVariableDecl)statement).init, name);
|
||||
if ( statement instanceof JCTypeCast ) doAssignmentCheck0(node, ((JCTypeCast)statement).expr, name);
|
||||
if ( statement instanceof JCIdent ) {
|
||||
if ( ((JCIdent)statement).name.contentEquals(name) ) {
|
||||
Node problemNode = node.getNodeFor(statement);
|
||||
if ( problemNode != null ) problemNode.addWarning(
|
||||
"You're assigning a guarded variable to something else. This is a bad idea.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user