aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-02 03:42:00 +0200
committerSven Gothel <[email protected]>2020-05-02 03:42:00 +0200
commit39d59fd1d0f660171b7421bd70c67ec9a0383919 (patch)
treef92b0cb320acabd17ed6621a3574d24a58cdf798
parentf2bae80bbb59a11461469e9755496c1b58700c12 (diff)
JNI helper_base: Robustness: Add java_exception_check_and_throw(..) and use InternalError for exceptions.
-rw-r--r--java/jni/helper_base.cxx19
-rw-r--r--java/jni/helper_base.hpp13
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 <typename T>
T *castInstance(jlong instance)
{
T *t = reinterpret_cast<T *>(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<T *>(instance);
- if (t == nullptr)
+ if (t == nullptr) {
throw std::runtime_error("Trying to acquire null object");
+ }
return t;
}
template <typename T>
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<jlong>(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<std::unique_ptr<T>>&
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);
}