This is an automated email from the ASF dual-hosted git repository. rmannibucau pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push: new 8ee9f5f [OWB-1376] fix unsafe usage to define classes 8ee9f5f is described below commit 8ee9f5fb288131444aeb880fad24cc99f86a9091 Author: Romain Manni-Bucau <rmannibu...@gmail.com> AuthorDate: Sat Mar 13 21:04:32 2021 +0100 [OWB-1376] fix unsafe usage to define classes --- .../java/org/apache/webbeans/proxy/Unsafe.java | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/proxy/Unsafe.java b/webbeans-impl/src/main/java/org/apache/webbeans/proxy/Unsafe.java index 2085883..9beb112 100644 --- a/webbeans-impl/src/main/java/org/apache/webbeans/proxy/Unsafe.java +++ b/webbeans-impl/src/main/java/org/apache/webbeans/proxy/Unsafe.java @@ -26,6 +26,7 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.ProtectionDomain; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.IntStream; import java.util.stream.Stream; import org.apache.webbeans.exception.ProxyGenerationException; @@ -38,8 +39,8 @@ public class Unsafe * We use it for creating the proxy instance without fully * initializing the class. */ - private Object unsafe; - private Object internalUnsafe; + private final Object unsafe; + private final Object internalUnsafe; private Method unsafeAllocateInstance; private final AtomicReference<Method> unsafeDefineClass = new AtomicReference<>(); @@ -66,7 +67,7 @@ public class Unsafe { final Field theInternalUnsafe = unsafeClass.getDeclaredField("theInternalUnsafe"); theInternalUnsafe.setAccessible(true); - return theInternalUnsafe.get(null).getClass(); + return theInternalUnsafe.get(null); } catch (final Exception notJ11OrMore) { @@ -172,7 +173,7 @@ public class Unsafe else { - definedClass = (Class<T>) unsafeDefineClass().invoke(unsafe, proxyName, proxyBytes, 0, proxyBytes.length, classLoader, null); + definedClass = (Class<T>) unsafeDefineClass().invoke(internalUnsafe, proxyName, proxyBytes, 0, proxyBytes.length, classLoader, null); } return (Class<T>) Class.forName(definedClass.getName(), true, classLoader); @@ -190,7 +191,10 @@ public class Unsafe // default error handling } } - throw new ProxyGenerationException(le.getCause()); + throw new ProxyGenerationException( + le.getMessage() + (isJava16OrMore() ? "\n" + + "On Java 16 ensure to set --add-exports java.base/jdk.internal.misc=ALL-UNNAMED on the JVM" : ""), + le.getCause()); } catch (Throwable e) { @@ -198,6 +202,23 @@ public class Unsafe } } + private boolean isJava16OrMore() + { + final String version = System.getProperty("java.version", "-1"); + final int end = IntStream.of(version.indexOf('-'), version.indexOf('.')) + .filter(i -> i > 0) + .min() + .orElseGet(version::length); + try + { + return Integer.parseInt(version.substring(0, end)) >= 16; + } + catch (final NumberFormatException nfe) + { + return false; + } + } + private Method unsafeDefineClass() { Method value = unsafeDefineClass.get();