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.
—
Communication APIs for bridging between different runtime environments, enabling seamless data marshalling and method calls between Native Image and HotSpot VM contexts.
Annotation-based automatic generation of bridge code for cross-runtime communication.
/**
* Generate bridge from HotSpot to Native Image
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface GenerateHotSpotToNativeBridge {
/** Target native interface */
Class<?> value();
/** Bridge name (defaults to interface name) */
String name() default "";
/** Whether to include debug information */
boolean debug() default false;
/** Custom marshaller configuration */
Class<? extends MarshallerConfig> marshallerConfig() default DefaultMarshallerConfig.class;
}
/**
* Generate bridge between native contexts
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface GenerateNativeToNativeBridge {
/** Target native interface */
Class<?> value();
/** Bridge name (defaults to interface name) */
String name() default "";
/** Include performance monitoring */
boolean monitoring() default false;
}Usage Examples:
import org.graalvm.nativebridge.*;
// Define interface for bridging
public interface DataProcessor {
String processData(byte[] input, ProcessingOptions options);
int getVersion();
}
// Generate HotSpot to Native bridge
@GenerateHotSpotToNativeBridge(DataProcessor.class)
public class DataProcessorBridge {
// Implementation generated automatically
}
// Generate Native to Native bridge
@GenerateNativeToNativeBridge(DataProcessor.class)
public class FastDataProcessorBridge {
// High-performance native-only bridge
}Comprehensive marshalling system for converting objects between different runtime representations.
/**
* Base interface for custom object marshalling between runtimes
*/
public interface BinaryMarshaller<T> {
/** Convert object to binary representation */
byte[] marshal(T object);
/** Convert binary representation back to object */
T unmarshal(byte[] data);
/** Get marshalled data size estimate */
int estimateSize(T object);
/** Check if object can be marshalled */
boolean canMarshal(T object);
}
/**
* Specialized marshaller for string objects with encoding support
*/
public interface StringMarshaller extends BinaryMarshaller<String> {
/** Convert string with specific encoding */
byte[] marshal(String string, Charset encoding);
/** Convert with encoding detection */
String unmarshal(byte[] data, Charset encoding);
/** Get supported encodings */
Set<Charset> getSupportedEncodings();
}
/**
* Marshaller that handles null values gracefully
*/
public interface NullableBinaryMarshaller<T> extends BinaryMarshaller<T> {
/** Marshal nullable object */
byte[] marshalNullable(T object);
/** Unmarshal potentially null object */
T unmarshalNullable(byte[] data);
/** Check if data represents null */
boolean isNull(byte[] data);
}Usage Examples:
public class CustomObjectMarshaller implements BinaryMarshaller<CustomObject> {
@Override
public byte[] marshal(CustomObject obj) {
// Custom serialization logic
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(obj);
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Failed to marshal object", e);
}
}
@Override
public CustomObject unmarshal(byte[] data) {
// Custom deserialization logic
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) {
return (CustomObject) ois.readObject();
} catch (Exception e) {
throw new RuntimeException("Failed to unmarshal object", e);
}
}
@Override
public int estimateSize(CustomObject object) {
return object.getEstimatedSize();
}
@Override
public boolean canMarshal(CustomObject object) {
return object.isSerializable();
}
}Configuration system for customizing marshalling behavior across different data types.
/**
* Configuration for marshaller behavior and type mappings
*/
public interface MarshallerConfig {
/** Get marshaller for specific type */
<T> BinaryMarshaller<T> getMarshaller(Class<T> type);
/** Register custom marshaller for type */
<T> void registerMarshaller(Class<T> type, BinaryMarshaller<T> marshaller);
/** Get default marshaller for unknown types */
BinaryMarshaller<Object> getDefaultMarshaller();
/** Check if type has registered marshaller */
boolean hasMarshaller(Class<?> type);
/** Get marshalling options */
MarshallingOptions getOptions();
}
/**
* Default marshaller configuration with built-in type support
*/
public class DefaultMarshallerConfig implements MarshallerConfig {
/** Create default configuration */
public static DefaultMarshallerConfig create();
/** Create with custom options */
public static DefaultMarshallerConfig create(MarshallingOptions options);
}
/**
* Options for controlling marshalling behavior
*/
public static class MarshallingOptions {
/** Enable compression for large objects */
public final boolean compression;
/** Maximum object size for marshalling */
public final int maxObjectSize;
/** Charset for string encoding */
public final Charset defaultCharset;
/** Enable type safety checks */
public final boolean typeSafety;
public MarshallingOptions(boolean compression, int maxObjectSize,
Charset defaultCharset, boolean typeSafety);
}APIs for managing native isolates and their lifecycle in bridge communications.
/**
* Configuration for native isolate creation and management
*/
public static class NativeIsolateConfig {
/** Maximum heap size for isolate */
public final long maxHeapSize;
/** Initial heap size */
public final long initialHeapSize;
/** Stack size per thread */
public final int stackSize;
/** Enable debugging support */
public final boolean debugMode;
/** Custom isolate parameters */
public final Map<String, String> parameters;
public NativeIsolateConfig(long maxHeapSize, long initialHeapSize,
int stackSize, boolean debugMode,
Map<String, String> parameters);
/** Create default configuration */
public static NativeIsolateConfig createDefault();
}
/**
* Process-level isolate management for bridge communication
*/
public final class ProcessIsolate {
/** Create new process isolate with configuration */
public static ProcessIsolate create(NativeIsolateConfig config);
/** Get current process isolate */
public static ProcessIsolate getCurrent();
/** Execute operation in isolate context */
public <T> T execute(Callable<T> operation);
/** Execute void operation in isolate context */
public void execute(Runnable operation);
/** Shutdown isolate and clean up resources */
public void shutdown();
/** Check if isolate is active */
public boolean isActive();
/** Get isolate configuration */
public NativeIsolateConfig getConfig();
}
/**
* Exception thrown when isolate creation fails
*/
public class IsolateCreateException extends Exception {
public IsolateCreateException(String message);
public IsolateCreateException(String message, Throwable cause);
public IsolateCreateException(Throwable cause);
}Usage Examples:
public class IsolateExample {
public void setupIsolate() throws IsolateCreateException {
// Configure isolate
NativeIsolateConfig config = new NativeIsolateConfig(
1024 * 1024 * 64, // 64MB max heap
1024 * 1024 * 16, // 16MB initial heap
8192, // 8KB stack size
true, // Enable debugging
Map.of("opt.level", "3") // Custom parameters
);
// Create and use isolate
try (ProcessIsolate isolate = ProcessIsolate.create(config)) {
String result = isolate.execute(() -> {
// Execute in isolate context
return performComputation();
});
System.out.println("Result: " + result);
}
}
private String performComputation() {
return "Computed in isolate";
}
}Control how objects are passed between runtime environments - by value or by reference.
/**
* Marker annotation for passing objects by remote reference
*/
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ByRemoteReference {
/** Reference timeout in milliseconds */
long timeout() default 30000L;
/** Whether to cache references */
boolean cache() default true;
}
/**
* Force all instances of type to be passed by remote reference
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AlwaysByRemoteReference {
/** Default timeout for all references */
long defaultTimeout() default 60000L;
}
/**
* Force all instances of type to be passed by local value
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AlwaysByLocalReference {
/** Whether to validate during marshalling */
boolean validate() default true;
}Usage Examples:
// Always pass by remote reference
@AlwaysByRemoteReference(defaultTimeout = 120000L)
public class ExpensiveResource {
public void performOperation() {
// Expensive operation executed remotely
}
}
// Control reference passing per method
public interface ServiceBridge {
// Pass by local value (default)
String processData(String input);
// Pass by remote reference with timeout
void updateResource(@ByRemoteReference(timeout = 60000L) ExpensiveResource resource);
// Return by remote reference
@ByRemoteReference
ExpensiveResource createResource();
}Thread representation and management across different runtime environments.
/**
* Represents a thread within a native isolate
*/
public interface NativeIsolateThread {
/** Get thread ID */
long getThreadId();
/** Get associated isolate */
Isolate getIsolate();
/** Check if thread is current thread */
boolean isCurrent();
/** Attach to this thread context */
void attach();
/** Detach from this thread context */
void detach();
/** Execute operation in thread context */
<T> T execute(Callable<T> operation);
}
/**
* Represents a thread within HotSpot isolate
*/
public interface HSIsolateThread {
/** Get Java thread object */
Thread getJavaThread();
/** Get thread name */
String getName();
/** Check if thread is daemon */
boolean isDaemon();
/** Get thread priority */
int getPriority();
/** Get thread state */
Thread.State getState();
/** Execute operation in HotSpot context */
<T> T execute(Callable<T> operation);
}/**
* Callable interface for operations that return values
*/
public interface Callable<V> {
V call() throws Exception;
}
/**
* Base isolate interface
*/
public interface Isolate {
/** Get isolate ID */
long getId();
/** Check if isolate is current */
boolean isCurrent();
/** Get isolate threads */
Set<IsolateThread> getThreads();
}
/**
* Base isolate thread interface
*/
public interface IsolateThread {
/** Get thread ID */
long getId();
/** Get owning isolate */
Isolate getIsolate();
}
/**
* Character set enumeration for string encoding
*/
public enum Charset {
UTF_8, UTF_16, ISO_8859_1, US_ASCII;
/** Get canonical name */
public String getCanonicalName();
/** Check if charset supports encoding */
public boolean canEncode();
}
/**
* Input/output stream interfaces for binary data
*/
public interface ByteArrayOutputStream extends AutoCloseable {
void write(int b);
void write(byte[] b);
byte[] toByteArray();
void close();
}
public interface ByteArrayInputStream extends AutoCloseable {
int read();
int read(byte[] b);
void close();
}Install with Tessl CLI
npx tessl i tessl/maven-org-graalvm-sdk--graal-sdk