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 <[email protected]> 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[]) + ''') + } }
