CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-graalvm-sdk--graal-sdk

GraalVM Standard Development Kit providing APIs for polyglot language embedding, native AOT compilation, memory-efficient collections, low-level memory access, JNI utilities, and native bridge communication.

Pending
Overview
Eval results
Files

native-image.mddocs/

Native Image Compilation

APIs for ahead-of-time (AOT) compilation of Java applications to native executables, with runtime customization, build-time optimization, and system integration capabilities.

Capabilities

Runtime Information

Core APIs for detecting execution context and native image properties at runtime.

/**
 * Provides information about the current execution environment
 */
public final class ImageInfo {
    /** Check if code is running in any image context (build-time or runtime) */
    public static boolean inImageCode();
    
    /** Check if code is running during image build process */
    public static boolean inImageBuildtimeCode();
    
    /** Check if code is running in compiled native image */
    public static boolean inImageRuntimeCode();
    
    /** Check if image is an executable (vs shared library) */
    public static boolean isExecutable();
    
    /** Check if image is a shared library */
    public static boolean isSharedLibrary();
    
    /** Property key for detecting image code context */
    public static final String PROPERTY_IMAGE_CODE_KEY = "org.graalvm.nativeimage.imagecode";
    
    /** Property values for image code detection */
    public static final String PROPERTY_IMAGE_CODE_VALUE_BUILDTIME = "buildtime";
    public static final String PROPERTY_IMAGE_CODE_VALUE_RUNTIME = "runtime";
}

Usage Examples:

public class RuntimeDetection {
    public static void configureBasedOnContext() {
        if (ImageInfo.inImageBuildtimeCode()) {
            // Configuration during native image build
            System.out.println("Configuring for native image build");
            
        } else if (ImageInfo.inImageRuntimeCode()) {
            // Runtime behavior in native image
            System.out.println("Running in native image");
            
        } else {
            // Regular JVM execution
            System.out.println("Running on JVM");
        }
        
        if (ImageInfo.isExecutable()) {
            System.out.println("This is a native executable");
        }
    }
}

Singleton Registry

Centralized registry for sharing singleton objects across the native image, replacing traditional static fields.

/**
 * Registry for singleton objects available throughout native image execution
 */
public final class ImageSingletons {
    /** Register singleton instance for given key type */
    public static <T> void add(Class<T> key, T value);
    
    /** Lookup singleton instance by key type */
    public static <T> T lookup(Class<T> key);
    
    /** Check if singleton is registered for key type */
    public static boolean contains(Class<?> key);
}

Usage Examples:

// Define service interface
interface DatabaseConfig {
    String getConnectionUrl();
}

// Register during image build
public class DatabaseConfigFeature implements Feature {
    @Override
    public void afterRegistration(AfterRegistrationAccess access) {
        DatabaseConfig config = new ProductionDatabaseConfig();
        ImageSingletons.add(DatabaseConfig.class, config);
    }
}

// Use at runtime
public class DatabaseService {
    private final DatabaseConfig config;
    
    public DatabaseService() {
        this.config = ImageSingletons.lookup(DatabaseConfig.class);
    }
    
    public void connect() {
        String url = config.getConnectionUrl();
        // Connect to database...
    }
}

Isolate Management

APIs for managing isolated heaps and threads in native images, enabling memory isolation and multi-tenancy.

/**
 * Represents an isolated heap in native image
 */
public interface Isolate extends PointerBase {
    /** Check if isolate pointer is null */
    boolean isNull();
}

/**
 * Represents a thread within an isolate
 */
public interface IsolateThread extends PointerBase {
    /** Check if isolate thread pointer is null */
    boolean isNull();
}

/**
 * Access to current isolate and thread
 */
public final class CurrentIsolate {
    /** Get current isolate thread */
    public static IsolateThread getCurrentThread();
    
    /** Get current isolate */
    public static Isolate getIsolate();
}

/**
 * Thread management utilities for native images
 */
public final class Threading {
    /** Check if current thread is main thread */
    public static boolean isMainThread();
    
    /** Get thread-local storage */
    public static ThreadLocal<Object> getThreadLocalStorage();
    
    /** Create new thread in current isolate */
    public static Thread createThread(Runnable task);
    
    /** Join thread execution */
    public static void joinThread(Thread thread);
}

/**
 * VM runtime operations and lifecycle management
 */
public final class VMRuntime {
    /** Initialize VM runtime */
    public static void initialize();
    
    /** Shutdown VM runtime */
    public static void shutdown();
    
    /** Register shutdown hook */
    public static void addShutdownHook(Runnable hook);
    
    /** Get VM version information */
    public static String getVersion();
    
    /** Check if VM is running */
    public static boolean isRunning();
}

Object Handle Management

Safe references to managed objects that survive garbage collection, essential for JNI and native code interaction.

/**
 * Handle to a managed object that prevents garbage collection
 */
public interface ObjectHandle extends ComparableWord {
    /** Check if handle is null */
    boolean isNull();
    
    /** Get underlying object from handle */
    Object getObject();
}

/**
 * Factory for creating and managing object handles
 */
public final class ObjectHandles {
    /** Create handle for object */
    public ObjectHandle create(Object object);
    
    /** Get global object handles instance */
    public static ObjectHandles getGlobal();
    
    /** Get null handle constant */
    public static ObjectHandle nullHandle();
    
    /** Destroy handle and allow object to be collected */
    public void destroy(ObjectHandle handle);
}

Memory Pinning

Prevent garbage collector from moving objects, enabling safe access from native code.

/**
 * Prevents GC from moving an object, allowing safe native access
 */
public interface PinnedObject extends AutoCloseable {
    /** Get address of array element at given index */
    <T> Pointer addressOfArrayElement(int index);
    
    /** Release pin and allow object to be moved */
    void close();
}

Usage Examples:

public class NativeMemoryAccess {
    public void processArray(byte[] data) {
        // Pin array for native access
        try (PinnedObject pinned = PinnedObject.create(data)) {
            Pointer address = pinned.addressOfArrayElement(0);
            
            // Pass address to native function
            processNativeData(address, data.length);
        } // Automatically unpinned
    }
    
    // Native method declaration
    private native void processNativeData(Pointer data, int length);
}

Platform Detection

APIs for detecting target platform characteristics and making platform-specific decisions.

/**
 * Platform detection with nested marker interfaces for specific platforms
 */
public interface Platform {
    // Nested platform marker interfaces
    interface HOSTED_ONLY extends Platform {}
    interface LINUX extends Platform {}
    interface DARWIN extends Platform {}
    interface WINDOWS extends Platform {}
    interface AMD64 extends Platform {}
    interface AARCH64 extends Platform {}
    
    // Combined platform markers
    interface LINUX_AMD64 extends LINUX, AMD64 {}
    interface DARWIN_AMD64 extends DARWIN, AMD64 {}
    interface WINDOWS_AMD64 extends WINDOWS, AMD64 {}
}

Build-Time Customization

Feature interface for customizing native image generation process with hooks for different build phases.

/**
 * Interface for customizing native image generation process
 */
public interface Feature {
    /** Called after all features are registered */
    default void afterRegistration(AfterRegistrationAccess access) {}
    
    /** Called before static analysis begins */
    default void beforeAnalysis(BeforeAnalysisAccess access) {}
    
    /** Called during iterative analysis phase */
    default void duringAnalysis(DuringAnalysisAccess access) {}
    
    /** Called after analysis is complete */
    default void afterAnalysis(AfterAnalysisAccess access) {}
    
    /** Called before heap creation */
    default void beforeCompilation(BeforeCompilationAccess access) {}
    
    /** Called after image heap is created */
    default void afterImageWrite(AfterImageWriteAccess access) {}
    
    /** Get feature description */
    default String getDescription() {
        return getClass().getName();
    }
    
    // Nested access interfaces for different build phases
    interface AfterRegistrationAccess {
        <T> void registerClassForReflection(Class<T> clazz);
        void registerMethodForReflection(Method method);
        void registerFieldForReflection(Field field);
    }
    
    interface BeforeAnalysisAccess extends FeatureAccess {
        void registerAsUsed(Class<?> clazz);
        void registerAsInstantiated(Class<?> clazz);
        void registerAsReachable(Executable executable);
    }
    
    interface DuringAnalysisAccess extends FeatureAccess {
        void requireAnalysisIteration();
    }
    
    interface FeatureAccess {
        Class<?> findClassByName(String className);
        List<Class<?>> findSubclasses(Class<?> baseClass);
        ApplicationClassLoader getApplicationClassLoader();
    }
}

Usage Examples:

@AutomaticFeature
public class CustomSerializationFeature implements Feature {
    @Override
    public void beforeAnalysis(BeforeAnalysisAccess access) {
        // Register classes for reflection
        access.registerClassForReflection(MySerializableClass.class);
        
        // Find and register all serializable classes
        for (Class<?> clazz : access.findSubclasses(Serializable.class)) {
            access.registerAsInstantiated(clazz);
        }
    }
    
    @Override
    public void afterRegistration(AfterRegistrationAccess access) {
        // Register singleton services
        MyService service = new MyService();
        ImageSingletons.add(MyService.class, service);
    }
}

Field Value Transformation

Transform static field values during image build time for optimization and configuration.

/**
 * Interface for transforming field values during image generation
 */
public interface FieldValueTransformer {
    /** Transform field value during image build */
    Object transform(Object receiver, Object originalValue);
}

Logging Support

Custom logging integration for native images with efficient log handling.

/**
 * Custom log handler for native image logging
 */
public interface LogHandler {
    /** Log message with specified log level */
    void log(LogLevel level, String message);
    
    /** Flush any buffered log output */
    void flush();
}

/**
 * Log levels for native image logging
 */
public enum LogLevel {
    ERROR(1),
    WARNING(2), 
    INFO(3),
    DEBUG(4);
    
    private final int level;
    
    LogLevel(int level) {
        this.level = level;
    }
    
    public int getLevel() {
        return level;
    }
}

Types

// Automatic feature registration annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutomaticFeature {
}

// Base word types for low-level operations
public interface WordBase {
}

public interface ComparableWord extends WordBase {
    boolean equal(ComparableWord val);
    boolean notEqual(ComparableWord val);
}

// Pointer base interface
public interface PointerBase extends ComparableWord {
    boolean isNull();
    boolean isNonNull();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-graalvm-sdk--graal-sdk

docs

c-interop.md

collections.md

config.md

index.md

jni-utils.md

native-bridge.md

native-image.md

polyglot.md

substitutions.md

word.md

tile.json