This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new b6b4b1a move code in Java9 which can safely run in Java8
b6b4b1a is described below
commit b6b4b1a4e543e6d5af7c60b8e5982cd4ecea5b38
Author: Paul King <[email protected]>
AuthorDate: Tue Jul 14 02:14:17 2020 +1000
move code in Java9 which can safely run in Java8
---
.../org/codehaus/groovy/vmplugin/v8/Java8.java | 44 +++++++++++++++-------
.../org/codehaus/groovy/vmplugin/v9/Java9.java | 10 -----
2 files changed, 31 insertions(+), 23 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
index b1fe4bd..4e56e63 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java
@@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.vmplugin.v8;
+import groovy.lang.GroovyRuntimeException;
import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;
import org.codehaus.groovy.GroovyBugError;
@@ -613,7 +614,16 @@ public class Java8 implements VMPlugin {
}
@Override
- public Object getInvokeSpecialHandle(final Method method, final Object
receiver) {
+ public Object getInvokeSpecialHandle(Method method, Object receiver) {
+ final Class<?> receiverType = receiver.getClass();
+ try {
+ return of(receiverType).unreflectSpecial(method,
receiverType).bindTo(receiver);
+ } catch (ReflectiveOperationException e) {
+ return getInvokeSpecialHandleFallback(method, receiver);
+ }
+ }
+
+ private Object getInvokeSpecialHandleFallback(Method method, Object
receiver) {
if (getLookupConstructor() == null) {
throw new GroovyBugError("getInvokeSpecialHandle requires at least
JDK 7 for private access to Lookup");
}
@@ -639,32 +649,40 @@ public class Java8 implements VMPlugin {
return mh.invokeWithArguments(args);
}
+ public static MethodHandles.Lookup of(final Class<?> declaringClass) {
+ try {
+ return getLookupConstructor().newInstance(declaringClass,
MethodHandles.Lookup.PRIVATE).in(declaringClass);
+ } catch (final IllegalAccessException | InstantiationException e) {
+ throw new IllegalArgumentException(e);
+ } catch (final InvocationTargetException e) {
+ throw new GroovyRuntimeException(e);
+ }
+ }
+
private static class LookupHolder {
private static final Constructor<MethodHandles.Lookup>
LOOKUP_Constructor;
static {
- Constructor<MethodHandles.Lookup> con;
+ Constructor<MethodHandles.Lookup> lookup;
try {
- con =
MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
- } catch (NoSuchMethodException e) {
- throw new GroovyBugError(e);
+ lookup =
MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE);
+ } catch (final NoSuchMethodException ex) {
+ throw new IllegalStateException("Incompatible JVM", ex);
}
try {
- if (!con.isAccessible()) {
- final Constructor tmp = con;
+ if (!lookup.isAccessible()) {
+ final Constructor tmp = lookup;
AccessController.doPrivileged((PrivilegedAction<Object>)
() -> {
ReflectionUtils.trySetAccessible(tmp);
return null;
});
}
- } catch (SecurityException se) {
- con = null;
+ } catch (SecurityException ignore) {
+ lookup = null;
} catch (RuntimeException re) {
- // test for JDK9 JIGSAW
- if
(!"java.lang.reflect.InaccessibleObjectException".equals(re.getClass().getName()))
throw re;
- con = null;
+ throw re;
}
- LOOKUP_Constructor = con;
+ LOOKUP_Constructor = lookup;
}
}
}
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 9249449..a004a67 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
@@ -188,16 +188,6 @@ public class Java9 extends Java8 {
return 9;
}
- @Override
- public Object getInvokeSpecialHandle(Method method, Object receiver) {
- final Class<?> receiverType = receiver.getClass();
- try {
- return of(receiverType).unreflectSpecial(method,
receiverType).bindTo(receiver);
- } catch (ReflectiveOperationException e) {
- return super.getInvokeSpecialHandle(method, receiver);
- }
- }
-
/**
* This method may be used by a caller in class C to check whether to
enable access to a member of declaring class D successfully
* if {@link
Java8#checkCanSetAccessible(java.lang.reflect.AccessibleObject,
java.lang.Class)} returns true and any of the following hold: