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.
- Host: GitHub
- URL: https://github.com/hoehermann/typedjni
- Owner: hoehermann
- License: mit
- Created: 2020-10-11T08:45:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-23T21:15:12.000Z (almost 5 years ago)
- Last Synced: 2024-10-12T10:10:23.279Z (over 1 year ago)
- Topics: java, jni, jni-wrapper
- Language: C++
- Homepage:
- Size: 33.2 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
}
.