From 3085035a25f089bc5de9254f6905c47fc844d004 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 24 Sep 2019 15:33:59 +0200 Subject: [PATCH] [jOOQ/jOOR#57] [jOOQ/jOOQ#9157] Prevent 'Illegal reflective access operation' warning in jOOQ Open Source Edition --- .../java/org/jooq/tools/reflect/Reflect.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java b/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java index 56105979ad..fe4de8fc82 100644 --- a/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java +++ b/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java @@ -25,6 +25,7 @@ import java.lang.reflect.Proxy; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; /** * A wrapper for an {@link Object} or {@link Class} upon which reflective calls @@ -295,10 +296,20 @@ public class Reflect { try { - result = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class); + try { + Optional.class.getMethod("stream"); + result = null; + } - if (!result.isAccessible()) - result.setAccessible(true); + // [jOOQ/jOOR#57] [jOOQ/jOOQ#9157] + // A JDK 9 guard that prevents "Illegal reflective access operation" + // warnings when running the below on JDK 9+ + catch (NoSuchMethodException e) { + result = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class); + + if (!result.isAccessible()) + result.setAccessible(true); + } } // Can no longer access the above in JDK 9 @@ -723,15 +734,15 @@ public class Reflect { } /** - * Create a proxy for the wrapped object allowing to typesafely invoke methods - * on it using a custom interface. - * - * @param proxyType The interface type that is implemented by the - * proxy - * @param additionalInterfaces Additional interfaces that are implemented by the - * proxy - * @return A proxy for the wrapped object - */ + * Create a proxy for the wrapped object allowing to typesafely invoke methods + * on it using a custom interface. + * + * @param proxyType The interface type that is implemented by the + * proxy + * @param additionalInterfaces Additional interfaces that are implemented by the + * proxy + * @return A proxy for the wrapped object + */ @SuppressWarnings("unchecked") public

P as(final Class

proxyType, final Class... additionalInterfaces) { final boolean isMap = (object instanceof Map);