From f4b3363be4767030c7b7079e86f00fdbfec28070 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 1 Aug 2023 23:51:53 +0900 Subject: [PATCH 1/5] Update `NativeImageUtil` and expose getter for SVM --- .../databind/util/NativeImageUtil.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java index f3683ad44a..d38389a56c 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java @@ -9,31 +9,41 @@ * @since 2.14 */ public class NativeImageUtil { - private static final boolean RUNNING_IN_SVM; - - static { - RUNNING_IN_SVM = System.getProperty("org.graalvm.nativeimage.imagecode") != null; - } + + private static final boolean RUNNING_IN_SVM = isInSVM(); private NativeImageUtil() { } /** - * Check whether we're running in substratevm native image runtime mode. - * This check cannot be a constant, because + * Check whether we're running in SubstrateVM native image and also in "runtime" mode. + * The "runtime" check cannot be a constant, because * the static initializer may run early during build time *

+ * As optimization, {@link #RUNNING_IN_SVM} is used to short-circuit on normal JVMs. + *

* NOTE: {@code public} since 2.16 (before that, {@code private}). */ - public static boolean isRunningInNativeImage() { + public static boolean isInNativeImageAndIsAtRuntime() { return RUNNING_IN_SVM && "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode")); } + /** + * Checks whether we're running in SubstrateVM native image only by the presence of + * {@code "org.graalvm.nativeimage.imagecode"} system property, regardless of its value (buildtime or runtime). + * We are irrespective of the build or runtime phase, because native-image can initialize static initializers at build time. + *

+ * @since 2.16 + */ + public static boolean isInSVM() { + return System.getProperty("org.graalvm.nativeimage.imagecode") != null; + } + /** * Check whether the given error is a substratevm UnsupportedFeatureError */ public static boolean isUnsupportedFeatureError(Throwable e) { - if (!isRunningInNativeImage()) { + if (!isInNativeImageAndIsAtRuntime()) { return false; } if (e instanceof InvocationTargetException) { @@ -47,7 +57,7 @@ public static boolean isUnsupportedFeatureError(Throwable e) { * members visible in reflection). */ public static boolean needsReflectionConfiguration(Class cl) { - if (!isRunningInNativeImage()) { + if (!isInNativeImageAndIsAtRuntime()) { return false; } // records list their fields but not other members From 381e460bca5ed98440f99b7e8ba5af883c1cba2b Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 2 Aug 2023 00:01:14 +0900 Subject: [PATCH 2/5] Make `isInNativeImageAndIsAtRuntime()` check private again --- .../com/fasterxml/jackson/databind/util/NativeImageUtil.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java index d38389a56c..a29bde8f13 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java @@ -21,10 +21,8 @@ private NativeImageUtil() { * the static initializer may run early during build time *

* As optimization, {@link #RUNNING_IN_SVM} is used to short-circuit on normal JVMs. - *

- * NOTE: {@code public} since 2.16 (before that, {@code private}). */ - public static boolean isInNativeImageAndIsAtRuntime() { + private static boolean isInNativeImageAndIsAtRuntime() { return RUNNING_IN_SVM && "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode")); } From b32bd6e523fba20068f8233eb1d465912b545947 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 2 Aug 2023 00:10:43 +0900 Subject: [PATCH 3/5] Update NativeImageUtil.java --- .../com/fasterxml/jackson/databind/util/NativeImageUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java index a29bde8f13..ca4deaf0f3 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java @@ -10,7 +10,7 @@ */ public class NativeImageUtil { - private static final boolean RUNNING_IN_SVM = isInSVM(); + private static final boolean RUNNING_IN_SVM = isInNativeImage(); private NativeImageUtil() { } @@ -33,7 +33,7 @@ private static boolean isInNativeImageAndIsAtRuntime() { *

* @since 2.16 */ - public static boolean isInSVM() { + public static boolean isInNativeImage() { return System.getProperty("org.graalvm.nativeimage.imagecode") != null; } From 3768fd74e3bf24621359226b54e2fe938e295943 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 2 Aug 2023 06:38:47 +0900 Subject: [PATCH 4/5] Update NativeImageUtil.java --- .../com/fasterxml/jackson/databind/util/NativeImageUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java index ca4deaf0f3..4cead1f048 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java @@ -10,7 +10,7 @@ */ public class NativeImageUtil { - private static final boolean RUNNING_IN_SVM = isInNativeImage(); + private static final boolean RUNNING_IN_SVM = System.getProperty("org.graalvm.nativeimage.imagecode") != null; private NativeImageUtil() { } From 292c5bacebe066a5cfe49fbb5c07855977d963e2 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 2 Aug 2023 10:50:36 +0900 Subject: [PATCH 5/5] Update NativeImageUtil.java --- .../com/fasterxml/jackson/databind/util/NativeImageUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java index 4cead1f048..fef5d80633 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/NativeImageUtil.java @@ -34,7 +34,7 @@ private static boolean isInNativeImageAndIsAtRuntime() { * @since 2.16 */ public static boolean isInNativeImage() { - return System.getProperty("org.graalvm.nativeimage.imagecode") != null; + return RUNNING_IN_SVM; } /**