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 2021/06/14 07:53:30 UTC

[groovy] 01/02: Tweak `ProxyMethodHandle`

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

sunlan pushed a commit to branch danielsun/tweak-build
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit a6f8dc48967408e76a2abea4791a465de8a309bd
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Jun 14 15:48:25 2021 +0800

    Tweak `ProxyMethodHandle`
---
 .../codehaus/groovy/vmplugin/v16/ProxyMethodHandle.java  | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v16/ProxyMethodHandle.java b/src/main/java/org/codehaus/groovy/vmplugin/v16/ProxyMethodHandle.java
index d6a1e39..90afca2 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v16/ProxyMethodHandle.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v16/ProxyMethodHandle.java
@@ -2,16 +2,23 @@ package org.codehaus.groovy.vmplugin.v16;
 
 import org.codehaus.groovy.GroovyBugError;
 
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 
 class ProxyMethodHandle {
-    private static final Method INVOKE_DEFAULT_METHOD;
+    private static final MethodHandle INVOKE_DEFAULT_METHOD_HANDLE;
     static {
         try {
-            INVOKE_DEFAULT_METHOD = InvocationHandler.class.getDeclaredMethod("invokeDefault", Object.class, Method.class, Object[].class);
-        } catch (NoSuchMethodException e) {
+            // `invokeDefault` is JDK 16+ API, but we still build Groovy with JDK11,
+            // so use method handle instead of invoking the method directly
+            INVOKE_DEFAULT_METHOD_HANDLE = MethodHandles.lookup().findStatic(
+                                                InvocationHandler.class, "invokeDefault",
+                                                MethodType.methodType(Object.class, Object.class, Method.class, Object[].class));
+        } catch (NoSuchMethodException | IllegalAccessException e) {
             throw new GroovyBugError(e);
         }
     }
@@ -25,6 +32,7 @@ class ProxyMethodHandle {
     }
 
     Object invokeWithArguments(Object... arguments) throws Throwable {
-        return INVOKE_DEFAULT_METHOD.invoke(null, proxy, method, arguments);
+        Object proxy = this.proxy;
+        return INVOKE_DEFAULT_METHOD_HANDLE.invokeExact(proxy, method, arguments);
     }
 }