diff options
author | Sven Gothel <[email protected]> | 2020-07-29 22:55:03 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-29 22:55:03 +0200 |
commit | 27c8ebdee3279b29cd6773bd6a8eed8542c2c1d3 (patch) | |
tree | 15e448c204897267481ea5c28f39d01f36e83045 /java | |
parent | a9be7a6fd8de3f76e18c6a651621c2739a6dc669 (diff) |
JNI: Complete C++ to Java Exception mapping
Diffstat (limited to 'java')
-rw-r--r-- | java/jni/BluetoothFactory.cxx | 6 | ||||
-rw-r--r-- | java/jni/helper_base.cxx | 88 | ||||
-rw-r--r-- | java/jni/helper_base.hpp | 12 |
3 files changed, 77 insertions, 29 deletions
diff --git a/java/jni/BluetoothFactory.cxx b/java/jni/BluetoothFactory.cxx index c8a61b6f..42a2eca4 100644 --- a/java/jni/BluetoothFactory.cxx +++ b/java/jni/BluetoothFactory.cxx @@ -38,11 +38,11 @@ jstring Java_org_tinyb_BluetoothFactory_getNativeAPIVersion(JNIEnv *env, jclass std::string api_version = std::string(gVERSION_API); return env->NewStringUTF(api_version.c_str()); } catch (std::bad_alloc &e) { - raise_java_oom_exception(env, e); + raise_java_exception(env, e); } catch (std::runtime_error &e) { - raise_java_runtime_exception(env, e); + raise_java_exception(env, e); } catch (std::invalid_argument &e) { - raise_java_invalid_arg_exception(env, e); + raise_java_exception(env, e); } catch (std::exception &e) { raise_java_exception(env, e); } diff --git a/java/jni/helper_base.cxx b/java/jni/helper_base.cxx index f7c499e7..33f12e08 100644 --- a/java/jni/helper_base.cxx +++ b/java/jni/helper_base.cxx @@ -31,7 +31,7 @@ #include <stdexcept> #include <vector> -#define VERBOSE_ON 1 +// #define VERBOSE_ON 1 #include <dbt_debug.hpp> #include "helper_base.hpp" @@ -185,50 +185,86 @@ jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add) return result; } -void raise_java_exception(JNIEnv *env, std::exception &e) -{ +void raise_java_exception(JNIEnv *env, std::exception &e) { env->ThrowNew(env->FindClass("java/lang/Error"), e.what()); } - -void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e) -{ +void raise_java_exception(JNIEnv *env, std::runtime_error &e) { env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); } - -void raise_java_runtime_exception(JNIEnv *env, direct_bt::RuntimeException &e) { +void raise_java_exception(JNIEnv *env, direct_bt::RuntimeException &e) { env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); } - -void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e) -{ - env->ThrowNew(env->FindClass("java/lang/OutOfMemoryException"), e.what()); +void raise_java_exception(JNIEnv *env, direct_bt::InternalError &e) { + env->ThrowNew(env->FindClass("java/lang/InternalError"), e.what()); } - -void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e) -{ +void raise_java_exception(JNIEnv *env, direct_bt::NullPointerException &e) { + env->ThrowNew(env->FindClass("java/lang/NullPointerException"), e.what()); +} +void raise_java_exception(JNIEnv *env, direct_bt::IllegalArgumentException &e) { env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); } - -void raise_java_bluetooth_exception(JNIEnv *env, direct_bt::BluetoothException &e) -{ +void raise_java_exception(JNIEnv *env, std::invalid_argument &e) { + env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); +} +void raise_java_exception(JNIEnv *env, direct_bt::IllegalStateException &e) { + env->ThrowNew(env->FindClass("java/lang/IllegalStateException"), e.what()); +} +void raise_java_exception(JNIEnv *env, direct_bt::UnsupportedOperationException &e) { + env->ThrowNew(env->FindClass("java/lang/UnsupportedOperationException"), e.what()); +} +void raise_java_exception(JNIEnv *env, direct_bt::IndexOutOfBoundsException &e) { + env->ThrowNew(env->FindClass("java/lang/IndexOutOfBoundsException"), e.what()); +} +void raise_java_exception(JNIEnv *env, std::bad_alloc &e) { + env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), e.what()); +} +void raise_java_exception(JNIEnv *env, direct_bt::BluetoothException &e) { env->ThrowNew(env->FindClass("org/tinyb/BluetoothException"), e.what()); } +void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e) { + raise_java_exception(env, e); +} +void raise_java_runtime_exception(JNIEnv *env, direct_bt::RuntimeException &e) { + raise_java_exception(env, e); +} +void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e) { + raise_java_exception(env, e); +} +void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e) { + raise_java_exception(env, e); +} +void raise_java_bluetooth_exception(JNIEnv *env, direct_bt::BluetoothException &e) { + raise_java_exception(env, e); +} + void rethrow_and_raise_java_exception(JNIEnv *env) { // std::exception_ptr e = std::current_exception(); try { // std::rethrow_exception(e); throw; // re-throw current exception } catch (std::bad_alloc &e) { \ - raise_java_oom_exception(env, e); + raise_java_exception(env, e); + } catch (direct_bt::InternalError &e) { + raise_java_exception(env, e); + } catch (direct_bt::NullPointerException &e) { + raise_java_exception(env, e); + } catch (direct_bt::IllegalArgumentException &e) { + raise_java_exception(env, e); + } catch (direct_bt::IllegalStateException &e) { + raise_java_exception(env, e); + } catch (direct_bt::UnsupportedOperationException &e) { + raise_java_exception(env, e); + } catch (direct_bt::IndexOutOfBoundsException &e) { + raise_java_exception(env, e); } catch (direct_bt::BluetoothException &e) { - raise_java_bluetooth_exception(env, e); + raise_java_exception(env, e); } catch (direct_bt::RuntimeException &e) { - raise_java_runtime_exception(env, e); + raise_java_exception(env, e); } catch (std::runtime_error &e) { - raise_java_runtime_exception(env, e); + raise_java_exception(env, e); } catch (std::invalid_argument &e) { - raise_java_invalid_arg_exception(env, e); + raise_java_exception(env, e); } catch (std::exception &e) { raise_java_exception(env, e); } catch (std::string &msg) { @@ -244,12 +280,12 @@ bool java_exception_check(JNIEnv *env, const char* file, int line) { jthrowable e = env->ExceptionOccurred(); if( nullptr != e ) { -#ifdef VERBOSE_ON - DBG_PRINT("Java exception occurred @ %s : %d and forwarded.", file, line); +#if 1 + INFO_PRINT("Java exception occurred @ %s : %d and forwarded.", file, line); // ExceptionDescribe prints an exception and a backtrace of the stack to a system error-reporting channel, such as stderr. // The pending exception is cleared as a side-effect of calling this function. This is a convenience routine provided for debugging. env->ExceptionDescribe(); -#endif /* VERBOSE_ON */ +#endif env->ExceptionClear(); // just be sure, to have same side-effects env->Throw(e); // re-throw the java exception - java side! return true; diff --git a/java/jni/helper_base.hpp b/java/jni/helper_base.hpp index 18ae8679..0fa56095 100644 --- a/java/jni/helper_base.hpp +++ b/java/jni/helper_base.hpp @@ -241,6 +241,18 @@ jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, std::vector<std::sha } void raise_java_exception(JNIEnv *env, std::exception &e); +void raise_java_exception(JNIEnv *env, std::runtime_error &e); +void raise_java_exception(JNIEnv *env, direct_bt::RuntimeException &e); +void raise_java_exception(JNIEnv *env, direct_bt::InternalError &e); +void raise_java_exception(JNIEnv *env, direct_bt::NullPointerException &e); +void raise_java_exception(JNIEnv *env, direct_bt::IllegalArgumentException &e); +void raise_java_exception(JNIEnv *env, std::invalid_argument &e); +void raise_java_exception(JNIEnv *env, direct_bt::IllegalStateException &e); +void raise_java_exception(JNIEnv *env, direct_bt::UnsupportedOperationException &e); +void raise_java_exception(JNIEnv *env, direct_bt::IndexOutOfBoundsException &e); +void raise_java_exception(JNIEnv *env, std::bad_alloc &e); +void raise_java_exception(JNIEnv *env, direct_bt::BluetoothException &e); + void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e); void raise_java_runtime_exception(JNIEnv *env, direct_bt::RuntimeException &e); void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e); |