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 2018/10/20 11:19:11 UTC

[3/4] groovy git commit: Minor refactoring: remove duplicated code of `MetaClassImpl`

Minor refactoring: remove duplicated code of `MetaClassImpl`

(cherry picked from commit d0cd0538b88e844096aeae64bcb2bd138546f856)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: a3fe79a987a4ecf10756994fe4bf21b4bbfc91e2
Parents: 1c60dc6
Author: Daniel Sun <su...@apache.org>
Authored: Sat Oct 20 18:45:37 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Sat Oct 20 19:18:01 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/lang/MetaClassImpl.java | 64 +++++++++++++--------
 1 file changed, 39 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a3fe79a9/src/main/groovy/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/MetaClassImpl.java b/src/main/groovy/groovy/lang/MetaClassImpl.java
index f22c62f..73dcb94 100644
--- a/src/main/groovy/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/groovy/lang/MetaClassImpl.java
@@ -1130,17 +1130,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                     }
                     break;
                 case Closure.DELEGATE_FIRST:
-                    if (method == null && delegate != closure && delegate != null) {
-                        MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
-                        method = delegateMetaClass.pickMethod(methodName, argClasses);
-                        if (method != null)
-                            return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
-                    }
-                    if (method == null && owner != closure) {
-                        MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
-                        method = ownerMetaClass.pickMethod(methodName, argClasses);
-                        if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+                    Object result = invokeMethod(method, delegate, closure, methodName, argClasses, originalArguments, owner);
+                    if (InvokeMethodResult.NONE != result) {
+                        return result;
                     }
+
                     if (method == null && resolveStrategy != Closure.TO_SELF) {
                         // still no methods found, test if delegate or owner are GroovyObjects
                         // and invoke the method on them if so.
@@ -1164,17 +1158,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
 
                     break;
                 default:
-                    if (method == null && delegate != closure && delegate != null) {
-                        MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
-                        method = delegateMetaClass.pickMethod(methodName, argClasses);
-                        if (method != null)
-                            return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
-                    }
-                    if (method == null && owner != closure) {
-                        MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
-                        method = ownerMetaClass.pickMethod(methodName, argClasses);
-                        if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+                    Object r = invokeMethod(method, delegate, closure, methodName, argClasses, originalArguments, owner);
+                    if (InvokeMethodResult.NONE != r) {
+                        return r;
                     }
+
                     if (method == null && resolveStrategy != Closure.TO_SELF) {
                         // still no methods found, test if delegate or owner are GroovyObjects
                         // and invoke the method on them if so.
@@ -1844,7 +1832,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         // special cases
         //----------------------------------------------------------------------
         if (method == null) {
-            /** todo these special cases should be special MetaClasses maybe */
+            /* todo these special cases should be special MetaClasses maybe */
             if (theClass != Class.class && object instanceof Class) {
                 MetaClass mc = registry.getMetaClass(Class.class);
                 return mc.getProperty(Class.class, object, name, useSuper, false);
@@ -1957,7 +1945,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         //----------------------------------------------------------------------
         // special cases
         //----------------------------------------------------------------------
-        /** todo these special cases should be special MetaClasses maybe */
+        /* todo these special cases should be special MetaClasses maybe */
         if (theClass != Class.class && object instanceof Class) {
             return new MetaProperty(name, Object.class) {
                 public Object getProperty(Object object) {
@@ -2254,7 +2242,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
      * This will build up the property map (Map of MetaProperty objects, keyed on
      * property name).
      *
-     * @param propertyDescriptors
+     * @param propertyDescriptors the property descriptors
      */
     @SuppressWarnings("unchecked")
     private void setupProperties(PropertyDescriptor[] propertyDescriptors) {
@@ -2946,7 +2934,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             if (url != null) {
                 try {
 
-                    /**
+                    /*
                      * todo there is no CompileUnit in scope so class name
                      * checking won't work but that mostly affects the bytecode
                      * generation rather than viewing the AST
@@ -3190,7 +3178,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
      * name.
      *
      * @param methodOrList   the possible methods to choose from
-     * @param arguments
+     * @param arguments the arguments
      */
     protected Object chooseMethod(String methodName, Object methodOrList, Class[] arguments) {
         Object method = chooseMethodInternal(methodName, methodOrList, arguments);
@@ -3977,4 +3965,30 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             return null;
         }
     }
+
+    private Object invokeMethod(MetaMethod method,
+                                Object delegate,
+                                Closure closure,
+                                String methodName,
+                                Class[] argClasses,
+                                Object[] originalArguments,
+                                Object owner) {
+        if (method == null && delegate != closure && delegate != null) {
+            MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
+            method = delegateMetaClass.pickMethod(methodName, argClasses);
+            if (method != null)
+                return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
+        }
+        if (method == null && owner != closure) {
+            MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
+            method = ownerMetaClass.pickMethod(methodName, argClasses);
+            if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+        }
+
+        return InvokeMethodResult.NONE;
+    }
+
+    private enum InvokeMethodResult {
+        NONE
+    }
 }