One thing I find myself doing a lot is writing "package/Class" strings in calls to C++ JNI. A shortcut— define them as static members of a struct hierarchy mirroring the Java package hierarchy. To illustrate, take java.lang.Exception. In some header the declaration:
extern struct java
{
struct lang
{
const char* Exception;
} lang;
} java; And elsewhere in the matching definition:
const char* java::lang::Exception = "java/lang/Exception";
(Remember that JNI uses slashes instead of dots to separate package names.)
Now you can write code like this:
inline void
throwJavaException(JNIEnv* env, const std::string& msg)
{
env->ThrowNew(env->FindClass(java.lang.Exception), msg.c_str());
} I find using C++ program text that looks like Java program text is easier for me to read and conceptualize. I more interesting problems to occupy my mind than mentally translating strings to class names.
No comments:
Post a Comment