Skip to content

Wrap variardic JNI method calls into type-safe lambdas.

License

Notifications You must be signed in to change notification settings

hoehermann/TypedJNI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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, "<init>", "(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<jlong,jstring>()(
            some_number, // implicit conversion where possible
            tenv.make_jstring(some_string) // explicit conversion with automated clean-up
        ).
        GetMethod<void()>("someMethod")();
} catch (std::exception & e) {
    // handle error
}

.

About

Wrap variardic JNI method calls into type-safe lambdas.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published