Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NativeImageUtil method name and expose new getter #4063

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@
* @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 = isInNativeImage();

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
*<p>
* NOTE: {@code public} since 2.16 (before that, {@code private}).
* As optimization, {@link #RUNNING_IN_SVM} is used to short-circuit on normal JVMs.
*/
public static boolean isRunningInNativeImage() {
private static boolean isInNativeImageAndIsAtRuntime() {
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
return RUNNING_IN_SVM && "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"));
}

/**
* Checks whether we're running in SubstrateVM native image <b>only by the presence</b> 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.
*<p>
* @since 2.16
*/
public static boolean isInNativeImage() {
return System.getProperty("org.graalvm.nativeimage.imagecode") != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check can be inlined into RUNNING_IN_SVM, because it doesnt care whether the value is buildtime or runtime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And still keep this method, right? Using in FasterXML/jackson-modules-base#217

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so this should just return RUNNING_IN_SVM, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

}

/**
* 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) {
Expand All @@ -47,7 +55,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
Expand Down