Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 0ba6ea5fd -> 1c5d81180


GROOVY-8892: Trait instance init blocks are called only when there are fields 
defined in same Trait


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 1c5d81180d8943c44d6c9259f6c8db14429112cf
Parents: 0ba6ea5
Author: Paul King <pa...@asert.com.au>
Authored: Mon Nov 19 13:44:16 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Mon Nov 19 13:47:54 2018 +1000

----------------------------------------------------------------------
 .../groovy/transform/trait/TraitComposer.java   | 12 +++++-----
 .../traitx/TraitASTTransformationTest.groovy    | 24 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1c5d8118/src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java 
b/src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java
index 70eaba9..5b80061 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/TraitComposer.java
@@ -320,13 +320,13 @@ public abstract class TraitComposer {
                     cNode.addMethod(impl);
                 }
             }
-            cNode.addObjectInitializerStatements(new ExpressionStatement(
-                    new MethodCallExpression(
-                            new ClassExpression(helperClassNode),
-                            Traits.INIT_METHOD,
-                            new ArgumentListExpression(new 
VariableExpression("this")))
-            ));
         }
+        cNode.addObjectInitializerStatements(new ExpressionStatement(
+                new MethodCallExpression(
+                        new ClassExpression(helperClassNode),
+                        Traits.INIT_METHOD,
+                        new ArgumentListExpression(new 
VariableExpression("this")))
+        ));
     }
 
     private static void createForwarderMethod(

http://git-wip-us.apache.org/repos/asf/groovy/blob/1c5d8118/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
----------------------------------------------------------------------
diff --git 
a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
 
b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index b20c4c6..27ecd8d 100644
--- 
a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ 
b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -2644,4 +2644,28 @@ assert c.b() == 2
             assert MyClass.full == 'FOOBAR'
         '''
     }
+
+    //GROOVY-8892
+    void testTraitWithStaticInitBlockWithAndWithoutProps() {
+        assertScript '''
+            class Counter {
+                static int count = 0
+            }
+            trait TraitNoProps {
+                {
+                    Counter.count += 1
+                }
+            }
+            trait TraitWithProp {
+                Integer instanceCounter //immutable, non-shareable
+                {
+                    Counter.count += 10
+                    instanceCounter = 1
+                }
+            }
+            class ClassWithTraits implements TraitNoProps, TraitWithProp { }
+            assert new ClassWithTraits().instanceCounter == 1
+            assert Counter.count == 11
+        '''
+    }
 }

Reply via email to