Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
helper_base.cxx
Go to the documentation of this file.
1 /*
2  * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Author: Sven Gothel <sgothel@jausoft.com>
6  * Copyright (c) 2020 Gothel Software e.K.
7  * Copyright (c) 2020 ZAFENA AB
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include <jni.h>
30 #include <memory>
31 #include <stdexcept>
32 #include <vector>
33 
34 #include "helper_base.hpp"
35 
36 #define JAVA_MAIN_PACKAGE "org/direct_bt"
37 
38 jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
39 {
40  jclass b_type_enum = jau::search_class(env, JAVA_MAIN_PACKAGE "/BTType");
41 
42  jfieldID b_type_field = jau::search_field(env, b_type_enum, field_name, "L" JAVA_MAIN_PACKAGE "/BTType;", true);
43 
44  jobject result = env->GetStaticObjectField(b_type_enum, b_type_field);
45  env->DeleteLocalRef(b_type_enum);
46  return result;
47 }
48 
49 void raise_java_exception(JNIEnv *env, const direct_bt::BTException &e, const char* file, int line) {
51  env->ThrowNew(env->FindClass("org/direct_bt/BTException"), e.what());
52 }
53 
54 static std::string _unknown_exception_type_msg("Unknown exception type");
55 
56 void rethrow_and_raise_java_exception_impl(JNIEnv *env, const char* file, int line) {
57  // std::exception_ptr e = std::current_exception();
58  try {
59  // std::rethrow_exception(e);
60  throw; // re-throw current exception
61  } catch (const std::bad_alloc &e) {
62  jau::raise_java_exception(env, e, file, line);
63  } catch (const jau::OutOfMemoryError &e) {
64  jau::raise_java_exception(env, e, file, line);
65  } catch (const jau::InternalError &e) {
66  jau::raise_java_exception(env, e, file, line);
67  } catch (const jau::NullPointerException &e) {
68  jau::raise_java_exception(env, e, file, line);
69  } catch (const jau::IllegalArgumentException &e) {
70  jau::raise_java_exception(env, e, file, line);
71  } catch (const jau::IllegalStateException &e) {
72  jau::raise_java_exception(env, e, file, line);
73  } catch (const jau::UnsupportedOperationException &e) {
74  jau::raise_java_exception(env, e, file, line);
75  } catch (const jau::IndexOutOfBoundsException &e) {
76  jau::raise_java_exception(env, e, file, line);
77  } catch (const direct_bt::BTException &e) {
78  raise_java_exception(env, e, file, line);
79  } catch (const jau::RuntimeException &e) {
80  jau::raise_java_exception(env, e, file, line);
81  } catch (const std::runtime_error &e) {
82  jau::raise_java_exception(env, e, file, line);
83  } catch (const std::invalid_argument &e) {
84  jau::raise_java_exception(env, e, file, line);
85  } catch (const std::exception &e) {
86  jau::raise_java_exception(env, e, file, line);
87  } catch (const std::string &msg) {
89  env->ThrowNew(env->FindClass("java/lang/Error"), msg.c_str());
90  } catch (const char *msg) {
92  env->ThrowNew(env->FindClass("java/lang/Error"), msg);
93  } catch (...) {
95  env->ThrowNew(env->FindClass("java/lang/Error"), _unknown_exception_type_msg.c_str());
96  }
97 }
rethrow_and_raise_java_exception_impl
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.
Definition: helper_base.cxx:56
jau::RuntimeException::what
virtual const char * what() const noexcept override
Definition: basic_types.hpp:88
jau::IllegalArgumentException
Definition: basic_types.hpp:111
helper_base.hpp
jau::IndexOutOfBoundsException
Definition: basic_types.hpp:129
raise_java_exception
void raise_java_exception(JNIEnv *env, const direct_bt::BTException &e, const char *file, int line)
Definition: helper_base.cxx:49
jau::print_native_caught_exception_fwd2java
void print_native_caught_exception_fwd2java(const std::exception &e, const char *file, int line)
jau::InternalError
Definition: basic_types.hpp:93
direct_bt::BTException
Definition: BTTypes0.hpp:43
jau::search_class
jclass search_class(JNIEnv *env, const char *clazz_name)
get_bluetooth_type
jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
Definition: helper_base.cxx:38
jau::raise_java_exception
void raise_java_exception(JNIEnv *env, const std::exception &e, const char *file, int line)
jau::RuntimeException
Definition: basic_types.hpp:66
JAVA_MAIN_PACKAGE
#define JAVA_MAIN_PACKAGE
Definition: helper_base.cxx:36
jau::IllegalStateException
Definition: basic_types.hpp:117
jau::OutOfMemoryError
Definition: basic_types.hpp:99
jau::search_field
jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, const char *type, bool is_static)
_unknown_exception_type_msg
static std::string _unknown_exception_type_msg("Unknown exception type")
jau::UnsupportedOperationException
Definition: basic_types.hpp:123
jau::NullPointerException
Definition: basic_types.hpp:105