Package org.graalvm.nativeimage.hosted
Interface Feature
public interface Feature
Features allow clients to intercept the native image generation and run custom initialization
code at various stages. All code within feature classes is executed during native image
generation, and never at run time.
Features have several advantages over static class initializers (which also run during native image generation):
- The different feature methods are called at different stages during native image generation, which gives clients control over when code is executed.
- Feature methods have an
access
parameter that allows callbacks into the native image generator. - Feature methods run when the
ImageSingletons
is already set up, which allows features to prepare data structures that are then used at run time by querying theImageSingletons
.
Implementation classes must have a no-argument constructor, which is used to instantiate a singleton instance for each feature using reflection. The following features are included during native image generation:
- Features explicitly specified on the command line.
- Features referenced as
required
by another included feature. Required features are added transitively, and initialization methods of required features are called after invoking the constructor andisInConfiguration(org.graalvm.nativeimage.hosted.Feature.IsInConfigurationAccess)
of the requiring features (unless the feature dependencies are cyclic).
- Since:
- 19.0
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Access methods available forafterAnalysis(org.graalvm.nativeimage.hosted.Feature.AfterAnalysisAccess)
.static interface
Access methods available forafterCompilation(org.graalvm.nativeimage.hosted.Feature.AfterCompilationAccess)
.static interface
Access methods available forafterHeapLayout(org.graalvm.nativeimage.hosted.Feature.AfterHeapLayoutAccess)
.static interface
Access methods available forafterImageWrite(org.graalvm.nativeimage.hosted.Feature.AfterImageWriteAccess)
.static interface
Access methods available forafterRegistration(org.graalvm.nativeimage.hosted.Feature.AfterRegistrationAccess)
.static interface
Access methods available forbeforeAnalysis(org.graalvm.nativeimage.hosted.Feature.BeforeAnalysisAccess)
.static interface
Access methods available forbeforeCompilation(org.graalvm.nativeimage.hosted.Feature.BeforeCompilationAccess)
.static interface
Access methods available forbeforeHeapLayout(org.graalvm.nativeimage.hosted.Feature.BeforeHeapLayoutAccess)
.static interface
Access methods available forbeforeImageWrite(org.graalvm.nativeimage.hosted.Feature.BeforeImageWriteAccess)
.static interface
Access methods available forbeforeUniverseBuilding(org.graalvm.nativeimage.hosted.Feature.BeforeUniverseBuildingAccess)
.static interface
static interface
Access methods available forduringAnalysis(org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess)
.static interface
Access methods available forduringSetup(org.graalvm.nativeimage.hosted.Feature.DuringSetupAccess)
.static interface
Access methods that are available for all feature methods.static interface
Access methods available forisInConfiguration(org.graalvm.nativeimage.hosted.Feature.IsInConfigurationAccess)
.static interface
Access methods available foronAnalysisExit(org.graalvm.nativeimage.hosted.Feature.OnAnalysisExitAccess)
.static interface
Access reachability methods available forafterAnalysis(org.graalvm.nativeimage.hosted.Feature.AfterAnalysisAccess)
andduringAnalysis(org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess)
. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Handler for initializations after analysis and before universe creation.default void
Handler for initializations after compilation, i.e., before the native image is written.default void
Handler for initializations after the native image heap and code layout.default void
Handler for altering the image (or shared object) that the linker command produced.default void
Handler for initializations after all features have been registered and all options have been parsed; but before any initializations for the static analysis have happened.default void
Handler for initializations before the static analysis.default void
Handler for initializations before compilation.default void
Handler for initializations before the native image heap and code layout.default void
Handler for altering the linker command after the native image has been built and before it is written.default void
Handler for code that needs to run before universe building, but after hosted meta-access has been created.default void
cleanup()
Handler for cleanup.default void
Handler for performing operations during the static analysis.default void
duringSetup
(Feature.DuringSetupAccess access) Handler for initializations at startup time.default String
A short description of the feature (e.g., "Enables Truffle support").Returns the list of features that this feature depends on.default String
getURL()
A URL to documentation or the sources of the feature.default boolean
This method is called immediately after the constructor, to check whether the feature is part of the configuration or not.default void
Handler for code that needs to run after the analysis, even if an error has occurred, e.g., like reporting code.
-
Method Details
-
getURL
A URL to documentation or the sources of the feature. The URL will be used as a link for the feature in the build output (if supported by the user's terminal).- Since:
- 22.2
-
getDescription
A short description of the feature (e.g., "Enables Truffle support"). The description is displayed to users as part of the build output.- Since:
- 22.2
-
isInConfiguration
This method is called immediately after the constructor, to check whether the feature is part of the configuration or not. If this method returns false, the feature is not included in the list of features and no other methods are called (in particular, therequired features
are not processed).- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
getRequiredFeatures
Returns the list of features that this feature depends on. As long as the dependency chain is non-cyclic, all required features are processed before this feature.- Since:
- 19.0
-
afterRegistration
Handler for initializations after all features have been registered and all options have been parsed; but before any initializations for the static analysis have happened.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
duringSetup
Handler for initializations at startup time. It allows customization of the static analysis setup.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
beforeAnalysis
Handler for initializations before the static analysis.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
duringAnalysis
Handler for performing operations during the static analysis. This handler is called after analysis is complete. So all analysis meta data is available. If the handler performs changes, e.g., makes new types or methods reachable, it needs to callFeature.DuringAnalysisAccess.requireAnalysisIteration()
. This triggers a new iteration: analysis is performed again and the handler is called again.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
afterAnalysis
Handler for initializations after analysis and before universe creation.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
onAnalysisExit
Handler for code that needs to run after the analysis, even if an error has occurred, e.g., like reporting code.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
beforeUniverseBuilding
Handler for code that needs to run before universe building, but after hosted meta-access has been created.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 21.1
-
beforeCompilation
Handler for initializations before compilation.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
afterCompilation
Handler for initializations after compilation, i.e., before the native image is written.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
beforeHeapLayout
Handler for initializations before the native image heap and code layout.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 23.2
-
afterHeapLayout
Handler for initializations after the native image heap and code layout. Objects and methods have their offsets assigned. At this point, no additional objects must be added to the native image heap, i.e., modifying object fields of native image objects that are part of the native image heap is not allowed at this point.- Parameters:
access
- The supported operations that the feature can perform at this time- Since:
- 19.0
-
beforeImageWrite
Handler for altering the linker command after the native image has been built and before it is written.- Parameters:
access
- The supported operations that the feature can perform at this time.- Since:
- 19.0
-
afterImageWrite
Handler for altering the image (or shared object) that the linker command produced.- Parameters:
access
- The supported operations that the feature can perform at this time.- Since:
- 19.0
-
cleanup
default void cleanup()Handler for cleanup. Can be used to cleanup static data. This can avoid memory leaks if native image generation is done many times, e.g. during unit tests.Usually, overriding this method can be avoided by putting a configuration object into the
ImageSingletons
.- Since:
- 19.0
-