public final class RuntimeClassInitialization extends Object
This class provides two different registration methods: Classes registered via
RuntimeClassInitialization.initializeAtRunTime(java.lang.Class<?>...)
are not initialized at all during image generation, and only
initialized at runtime, i.e., the class initializer is executed once at runtime. Classes
registered via RuntimeClassInitialization.initializeAtBuildTime(java.lang.Class<?>...)
will be initialized during image building.
It is also possible define initialization for whole packages with
RuntimeClassInitialization.initializeAtRunTime(String[])
and RuntimeClassInitialization.initializeAtBuildTime(String[])
. The rules
for packages can be further refined by using methods for individual classes.
Initializing classes at runtime comes with some costs and restrictions:
Modifier and Type | Method and Description |
---|---|
static void |
initializeAtBuildTime(Class<?>... classes)
Registers the provided classes as eagerly initialized during image-build time.
|
static void |
initializeAtBuildTime(String... packages)
Registers all classes in provided packages as eagerly initialized during image-build time.
|
static void |
initializeAtRunTime(Class<?>... classes)
Registers the provided classes, and all of their subclasses, for class initialization at
runtime.
|
static void |
initializeAtRunTime(String... packages)
Registers all classes in provided packages, and all of their subclasses, for class
initialization at runtime.
|
public static void initializeAtRunTime(Class<?>... classes)
Unfortunately, classes are initialized for many reasons, and it is not possible to intercept class initialization and report an error at this time. If a registered class gets initialized, an error can be reported only later and the user must manually debug the reason for class initialization. This can be done by, e.g., setting a breakpoint in the class initializer or adding debug printing (print the stack trace) in the class initializer.
public static void initializeAtBuildTime(Class<?>... classes)
All static initializers of classes
will be executed during image-build time and
static fields that are assigned values will be available at runtime. static final
fields will be considered as constant.
It is up to the user to ensure that this behavior makes sense and does not lead to wrong application behavior.
public static void initializeAtRunTime(String... packages)
Unfortunately, classes are initialized for many reasons, and it is not possible to intercept class initialization and report an error at this time. If a registered class gets initialized, an error can be reported only later and the user must manually debug the reason for class initialization. This can be done by, e.g., setting a breakpoint in the class initializer or adding debug printing (print the stack trace) in the class initializer.
public static void initializeAtBuildTime(String... packages)
All static initializers of classes
will be executed during image-build time and
static fields that are assigned values will be available at runtime. static final
fields will be considered as constant.
It is up to the user to ensure that this behavior makes sense and does not lead to wrong application behavior.