An open API service indexing awesome lists of open source software.

https://github.com/hoehermann/typedjni

Wrap variardic JNI method calls into type-safe lambdas.
https://github.com/hoehermann/typedjni

java jni jni-wrapper

Last synced: 10 months ago
JSON representation

Wrap variardic JNI method calls into type-safe lambdas.

Awesome Lists containing this project

README

          

# TypedJNI

This module wraps the variardic JNI functions using C++ templates for offering type-safe and compact alternative.

Instead of the lengthy and error-prone

jclass jcls = (*env)->FindClass(env, "SomeClass");
if (jcls == NULL) {
return; // handle error
}
jmethodID constructor = (*env)->GetMethodID(env, jcls, "", "(JLjava/lang/String;)V");
if (constructor == NULL) {
return; // handle error
}
jstring jsomestring = (*env)->NewStringUTF(env, somestring);
if (jsomestring == NULL) {
return; // handle error
}
jlong jsomelong = somelong; // explicitly convert
jobject jobj = (*env)->NewObject(env, jcls, constructor, jsomelong, jsomestring);
if (jobj == NULL) {
return; // handle error
}
jmethodID method = (*env)->GetMethodID(env, jcls, "someMethod", "()V");
if (method == NULL) {
return; // handle error
}
(*env)->CallVoidMethod(env, jobj, method);
(*env)->DeleteLocalRef(jsomestring); // late cleanup
(*env)->DeleteLocalRef(jobj);

you can now write

try {
tenv.find_class("SomeClass").
GetConstructor()(
some_number, // implicit conversion where possible
tenv.make_jstring(some_string) // explicit conversion with automated clean-up
).
GetMethod("someMethod")();
} catch (std::exception & e) {
// handle error
}

.