From a85c687d6d6f8bb626444464a0558a2dd151e5f1 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 17 Jun 2021 20:13:22 +0300 Subject: [PATCH] Use the GraalVM API instead of flag to force build-time init Relates to: #17887 --- .../deployment/pkg/steps/NativeImageBuildStep.java | 1 - .../steps/NativeImageAutoFeatureStep.java | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java index f7f4b2bb6b95c..2a60fbdc8d2bd 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java @@ -553,7 +553,6 @@ public NativeImageInvokerInfo build() { } handleAdditionalProperties(nativeConfig, nativeImageArgs, isContainerBuild, outputDir); - nativeImageArgs.add("--initialize-at-build-time="); nativeImageArgs.add( "-H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy$BySpaceAndTime"); //the default collection policy results in full GC's 50% of the time nativeImageArgs.add("-H:+JNI"); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java index d0dd44c9052d2..97b3170ba62b2 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java @@ -51,6 +51,9 @@ public class NativeImageAutoFeatureStep { private static final String GRAAL_AUTOFEATURE = "io/quarkus/runner/AutoFeature"; private static final MethodDescriptor IMAGE_SINGLETONS_LOOKUP = ofMethod(ImageSingletons.class, "lookup", Object.class, Class.class); + private static final MethodDescriptor BUILD_TIME_INITIALIZATION = ofMethod( + "org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport", + "initializeAtBuildTime", void.class, String.class, String.class); private static final MethodDescriptor INITIALIZE_CLASSES_AT_RUN_TIME = ofMethod(RuntimeClassInitialization.class, "initializeAtRunTime", void.class, Class[].class); private static final MethodDescriptor INITIALIZE_PACKAGES_AT_RUN_TIME = ofMethod(RuntimeClassInitialization.class, @@ -117,6 +120,13 @@ public void write(String s, byte[] bytes) { cc.invokeVirtualMethod(ofMethod(Throwable.class, "printStackTrace", void.class), cc.getCaughtException()); } + ResultHandle imageSingleton = overallCatch.invokeStaticMethod(IMAGE_SINGLETONS_LOOKUP, + overallCatch.loadClass("org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport")); + overallCatch.invokeInterfaceMethod(BUILD_TIME_INITIALIZATION, + imageSingleton, + overallCatch.load(""), // empty string means everything + overallCatch.load("Quarkus build time init default")); + if (!runtimeInitializedClassBuildItems.isEmpty()) { ResultHandle thisClass = overallCatch.loadClass(GRAAL_AUTOFEATURE); ResultHandle cl = overallCatch.invokeVirtualMethod(ofMethod(Class.class, "getClassLoader", ClassLoader.class), @@ -153,15 +163,13 @@ public void write(String s, byte[] bytes) { ResultHandle thisClass = overallCatch.loadClass(GRAAL_AUTOFEATURE); ResultHandle cl = overallCatch.invokeVirtualMethod(ofMethod(Class.class, "getClassLoader", ClassLoader.class), thisClass); - ResultHandle initSingleton = overallCatch.invokeStaticMethod(IMAGE_SINGLETONS_LOOKUP, - overallCatch.loadClass("org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport")); ResultHandle quarkus = overallCatch.load("Quarkus"); for (RuntimeReinitializedClassBuildItem runtimeReinitializedClass : runtimeReinitializedClassBuildItems) { TryBlock tc = overallCatch.tryBlock(); ResultHandle clazz = tc.invokeStaticMethod( ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class), tc.load(runtimeReinitializedClass.getClassName()), tc.load(false), cl); - tc.invokeInterfaceMethod(RERUN_INITIALIZATION, initSingleton, clazz, quarkus); + tc.invokeInterfaceMethod(RERUN_INITIALIZATION, imageSingleton, clazz, quarkus); CatchBlockCreator cc = tc.addCatch(Throwable.class); cc.invokeVirtualMethod(ofMethod(Throwable.class, "printStackTrace", void.class), cc.getCaughtException());