diff options
author | Sven Gothel <[email protected]> | 2020-09-19 02:38:40 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-09-19 02:38:40 +0200 |
commit | 98a699f66fefefa246d4c363212532efad9e0abb (patch) | |
tree | da46a9c997cc8d78ae67f35daae052e1224e03d5 /java/jni/helper_base.hpp | |
parent | 0a9ac585e0481b165f5bdb7184f4e0eb1dc79cc9 (diff) |
C++/JNI: helper_base.hpp/cxx: Rework raise_java_exception(..) and rethrow_and_raise_java_exception[->_impl](..) ..
Commit 5cfe7e13c4dbf07360d928e421050de5e00684a1 intention was noble,
however, 'inline' is not guaranteed and doesn't work here.
I.e. the desired inline reduction with automatic elision and using __FILE__ and __LINE__ macros is not supported.
Instead, move the functions back to helper_base.cxx and add paramter for __FILE__ and __LINE__
to be passed by the only caller macro 'rethrow_and_raise_java_exception(E)'.
The latter simply explodes to 'rethrow_and_raise_java_exception_impl((E), __FILE__, __LINE__)',
which serves the purposed of having exposed the file and line position
where the exceotion got caught.
Also support api/tinyb/BluetoothException, rendering commit 3139f93409cac61ba5b80caf506375d94eff8778 valid,
i.e. use rethrow_and_raise_java_exception(..) in the tinyb C++/JNI code instead of exploded exception case blocks.
Diffstat (limited to 'java/jni/helper_base.hpp')
-rw-r--r-- | java/jni/helper_base.hpp | 138 |
1 files changed, 22 insertions, 116 deletions
diff --git a/java/jni/helper_base.hpp b/java/jni/helper_base.hpp index 9d920f4d..45372406 100644 --- a/java/jni/helper_base.hpp +++ b/java/jni/helper_base.hpp @@ -34,7 +34,8 @@ #include <functional> #include <jni.h> -#include "BasicTypes.hpp" +#include "direct_bt/BasicTypes.hpp" +#include "tinyb/BluetoothException.hpp" /** * Return true if a java exception occurred, otherwise false. @@ -240,126 +241,31 @@ jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, std::vector<std::sha return result; } -inline void print_exception(const std::exception &e) { - fprintf(stderr, "Native exception occurred @ %s : %d and forwarded to Java: %s\n", __FILE__, __LINE__, e.what()); fflush(stderr); -} -inline void print_exception(const std::string &msg) { - fprintf(stderr, "Native exception occurred @ %s : %d and forwarded to Java: %s\n", __FILE__, __LINE__, msg.c_str()); fflush(stderr); -} -inline void print_exception(const char * cmsg) { - fprintf(stderr, "Native exception occurred @ %s : %d and forwarded to Java: %s\n", __FILE__, __LINE__, cmsg); fflush(stderr); -} - -inline void raise_java_exception(JNIEnv *env, const std::exception &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/Error"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const std::runtime_error &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::RuntimeException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::InternalError &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/InternalError"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::NullPointerException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/NullPointerException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::IllegalArgumentException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const std::invalid_argument &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::IllegalStateException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/IllegalStateException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::UnsupportedOperationException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/UnsupportedOperationException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::IndexOutOfBoundsException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/IndexOutOfBoundsException"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const std::bad_alloc &e) { - print_exception(e); - env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), e.what()); -} -inline void raise_java_exception(JNIEnv *env, const direct_bt::BluetoothException &e) { - print_exception(e); - env->ThrowNew(env->FindClass("org/tinyb/BluetoothException"), e.what()); -} - -inline void raise_java_runtime_exception(JNIEnv *env, const std::runtime_error &e) { - raise_java_exception(env, e); -} -inline void raise_java_runtime_exception(JNIEnv *env, const direct_bt::RuntimeException &e) { - raise_java_exception(env, e); -} -inline void raise_java_oom_exception(JNIEnv *env, const std::bad_alloc &e) { - raise_java_exception(env, e); -} -inline void raise_java_invalid_arg_exception(JNIEnv *env, const std::invalid_argument &e) { - raise_java_exception(env, e); -} -inline void raise_java_bluetooth_exception(JNIEnv *env, const direct_bt::BluetoothException &e) { - raise_java_exception(env, e); -} +void raise_java_exception(JNIEnv *env, const std::exception &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const std::runtime_error &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::RuntimeException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::InternalError &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::NullPointerException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::IllegalArgumentException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const std::invalid_argument &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::IllegalStateException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::UnsupportedOperationException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::IndexOutOfBoundsException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const std::bad_alloc &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const direct_bt::BluetoothException &e, const char* file, int line); +void raise_java_exception(JNIEnv *env, const tinyb::BluetoothException &e, const char* file, int line); -extern const std::string unknown_exception_type_msg; +/** + * Re-throw current exception and raise respective java exception + * using any matching function above. + */ +void rethrow_and_raise_java_exception_impl(JNIEnv *env, const char* file, int line); /** * Re-throw current exception and raise respective java exception * using any matching function above. */ -inline 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_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_exception(env, e); - } catch (direct_bt::RuntimeException &e) { - raise_java_exception(env, e); - } catch (std::runtime_error &e) { - raise_java_exception(env, e); - } catch (std::invalid_argument &e) { - raise_java_exception(env, e); - } catch (std::exception &e) { - raise_java_exception(env, e); - } catch (std::string &msg) { - print_exception(msg); - env->ThrowNew(env->FindClass("java/lang/Error"), msg.c_str()); - } catch (const char *msg) { - print_exception(msg); - env->ThrowNew(env->FindClass("java/lang/Error"), msg); - } catch (...) { - print_exception(unknown_exception_type_msg); - env->ThrowNew(env->FindClass("java/lang/Error"), unknown_exception_type_msg.c_str()); - } -} +#define rethrow_and_raise_java_exception(E) rethrow_and_raise_java_exception_impl((E), __FILE__, __LINE__) +// inline void rethrow_and_raise_java_exception(JNIEnv *env) { rethrow_and_raise_java_exception_impl(env, __FILE__, __LINE__); } #endif /* HELPER_BASE_HPP_ */ |