public class ServiceLoaderFeature extends Object implements org.graalvm.nativeimage.hosted.Feature
ServiceLoader
on Substrate VM.
Services are registered in the folder "META-INF/services/"
using files
whose name is the fully qualified service interface name. We do not know which services are going
to be used by a native image: The parameter of ServiceLoader.load(java.lang.Class<S>, java.lang.ClassLoader)
is often but not always
a compile-time constant that we can track. But we also cannot put all registered services into
the native image.
We therefore use the following heuristic: we add all service loader files and service
implementation classes when the service interfaces that are seen as reachable by the static
analysis.
Each used service implementation class is added for reflection (using
RuntimeReflection.register(Class[])
) and for reflective
instantiation (using RuntimeReflection.registerForReflectiveInstantiation(Class[])
).
For each service interface, a single service loader file is added as a resource to the image. The
single file combines all the individual files that can come from different .jar files.
Unfortunately, state of the art module support in SVM is not sophisticated enough to allow the
original ServiceLoader infrastructure to discover providers registered in modules. Therefore, as
a temporary solution, we're disabling the ModuleServicesLookupIterator in favour of the
LazyClassPathLookupIterator looking for files in META-INF directory. Therefore this feature
writes all services, even the ones from modules, into the corresponding META-INF file. All of
them are then discovered by the LazyClassPathLookupIterator. TODO fix this once GR-19320 is done
One possible problem might be inconsistency between JVM and SVM, but since the lookup in JVM is
very dynamic in nature (depends on from which classloader or module you are starting), it might
not be possible for us to deliver services in the exact same order with "flat" single loader
approach.Modifier and Type | Class and Description |
---|---|
static class |
ServiceLoaderFeature.Options |
org.graalvm.nativeimage.hosted.Feature.AfterAnalysisAccess, org.graalvm.nativeimage.hosted.Feature.AfterCompilationAccess, org.graalvm.nativeimage.hosted.Feature.AfterHeapLayoutAccess, org.graalvm.nativeimage.hosted.Feature.AfterImageWriteAccess, org.graalvm.nativeimage.hosted.Feature.AfterRegistrationAccess, org.graalvm.nativeimage.hosted.Feature.BeforeAnalysisAccess, org.graalvm.nativeimage.hosted.Feature.BeforeCompilationAccess, org.graalvm.nativeimage.hosted.Feature.BeforeImageWriteAccess, org.graalvm.nativeimage.hosted.Feature.BeforeUniverseBuildingAccess, org.graalvm.nativeimage.hosted.Feature.CompilationAccess, org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess, org.graalvm.nativeimage.hosted.Feature.DuringSetupAccess, org.graalvm.nativeimage.hosted.Feature.FeatureAccess, org.graalvm.nativeimage.hosted.Feature.IsInConfigurationAccess, org.graalvm.nativeimage.hosted.Feature.OnAnalysisExitAccess, org.graalvm.nativeimage.hosted.Feature.QueryReachabilityAccess
Constructor and Description |
---|
ServiceLoaderFeature() |
Modifier and Type | Method and Description |
---|---|
void |
afterRegistration(org.graalvm.nativeimage.hosted.Feature.AfterRegistrationAccess access) |
void |
beforeAnalysis(org.graalvm.nativeimage.hosted.Feature.BeforeAnalysisAccess access) |
void |
duringAnalysis(org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess a) |
boolean |
isInConfiguration(org.graalvm.nativeimage.hosted.Feature.IsInConfigurationAccess access) |
public boolean isInConfiguration(org.graalvm.nativeimage.hosted.Feature.IsInConfigurationAccess access)
isInConfiguration
in interface org.graalvm.nativeimage.hosted.Feature
public void afterRegistration(org.graalvm.nativeimage.hosted.Feature.AfterRegistrationAccess access)
afterRegistration
in interface org.graalvm.nativeimage.hosted.Feature
public void beforeAnalysis(org.graalvm.nativeimage.hosted.Feature.BeforeAnalysisAccess access)
beforeAnalysis
in interface org.graalvm.nativeimage.hosted.Feature
public void duringAnalysis(org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess a)
duringAnalysis
in interface org.graalvm.nativeimage.hosted.Feature