You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2023/03/07 12:35:02 UTC

[felix-dev] branch master updated: [FELIX-6597] IllegalAccess when using reflection on public methods

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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new a618785e15 [FELIX-6597] IllegalAccess when using reflection on public methods
a618785e15 is described below

commit a618785e151488ccae1c1182d10361d181c991c2
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Wed Mar 1 08:07:47 2023 +0100

    [FELIX-6597] IllegalAccess when using reflection on public methods
---
 .../org/apache/felix/gogo/runtime/Reflective.java  | 46 +++++++++++++++-------
 1 file changed, 32 insertions(+), 14 deletions(-)

diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
index 3546a9e530..db23d1d337 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
@@ -23,12 +23,15 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.AbstractList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+import java.util.LinkedHashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -63,7 +66,6 @@ public final class Reflective
     public static Object invoke(CommandSession session, Object target, String name,
         List<Object> args) throws Exception
     {
-        Method[] methods = target.getClass().getMethods();
         name = name.toLowerCase(Locale.ENGLISH);
 
         String org = name;
@@ -76,19 +78,15 @@ public final class Reflective
             name = "_" + name;
         }
 
-        if (target instanceof Class<?>)
-        {
-            Method[] staticMethods = ((Class<?>) target).getMethods();
-            for (Method m : staticMethods)
-            {
-                String mname = m.getName().toLowerCase(Locale.ENGLISH);
-                if (mname.equals(name) || mname.equals(get) || mname.equals(set)
-                    || mname.equals(is) || mname.equals(MAIN))
-                {
-                    methods = staticMethods;
-                    break;
-                }
-            }
+        Set<Class<?>> publicClasses = new LinkedHashSet<>();
+        Set<Class<?>> nonPublicClasses = new LinkedHashSet<>();
+        getClassAndAncestors(publicClasses, nonPublicClasses, target.getClass());
+        List<Method> methods = new ArrayList<>();
+        for (Class<?> cl : publicClasses) {
+            Collections.addAll(methods, cl.getMethods());
+        }
+        for (Class<?> cl : nonPublicClasses) {
+            Collections.addAll(methods, cl.getMethods());
         }
 
         Method bestMethod = null;
@@ -206,6 +204,26 @@ public final class Reflective
         }
     }
 
+    private static void getClassAndAncestors(Set<Class<?>> publicClasses, Set<Class<?>> nonPublicClasses, Class<?> aClass)
+    {
+        for (Class<?> itf : aClass.getInterfaces())
+        {
+            getClassAndAncestors(publicClasses, nonPublicClasses, itf);
+        }
+        if (aClass.getSuperclass() != null)
+        {
+            getClassAndAncestors(publicClasses, nonPublicClasses, aClass.getSuperclass());
+        }
+        if (Modifier.isPublic(aClass.getModifiers()))
+        {
+            publicClasses.add(aClass);
+        }
+        else
+        {
+            nonPublicClasses.add(aClass);
+        }
+    }
+
     /**
      * transform name/value parameters into ordered argument list.
      * params: --param2, value2, --flag1, arg3