summaryrefslogtreecommitdiffstats
path: root/java/jni/helper.cxx
diff options
context:
space:
mode:
authorAndrei Vasiliu <[email protected]>2016-02-03 18:24:03 +0200
committerAndrei Vasiliu <[email protected]>2016-02-04 18:37:14 +0200
commitd7d995948a4de13241cfd1e680b640d4969d4bab (patch)
tree0022085c9285c968415bb8bcce12fe3691443447 /java/jni/helper.cxx
parent636680b1c451702431bd73db6c0d763ca102b61b (diff)
jni: Implemented getObject* functions and added defensive checks
Signed-off-by: Andrei Vasiliu <[email protected]>
Diffstat (limited to 'java/jni/helper.cxx')
-rw-r--r--java/jni/helper.cxx67
1 files changed, 60 insertions, 7 deletions
diff --git a/java/jni/helper.cxx b/java/jni/helper.cxx
index 2fd70fab..c701b931 100644
--- a/java/jni/helper.cxx
+++ b/java/jni/helper.cxx
@@ -44,7 +44,7 @@ jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object)
jclass search_class(JNIEnv *env, const char *clazz_name)
{
jclass clazz = env->FindClass(clazz_name);
- if (clazz == NULL)
+ if (!clazz)
{
std::string error = "no class found: "; error += clazz_name;
throw std::runtime_error(error);
@@ -56,7 +56,7 @@ jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name,
const char *prototype, bool is_static)
{
jmethodID method;
- if(is_static)
+ if (is_static)
{
method = env->GetStaticMethodID(clazz, method_name, prototype);
}
@@ -65,7 +65,7 @@ jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name,
method = env->GetMethodID(clazz, method_name, prototype);
}
- if(method == NULL)
+ if (!method)
{
throw std::runtime_error("no method found\n");
}
@@ -77,7 +77,7 @@ jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name,
const char *type, bool is_static)
{
jfieldID field;
- if(is_static)
+ if (is_static)
{
field = env->GetStaticFieldID(clazz, field_name, type);
}
@@ -86,7 +86,7 @@ jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name,
field = env->GetFieldID(clazz, field_name, type);
}
- if(field == NULL)
+ if (!field)
{
throw std::runtime_error("no method found\n");
}
@@ -117,6 +117,60 @@ bool from_jboolean_to_bool(jboolean val)
return result;
}
+std::string from_jstring_to_string(JNIEnv *env, jstring str)
+{
+ jboolean is_copy = JNI_TRUE;
+ const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy);
+ if (!str_chars)
+ {
+ throw std::runtime_error("GetStringUTFChars returned NULL\n");
+ }
+ const std::string string_to_write = std::string(str_chars);
+
+ env->ReleaseStringUTFChars(str, str_chars);
+
+ return string_to_write;
+}
+
+tinyb::BluetoothType from_int_to_btype(int type)
+{
+ tinyb::BluetoothType result = tinyb::BluetoothType::NONE;
+
+ switch (type)
+ {
+ case 0:
+ result = tinyb::BluetoothType::NONE;
+ break;
+
+ case 1:
+ result = tinyb::BluetoothType::ADAPTER;
+ break;
+
+ case 2:
+ result = tinyb::BluetoothType::DEVICE;
+ break;
+
+ case 3:
+ result = tinyb::BluetoothType::GATT_SERVICE;
+ break;
+
+ case 4:
+ result = tinyb::BluetoothType::GATT_CHARACTERISTIC;
+ break;
+
+ case 5:
+ result = tinyb::BluetoothType::GATT_CHARACTERISTIC;
+ break;
+
+ default:
+ result = tinyb::BluetoothType::NONE;
+ break;
+ }
+
+ return result;
+}
+
+
jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
{
jclass b_type_enum = search_class(env, JAVA_PACKAGE "/BluetoothType");
@@ -127,14 +181,13 @@ jobject get_bluetooth_type(JNIEnv *env, const char *field_name)
return result;
}
-
jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add)
{
jclass arraylist_class = search_class(env, "Ljava/util/ArrayList;");
jmethodID arraylist_ctor = search_method(env, arraylist_class, "<init>", "(I)V", false);
jobject result = env->NewObject(arraylist_class, arraylist_ctor, size);
- if (result == NULL)
+ if (!result)
{
throw std::runtime_error("cannot create instance of class\n");
}