summaryrefslogtreecommitdiffstats
path: root/java/jni
diff options
context:
space:
mode:
Diffstat (limited to 'java/jni')
-rw-r--r--java/jni/BluetoothAdapter.cxx3
-rw-r--r--java/jni/BluetoothDevice.cxx5
-rw-r--r--java/jni/BluetoothGattCharacteristic.cxx5
-rw-r--r--java/jni/BluetoothGattDescriptor.cxx4
-rw-r--r--java/jni/BluetoothGattService.cxx5
-rw-r--r--java/jni/BluetoothManager.cxx5
-rw-r--r--java/jni/BluetoothObject.cxx2
-rw-r--r--java/jni/helper.cxx10
-rw-r--r--java/jni/helper.hpp (renamed from java/jni/helper.h)14
9 files changed, 27 insertions, 26 deletions
diff --git a/java/jni/BluetoothAdapter.cxx b/java/jni/BluetoothAdapter.cxx
index 1ac72773..8538ba8c 100644
--- a/java/jni/BluetoothAdapter.cxx
+++ b/java/jni/BluetoothAdapter.cxx
@@ -28,7 +28,7 @@
#include "tinyb_BluetoothAdapter.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -63,7 +63,6 @@ jobject Java_tinyb_BluetoothAdapter_getDevices(JNIEnv *env, jobject obj)
BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
std::vector<std::unique_ptr<BluetoothDevice>> array = obj_adapter->get_devices();
jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array,
- "BluetoothDevice",
"(J)V");
return result;
diff --git a/java/jni/BluetoothDevice.cxx b/java/jni/BluetoothDevice.cxx
index 9971c6ed..da15cd00 100644
--- a/java/jni/BluetoothDevice.cxx
+++ b/java/jni/BluetoothDevice.cxx
@@ -29,7 +29,7 @@
#include "tinyb_BluetoothDevice.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -104,7 +104,6 @@ jobject Java_tinyb_BluetoothDevice_getServices(JNIEnv *env, jobject obj)
BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj);
std::vector<std::unique_ptr<BluetoothGattService>> array = obj_device->get_services();
jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array,
- "BluetoothGattService",
"(J)V");
return result;
@@ -259,7 +258,7 @@ jobject Java_tinyb_BluetoothDevice_getAdapter(JNIEnv *env, jobject obj)
BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj);
BluetoothAdapter *obj_adapter = obj_device->get_adapter().clone();
- jclass b_adapter_class = search_class(env, "BluetoothAdapter");
+ jclass b_adapter_class = search_class(env, *obj_adapter);
jmethodID b_adapter_ctor = search_method(env, b_adapter_class, "<init>",
"(J)V", false);
jobject result = env->NewObject(b_adapter_class, b_adapter_ctor, (jlong)obj_adapter);
diff --git a/java/jni/BluetoothGattCharacteristic.cxx b/java/jni/BluetoothGattCharacteristic.cxx
index b0c4a0b0..0c7dfb74 100644
--- a/java/jni/BluetoothGattCharacteristic.cxx
+++ b/java/jni/BluetoothGattCharacteristic.cxx
@@ -29,7 +29,7 @@
#include "tinyb_BluetoothGattCharacteristic.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -105,7 +105,7 @@ jobject Java_tinyb_BluetoothGattCharacteristic_getService(JNIEnv *env, jobject o
getInstance<BluetoothGattCharacteristic>(env, obj);
BluetoothGattService *obj_gatt_serv = obj_gatt_char->get_service().clone();
- jclass b_gatt_serv_class = search_class(env, "BluetoothGattService");
+ jclass b_gatt_serv_class = search_class(env, *obj_gatt_serv);
jmethodID b_gatt_serv_ctor = search_method(env, b_gatt_serv_class, "<init>",
"(J)V", false);
jobject result = env->NewObject(b_gatt_serv_class, b_gatt_serv_ctor, (jlong)obj_gatt_serv);
@@ -164,7 +164,6 @@ jobject Java_tinyb_BluetoothGattCharacteristic_getDescriptors(JNIEnv *env, jobje
std::vector<std::unique_ptr<BluetoothGattDescriptor>> array = obj_gatt_char->get_descriptors();
jobject result = convert_vector_to_jobject<BluetoothGattDescriptor>(env, array,
- "BluetoothGattDescriptor",
"(J)V");
return result;
}
diff --git a/java/jni/BluetoothGattDescriptor.cxx b/java/jni/BluetoothGattDescriptor.cxx
index 609e2715..05c45c3c 100644
--- a/java/jni/BluetoothGattDescriptor.cxx
+++ b/java/jni/BluetoothGattDescriptor.cxx
@@ -28,7 +28,7 @@
#include "tinyb_BluetoothGattDescriptor.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -86,7 +86,7 @@ jobject Java_tinyb_BluetoothGattDescriptor_getCharacteristic(JNIEnv *env, jobjec
BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj);
BluetoothGattCharacteristic *obj_gatt_char = obj_gatt_desc->get_characteristic().clone();
- jclass b_gatt_char_class = search_class(env, "BluetoothGattCharacteristic");
+ jclass b_gatt_char_class = search_class(env, *obj_gatt_char);
jmethodID b_gatt_char_ctor = search_method(env, b_gatt_char_class, "<init>",
"(J)V", false);
jobject result = env->NewObject(b_gatt_char_class, b_gatt_char_ctor, (jlong)obj_gatt_char);
diff --git a/java/jni/BluetoothGattService.cxx b/java/jni/BluetoothGattService.cxx
index d60c6afe..c42e5c18 100644
--- a/java/jni/BluetoothGattService.cxx
+++ b/java/jni/BluetoothGattService.cxx
@@ -29,7 +29,7 @@
#include "tinyb_BluetoothGattService.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -58,7 +58,7 @@ jobject Java_tinyb_BluetoothGattService_getDevice(JNIEnv *env, jobject obj)
BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj);
BluetoothDevice *obj_device = obj_gatt_serv->get_device().clone();
- jclass b_device_class = search_class(env, "BluetoothDevice");
+ jclass b_device_class = search_class(env, *obj_device);
jmethodID b_device_ctor = search_method(env, b_device_class, "<init>",
"(J)V", false);
jobject result = env->NewObject(b_device_class, b_device_ctor, (jlong)obj_device);
@@ -83,7 +83,6 @@ jobject Java_tinyb_BluetoothGattService_getCharacteristics(JNIEnv *env, jobject
std::vector<std::unique_ptr<BluetoothGattCharacteristic>> array =
obj_gatt_serv->get_characteristics();
jobject result = convert_vector_to_jobject<BluetoothGattCharacteristic>(env, array,
- "BluetoothGattCharacteristic",
"(J)V");
return result;
}
diff --git a/java/jni/BluetoothManager.cxx b/java/jni/BluetoothManager.cxx
index 476f9baa..6788f2c1 100644
--- a/java/jni/BluetoothManager.cxx
+++ b/java/jni/BluetoothManager.cxx
@@ -29,7 +29,7 @@
#include "tinyb_BluetoothManager.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
@@ -72,7 +72,6 @@ jobject Java_tinyb_BluetoothManager_getAdapters(JNIEnv *env, jobject obj)
std::vector<std::unique_ptr<BluetoothAdapter>> array = manager->get_adapters();
jobject result = convert_vector_to_jobject<BluetoothAdapter>(env, array,
- "BluetoothAdapter",
"(J)V");
return result;
}
@@ -83,7 +82,6 @@ jobject Java_tinyb_BluetoothManager_getDevices(JNIEnv *env, jobject obj)
std::vector<std::unique_ptr<BluetoothDevice>> array = manager->get_devices();
jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array,
- "BluetoothDevice",
"(J)V");
return result;
@@ -95,7 +93,6 @@ jobject Java_tinyb_BluetoothManager_getServices(JNIEnv *env, jobject obj)
std::vector<std::unique_ptr<BluetoothGattService>> array = manager->get_services();
jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array,
- "BluetoothGattService",
"(J)V");
return result;
}
diff --git a/java/jni/BluetoothObject.cxx b/java/jni/BluetoothObject.cxx
index ce62db43..cdd51614 100644
--- a/java/jni/BluetoothObject.cxx
+++ b/java/jni/BluetoothObject.cxx
@@ -26,7 +26,7 @@
#include "tinyb_BluetoothObject.h"
-#include "helper.h"
+#include "helper.hpp"
using namespace tinyb;
diff --git a/java/jni/helper.cxx b/java/jni/helper.cxx
index d5cea134..3344ab85 100644
--- a/java/jni/helper.cxx
+++ b/java/jni/helper.cxx
@@ -27,7 +27,7 @@
#include <stdexcept>
#include <vector>
-#include "helper.h"
+#include "helper.hpp"
jfieldID getInstanceField(JNIEnv *env, jobject obj)
{
@@ -36,12 +36,18 @@ jfieldID getInstanceField(JNIEnv *env, jobject obj)
return env->GetFieldID(clazz, "nativeInstance", "J");
}
+jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object)
+{
+ return search_class(env, object.get_java_class().c_str());
+}
+
jclass search_class(JNIEnv *env, const char *clazz_name)
{
jclass clazz = env->FindClass(clazz_name);
if (clazz == NULL)
{
- throw std::runtime_error("no class found\n");
+ std::string error = "no class found: "; error += clazz_name;
+ throw std::runtime_error(error);
}
return clazz;
}
diff --git a/java/jni/helper.h b/java/jni/helper.hpp
index accf2609..a6b698f1 100644
--- a/java/jni/helper.h
+++ b/java/jni/helper.hpp
@@ -22,14 +22,15 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-#ifndef _HELPER_H_
-#define _HELPER_H_
+#pragma once
#include <vector>
+#include "tinyb/BluetoothObject.hpp"
jfieldID getInstanceField(JNIEnv *env, jobject obj);
jclass search_class(JNIEnv *env, const char *clazz_name);
+jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object);
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name,
const char *prototype, bool is_static);
jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name,
@@ -72,14 +73,17 @@ jobject generic_clone(JNIEnv *env, jobject obj, const char *class_name)
template <typename T>
jobject convert_vector_to_jobject(JNIEnv *env, std::vector<std::unique_ptr<T>>& array,
- const char *class_name, const char *ctor_prototype)
+ const char *ctor_prototype)
{
unsigned int array_size = array.size();
jmethodID arraylist_add;
jobject result = get_new_arraylist(env, array_size, &arraylist_add);
- jclass clazz = search_class(env, class_name);
+ if (array_size == 0)
+ return result;
+
+ jclass clazz = search_class(env, T::java_class().c_str());
jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false);
for (unsigned int i = 0; i < array_size; ++i)
@@ -94,5 +98,3 @@ jobject convert_vector_to_jobject(JNIEnv *env, std::vector<std::unique_ptr<T>>&
}
return result;
}
-
-#endif