openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject [openwebbeans] branch master updated: OWB-1306 on master java, sun.mic.Unsafe does not exist so grab it where it is
Date Thu, 12 Dec 2019 09:25:06 GMT
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 c0d32f6  OWB-1306 on master java, sun.mic.Unsafe does not exist so grab it where
it is
c0d32f6 is described below

commit c0d32f61906a1c0efe24159004c0d6edbaceda77
Author: Romain Manni-Bucau <rmannibucau@gmail.com>
AuthorDate: Thu Dec 12 10:25:00 2019 +0100

    OWB-1306 on master java, sun.mic.Unsafe does not exist so grab it where it is
---
 .../java/org/apache/webbeans/proxy/Unsafe.java     | 53 ++++++++++++----------
 1 file changed, 28 insertions(+), 25 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 8df2a12..2085883 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.Stream;
 
 import org.apache.webbeans.exception.ProxyGenerationException;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
@@ -104,13 +105,16 @@ public class Unsafe
                 try // some j>8, since we have unsafe let's use it
                 {
                     final Class<?> rootLoaderClass = Class.forName("java.lang.ClassLoader");
-                    final sun.misc.Unsafe un = sun.misc.Unsafe.class.cast(unsafe);
-                    final long accOffset = un.objectFieldOffset(AccessibleObject.class.getDeclaredField("override"));
-
-                    un.putBoolean(rootLoaderClass.getDeclaredMethod("defineClass",
-                            new Class[]{String.class, byte[].class, int.class, int.class}),
accOffset, true);
-                    un.putBoolean(rootLoaderClass.getDeclaredMethod("defineClass",
-                            new Class[]{String.class, byte[].class, int.class, int.class,
ProtectionDomain.class}),
+                    final Method objectFieldOffset = unsafe.getClass().getDeclaredMethod("objectFieldOffset",
Field.class);
+                    final Method putBoolean = unsafe.getClass().getDeclaredMethod("putBoolean",
Object.class, long.class, boolean.class);
+                    objectFieldOffset.setAccessible(true);
+                    final long accOffset = Long.class.cast(objectFieldOffset.invoke(unsafe,
AccessibleObject.class.getDeclaredField("override")));
+                    putBoolean.invoke(unsafe, rootLoaderClass.getDeclaredMethod("defineClass",
+                            new Class[]{String.class, byte[].class, int.class, int.class}),
+                            accOffset, true);
+                    putBoolean.invoke(unsafe, rootLoaderClass
+                                    .getDeclaredMethod("defineClass", new Class[]{String.class,
byte[].class,
+                                            int.class, int.class, ProtectionDomain.class}),
                             accOffset, true);
                 }
                 catch (final Exception ex)
@@ -255,27 +259,26 @@ public class Unsafe
     {
         try
         {
-            return AccessController.doPrivileged((PrivilegedAction<Class<?>>)
() -> {
-                try
-                {
-                    return Thread.currentThread().getContextClassLoader().loadClass("sun.misc.Unsafe");
-                }
-                catch (Exception e)
-                {
-                    try
-                    {
-                        return ClassLoader.getSystemClassLoader().loadClass("sun.misc.Unsafe");
-                    }
-                    catch (ClassNotFoundException e1)
-                    {
-                        throw new IllegalStateException("Cannot get sun.misc.Unsafe", e);
-                    }
-                }
-            });
+            return AccessController.doPrivileged((PrivilegedAction<Class<?>>)
() ->
+                    Stream.of(Thread.currentThread().getContextClassLoader(), ClassLoader.getSystemClassLoader())
+                            .flatMap(classloader -> Stream.of("sun.misc.Unsafe", "jdk.internal.misc.Unsafe")
+                            .flatMap(name ->
+                            {
+                                try
+                                {
+                                    return Stream.of(classloader.loadClass(name));
+                                }
+                                catch (final ClassNotFoundException e)
+                                {
+                                    return Stream.empty();
+                                }
+                            }))
+                            .findFirst()
+                            .orElseThrow(() -> new IllegalStateException("Cannot get Unsafe")));
         }
         catch (final Exception e)
         {
-            throw new IllegalStateException("Cannot get sun.misc.Unsafe class", e);
+            throw new IllegalStateException("Cannot get Unsafe class", e);
         }
     }
 }


Mime
View raw message