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
  • Loading branch information
zakkak committed Oct 21, 2024
1 parent a7f0966 commit b95789b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 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
@@ -1,7 +1,6 @@
package io.quarkus.awt.deployment;

import static io.quarkus.deployment.builditem.nativeimage.UnsupportedOSBuildItem.Os.WINDOWS;
import static io.quarkus.deployment.pkg.steps.GraalVM.Version.CURRENT;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,6 +9,7 @@

import org.jboss.logging.Logger;

import io.quarkus.awt.runtime.graal.AwtFeature;
import io.quarkus.awt.runtime.graal.DarwinAwtFeature;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -26,9 +26,7 @@
import io.quarkus.deployment.pkg.builditem.NativeImageRunnerBuildItem;
import io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabled;
import io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabledBuildItem;
import io.quarkus.deployment.pkg.steps.GraalVM;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.deployment.pkg.steps.NoopNativeImageBuildRunner;

class AwtProcessor {

Expand All @@ -42,6 +40,7 @@ FeatureBuildItem feature() {
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void nativeImageFeatures(BuildProducer<NativeImageFeatureBuildItem> nativeImageFeatures) {
nativeImageFeatures.produce(new NativeImageFeatureBuildItem(DarwinAwtFeature.class));
nativeImageFeatures.produce(new NativeImageFeatureBuildItem(AwtFeature.class));
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
Expand Down Expand Up @@ -128,12 +127,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 +251,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
@@ -0,0 +1,24 @@
package io.quarkus.awt.runtime.graal;

import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;

import io.quarkus.runtime.util.JavaVersionUtil;

public class AwtFeature implements Feature {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
// Added for JDK 19+ due to: https://github.com/openjdk/jdk20/commit/9bc023220 calling FontUtilities
if (JavaVersionUtil.isJava19OrHigher()) {
try {
Class<?> fontUtilitiesClass = Class.forName("sun.font.FontUtilities");
RuntimeJNIAccess.register(fontUtilitiesClass);
RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredFields());
RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredMethods());
RuntimeJNIAccess.register(fontUtilitiesClass.getDeclaredConstructors());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit b95789b

Please sign in to comment.