You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/09/05 18:06:25 UTC

[groovy] branch GROOVY_4_0_X updated (ad16a236c9 -> e81e1f7118)

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a change to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


    from ad16a236c9 Update AST checks in test
     new ec92eba6f4 Trivial tweak for `clone` via `InvokerHelper`
     new e81e1f7118 Further tweak for `clone` via `InvokerHelper`

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/codehaus/groovy/runtime/InvokerHelper.java | 16 +++++-----
 src/test/groovy/bugs/Groovy9103.groovy             | 35 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 8 deletions(-)


[groovy] 01/02: Trivial tweak for `clone` via `InvokerHelper`

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit ec92eba6f40707daa13f3dda8088a299f0dc84c5
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Sep 6 01:45:18 2022 +0800

    Trivial tweak for `clone` via `InvokerHelper`
    
    (cherry picked from commit e103174a8ea4d85b7446a5860f2f19628ea9b2fb)
---
 .../org/codehaus/groovy/runtime/InvokerHelper.java | 17 +++++------
 src/test/groovy/bugs/Groovy9103.groovy             | 33 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
index d292a08329..b4fcf192a4 100644
--- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -44,6 +44,7 @@ import org.codehaus.groovy.runtime.wrappers.PojoWrapper;
 import java.beans.Introspector;
 import java.io.IOException;
 import java.io.Writer;
+import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
@@ -586,6 +587,14 @@ public class InvokerHelper {
             return metaClass.invokeStaticMethod(object, methodName, asArray(arguments));
         }
 
+        if ("clone".equals(methodName) && (null == arguments || arguments.getClass().isArray() && 0 == Array.getLength(arguments))) {
+            try {
+                return ObjectUtil.cloneObject(object);
+            } catch (Throwable t) {
+                UncheckedThrow.rethrow(t);
+            }
+        }
+
         // it's an instance; check if it's a Java one
         if (!(object instanceof GroovyObject)) {
             return invokePojoMethod(object, methodName, arguments);
@@ -596,14 +605,6 @@ public class InvokerHelper {
     }
 
     static Object invokePojoMethod(Object object, String methodName, Object arguments) {
-        if ("clone".equals(methodName) && (null == arguments || arguments.getClass().isArray() && 0 == ((Object[]) arguments).length)) {
-            try {
-                return ObjectUtil.cloneObject(object);
-            } catch (Throwable t) {
-                UncheckedThrow.rethrow(t);
-            }
-        }
-
         MetaClass metaClass = InvokerHelper.getMetaClass(object);
         return metaClass.invokeMethod(object, methodName, asArray(arguments));
     }
diff --git a/src/test/groovy/bugs/Groovy9103.groovy b/src/test/groovy/bugs/Groovy9103.groovy
index d2be689410..f4f8117426 100644
--- a/src/test/groovy/bugs/Groovy9103.groovy
+++ b/src/test/groovy/bugs/Groovy9103.groovy
@@ -109,4 +109,37 @@ final class Groovy9103 {
             dolly.clone()
         ''')
     }
+
+    @Test
+    void testClone7() {
+        ['Object', 'Dolly'].each { typeName ->
+            assertScript """
+                import org.codehaus.groovy.runtime.InvokerHelper
+                class Dolly implements Cloneable {
+                    String name
+
+                    public ${typeName} clone() {
+                        return super.clone()
+                    }
+                }
+
+                def dolly = new Dolly(name: "The Sheep")
+                def cloned = InvokerHelper.invokeMethod(dolly, 'clone', [] as Object[])
+                assert cloned instanceof Dolly
+            """
+        }
+    }
+
+    @Test
+    void testClone8() {
+        shouldFail(CloneNotSupportedException, '''
+            import org.codehaus.groovy.runtime.InvokerHelper
+            class Dolly {
+                String name
+            }
+
+            def dolly = new Dolly(name: "The Sheep")
+            InvokerHelper.invokeMethod(dolly, 'clone', [] as Object[])
+        ''')
+    }
 }


[groovy] 02/02: Further tweak for `clone` via `InvokerHelper`

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit e81e1f711849d32ff96c10ac7d0f6b94f7185a9c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Sep 6 02:04:52 2022 +0800

    Further tweak for `clone` via `InvokerHelper`
    
    (cherry picked from commit 38e45ceabbc9a597ac5c7d667036af1dc9af69ee)
---
 src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java | 3 +--
 src/test/groovy/bugs/Groovy9103.groovy                       | 2 ++
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
index b4fcf192a4..a83fbbaced 100644
--- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -44,7 +44,6 @@ import org.codehaus.groovy.runtime.wrappers.PojoWrapper;
 import java.beans.Introspector;
 import java.io.IOException;
 import java.io.Writer;
-import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
@@ -587,7 +586,7 @@ public class InvokerHelper {
             return metaClass.invokeStaticMethod(object, methodName, asArray(arguments));
         }
 
-        if ("clone".equals(methodName) && (null == arguments || arguments.getClass().isArray() && 0 == Array.getLength(arguments))) {
+        if ("clone".equals(methodName) && 0 == asArray(arguments).length) {
             try {
                 return ObjectUtil.cloneObject(object);
             } catch (Throwable t) {
diff --git a/src/test/groovy/bugs/Groovy9103.groovy b/src/test/groovy/bugs/Groovy9103.groovy
index f4f8117426..53c006e141 100644
--- a/src/test/groovy/bugs/Groovy9103.groovy
+++ b/src/test/groovy/bugs/Groovy9103.groovy
@@ -86,6 +86,7 @@ final class Groovy9103 {
                 class Dolly implements Cloneable {
                     String name
 
+                    @Override
                     public ${typeName} clone() {
                         return super.clone()
                     }
@@ -118,6 +119,7 @@ final class Groovy9103 {
                 class Dolly implements Cloneable {
                     String name
 
+                    @Override
                     public ${typeName} clone() {
                         return super.clone()
                     }