Skip to content

Commit

Permalink
Merge pull request #44650 from zakkak/2024-11-22-register-code-reflec…
Browse files Browse the repository at this point in the history
…tively-accessed-by-Console

Remove `jdk.internal.io.JdkConsoleProvider` service providers from `ServiceCatalog` in a similar way to GraalVM's `ServiceLoaderFeature` which Quarkus disables by default
  • Loading branch information
zakkak authored Nov 27, 2024
2 parents 4864b93 + e1823b2 commit de2a32b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@ public NativeImageInvokerInfo build() {
for (NativeImageFeatureBuildItem nativeImageFeature : nativeImageFeatures) {
featuresList.add(nativeImageFeature.getQualifiedName());
}
if (!nativeConfig.autoServiceLoaderRegistration()) {
featuresList.add("io.quarkus.runtime.graal.SkipConsoleServiceProvidersFeature");
// required by the feature
nativeImageArgs.add("-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED");
}
nativeImageArgs.add("--features=" + String.join(",", featuresList));

if (nativeConfig.debug().enabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.runtime.graal;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.graalvm.nativeimage.hosted.Feature;

/**
* Removes {@code jdk.internal.io.JdkConsoleProvider} service providers from the {@code ServiceCatalog} in a similar way to
* GraalVM's {@code ServiceLoaderFeature} which Quarkus disables by default.
*/
public class SkipConsoleServiceProvidersFeature implements Feature {
static final HashMap<String, Set<String>> omittedServiceProviders;

@Override
public String getDescription() {
return "Skip unsupported console service providers when quarkus.native.auto-service-loader-registration is false";
}

static {
omittedServiceProviders = new HashMap<>(1);
omittedServiceProviders.put("jdk.internal.io.JdkConsoleProvider",
new HashSet<>(Arrays.asList("jdk.jshell.execution.impl.ConsoleImpl$ConsoleProviderImpl",
"jdk.internal.org.jline.JdkConsoleProviderImpl")));
}

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
Class<?> serviceCatalogSupport;
Method singleton;
Method removeServices;
try {
serviceCatalogSupport = Class.forName("com.oracle.svm.core.jdk.ServiceCatalogSupport");
singleton = serviceCatalogSupport.getDeclaredMethod("singleton");
removeServices = serviceCatalogSupport.getDeclaredMethod("removeServicesFromServicesCatalog", String.class,
Set.class);
var result = singleton.invoke(null);
omittedServiceProviders.forEach((key, value) -> {
try {
removeServices.invoke(result, key, value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit de2a32b

Please sign in to comment.