Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,14 +2,20 @@

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.util.NativeImageUtil;
import com.fasterxml.jackson.module.afterburner.ser.SerializerModifier;
import com.fasterxml.jackson.module.afterburner.deser.DeserializerModifier;

public class AfterburnerModule extends Module
implements java.io.Serializable // is this necessary?
{
// TODO: replace with jackson-databind/NativeImageUtil.RUNNING_IN_SVM
private static final boolean RUNNING_IN_SVM = System.getProperty("org.graalvm.nativeimage.imagecode") != null;
/**
* <a href="https://github.com/FasterXML/jackson-modules-base/issues/191">[modules-base#191] Native image detection</a>
*
* @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.


private static final long serialVersionUID = 1L;

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.module.blackbird;

import com.fasterxml.jackson.databind.util.NativeImageUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.util.function.Function;
Expand All @@ -12,8 +13,13 @@

public class BlackbirdModule extends Module
{
// TODO: replace with jackson-databind/NativeImageUtil.RUNNING_IN_SVM
private static final boolean RUNNING_IN_SVM = System.getProperty("org.graalvm.nativeimage.imagecode") != null;
/**
* <a href="https://github.com/FasterXML/jackson-modules-base/issues/191">[modules-base#191] Native image detection</a>
*
* @since 2.16
*/
private static final boolean RUNNING_IN_SVM = NativeImageUtil.isRunningInNativeImage();

private Function<Class<?>, Lookup> _lookups;

public BlackbirdModule() {
Expand Down