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 2021/06/16 06:39:29 UTC

[groovy] branch danielsun/tweak-build updated: Fix calling method derived from super type

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

sunlan pushed a commit to branch danielsun/tweak-build
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/danielsun/tweak-build by this push:
     new db5964c  Fix calling method derived from super type
db5964c is described below

commit db5964c17e364b8ce4b8fee6997b65933968abe9
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Jun 16 14:39:08 2021 +0800

    Fix calling method derived from super type
---
 src/main/java/groovy/lang/MetaClassImpl.java | 41 ++++++++++++++--------------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 1394318..06ed1a5 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -1134,36 +1134,37 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass) {
         try {
             return doInvokeMethod(sender, object, methodName, originalArguments, isCallToSuper, fromInsideClass);
-        } catch (MissingMethodException ex) {
-            if (!isCallToSuper) {
-                throw ex;
+        } catch (MissingMethodException mme) {
+            MethodHandles.Lookup lookup = null;
+            if (object instanceof GroovyObject) {
+                Optional<MethodHandles.Lookup> lookupOptional = GroovyObjectHelper.lookup((GroovyObject) object);
+                if (!lookupOptional.isPresent()) throw mme;
+                lookup = lookupOptional.get();
             }
 
-            if (!(object instanceof GroovyObject)) {
-                throw ex;
-            }
-
-            Optional<MethodHandles.Lookup> lookupOptional = GroovyObjectHelper.lookup((GroovyObject) object);
-            if (!lookupOptional.isPresent()) {
-                throw ex;
-            }
-            MethodHandles.Lookup lookup = lookupOptional.get();
-            Method superMethod = findSuperMethod(sender, methodName, originalArguments);
-            if (null == superMethod) {
-                throw ex;
-            }
             try {
-                MethodHandle superMethodHandle = lookup.unreflectSpecial(superMethod, object.getClass());
-                return superMethodHandle.bindTo(object).invokeWithArguments(originalArguments);
+                if (isCallToSuper) {
+                    if (null == lookup) throw mme;
+                    Method superMethod = findMethod(sender, methodName, originalArguments);
+                    if (null == superMethod) throw mme;
+                    MethodHandle superMethodHandle = lookup.unreflectSpecial(superMethod, object.getClass());
+                    return superMethodHandle.bindTo(object).invokeWithArguments(originalArguments);
+                } else {
+                    if (null == lookup) lookup = MethodHandles.lookup().in(object.getClass());
+                    Method thisMethod = findMethod(object.getClass(), methodName, originalArguments);
+                    if (null == thisMethod) throw mme;
+                    MethodHandle thisMethodHandle = lookup.unreflect(thisMethod);
+                    return thisMethodHandle.bindTo(object).invokeWithArguments(originalArguments);
+                }
             } catch (Throwable t) {
                 throw new GroovyRuntimeException(t);
             }
         }
     }
 
-    private static Method findSuperMethod(Class senderClass, String messageName, Object[] messageArguments) {
+    private static Method findMethod(Class clazz, String messageName, Object[] messageArguments) {
         Class[] parameterTypes = MetaClassHelper.castArgumentsToClassArray(messageArguments);
-        for (Class<?> c = senderClass; null != c; c = c.getSuperclass()) {
+        for (Class<?> c = clazz; null != c; c = c.getSuperclass()) {
             List<Method> declaredMethodList = ReflectionUtils.getDeclaredMethods(c, messageName, parameterTypes);
             if (!declaredMethodList.isEmpty()) {
                 Method superMethod = declaredMethodList.get(0);