groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject groovy git commit: GROOVY-6584: Cannot use memoize() to cache metaclass method call (closes #434)
Date Fri, 07 Oct 2016 03:43:03 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X b6eaffb98 -> 7eea223df


GROOVY-6584: Cannot use memoize() to cache metaclass method call (closes #434)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 7eea223dff2dec134e97ba30f2bf14e27a9da5f1
Parents: b6eaffb
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sun Oct 2 12:23:16 2016 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Oct 6 20:40:49 2016 -0700

----------------------------------------------------------------------
 .../groovy/runtime/memoize/Memoize.java         |  2 ++
 .../groovy/runtime/memoize/MemoizeTest.groovy   | 33 ++++++++++++++++++++
 2 files changed, 35 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7eea223d/src/main/org/codehaus/groovy/runtime/memoize/Memoize.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/Memoize.java b/src/main/org/codehaus/groovy/runtime/memoize/Memoize.java
index 7858f93..544e06c 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/Memoize.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/Memoize.java
@@ -123,6 +123,8 @@ public abstract class Memoize {
             super(closure.getOwner());
             this.cache = cache;
             this.closure = closure;
+            parameterTypes = closure.getParameterTypes();
+            maximumNumberOfParameters = closure.getMaximumNumberOfParameters();
         }
         
         @Override public V call(final Object... args) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/7eea223d/src/test/org/codehaus/groovy/runtime/memoize/MemoizeTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/memoize/MemoizeTest.groovy b/src/test/org/codehaus/groovy/runtime/memoize/MemoizeTest.groovy
index 0d3d7a9..8bc50d4 100644
--- a/src/test/org/codehaus/groovy/runtime/memoize/MemoizeTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/memoize/MemoizeTest.groovy
@@ -48,4 +48,37 @@ public class MemoizeTest extends AbstractMemoizeTestCase {
         assert maxExecutionCount == 2
         assert minExecutionCount == 2
     }
+
+    // GROOVY-6584
+    void testMemoizeFunctionClosure() {
+        int timesMethodBodyExecuted = 0
+        def lst = []
+        lst.metaClass.getTotalCount = {
+            ++timesMethodBodyExecuted
+            12
+        }.memoize()
+
+        assert lst.getTotalCount() == 12
+        assert lst.getTotalCount() == 12
+        assert timesMethodBodyExecuted == 1
+
+        timesMethodBodyExecuted = 0
+        lst.metaClass.getUsersByDeptAndMgrId = { String dept, int id ->
+            ++timesMethodBodyExecuted
+            [dept, "${id}"]
+        }.memoize()
+
+        assert lst.getUsersByDeptAndMgrId('123', 555) == ['123', '555']
+        assert lst.getUsersByDeptAndMgrId('456', 999) == ['456', '999']
+
+        assert timesMethodBodyExecuted == 2
+
+        assert lst.getUsersByDeptAndMgrId('123', 555) == ['123', '555']
+        assert lst.getUsersByDeptAndMgrId('456', 999) == ['456', '999']
+
+        assert lst.getUsersByDeptAndMgrId('123', 555) == ['123', '555']
+        assert lst.getUsersByDeptAndMgrId('456', 999) == ['456', '999']
+
+        assert timesMethodBodyExecuted == 2
+    }
 }


Mime
View raw message