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/03/01 00:30:52 UTC

groovy git commit: Minor refactoring: remove duplicated code of `MetaClassImpl`

Repository: groovy
Updated Branches:
  refs/heads/master 4d7884edc -> a949c8c68


Minor refactoring: remove duplicated code of `MetaClassImpl`


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

Branch: refs/heads/master
Commit: a949c8c688c45290aaf4d7c9da399f331f7dbbb5
Parents: 4d7884e
Author: sunlan <su...@apache.org>
Authored: Thu Mar 1 08:18:43 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Mar 1 08:27:05 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/lang/MetaClassImpl.java | 90 +++++++++------------
 1 file changed, 38 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a949c8c6/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 c453a3f..99c0fa4 100644
--- a/src/main/groovy/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/groovy/lang/MetaClassImpl.java
@@ -525,14 +525,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                         String mopName = method.getMopName();
                         int index = Arrays.binarySearch(mopMethods, mopName, CachedClass.CachedMethodComparatorWithString.INSTANCE);
                         if (index >= 0) {
-                            int from = index;
-                            while (from > 0 && mopMethods[from-1].getName().equals(mopName))
-                              from--;
-                            int to = index;
-                            while (to < mopMethods.length-1 && mopMethods[to+1].getName().equals(mopName))
-                              to++;
-
-                            int matchingMethod = findMatchingMethod(mopMethods, from, to, method);
+                            int matchingMethod = findMatchingMethod(method, mopName, index, mopMethods);
                             if (matchingMethod != -1) {
                                 e.methods = mopMethods[matchingMethod];
                             }
@@ -561,14 +554,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                             String fixedMopName = decomposedMopName[0] + distance + decomposedMopName[2];
                             int index = Arrays.binarySearch(mopMethods, fixedMopName, CachedClass.CachedMethodComparatorWithString.INSTANCE);
                             if (index >= 0) {
-                                int from = index;
-                                while (from > 0 && mopMethods[from-1].getName().equals(fixedMopName))
-                                  from--;
-                                int to = index;
-                                while (to < mopMethods.length-1 && mopMethods[to+1].getName().equals(fixedMopName))
-                                  to++;
-
-                                int matchingMethod = findMatchingMethod(mopMethods, from, to, method);
+                                int matchingMethod = findMatchingMethod(method, fixedMopName, index, mopMethods);
                                 if (matchingMethod != -1) {
                                     e.methodsForSuper = mopMethods[matchingMethod];
                                     distance = 0;
@@ -606,14 +592,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                     String mopName = method.getMopName();
                     int index = Arrays.binarySearch(mopMethods, mopName, CachedClass.CachedMethodComparatorWithString.INSTANCE);
                     if (index >= 0) {
-                        int from = index;
-                        while (from > 0 && mopMethods[from-1].getName().equals(mopName))
-                          from--;
-                        int to = index;
-                        while (to < mopMethods.length-1 && mopMethods[to+1].getName().equals(mopName))
-                          to++;
-
-                        int matchingMethod = findMatchingMethod(mopMethods, from, to, method);
+                        int matchingMethod = findMatchingMethod(method, mopName, index, mopMethods);
                         if (matchingMethod != -1) {
                             methods.set(i, mopMethods[matchingMethod]);
                         }
@@ -631,6 +610,17 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         iter.iterate();
     }
 
+    private int findMatchingMethod(MetaMethod method, String mopName, int index, CachedMethod[] mopMethods) {
+        int from = index;
+        while (from > 0 && mopMethods[from-1].getName().equals(mopName))
+          from--;
+        int to = index;
+        while (to < mopMethods.length-1 && mopMethods[to+1].getName().equals(mopName))
+          to++;
+
+        return findMatchingMethod(mopMethods, from, to, method);
+    }
+
     private void inheritInterfaceNewMetaMethods(Set<CachedClass> interfaces) {
         // add methods declared by DGM for interfaces
         for (CachedClass cls : interfaces) {
@@ -3693,6 +3683,14 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
 
         method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);
 
+        method = getMetaMethod(instanceKlazz, methodName, arguments, metaClass, method);
+
+        method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
+
+        return method;
+    }
+
+    private static MetaMethod getMetaMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) {
         MetaMethod infMethod = searchInterfacesForMetaMethod(instanceKlazz, methodName, arguments, metaClass);
         if (infMethod != null) {
             if (method == null)
@@ -3700,9 +3698,6 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             else
               method = mostSpecific(method, infMethod, instanceKlazz);
         }
-
-        method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
-
         return method;
     }
 
@@ -3712,29 +3707,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             if (list != null) {
                 if (list instanceof MetaMethod) {
                     MetaMethod m = (MetaMethod) list;
-                    if (m.getDeclaringClass().getTheClass().isAssignableFrom(instanceKlazz)) {
-                        if (m.isValidExactMethod(arguments)) {
-                            if (method == null)
-                              method = m;
-                            else {
-                              method = mostSpecific (method, m, instanceKlazz);
-                            }
-                        }
-                    }
+                    method = findSubClassMethod(instanceKlazz, arguments, method, m);
                 }
                 else {
                     FastArray arr = (FastArray) list;
                     for (int i = 0; i != arr.size(); ++i) {
                         MetaMethod m = (MetaMethod) arr.get(i);
-                        if (m.getDeclaringClass().getTheClass().isAssignableFrom(instanceKlazz)) {
-                            if (m.isValidExactMethod(arguments)) {
-                                if (method == null)
-                                  method = m;
-                                else {
-                                  method = mostSpecific (method, m, instanceKlazz);
-                                }
-                            }
-                        }
+                        method = findSubClassMethod(instanceKlazz, arguments, method, m);
                     }
                 }
             }
@@ -3742,6 +3721,19 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         return method;
     }
 
+    private static MetaMethod findSubClassMethod(Class instanceKlazz, Class[] arguments, MetaMethod method, MetaMethod m) {
+        if (m.getDeclaringClass().getTheClass().isAssignableFrom(instanceKlazz)) {
+            if (m.isValidExactMethod(arguments)) {
+                if (method == null)
+                  method = m;
+                else {
+                  method = mostSpecific (method, m, instanceKlazz);
+                }
+            }
+        }
+        return method;
+    }
+
     private static MetaMethod mostSpecific(MetaMethod method, MetaMethod newMethod, Class instanceKlazz) {
         Class newMethodC = newMethod.getDeclaringClass().getTheClass();
         Class methodC = method.getDeclaringClass().getTheClass();
@@ -3769,13 +3761,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         MetaMethod method = null;
         for (Class anInterface : interfaces) {
             MetaClass infMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(anInterface);
-            MetaMethod infMethod = searchInterfacesForMetaMethod(instanceKlazz, methodName, arguments, infMetaClass);
-            if (infMethod != null) {
-                if (method == null)
-                    method = infMethod;
-                else
-                    method = mostSpecific(method, infMethod, instanceKlazz);
-            }
+            method = getMetaMethod(instanceKlazz, methodName, arguments, infMetaClass, method);
         }
 
         method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);