diff options
author | Andrej Shadura <andrewsh@debian.org> | 2019-02-27 17:03:02 +0100 |
---|---|---|
committer | Andrej Shadura <andrewsh@debian.org> | 2019-02-28 17:05:58 +0100 |
commit | 2a3ba5e626e000f190c519cfc96ab62bb13b5309 (patch) | |
tree | d4c62e8ba03981659d6e7a0a507668b37718f389 | |
parent | 3b416fa88fcceca6aa7611ebd798c30299effe5e (diff) |
Fixes the compatibility with Java 11
Gbp-Pq: Name java11-compatiblitily.patch
-rwxr-xr-x | pom.xml | 8 | ||||
-rw-r--r-- | src/main/java/net/openhft/chronicle/core/ClassLoading.java | 6 | ||||
-rw-r--r-- | src/main/java/net/openhft/chronicle/core/UnsafeMemory.java | 12 | ||||
-rw-r--r-- | src/main/java/net/openhft/chronicle/core/io/IOTools.java | 5 |
4 files changed, 25 insertions, 6 deletions
@@ -105,8 +105,12 @@ <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgument>-Xlint:deprecation</compilerArgument> - <source>1.8</source> - <target>1.8</target> + <source>1.9</source> + <target>1.9</target> + <compilerArgs> + <arg>--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED</arg> + <arg>--add-exports=java.base/sun.nio.ch=ALL-UNNAMED</arg> + </compilerArgs> </configuration> </plugin> <!-- diff --git a/src/main/java/net/openhft/chronicle/core/ClassLoading.java b/src/main/java/net/openhft/chronicle/core/ClassLoading.java index 5465e63..d6744d2 100644 --- a/src/main/java/net/openhft/chronicle/core/ClassLoading.java +++ b/src/main/java/net/openhft/chronicle/core/ClassLoading.java @@ -41,6 +41,10 @@ public enum ClassLoading { * @return the class loaded. */ private static Class defineClass(ClassLoader classLoader, String className, byte[] bytes) { - return UnsafeMemory.UNSAFE.defineClass(className, bytes, 0, bytes.length, classLoader, null); + try { + return (Class) UnsafeMemory.defineClassMethodHandle.bindTo(classLoader).invokeWithArguments(className, bytes, 0, bytes.length); + } catch (Throwable e) { + throw new RuntimeException(e); + } } } diff --git a/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java b/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java index d7192d9..a6030e2 100644 --- a/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java +++ b/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java @@ -18,12 +18,16 @@ package net.openhft.chronicle.core; import net.openhft.chronicle.core.annotation.ForceInline; import sun.misc.Unsafe; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.reflect.Field; import java.util.concurrent.atomic.AtomicLong; public class UnsafeMemory implements Memory { static Unsafe UNSAFE; + static MethodHandle defineClassMethodHandle; private final AtomicLong nativeMemoryUsed = new AtomicLong(); public static Memory create() { @@ -34,6 +38,14 @@ public class UnsafeMemory implements Memory { } catch (IllegalAccessException | IllegalArgumentException e) { throw new AssertionError(e); } + try { + MethodHandles.Lookup baseLookup = MethodHandles.lookup(); + MethodType defineClassMethodType = MethodType.methodType(Class.class, new Class[]{String.class, byte[].class, int.class, int.class}); + MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(ClassLoader.class, baseLookup); + defineClassMethodHandle = lookup.findVirtual(ClassLoader.class, "defineClass", defineClassMethodType); + } catch (Throwable e) { + throw new RuntimeException(e); + } } return new UnsafeMemory(); } diff --git a/src/main/java/net/openhft/chronicle/core/io/IOTools.java b/src/main/java/net/openhft/chronicle/core/io/IOTools.java index 2b8fc78..30aec3a 100644 --- a/src/main/java/net/openhft/chronicle/core/io/IOTools.java +++ b/src/main/java/net/openhft/chronicle/core/io/IOTools.java @@ -16,8 +16,6 @@ package net.openhft.chronicle.core.io; -import sun.reflect.Reflection; - import java.io.*; /** @@ -29,7 +27,8 @@ public enum IOTools { public static byte[] readFile(String name) throws IOException { ClassLoader classLoader; try { - classLoader = Reflection.getCallerClass().getClassLoader(); + StackWalker instance = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + classLoader = instance.getCallerClass().getClassLoader(); } catch (Throwable e) { classLoader = Thread.currentThread().getContextClassLoader(); } |