groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-7872: Nested calls between @Lazy static properties throws MissingPropertyException (closes #354)
Date Mon, 18 Jul 2016 00:25:15 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 8127d47a8 -> ff44e38de


GROOVY-7872: Nested calls between @Lazy static properties throws MissingPropertyException
(closes #354)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: ff44e38de9aefdfcbb13c7726ac98409f6176507
Parents: 8127d47
Author: Endika GutiƩrrez <endika.gutierrez@ticketbis.com>
Authored: Thu Jun 23 09:22:47 2016 +0200
Committer: paulk <paulk@asert.com.au>
Committed: Mon Jul 18 10:24:49 2016 +1000

----------------------------------------------------------------------
 .../groovy/transform/LazyASTTransformation.java | 12 ++++++++-
 .../groovy/transform/LazyTransformTest.groovy   | 27 +++++++++++++++++---
 2 files changed, 35 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ff44e38d/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java b/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java
index 5e25a3c..3aa6a9e 100644
--- a/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/LazyASTTransformation.java
@@ -116,7 +116,17 @@ public class LazyASTTransformation extends AbstractASTTransformation
{
         final String fullName = declaringClass.getName() + "$" + fieldType.getNameWithoutPackage()
+ "Holder_" + fieldNode.getName().substring(1);
         final InnerClassNode holderClass = new InnerClassNode(declaringClass, fullName, visibility,
ClassHelper.OBJECT_TYPE);
         final String innerFieldName = "INSTANCE";
-        holderClass.addField(innerFieldName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType,
initExpr);
+
+        // we have two options:
+        // (1) embed initExpr within holder class but redirect field access/method calls
to declaring class members
+        // (2) keep initExpr within a declaring class method that is only called by the holder
class
+        // currently we have gone with (2) for simplicity with only a slight memory footprint
increase in the declaring class
+        final String initializeMethodName = (fullName + "_initExpr").replace('.', '_');
+        declaringClass.addMethod(initializeMethodName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL,
fieldType,
+                Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(initExpr));
+        holderClass.addField(innerFieldName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType,
+                callX(declaringClass, initializeMethodName));
+
         final Expression innerField = propX(classX(holderClass), innerFieldName);
         declaringClass.getModule().addClass(holderClass);
         body.addStatement(returnS(innerField));

http://git-wip-us.apache.org/repos/asf/groovy/blob/ff44e38d/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy b/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy
index 7ef7c1d..fe729b7 100644
--- a/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/LazyTransformTest.groovy
@@ -22,8 +22,7 @@ import java.lang.ref.SoftReference
 import java.lang.reflect.Modifier
 
 /**
- * @author Alex Tkachman
- * @author Paul King
+ * Tests for the {@code @Lazy} transform.
  */
 class LazyTransformTest extends GroovyShellTestCase {
 
@@ -167,4 +166,26 @@ class LazyTransformTest extends GroovyShellTestCase {
         assertTrue res.@'$list' instanceof SoftReference
         assertEquals([1,2,3], res.list)
     }
-}
\ No newline at end of file
+
+    void testNestedLazyCalls() {
+        def res = evaluate("""
+            class X {
+              @Lazy def smallSet = [1, 2, 3]
+              @Lazy def biggerSet = (smallSet + [4, 5, 6])
+            }
+            new X().biggerSet
+        """)
+        assertEquals([1,2,3,4,5,6], res)
+    }
+
+    void testNestedStaticLazyCalls() {
+        def res = evaluate("""
+            class X {
+              @Lazy static final SMALL_SET = [10, 20, 30]
+              @Lazy static final BIGGER_SET = (SMALL_SET + [40, 50, 60])
+            }
+            X.BIGGER_SET
+        """)
+        assertEquals([10,20,30,40,50,60], res)
+    }
+}


Mime
View raw message