DigitalSmile / native-memory-processor

Java FFM API code generation library
Apache License 2.0
0 stars 0 forks source link
c clang code-generation cpp java java-22 java-ffm libclang native-code

Java Native Memory Processing

Maven Central Version Maven Central Version GitHub Actions Workflow Status

Create an interop code with ease.

Introduction

With the release of JDK 22 the new Foreign Function & Memory API (FFM API) has been introduced from preview phase.

This API is intended to substitute Java Native Interface (JNI) and become a standard of interacting with native code from Java.

While FFM API itself is very powerful and can provide access to onheap/offheap memory segments and call a native function, it is lacking of extended features of converting C/C++ structures to Java classes. Of course, we have jextract tool, but the code it generates is kind of messy and hard to understand.

This project goal is to combine the power of FFM API and Java, like annotation processing, to make you happy while working with native functions and structures.

Features

Requirements

Quick start

1) Add dependencies to your build.gradle:

dependencies {
    // Annotations to use for code generation
    implementation 'io.github.digitalsmile.native:annotation:${version}'
    // Process annotations and generate code at compile time
    annotationProcessor 'io.github.digitalsmile.native:annotation-processor:${version}'
}

2) Define interface class and run your build:

@NativeMemory(header = "gpio.h")
@Structs
@Enums
public interface GPIO {
    @NativeManualFunction(name = "ioctl", useErrno = true)
    int nativeCall(int fd, long command, int data) throws NativeMemoryException;
}

This code snippet will generate all structures and enums within the header file gpio.h (located in resources folder), as well as GPIONative class with call implementation of ioctl native function.

Observe concepts in doucmentation or find more examples in getting started. You can also look at tests or in my other project https://github.com/digitalsmile/gpio

3) Enjoy! :)

Plans