You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/07/05 15:36:00 UTC

[groovy] 01/01: GROOVY-9620: do not loop over findPropertyInClassHierarchy

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

emilles pushed a commit to branch GROOVY-9620
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 354e83eedc3f67db48eff0e60a4a695475762fa5
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jul 5 10:34:50 2020 -0500

    GROOVY-9620: do not loop over findPropertyInClassHierarchy
---
 src/main/java/groovy/lang/MetaClassImpl.java | 43 ++++++++++++----------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 63f95d3..7324b14 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -290,15 +290,10 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 propertyMap = classPropertyIndexForSuper.getNotNull(theCachedClass);
                 metaProperty = (MetaProperty) propertyMap.get(name);
                 if (metaProperty == null) {
-                    CachedClass superClass = theCachedClass;
-                    while (superClass != null && superClass != ReflectionCache.OBJECT_CLASS) {
-                        MetaBeanProperty property = findPropertyInClassHierarchy(name, superClass);
-                        if (property != null) {
-                            onSuperPropertyFoundInHierarchy(property);
-                            metaProperty = property;
-                            break;
-                        }
-                        superClass = superClass.getCachedSuperClass();
+                    MetaBeanProperty property = findPropertyInClassHierarchy(name, theCachedClass);
+                    if (property != null) {
+                        onSuperPropertyFoundInHierarchy(property);
+                        metaProperty = property;
                     }
                 }
             }
@@ -840,21 +835,17 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
      * @return The result of the method invocation.
      */
     public Object invokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) {
-        Class theClass = instance instanceof Class ? (Class) instance : instance.getClass();
-        CachedClass superClass = theCachedClass;
-        while (superClass != null && superClass != ReflectionCache.OBJECT_CLASS) {
-            final MetaBeanProperty property = findPropertyInClassHierarchy(propertyName, superClass);
-            if (property != null) {
-                onSuperPropertyFoundInHierarchy(property);
-                if (!isGetter) {
-                    property.setProperty(instance, optionalValue);
-                    return null;
-                }
-                return property.getProperty(instance);
+        MetaBeanProperty property = findPropertyInClassHierarchy(propertyName, theCachedClass);
+        if (property != null) {
+            onSuperPropertyFoundInHierarchy(property);
+            if (!isGetter) {
+                property.setProperty(instance, optionalValue);
+                return null;
             }
-            superClass = superClass.getCachedSuperClass();
+            return property.getProperty(instance);
         }
-        // got here to property not found, look for getProperty or setProperty overrides
+
+        // look for getProperty or setProperty overrides
         if (isGetter) {
             final Class[] getPropertyArgs = {String.class};
             final MetaMethod method = findMethodInClassHierarchy(instance.getClass(), GET_PROPERTY_METHOD, getPropertyArgs, this);
@@ -892,6 +883,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             throw iie;
         }
 
+        Class theClass = instance instanceof Class ? (Class) instance : instance.getClass();
         if (instance instanceof Class && theClass != Class.class) {
             final MetaProperty metaProperty = InvokerHelper.getMetaClass(Class.class).hasProperty(instance, propertyName);
             if (metaProperty != null) {
@@ -902,6 +894,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 return null;
             }
         }
+
         throw new MissingPropertyExceptionNoStack(propertyName, theClass);
     }
 
@@ -3605,14 +3598,15 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     }
 
     protected MetaBeanProperty findPropertyInClassHierarchy(String propertyName, CachedClass theClass) {
-        MetaBeanProperty property = null;
-        if (theClass == null)
+        if (theClass == null || theClass == ReflectionCache.OBJECT_CLASS)
             return null;
 
         final CachedClass superClass = theClass.getCachedSuperClass();
         if (superClass == null)
             return null;
 
+        MetaBeanProperty property = null;
+
         MetaClass metaClass = this.registry.getMetaClass(superClass.getTheClass());
         if (metaClass instanceof MutableMetaClass) {
             property = getMetaPropertyFromMutableMetaClass(propertyName, metaClass);
@@ -3627,7 +3621,6 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             }
         }
         return property;
-
     }
 
     private MetaBeanProperty searchInterfacesForMetaProperty(String propertyName, Class[] interfaces) {