From commits-return-8580-archive-asf-public=cust-asf.ponee.io@openwebbeans.apache.org Thu Dec 12 09:25:08 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id A151E18061A for ; Thu, 12 Dec 2019 10:25:07 +0100 (CET) Received: (qmail 41090 invoked by uid 500); 12 Dec 2019 09:25:07 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 41078 invoked by uid 99); 12 Dec 2019 09:25:07 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Dec 2019 09:25:06 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id D4A828D80D; Thu, 12 Dec 2019 09:25:06 +0000 (UTC) Date: Thu, 12 Dec 2019 09:25:06 +0000 To: "commits@openwebbeans.apache.org" Subject: [openwebbeans] branch master updated: OWB-1306 on master java, sun.mic.Unsafe does not exist so grab it where it is MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <157614270681.24657.11223193603031520242@gitbox.apache.org> From: rmannibucau@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: openwebbeans X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: 26aaec0274c994336f9bca30b4609065ec323864 X-Git-Newrev: c0d32f61906a1c0efe24159004c0d6edbaceda77 X-Git-Rev: c0d32f61906a1c0efe24159004c0d6edbaceda77 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated 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 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>) () -> { - 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>) () -> + 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); } } }