Skip to content

Commit

Permalink
Avoid getting the GraalVM version at deployment time for AWT
Browse files Browse the repository at this point in the history
Reduces the times we start a container when using a builder-image
  • Loading branch information
zakkak committed Oct 11, 2024
1 parent aea9c27 commit ab9bb13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class JavaVersionUtil {
private static boolean IS_GRAALVM_JDK;
private static boolean IS_JAVA_16_OR_OLDER;
private static boolean IS_JAVA_17_OR_NEWER;
private static boolean IS_JAVA_19_OR_NEWER;

static {
performChecks();
Expand All @@ -26,11 +27,13 @@ static void performChecks() {
IS_JAVA_13_OR_NEWER = (first >= 13);
IS_JAVA_16_OR_OLDER = (first <= 16);
IS_JAVA_17_OR_NEWER = (first >= 17);
IS_JAVA_19_OR_NEWER = (first >= 19);
} else {
IS_JAVA_11_OR_NEWER = false;
IS_JAVA_13_OR_NEWER = false;
IS_JAVA_16_OR_OLDER = false;
IS_JAVA_17_OR_NEWER = false;
IS_JAVA_19_OR_NEWER = false;
}

String vmVendor = System.getProperty("java.vm.vendor");
Expand All @@ -53,6 +56,10 @@ public static boolean isJava17OrHigher() {
return IS_JAVA_17_OR_NEWER;
}

public static boolean isJava19OrHigher() {
return IS_JAVA_19_OR_NEWER;
}

public static boolean isGraalvmJdk() {
return IS_GRAALVM_JDK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ JniRuntimeAccessBuildItem setupJava2DClasses(NativeImageRunnerBuildItem nativeIm
Optional<ProcessInheritIODisabledBuildItem> processInheritIODisabledBuildItem) {
nativeImageRunnerBuildItem.getBuildRunner()
.setup(processInheritIODisabled.isPresent() || processInheritIODisabledBuildItem.isPresent());
final GraalVM.Version v;
if (nativeImageRunnerBuildItem.getBuildRunner() instanceof NoopNativeImageBuildRunner) {
v = CURRENT;
} else {
v = nativeImageRunnerBuildItem.getBuildRunner().getGraalVMVersion();
}
final List<String> classes = new ArrayList<>();
classes.add("com.sun.imageio.plugins.jpeg.JPEGImageReader");
classes.add("com.sun.imageio.plugins.jpeg.JPEGImageWriter");
Expand Down Expand Up @@ -258,18 +252,11 @@ JniRuntimeAccessBuildItem setupJava2DClasses(NativeImageRunnerBuildItem nativeIm
// A new way of dynamically loading shared objects instead
// of baking in static libs: https://github.com/oracle/graal/issues/4921
classes.add("sun.awt.X11FontManager");
if (v.javaVersion.feature() != 19) {
classes.add("java.awt.GraphicsEnvironment");
classes.add("sun.awt.X11GraphicsConfig");
classes.add("sun.awt.X11GraphicsDevice");
classes.add("sun.java2d.SunGraphicsEnvironment");
classes.add("sun.java2d.xr.XRSurfaceData");
}

// Added for JDK 19+ due to: https://github.com/openjdk/jdk20/commit/9bc023220 calling FontUtilities
if (v.jdkVersionGreaterOrEqualTo("19")) {
classes.add("sun.font.FontUtilities");
}
classes.add("java.awt.GraphicsEnvironment");
classes.add("sun.awt.X11GraphicsConfig");
classes.add("sun.awt.X11GraphicsDevice");
classes.add("sun.java2d.SunGraphicsEnvironment");
classes.add("sun.java2d.xr.XRSurfaceData");

return new JniRuntimeAccessBuildItem(true, true, true, classes.toArray(new String[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;

import io.quarkus.runtime.util.JavaVersionUtil;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;

@Platforms({ Platform.DARWIN_AMD64.class, Platform.DARWIN_AARCH64.class })
public class DarwinAwtFeature implements Feature {
Expand All @@ -15,5 +16,14 @@ public void afterRegistration(AfterRegistrationAccess access) {
// Quarkus run time init for AWT in Darwin
RuntimeClassInitialization.initializeAtRunTime("sun.lwawt.macosx");
}

// Added for JDK 19+ due to: https://github.com/openjdk/jdk20/commit/9bc023220 calling FontUtilities
if (JavaVersionUtil.isJava19OrHigher()) {
try {
RuntimeJNIAccess.register(Class.forName("sun.font.FontUtilities"));
} catch (ClassNotFoundException e) {
throw new RuntimeException("Failed to register FontUtilities for JNI Access", e);
}
}
}
}

0 comments on commit ab9bb13

Please sign in to comment.