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

Follow up wrt #191 by using NativeImageUtil.isRunningInNativeImage() in databind #217

Merged

Conversation

JooHyukKim
Copy link
Member

Motivation

Seems like the TODO: was written before exposing NativeImageUtil.isRunningInNativeImage() in databind by FasterXML/jackson-databind#4060

@JooHyukKim
Copy link
Member Author

JooHyukKim commented Jul 31, 2023

@stevenschlansker Could you please take a look? Just to make sure :)

*
* @since 2.16
*/
private static final boolean RUNNING_IN_SVM = NativeImageUtil.isRunningInNativeImage();
Copy link
Member

Choose a reason for hiding this comment

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

Not sure this can be done, since method Javadoc states:

     * This check cannot be a constant, because
     * the static initializer may run early during build time

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. I missed the JavaDoc. Maybe we can instead inline the check inside ‘setupModule()’?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think inlining would make sense: it will only be called once anyway.

Copy link
Member Author

@JooHyukKim JooHyukKim Aug 1, 2023

Choose a reason for hiding this comment

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

Hmmmm strange, the doc says the check cannot be constant, but with current jackson-databind version, the check is still partly static. If we look at the RUNNING_IN_SVM in below, it is static final, so we can't use that method (according to JavaDoc).

image

Copy link
Member Author

@JooHyukKim JooHyukKim Aug 1, 2023

Choose a reason for hiding this comment

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

For other modules to use NativeImageUtil.isRunningInNativeImage(), what I would expect is

public class NativeImageUtil {
   
    private static final boolean RUNNING_IN_SVM = isRunningInNativeImage();

    /**
     * Check whether we're running in substratevm native image runtime mode.
     * This 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}).
     */
    public static boolean isRunningInNativeImage() {
        return System.getProperty("org.graalvm.nativeimage.imagecode") != null && "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"));
    }
   // ...rest omitted

.... as such so that actual check is done at call time. 🤔 Sorry if I misunderstood the whole mechanism though.

Copy link
Member

Choose a reason for hiding this comment

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

as discussed in #216, isRunningInNativeImage() should not be used here. There needs to be a new getter for RUNNING_IN_SVM that can be used instead.

Copy link
Member

Choose a reason for hiding this comment

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

maybe isRunningInNativeImage() should be renamed also. i did not think enough about the name when i wrote it, since it wasn't public api. now that it is, a better name would be appropriate.

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe isRunningInNativeImage() should be renamed also. i did not think enough about the name when i wrote it, since it wasn't public api. now that it is, a better name would be appropriate.

+1. Method names are also subject to change.

Copy link
Member Author

@JooHyukKim JooHyukKim Aug 1, 2023

Choose a reason for hiding this comment

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

So I....

  1. Made a PR in Update NativeImageUtil method name and expose new getter jackson-databind#4063.
  2. Adopted new getter early (CI will temporarily fail)

Copy link
Member

Choose a reason for hiding this comment

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

Since the method was non-public before 2.16, it's fine to change the name I think.

@@ -51,7 +50,8 @@ public AfterburnerModule() { }
@Override
public void setupModule(SetupContext context)
{
if (RUNNING_IN_SVM)
// [modules-base#191] Since 2.16, Native image detection
if (NativeImageUtil.isInSVM())
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you missed updating this line with the new name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, thank you! @stevenschlansker 🙏🏽🙏🏽

@cowtowncoder cowtowncoder merged commit 758b700 into FasterXML:2.16 Aug 3, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants