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

jni-utils.mddocs/

JNI Utils

Simplified and safer JNI programming utilities providing type-safe wrappers, automatic exception handling, and streamlined native method integration.

Capabilities

JNI Helper Functions

Core utility class providing simplified JNI operations with automatic error handling and type safety.

/**
 * Main JNI utility class with helper methods for common operations
 */
public final class JNI {
    /** Create new local reference to object */
    public static <T> T NewLocalRef(T obj);
    
    /** Delete local reference */
    public static void DeleteLocalRef(Object obj);
    
    /** Create new global reference */
    public static <T> T NewGlobalRef(T obj);
    
    /** Delete global reference */
    public static void DeleteGlobalRef(Object obj);
    
    /** Create new weak global reference */
    public static <T> T NewWeakGlobalRef(T obj);
    
    /** Delete weak global reference */
    public static void DeleteWeakGlobalRef(Object obj);
    
    /** Get Java object class */
    public static Class<?> GetObjectClass(Object obj);
    
    /** Create new object array */
    public static Object[] NewObjectArray(int length, Class<?> elementClass, Object initialElement);
    
    /** Get object array element */
    public static Object GetObjectArrayElement(Object[] array, int index);
    
    /** Set object array element */
    public static void SetObjectArrayElement(Object[] array, int index, Object value);
    
    /** Throw new Java exception */
    public static void ThrowNew(Class<? extends Throwable> exceptionClass, String message);
    
    /** Check for pending exception */
    public static boolean ExceptionCheck();
    
    /** Clear pending exception */
    public static void ExceptionClear();
}

Usage Examples:

import org.graalvm.jniutils.*;

public class JNIExample {
    public void manageReferences(Object javaObject) {
        // Create global reference for long-term storage
        Object globalRef = JNI.NewGlobalRef(javaObject);
        
        try {
            // Use global reference in native operations
            processObject(globalRef);
        } finally {
            // Always clean up global references
            JNI.DeleteGlobalRef(globalRef);
        }
    }
    
    public void handleExceptions() {
        try {
            riskyOperation();
        } catch (Exception e) {
            // Convert to JNI exception
            JNI.ThrowNew(RuntimeException.class, e.getMessage());
        }
    }
}

JNI Utilities

Extended utility functions for advanced JNI operations and native integration.

/**
 * Additional JNI convenience methods for advanced operations
 */
public final class JNIUtil {
    /** Get field ID for named field */
    public static long GetFieldID(Class<?> clazz, String name, String signature);
    
    /** Get method ID for named method */
    public static long GetMethodID(Class<?> clazz, String name, String signature);
    
    /** Get static field ID */
    public static long GetStaticFieldID(Class<?> clazz, String name, String signature);
    
    /** Get static method ID */
    public static long GetStaticMethodID(Class<?> clazz, String name, String signature);
    
    /** Call object method with arguments */
    public static Object CallObjectMethod(Object obj, long methodID, Object... args);
    
    /** Call static object method */
    public static Object CallStaticObjectMethod(Class<?> clazz, long methodID, Object... args);
    
    /** Get object field value */
    public static Object GetObjectField(Object obj, long fieldID);
    
    /** Set object field value */
    public static void SetObjectField(Object obj, long fieldID, Object value);
    
    /** Get static object field value */
    public static Object GetStaticObjectField(Class<?> clazz, long fieldID);
    
    /** Set static object field value */
    public static void SetStaticObjectField(Class<?> clazz, long fieldID, Object value);
    
    /** Convert Java string to C string */
    public static CCharPointer GetStringUTFChars(String str);
    
    /** Release C string created from Java string */
    public static void ReleaseStringUTFChars(String str, CCharPointer chars);
}

Method Scope Management

Scoped execution wrapper for JNI methods ensuring proper cleanup and exception handling.

/**
 * Scoped JNI method execution with automatic resource management
 */
public final class JNIMethodScope implements AutoCloseable {
    /** Create new method scope for JNI operations */
    public static JNIMethodScope scope();
    
    /** Execute operation within this scope */
    public <T> T scope(Supplier<T> operation);
    
    /** Execute void operation within this scope */
    public void scope(Runnable operation);
    
    /** Close scope and clean up resources */
    public void close();
    
    /** Get JNI environment for this scope */
    public JNIEnvironment getEnvironment();
}

Usage Examples:

