diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-03-18 19:30:02 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-03-18 19:30:02 +0100 |
commit | 04e9397971d3689e0710df026f673f8b0a6cb685 (patch) | |
tree | 3ca47a3bc926a5171f5e939743f6b2d3daea63e1 | |
parent | 38b4128f899e81d8a5d19dca8b6cd1357ca3bd1a (diff) | |
download | lombok-04e9397971d3689e0710df026f673f8b0a6cb685.tar.gz lombok-04e9397971d3689e0710df026f673f8b0a6cb685.tar.bz2 lombok-04e9397971d3689e0710df026f673f8b0a6cb685.zip |
Rewritten @Cleanup's new null analysis prevention to not use
Lombok.preventNullAnalysis but go with Collections.singletonList(expr).get(0)
instead; while this does create a pointless object, it doesn't cause a clash
when eclipse has lombok 0.10 installed but the project uses 0.9, which doesn't
have preventNullAnalysis. Eventually, once 0.9 is long forgotten, this can be reverted.
6 files changed, 46 insertions, 21 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleCleanup.java b/src/core/lombok/eclipse/handlers/HandleCleanup.java index 964653bc..9a63ce47 100644 --- a/src/core/lombok/eclipse/handlers/HandleCleanup.java +++ b/src/core/lombok/eclipse/handlers/HandleCleanup.java @@ -42,6 +42,7 @@ import org.eclipse.jdt.internal.compiler.ast.CastExpression; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.IfStatement; +import org.eclipse.jdt.internal.compiler.ast.IntLiteral; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -174,16 +175,7 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { NullLiteral nullLiteral = new NullLiteral(pS, pE); Eclipse.setGeneratedBy(nullLiteral, ast); - MessageSend preventNullAnalysis = new MessageSend(); - Eclipse.setGeneratedBy(preventNullAnalysis, ast); - - preventNullAnalysis.receiver = createNameReference("lombok.Lombok", ast); - preventNullAnalysis.selector = "preventNullAnalysis".toCharArray(); - - preventNullAnalysis.arguments = new Expression[] { varName }; - preventNullAnalysis.nameSourcePosition = p; - preventNullAnalysis.sourceStart = pS; - preventNullAnalysis.sourceEnd = preventNullAnalysis.statementEnd = pE; + MessageSend preventNullAnalysis = preventNullAnalysis(ast, varName); EqualExpression equalExpression = new EqualExpression(preventNullAnalysis, nullLiteral, OperatorIds.NOT_EQUAL); equalExpression.sourceStart = pS; equalExpression.sourceEnd = pE; @@ -195,8 +187,6 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { Eclipse.setGeneratedBy(closeBlock, ast); IfStatement ifStatement = new IfStatement(equalExpression, closeBlock, 0, 0); Eclipse.setGeneratedBy(ifStatement, ast); - - finallyBlock[0] = ifStatement; tryStatement.finallyBlock = new Block(0); @@ -219,6 +209,35 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { return true; } + private MessageSend preventNullAnalysis(Annotation ast, Expression expr) { + MessageSend singletonList = new MessageSend(); + Eclipse.setGeneratedBy(singletonList, ast); + + int pS = ast.sourceStart, pE = ast.sourceEnd; + long p = (long)pS << 32 | pE; + + singletonList.receiver = createNameReference("java.util.Collections", ast); + singletonList.selector = "singletonList".toCharArray(); + + singletonList.arguments = new Expression[] { expr }; + singletonList.nameSourcePosition = p; + singletonList.sourceStart = pS; + singletonList.sourceEnd = singletonList.statementEnd = pE; + + MessageSend preventNullAnalysis = new MessageSend(); + Eclipse.setGeneratedBy(preventNullAnalysis, ast); + + preventNullAnalysis.receiver = singletonList; + preventNullAnalysis.selector = "get".toCharArray(); + + preventNullAnalysis.arguments = new Expression[] { new IntLiteral(new char[] { '0' }, pS, pE) }; + preventNullAnalysis.nameSourcePosition = p; + preventNullAnalysis.sourceStart = pS; + preventNullAnalysis.sourceEnd = singletonList.statementEnd = pE; + + return preventNullAnalysis; + } + private void doAssignmentCheck(EclipseNode node, Statement[] tryBlock, char[] varName) { for (Statement statement : tryBlock) doAssignmentCheck0(node, statement, varName); } diff --git a/src/core/lombok/javac/handlers/HandleCleanup.java b/src/core/lombok/javac/handlers/HandleCleanup.java index 9e2fddf6..b681ab28 100644 --- a/src/core/lombok/javac/handlers/HandleCleanup.java +++ b/src/core/lombok/javac/handlers/HandleCleanup.java @@ -117,7 +117,7 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec( maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil()))); - JCMethodInvocation preventNullAnalysis = maker.Apply(List.<JCExpression>nil(), JavacHandlerUtil.chainDotsString(maker, annotationNode, "lombok.Lombok.preventNullAnalysis"), List.<JCExpression>of(maker.Ident(decl.name))); + JCMethodInvocation preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name)); JCBinary isNull = maker.Binary(Javac.getCTCint(JCTree.class, "NE"), preventNullAnalysis, maker.Literal(Javac.getCTCint(TypeTags.class, "BOT"), null)); JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null); @@ -139,6 +139,12 @@ public class HandleCleanup implements JavacAnnotationHandler<Cleanup> { return true; } + private JCMethodInvocation preventNullAnalysis(TreeMaker maker, JavacNode node, JCExpression expression) { + JCMethodInvocation singletonList = maker.Apply(List.<JCExpression>nil(), JavacHandlerUtil.chainDotsString(maker, node, "java.util.Collections.singletonList"), List.of(expression)); + JCMethodInvocation cleanedExpr = maker.Apply(List.<JCExpression>nil(), maker.Select(singletonList, node.toName("get")) , List.<JCExpression>of(maker.Literal(TypeTags.INT, 0))); + return cleanedExpr; + } + private void doAssignmentCheck(JavacNode node, List<JCStatement> statements, Name name) { for (JCStatement statement : statements) doAssignmentCheck0(node, statement, name); } diff --git a/test/transform/resource/after-delombok/CleanupName.java b/test/transform/resource/after-delombok/CleanupName.java index 37a8d117..a4ab8267 100644 --- a/test/transform/resource/after-delombok/CleanupName.java +++ b/test/transform/resource/after-delombok/CleanupName.java @@ -4,7 +4,7 @@ class CleanupName { try { System.out.println(o); } finally { - if (lombok.Lombok.preventNullAnalysis(o) != null) { + if (java.util.Collections.singletonList(o).get(0) != null) { o.toString(); } } @@ -14,7 +14,7 @@ class CleanupName { try { System.out.println(o); } finally { - if (lombok.Lombok.preventNullAnalysis(o) != null) { + if (java.util.Collections.singletonList(o).get(0) != null) { o.toString(); } } diff --git a/test/transform/resource/after-delombok/CleanupPlain.java b/test/transform/resource/after-delombok/CleanupPlain.java index 67c82f60..f91f35ee 100644 --- a/test/transform/resource/after-delombok/CleanupPlain.java +++ b/test/transform/resource/after-delombok/CleanupPlain.java @@ -9,12 +9,12 @@ class CleanupPlain { out.flush(); } } finally { - if (lombok.Lombok.preventNullAnalysis(out) != null) { + if (java.util.Collections.singletonList(out).get(0) != null) { out.close(); } } } finally { - if (lombok.Lombok.preventNullAnalysis(in) != null) { + if (java.util.Collections.singletonList(in).get(0) != null) { in.close(); } } diff --git a/test/transform/resource/after-ecj/CleanupName.java b/test/transform/resource/after-ecj/CleanupName.java index 8948e91d..f3f725f2 100644 --- a/test/transform/resource/after-ecj/CleanupName.java +++ b/test/transform/resource/after-ecj/CleanupName.java @@ -10,7 +10,7 @@ class CleanupName { } finally { - if ((lombok.Lombok.preventNullAnalysis(o) != null)) + if ((java.util.Collections.singletonList(o).get(0) != null)) { o.toString(); } @@ -24,7 +24,7 @@ class CleanupName { } finally { - if ((lombok.Lombok.preventNullAnalysis(o) != null)) + if ((java.util.Collections.singletonList(o).get(0) != null)) { o.toString(); } diff --git a/test/transform/resource/after-ecj/CleanupPlain.java b/test/transform/resource/after-ecj/CleanupPlain.java index 6eaa4377..a9b9eceb 100644 --- a/test/transform/resource/after-ecj/CleanupPlain.java +++ b/test/transform/resource/after-ecj/CleanupPlain.java @@ -18,7 +18,7 @@ class CleanupPlain { } finally { - if ((lombok.Lombok.preventNullAnalysis(out) != null)) + if ((java.util.Collections.singletonList(out).get(0) != null)) { out.close(); } @@ -26,7 +26,7 @@ class CleanupPlain { } finally { - if ((lombok.Lombok.preventNullAnalysis(in) != null)) + if ((java.util.Collections.singletonList(in).get(0) != null)) { in.close(); } |