29 #ifndef JAU_HELPER_JNI_HPP_
30 #define JAU_HELPER_JNI_HPP_
101 #define rethrow_and_raise_java_exception_jau(E) rethrow_and_raise_java_exception_jauimpl((E), __FILE__, __LINE__)
108 jfieldID
getField(JNIEnv *env, jobject obj,
const char* field_name,
const char* field_signature);
110 return getField(env, obj,
"nativeInstance",
"J");
118 const char *prototype,
bool is_static);
119 jfieldID
search_field(JNIEnv *env, jclass clazz,
const char *field_name,
120 const char *type,
bool is_static);
139 jmethodID mNotifyDeleted;
142 static inline void check(
const std::shared_ptr<JavaAnon> & shref,
const char* file,
int line) {
143 if(
nullptr == shref ) {
144 throw RuntimeException(
"JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
146 if( 0 == shref.use_count() ) {
147 throw RuntimeException(
"JavaGlobalObj::check: Empty shared-JavaAnonObj", file, line);
150 if(
nullptr == obj ) {
154 static bool isValid(
const std::shared_ptr<JavaAnon> & shref) noexcept {
155 if(
nullptr == shref || 0 == shref.use_count() ) {
159 if(
nullptr == obj ) {
165 : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted_) { }
175 const uint64_t ref = (uint64_t)(
void*)javaObjectRef.
getObject();
194 static jobject
GetObject(
const std::shared_ptr<JavaAnon> & shref) noexcept {
199 static jclass
GetClass(
const std::shared_ptr<JavaAnon> & shref) noexcept {
208 template <
typename T>
212 T *t =
reinterpret_cast<T *
>(instance);
220 template <
typename T>
224 return reinterpret_cast<T *
>(instance);
227 template <
typename T>
233 jlong instance =
reinterpret_cast<jlong
>(t);
241 template <
typename T>
244 T *t =
reinterpret_cast<T *
>(instance);
251 template <
typename T>
254 jlong jobj = env->GetLongField(obj,
getField(env, obj, field_name,
"J"));
256 return reinterpret_cast<T *
>(jobj);
259 template <
typename T>
260 void setObjectRef(JNIEnv *env, jobject obj, T *t,
const char* field_name)
262 jlong jobj =
reinterpret_cast<jlong
>(t);
263 env->SetLongField(obj,
getField(env, obj, field_name,
"J"), jobj);
267 template <
typename T>
271 T *t =
reinterpret_cast<T *
>(instance);
278 template <
typename T>
282 return reinterpret_cast<T *
>(instance);
285 template <
typename T>
291 jlong instance =
reinterpret_cast<jlong
>(t);
299 template <
typename T>
302 T *obj_generic = getInstance<T>(env, obj);
303 T *copy_generic = obj_generic->clone();
305 jclass generic_class =
search_class(env, *copy_generic);
306 jmethodID generic_ctor =
search_method(env, generic_class,
"<init>",
"(J)V",
false);
308 jobject result = env->NewObject(generic_class, generic_ctor, (jlong)copy_generic);
321 template <
typename T>
323 const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, T*)> ctor)
325 jclass clazz =
search_class(env, T::java_class().c_str());
326 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
328 jobject
object = ctor(env, clazz, clazz_ctor, elem);
338 template <
typename T>
341 nsize_t array_size = array.size();
343 jmethodID arraylist_add;
346 if (0 == array_size) {
350 jau::for_each(array.begin(), array.end(), [&](
typename T::value_type & elem){
351 std::shared_ptr<JavaAnon> objref = elem->getJavaObject();
352 if ( nullptr == objref ) {
353 throw InternalError(
"JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE);
360 template <
typename T,
typename U>
363 unsigned int array_size = array.size();
365 jmethodID arraylist_add;
373 jclass clazz =
search_class(env, U::java_class().c_str());
374 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
376 for (
unsigned int i = 0; i < array_size; ++i)
378 U *elem = array[i].release();
379 jobject
object = env->NewObject(clazz, clazz_ctor, (jlong)elem);
384 env->CallBooleanMethod(result, arraylist_add,
object);
390 template <
typename T,
typename U>
392 const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, U*)> ctor)
394 unsigned int array_size = array.size();
396 jmethodID arraylist_add;
404 jclass clazz =
search_class(env, U::java_class().c_str());
405 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
407 for (
unsigned int i = 0; i < array_size; ++i)
409 U *elem = array[i].release();
410 jobject
object = ctor(env, clazz, clazz_ctor, elem);
415 env->CallBooleanMethod(result, arraylist_add,
object);
421 template <
typename T,
typename U>
423 const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, U*)> ctor)
425 unsigned int array_size = array.size();
427 jmethodID arraylist_add;
435 jclass clazz =
search_class(env, U::java_class().c_str());
436 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
438 for (
unsigned int i = 0; i < array_size; ++i)
440 U *elem = array[i].get();
441 jobject
object = ctor(env, clazz, clazz_ctor, elem);
446 env->CallBooleanMethod(result, arraylist_add,
object);