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 2025/11/29 01:19:26 UTC

(groovy) branch GROOVY-10198 created (now 616e0302df)

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

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


      at 616e0302df GROOVY-10198: ignore pickMethod result if private or package-private

This branch includes the following new commits:

     new 1fd22d3561 minor refactor
     new 616e0302df GROOVY-10198: ignore pickMethod result if private or package-private

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(groovy) 01/02: minor refactor

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1fd22d3561dbd010dcae767f65d20f9e6b8c3342
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Nov 28 18:47:37 2025 -0600

    minor refactor
---
 src/main/java/groovy/lang/MetaClassImpl.java | 90 ++++++++++------------------
 1 file changed, 31 insertions(+), 59 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index c6d3bd94e0..7306845de8 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -3620,23 +3620,20 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     }
 
     protected static MetaMethod findMethodInClassHierarchy(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass) {
-        if (metaClass instanceof MetaClassImpl) {
-            boolean check = false;
-            for (ClassInfo ci : ((MetaClassImpl) metaClass).theCachedClass.getHierarchy()) {
-                final MetaClass aClass = ci.getStrongMetaClass();
-                if (aClass instanceof MutableMetaClass && ((MutableMetaClass) aClass).isModified()) {
-                    check = true;
-                    break;
+out:    if (metaClass instanceof MetaClassImpl metaClassImpl) {
+            for (ClassInfo ci : metaClassImpl.theCachedClass.getHierarchy()) {
+                if (ci.getStrongMetaClass() instanceof MutableMetaClass mmc && mmc.isModified()) {
+                    break out;
                 }
             }
-
-            if (!check) return null;
+            return null;
         }
 
-        final Class<?> superClass;
-        final Class<?> theClass = metaClass.getTheClass();
+        final Class<?> superClass, theClass = metaClass.getTheClass();
         if (theClass.isArray() && !theClass.getComponentType().isPrimitive() && theClass.getComponentType() != Object.class) {
             superClass = Object[].class;
+        } else if (theClass.isInterface()) {
+            superClass = Object.class;
         } else {
             superClass = theClass.getSuperclass();
         }
@@ -3645,43 +3642,38 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         if (superClass != null) {
             MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(superClass);
             method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
-        } else {
-            if (theClass.isInterface()) {
-                MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(Object.class);
-                method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
-            }
         }
 
-        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);
+        MetaMethod infMethod = null;
+        for (Class face : metaClass.getTheClass().getInterfaces()) {
+            MetaClass faceMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(face);
+            infMethod = getMetaMethod(instanceKlazz, methodName, arguments, faceMetaClass, infMethod);
+        }
         if (infMethod != null) {
             method = (method == null ? infMethod : mostSpecific(method, infMethod, instanceKlazz));
         }
+
+        method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);
+
+        method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
+
         return method;
     }
 
     private static MetaMethod findSubClassMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) {
-        if (metaClass instanceof MetaClassImpl) {
-            Object list = ((MetaClassImpl) metaClass).getSubclassMetaMethods(methodName);
-            if (list != null) {
-                if (list instanceof MetaMethod) {
-                    MetaMethod m = (MetaMethod) list;
-                    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);
-                        method = findSubClassMethod(instanceKlazz, arguments, method, m);
-                    }
+        if (metaClass instanceof MetaClassImpl metaClassImpl) {
+            var result = metaClassImpl.getSubclassMetaMethods(methodName);
+            if (result instanceof MetaMethod mm) {
+                method = findSubClassMethod(instanceKlazz, arguments, method, mm);
+            } else if (result instanceof FastArray arr) {
+                for (int i = 0; i < arr.size(); i += 1) {
+                    method = findSubClassMethod(instanceKlazz, arguments, method, (MetaMethod) arr.get(i));
                 }
             }
         }
@@ -3696,44 +3688,24 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     }
 
     private static MetaMethod mostSpecific(MetaMethod method, MetaMethod newMethod, Class instanceKlazz) {
-        Class newMethodC = newMethod.getDeclaringClass().getTheClass();
-        Class methodC = method.getDeclaringClass().getTheClass();
-
-        if (!newMethodC.isAssignableFrom(instanceKlazz))
+        Class<?> newMethodC = newMethod.getDeclaringClass().getTheClass();
+        if (!newMethodC.isAssignableFrom(instanceKlazz)) {
             return method;
-
-        if (newMethodC == methodC)
+        }
+        Class<?> methodC = method.getDeclaringClass().getTheClass();
+        if (newMethodC == methodC) {
             return newMethod;
-
+        }
         if (newMethodC.isAssignableFrom(methodC)) {
             return method;
         }
-
         if (methodC.isAssignableFrom(newMethodC)) {
             return newMethod;
         }
-
         return newMethod;
     }
 
-    private static MetaMethod searchInterfacesForMetaMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass) {
-        Class[] interfaces = metaClass.getTheClass().getInterfaces();
-
-        MetaMethod method = null;
-        for (Class anInterface : interfaces) {
-            MetaClass infMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(anInterface);
-            method = getMetaMethod(instanceKlazz, methodName, arguments, infMetaClass, method);
-        }
-
-        method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);
-
-        method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
-
-        return method;
-    }
-
     protected static MetaMethod findOwnMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) {
-        // we trick ourselves here
         if (instanceKlazz != metaClass.getTheClass()) {
             MetaMethod ownMethod = metaClass.pickMethod(methodName, arguments);
             if (ownMethod != null) {


(groovy) 02/02: GROOVY-10198: ignore pickMethod result if private or package-private

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 616e0302df19e2bfc3b88494faa6d973885412bf
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Nov 28 19:12:33 2025 -0600

    GROOVY-10198: ignore pickMethod result if private or package-private
---
 src/main/java/groovy/lang/MetaClassImpl.java |  6 +++-
 src/test/groovy/bugs/Groovy10198.groovy      | 51 ++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 7306845de8..a334b1236a 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -3708,13 +3708,17 @@ out:    if (metaClass instanceof MetaClassImpl metaClassImpl) {
     protected static MetaMethod findOwnMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) {
         if (instanceKlazz != metaClass.getTheClass()) {
             MetaMethod ownMethod = metaClass.pickMethod(methodName, arguments);
-            if (ownMethod != null) {
+            if (ownMethod != null && !isPrivate(ownMethod, instanceKlazz)) { // GROOVY-10198
                 method = (method == null ? ownMethod : mostSpecific(method, ownMethod, instanceKlazz));
             }
         }
         return method;
     }
 
+    private static boolean isPrivate(MetaMethod method, Class instanceKlazz) {
+        return method.isPrivate() || (method.isPackagePrivate() && !inSamePackage(method.getDeclaringClass().getTheClass(), instanceKlazz));
+    }
+
     protected Object getSubclassMetaMethods(String methodName) {
         return null;
     }
diff --git a/src/test/groovy/bugs/Groovy10198.groovy b/src/test/groovy/bugs/Groovy10198.groovy
new file mode 100644
index 0000000000..66d74fb12f
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10198.groovy
@@ -0,0 +1,51 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package bugs
+
+import org.junit.jupiter.api.Test
+
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy10198 {
+
+    @Test
+    void testMetaClassChangeKeepsPrivate() {
+        def pogo = new Two()
+        shouldFail(MissingMethodException) {
+            def value = pogo.getValue()
+        }
+        One.metaClass.getThing << { -> 'thing' }
+        try {
+            shouldFail(MissingMethodException) {
+                def value = pogo.getValue()
+            }
+        } finally {
+            One.metaClass = null
+        }
+    }
+
+    static class One {
+        private String getValue() {
+            'something private'
+        }
+    }
+
+    static class Two extends One {
+    }
+}