Top Description Constructors Methods
org.graalvm.nativeimage.hosted

public final Class RuntimeClassInitialization

extends Object
Class Inheritance
Annotations
@Platforms:HOSTED_ONLY
Imports
org.graalvm.nativeimage.ImageSingletons, .Platform, .Platforms, org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport

This class provides methods that can be called during native-image building to configure class initialization behavior. By default, all JDK classes that are seen as reachable for a native image are initialized during image building, i.e. the class initialization method is executed during image building and is not seen as a reachable method at runtime. Application classes, on the other hand, are initialized during image building if they can be proven safe. Unsafe classes, e.g. ones that create threads, will be initialized at image run time. For classes that can't be proven safe, it is sometimes beneficial to ensure initialization during image building, and for some that are safe, it is still necessary to initialize at runtime (e.g., the order of initializer execution matters).

This class provides two different registration methods: Classes registered via initializeAtRunTime 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 initializeAtBuildTime will be initialized during image building. It is also possible define initialization for whole packages with initializeAtRunTime(String[]) and 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:

Since
19.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
classReason(StackTraceElement[] stacktrace, String simpleName)

private static String
private static String
getUnqualifiedName(Class<?> aClass)

Alternative to Class#getSimpleName that does not probe the enclosing class or method which fails when they are not yet loaded.

public static void
initializeAtBuildTime(Class<?>... classes)

Registers the provided classes as eagerly initialized during image-build time.

public static void
initializeAtBuildTime(String... packages)

Registers all classes in provided packages as eagerly initialized during image-build time.

public static void
initializeAtRunTime(Class<?>... classes)

Registers the provided classes, and all of their subclasses, for class initialization at runtime.

public static void
initializeAtRunTime(String... packages)

Registers all classes in provided packages, and all of their subclasses, for class initialization at runtime.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

RuntimeClassInitializationback to summary
private RuntimeClassInitialization()

Method Detail

classReasonback to summary
private static String classReason(StackTraceElement[] stacktrace, String simpleName)
getCallerback to summary
private static String getCaller(StackTraceElement[] stackTrace)
getUnqualifiedNameback to summary
private static String getUnqualifiedName(Class<?> aClass)

Alternative to Class#getSimpleName that does not probe the enclosing class or method which fails when they are not yet loaded. Duplicated from ClassUtil.getUnqualifiedName which is not reachable here.

initializeAtBuildTimeback to summary
public static void initializeAtBuildTime(Class<?>... classes)

Registers the provided classes as eagerly initialized during image-build time.

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.

Since
19.0
initializeAtBuildTimeback to summary
public static void initializeAtBuildTime(String... packages)

Registers all classes in provided packages as eagerly initialized during image-build time.

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.

Since
19.0
initializeAtRunTimeback to summary
public static void initializeAtRunTime(Class<?>... classes)

Registers the provided classes, and all of their subclasses, for class initialization at runtime. The classes are not initialized automatically during image generation, and also must not be initialized manually by the user during image generation.

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.

Since
19.0
initializeAtRunTimeback to summary
public static void initializeAtRunTime(String... packages)

Registers all classes in provided packages, and all of their subclasses, for class initialization at runtime. The classes are not initialized automatically during image generation, and also must not be initialized manually by the user during image generation.

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.

Since
19.0