From 39d59fd1d0f660171b7421bd70c67ec9a0383919 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 2 May 2020 03:42:00 +0200 Subject: JNI helper_base: Robustness: Add java_exception_check_and_throw(..) and use InternalError for exceptions. --- java/jni/helper_base.cxx | 19 +++++++++++-------- java/jni/helper_base.hpp | 13 ++++++++----- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/java/jni/helper_base.cxx b/java/jni/helper_base.cxx index c72de97..f96dd9f 100644 --- a/java/jni/helper_base.cxx +++ b/java/jni/helper_base.cxx @@ -41,6 +41,7 @@ jfieldID getInstanceField(JNIEnv *env, jobject obj) { jclass clazz = env->GetObjectClass(obj); + java_exception_check_and_throw(env, E_FILE_LINE); // J == long return env->GetFieldID(clazz, "nativeInstance", "J"); } @@ -48,10 +49,10 @@ jfieldID getInstanceField(JNIEnv *env, jobject obj) jclass search_class(JNIEnv *env, const char *clazz_name) { jclass clazz = env->FindClass(clazz_name); + java_exception_check_and_throw(env, E_FILE_LINE); if (!clazz) { - std::string error = "no class found: "; error += clazz_name; - throw std::runtime_error(error); + throw direct_bt::InternalError(std::string("no class found: ")+clazz_name, E_FILE_LINE); } return clazz; } @@ -59,10 +60,10 @@ jclass search_class(JNIEnv *env, const char *clazz_name) jclass search_class(JNIEnv *env, jobject obj) { jclass clazz = env->GetObjectClass(obj); + java_exception_check_and_throw(env, E_FILE_LINE); if (!clazz) { - std::string error = "no class found: "; - throw std::runtime_error(error); + throw direct_bt::InternalError("no class found", E_FILE_LINE); } return clazz; } @@ -79,10 +80,11 @@ jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, { method = env->GetMethodID(clazz, method_name, prototype); } + java_exception_check_and_throw(env, E_FILE_LINE); if (!method) { - throw std::runtime_error("no method found\n"); + throw direct_bt::InternalError(std::string("no method found: ")+method_name, E_FILE_LINE); } return method; @@ -100,10 +102,11 @@ jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, { field = env->GetFieldID(clazz, field_name, type); } + java_exception_check_and_throw(env, E_FILE_LINE); if (!field) { - throw std::runtime_error("no method found\n"); + direct_bt::InternalError(std::string("no field found: ")+field_name, E_FILE_LINE); } return field; @@ -125,7 +128,7 @@ bool from_jboolean_to_bool(jboolean val) } else { - throw std::invalid_argument("the jboolean value is not true/false\n"); + throw direct_bt::InternalError("the jboolean value is not true/false", E_FILE_LINE); } } @@ -173,7 +176,7 @@ jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add) jobject result = env->NewObject(arraylist_class, arraylist_ctor, size); if (!result) { - throw std::runtime_error("cannot create instance of class\n"); + throw direct_bt::InternalError("Cannot create instance of class ArrayList", E_FILE_LINE); } *add = search_method(env, arraylist_class, "add", "(Ljava/lang/Object;)Z", false); diff --git a/java/jni/helper_base.hpp b/java/jni/helper_base.hpp index ad6709b..e82df0f 100644 --- a/java/jni/helper_base.hpp +++ b/java/jni/helper_base.hpp @@ -54,8 +54,9 @@ template T *castInstance(jlong instance) { T *t = reinterpret_cast(instance); - if (t == nullptr) + if (t == nullptr) { throw std::runtime_error("Trying to cast null object"); + } return t; } @@ -64,16 +65,18 @@ T *getInstance(JNIEnv *env, jobject obj) { jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); T *t = reinterpret_cast(instance); - if (t == nullptr) + if (t == nullptr) { throw std::runtime_error("Trying to acquire null object"); + } return t; } template void setInstance(JNIEnv *env, jobject obj, T *t) { - if (t == nullptr) + if (t == nullptr) { throw std::runtime_error("Trying to create null object"); + } jlong instance = reinterpret_cast(t); env->SetLongField(obj, getInstanceField(env, obj), instance); } @@ -90,7 +93,7 @@ jobject generic_clone(JNIEnv *env, jobject obj) jobject result = env->NewObject(generic_class, generic_ctor, (jlong)copy_generic); if (!result) { - throw std::runtime_error("cannot create instance of class\n"); + throw std::runtime_error("cannot create instance of class"); } return result; @@ -119,7 +122,7 @@ jobject convert_vector_to_jobject(JNIEnv *env, std::vector>& jobject object = env->NewObject(clazz, clazz_ctor, (jlong)elem); if (!object) { - throw std::runtime_error("cannot create instance of class\n"); + throw direct_bt::InternalError("cannot create instance of class", E_FILE_LINE); } env->CallBooleanMethod(result, arraylist_add, object); } -- cgit v1.2.3