diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java index b624b2ae17..500478b889 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java @@ -54,6 +54,11 @@ final class PlatformDependent0 { private static final Throwable UNSAFE_UNAVAILABILITY_CAUSE; private static final boolean IS_EXPLICIT_TRY_REFLECTION_SET_ACCESSIBLE = explicitTryReflectionSetAccessible0(); + // See https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ + // ImageInfo.java + private static final boolean RUNNING_IN_NATIVE_IMAGE = SystemPropertyUtil.contains( + "org.graalvm.nativeimage.imagecode"); + static final Unsafe UNSAFE; // constants borrowed from murmur3 @@ -254,7 +259,7 @@ final class PlatformDependent0 { Class bitsClass = Class.forName("java.nio.Bits", false, getSystemClassLoader()); int version = javaVersion(); - if (version >= 9) { + if (unsafeStaticFieldOffsetSupported() && version >= 9) { // Java9/10 use all lowercase and later versions all uppercase. String fieldName = version >= 11 ? "UNALIGNED" : "unaligned"; // On Java9 and later we try to directly access the field as we can do this without @@ -352,6 +357,10 @@ final class PlatformDependent0 { DIRECT_BUFFER_CONSTRUCTOR_HANDLE != null ? "available" : "unavailable"); } + private static boolean unsafeStaticFieldOffsetSupported() { + return !RUNNING_IN_NATIVE_IMAGE; + } + static boolean isExplicitNoUnsafe() { return EXPLICIT_NO_UNSAFE_CAUSE != null; }