public class ScopedJNIExample {
    public String processWithScope(Object javaObject) {
        try (JNIMethodScope scope = JNIMethodScope.scope()) {
            return scope.scope(() -> {
                // All JNI operations automatically cleaned up
                Class<?> clazz = JNI.GetObjectClass(javaObject);
                long methodID = JNIUtil.GetMethodID(clazz, "toString", "()Ljava/lang/String;");
                return (String) JNIUtil.CallObjectMethod(javaObject, methodID);
            });
        }
    }
}

Exception Handling

Automatic exception translation between Java and JNI with proper cleanup.

/**
 * Automatic exception translation and handling for JNI operations
 */
public final class JNIExceptionWrapper {
    /** Wrap JNI operation with exception handling */
    public static <T> T wrapException(Supplier<T> operation);
    
    /** Wrap void JNI operation with exception handling */
    public static void wrapException(Runnable operation);
    
    /** Convert native exception to Java exception */
    public static RuntimeException convertException(Throwable nativeException);
    
    /** Handle pending JNI exception */
    public static void handlePendingException();
}

/**
 * Entry point generation for JNI exception wrappers
 */
public final class JNIExceptionWrapperEntryPoints {
    /** Generate entry point with exception wrapping */
    public static void generateEntryPoint(String methodName, Class<?>[] parameterTypes);
    
    /** Register exception wrapper for method */
    public static void registerWrapper(String methodName, Object wrapper);
}

HotSpot Object Support

Utilities for working with HotSpot VM objects in native image contexts.

/**
 * HotSpot object handling utilities for native image integration
 */
public final class HSObject {
    /** Convert HotSpot object to native representation */
    public static Object toNative(Object hotSpotObject);
    
    /** Convert native object to HotSpot representation */
    public static Object fromNative(Object nativeObject);
    
    /** Check if object is HotSpot object */
    public static boolean isHotSpotObject(Object obj);
    
    /** Get HotSpot object class */
    public static Class<?> getHotSpotClass(Object obj);
    
    /** Invoke HotSpot method on object */
    public static Object invokeHotSpotMethod(Object obj, String methodName, Object... args);
}

JNI Entry Points

Annotation-based JNI entry point generation for simplified native method binding.

/**
 * Annotation for generating JNI entry points
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JNIEntryPoint {
    /** JNI method name (defaults to Java method name) */
    String name() default "";
    
    /** JNI signature (auto-generated if not specified) */
    String signature() default "";
    
    /** Whether to include exception handling */
    boolean exceptionHandling() default true;
    
    /** Whether to use method scope */
    boolean useScope() default true;
}

Usage Examples:

public class NativeMethodExample {
    @JNIEntryPoint(name = "processData")
    public static String processData(byte[] data, int offset, int length) {
        // Implementation automatically wrapped with exception handling
        return new String(data, offset, length);
    }
    
    @JNIEntryPoint(exceptionHandling = false, useScope = false)
    public static int fastComputation(int a, int b) {
        // High-performance method without overhead
        return a * b + a / b;
    }
}

Native Bridge Support

Integration support for native bridge communication between different runtime environments.

/**
 * Native bridge integration support for JNI utilities
 */
public final class NativeBridgeSupport {
    /** Initialize native bridge for JNI operations */
    public static void initializeBridge();
    
    /** Create bridge-compatible JNI environment */
    public static JNIEnvironment createBridgeEnvironment();
    
    /** Convert bridge object to JNI object */
    public static Object bridgeToJNI(Object bridgeObject);
    
    /** Convert JNI object to bridge object */
    public static Object jniToBridge(Object jniObject);
    
    /** Check if native bridge is available */
    public static boolean isBridgeAvailable();
}

Types

/**
 * JNI environment interface for low-level operations
 */
public interface JNIEnvironment {
    /** Get JNI version */
    int getVersion();
    
    /** Find Java class by name */
    Class<?> findClass(String name);
    
    /** Register native methods */
    void registerNatives(Class<?> clazz, JNINativeMethod[] methods);
    
    /** Unregister native methods */
    void unregisterNatives(Class<?> clazz);
}

/**
 * JNI native method descriptor
 */
public static class JNINativeMethod {
    /** Method name */
    public final String name;
    
    /** Method signature */
    public final String signature;
    
    /** Function pointer */
    public final CFunctionPointer fnPtr;
    
    public JNINativeMethod(String name, String signature, CFunctionPointer fnPtr);
}

/**
 * Functional interface for supplying values
 */
public interface Supplier<T> {
    T get();
}

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