GROOVY-8245: @Newify(auto=false) not transforming declarations (closes #571)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/891c35f2
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/891c35f2
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/891c35f2

Branch: refs/heads/master
Commit: 891c35f28b0419e4080f222d1d8722351817b7f7
Parents: 51f9294
Author: John Wagenleitner <jwagenleit...@apache.org>
Authored: Sat Aug 5 11:15:04 2017 -0700
Committer: John Wagenleitner <jwagenleit...@apache.org>
Committed: Sat Aug 5 14:16:20 2017 -0700

----------------------------------------------------------------------
 .../transform/NewifyASTTransformation.java      | 10 ++++++++-
 .../groovy/transform/NewifyTransformTest.groovy | 22 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/891c35f2/src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java
----------------------------------------------------------------------
diff --git 
a/src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java 
b/src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java
index 55ae7de..8c31449 100644
--- a/src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/NewifyASTTransformation.java
@@ -178,7 +178,7 @@ public class NewifyASTTransformation extends 
ClassCodeExpressionTransformer impl
             }
         } else if (expr instanceof DeclarationExpression) {
             DeclarationExpression de = (DeclarationExpression) expr;
-            if (de == candidate || auto) {
+            if (shouldTransform(de)) {
                 candidate = null;
                 Expression left = de.getLeftExpression();
                 Expression right = transform(de.getRightExpression());
@@ -191,6 +191,14 @@ public class NewifyASTTransformation extends 
ClassCodeExpressionTransformer impl
         return expr.transformExpression(this);
     }
 
+    private boolean shouldTransform(DeclarationExpression exp) {
+        return exp == candidate || auto || hasClassesToNewify();
+    }
+
+    private boolean hasClassesToNewify() {
+        return classesToNewify != null && 
!classesToNewify.getExpressions().isEmpty();
+    }
+
     private void newifyClass(ClassNode cNode, boolean autoFlag, ListExpression 
list) {
         String cName = cNode.getName();
         if (cNode.isInterface()) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/891c35f2/src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy 
b/src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy
index e3e9890..d585c8a 100644
--- a/src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/NewifyTransformTest.groovy
@@ -189,4 +189,26 @@ class NewifyTransformTest extends GroovyShellTestCase {
             assert !new Z().foo()
         '''
     }
+
+    // GROOVY-8245
+    void testDeclarationWhenAutoIsFalse() {
+        assertScript '''
+            class Foo {
+                static int answer = 7
+                Foo() {
+                    answer = 42
+                }
+            }
+            @Newify(auto=false, value=Foo)
+            class Bar {
+                static {
+                    Foo foo = Foo()
+                }
+                static void method() {}
+            }
+            assert Foo.answer == 7
+            Bar.method()
+            assert Foo.answer == 42
+        '''
+    }
 }
\ No newline at end of file

Reply via email to