groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch GROOVY_3_0_X updated: Tweak Java9 vmplugin
Date Sat, 14 Dec 2019 17:32:58 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 3059452  Tweak Java9 vmplugin
3059452 is described below

commit 30594520df7249bb101b6f99ba3fff79bc79cc46
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sun Dec 15 00:40:30 2019 +0800

    Tweak Java9 vmplugin
    
    (cherry picked from commit e8958d7eb07ae36ebf2c8900bd3714a0fc4c1035)
---
 .../org/codehaus/groovy/reflection/ReflectionUtils.java | 15 +++++++++++++--
 .../java/org/codehaus/groovy/vmplugin/v9/Java9.java     | 17 ++++++++++-------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/reflection/ReflectionUtils.java b/src/main/java/org/codehaus/groovy/reflection/ReflectionUtils.java
index 4a5fd6c..aba0d55 100644
--- a/src/main/java/org/codehaus/groovy/reflection/ReflectionUtils.java
+++ b/src/main/java/org/codehaus/groovy/reflection/ReflectionUtils.java
@@ -35,6 +35,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Function;
 
 /**
  * This class contains utility methods to determine which class called the
@@ -128,11 +129,21 @@ public class ReflectionUtils {
         }
     }
 
-    public static List<Method> getMethods(Class type, String name, Class<?>...
parameterTypes) {
+    private static final Function<Class<?>, Method[]> GET_DECLARED_METHODS =
Class::getDeclaredMethods;
+    public static List<Method> getDeclaredMethods(Class<?> type, String name,
Class<?>... parameterTypes) {
+        return doGetMethods(type, name, parameterTypes, GET_DECLARED_METHODS);
+    }
+
+    private static final Function<Class<?>, Method[]> GET_METHODS = Class::getMethods;
+    public static List<Method> getMethods(Class<?> type, String name, Class<?>...
parameterTypes) {
+        return doGetMethods(type, name, parameterTypes, GET_METHODS);
+    }
+
+    private static List<Method> doGetMethods(Class<?> type, String name, Class<?>[]
parameterTypes, Function<? super Class<?>, ? extends Method[]> f) {
         List<Method> methodList = new LinkedList<>();
 
         out:
-        for (Method m : type.getMethods()) {
+        for (Method m : f.apply(type)) {
             if (!m.getName().equals(name)) {
                 continue;
             }
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
index 9dcadb7..806191d 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
@@ -222,7 +222,7 @@ public class Java9 extends Java8 {
             classList.add(0, theClass);
 
             for (Class<?> sc : classList) {
-                Optional<CachedMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, sc);
+                Optional<CachedMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, sc, true);
                 if (optionalMetaMethod.isPresent()) {
                     return optionalMetaMethod.get();
                 }
@@ -235,7 +235,7 @@ public class Java9 extends Java8 {
             // GROOVY-9081 Sub-class derives the protected members from public class, "Invoke
the members on the sub class instances"
             // e.g. StringBuilder sb = new StringBuilder(); sb.setLength(0);
             // `setLength` is the method of `AbstractStringBuilder`, which is `package-private`
-            Optional<CachedMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, theClass);
+            Optional<CachedMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, theClass, false);
             if (optionalMetaMethod.isPresent()) {
                 return optionalMetaMethod.get();
             }
@@ -257,8 +257,8 @@ public class Java9 extends Java8 {
         return metaMethod;
     }
 
-    private Optional<CachedMethod> getAccessibleMetaMethod(CachedMethod metaMethod,
Class<?>[] params, Class<?> caller, Class<?> sc) {
-        List<CachedMethod> metaMethodList = getMetaMethods(metaMethod, params, sc);
+    private Optional<CachedMethod> getAccessibleMetaMethod(CachedMethod metaMethod,
Class<?>[] params, Class<?> caller, Class<?> sc, boolean declared) {
+        List<CachedMethod> metaMethodList = getMetaMethods(metaMethod, params, sc,
declared);
         for (CachedMethod mm : metaMethodList) {
             if (checkAccessible(caller, mm.getDeclaringClass().getTheClass(), mm.getModifiers(),
false)) {
                 return Optional.of(mm);
@@ -267,9 +267,12 @@ public class Java9 extends Java8 {
         return Optional.empty();
     }
 
-    private static List<CachedMethod> getMetaMethods(CachedMethod metaMethod, Class<?>[]
params, Class<?> sc) {
-        List<Method> optionalMethod = ReflectionUtils.getMethods(sc, metaMethod.getName(),
params);
-        return optionalMethod.stream().map(CachedMethod::new).collect(Collectors.toList());
+    private static List<CachedMethod> getMetaMethods(CachedMethod metaMethod, Class<?>[]
params, Class<?> sc, boolean declared) {
+        String metaMethodName = metaMethod.getName();
+        List<Method> optionalMethodList = declared
+                                            ? ReflectionUtils.getDeclaredMethods(sc, metaMethodName,
params)
+                                            : ReflectionUtils.getMethods(sc, metaMethodName,
params);
+        return optionalMethodList.stream().map(CachedMethod::new).collect(Collectors.toList());
     }
 
     @Override


Mime
View raw message