26#ifndef JAU_HELPER_JNI_HPP_
27#define JAU_HELPER_JNI_HPP_
107 #define rethrow_and_raise_java_exception_jau(E) jau::jni::rethrow_and_raise_java_exception_jauimpl((E), __FILE__, __LINE__)
114 jfieldID
getField(JNIEnv *env, jobject obj,
const char* field_name,
const char* field_signature);
116 return getField(env, obj,
"nativeInstance",
"J");
118 jobject
getObjectFieldValue(JNIEnv *env, jobject obj,
const char* field_name,
const char* field_signature);
128 const char *prototype,
bool is_static);
129 jfieldID
search_field(JNIEnv *env, jclass clazz,
const char *field_name,
130 const char *type,
bool is_static);
146 template<
class byte_container_type,
147 std::enable_if_t<std::is_integral_v<typename byte_container_type::value_type> &&
148 std::is_convertible_v<typename byte_container_type::value_type, jbyte>,
151 const size_t data_size = data.size();
152 jbyteArray jdata = env->NewByteArray((jsize)data_size);
153 env->SetByteArrayRegion(jdata, 0, (jsize)data_size, (
const jbyte *)data.data());
169 jmethodID mNotifyDeleted;
173 if(
nullptr == shref ) {
174 throw RuntimeException(
"JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
176 if( 0 == shref.use_count() ) {
177 throw RuntimeException(
"JavaGlobalObj::check: Empty shared-JavaAnonObj", file, line);
180 if(
nullptr == obj ) {
185 if(
nullptr == shref ) {
186 throw RuntimeException(
"JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
188 if( 0 == shref.use_count() ) {
189 throw RuntimeException(
"JavaGlobalObj::check: Empty shared-JavaAnonObj", file, line);
192 if(
nullptr == obj ) {
198 if(
nullptr == shref || 0 == shref.use_count() ) {
202 if(
nullptr == obj ) {
208 : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted_) { }
211 : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted_) { }
214 : javaObjectRef(std::move(obj)), mNotifyDeleted(mNotifyDeleted_) { }
224 const uint64_t ref = (uint64_t)(
void*)javaObjectRef.
getObject();
259 template <
typename T>
262 jlong jobj = env->GetLongField(obj,
getField(env, obj, field_name,
"J"));
264 return reinterpret_cast<T *
>(jobj);
267 template <
typename T>
268 void setObjectRef(JNIEnv *env, jobject obj, T *t,
const char* field_name)
270 jlong jobj =
reinterpret_cast<jlong
>(t);
271 env->SetLongField(obj,
getField(env, obj, field_name,
"J"), jobj);
285 template <
typename T>
286 std::shared_ptr<T> *
castInstance(jlong instance,
const bool throw_on_nullptr=
true)
288 std::shared_ptr<T> * ref_ptr =
reinterpret_cast<std::shared_ptr<T> *
>(instance);
289 if( throw_on_nullptr ) {
290 if (
nullptr == ref_ptr) {
308 template <
typename T>
309 std::shared_ptr<T>*
getInstance(JNIEnv *env, jobject obj,
const bool throw_on_nullptr=
true) {
310 const jlong nativeInstance = env->GetLongField(obj,
getInstanceField(env, obj));
312 std::shared_ptr<T>* ref_ptr =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
313 if( throw_on_nullptr ) {
314 if (
nullptr == ref_ptr) {
317 if (
nullptr == *ref_ptr) {
333 template <
typename T>
334 void setInstance(JNIEnv *env, jobject obj,
const std::shared_ptr<T>& t)
339 const jlong instance = (jlong) (intptr_t) &t;
344 const jlong nativeInstance = env->GetLongField(obj, instance_field);
346 std::shared_ptr<T>* other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
347 if(
nullptr != other ) {
351 env->SetLongField(obj, instance_field, instance);
364 template <
typename T>
369 const jlong nativeInstance = env->GetLongField(obj, instance_field);
371 std::shared_ptr<T>* other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
372 if(
nullptr != other ) {
376 env->SetLongField(obj, instance_field, 0);
392 template <
typename T>
395 std::shared_ptr<T>* ref_ptr;
398 std::shared_ptr<T>* ref_ptr_ = ref_ptr;
403 static jlong get_instance(JNIEnv *env, jobject obj) {
404 if(
nullptr != obj ) {
412 static jlong get_instance(JNIEnv *env, jobject obj, jfieldID instance_field) {
413 if(
nullptr != obj ) {
414 const jlong res = env->GetLongField(obj, instance_field);
430 : ref_ptr(
new std::shared_ptr<T>( o.shared_ptr() ) )
435 : ref_ptr( o.ref_ptr )
443 if(
nullptr != ref_ptr ) {
446 ref_ptr =
new std::shared_ptr<T>( o.
shared_ptr() );
454 if(
nullptr != ref_ptr ) {
463 if(
nullptr != ref_ptr ) {
470 : ref_ptr(
new std::shared_ptr<T>( ptr ) )
475 : ref_ptr(
new std::shared_ptr<T>( ref ) )
480 : ref_ptr(
new std::shared_ptr<T>( std::move(ref) ) )
485 if(
nullptr != ref_ptr ) {
488 ref_ptr =
new std::shared_ptr<T>( o );
499 if (
nullptr == ref_ptr) {
509 if (
nullptr == ref_ptr) {
512 if (
nullptr == *ref_ptr) {
532 std::shared_ptr<T> * other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
533 if(
nullptr != other &&
nullptr != *other ) {
536 if( throw_on_nullptr ) {
564 const std::shared_ptr<T>* res = ref_ptr;
575 const jlong res = (jlong) (intptr_t)ref_ptr;
595 if(
nullptr == obj ) {
601 std::shared_ptr<T> * other =
reinterpret_cast<std::shared_ptr<T> *
>( get_instance(env, obj, instance_field) );
602 if(
nullptr != other ) {
615 return nullptr == ref_ptr ||
nullptr == *ref_ptr;
643 return *(ref_ptr->get());
654 return ref_ptr->get();
659 ", obj "+ (
nullptr != ref_ptr ?
jau::to_hexstring(ref_ptr->get()) :
"null" ) +
"]";
667 template <
typename T>
669 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<T>&)> ctor)
671 jclass clazz =
search_class(env, T::java_class().c_str());
672 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
674 jobject
object = ctor(env, clazz, clazz_ctor, elem);
684 template <
typename T>
686 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<T>&)> ctor,
687 const std::shared_ptr<T>& elem)
689 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
691 jobject
object = ctor(env, clazz, clazz_ctor, elem);
701 template <
typename T>
704 nsize_t array_size = array.size();
706 jmethodID arraylist_add;
709 if (0 == array_size) {
713 jau::for_each(array.begin(), array.end(), [&](
typename T::value_type & elem){
714 JavaAnonRef objref = elem->getJavaObject();
715 if ( nullptr == objref ) {
716 throw InternalError(
"JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE);
723 template <
typename T,
typename U>
725 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<U>&)> ctor)
727 const size_t array_size = array.size();
732 const jsize jarray_size = array_size;
734 jmethodID arraylist_add;
737 if (jarray_size == 0)
742 jclass clazz =
search_class(env, U::java_class().c_str());
743 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
745 for (jsize i = 0; i < jarray_size; ++i)
747 jobject
object = ctor(env, clazz, clazz_ctor, array[i] );
752 env->CallBooleanMethod(result, arraylist_add,
object);
758 template <
typename T,
typename U>
761 const size_t array_size = array.size();
766 const jsize jarray_size = array_size;
768 jmethodID arraylist_add;
771 if (jarray_size == 0)
776 for (jsize i = 0; i < jarray_size; ++i)
778 jobject
object = ctor(env, array[i] );
783 env->CallBooleanMethod(result, arraylist_add,
object);
789 template <
typename T,
typename U>
792 const size_t array_size = array.size();
797 const jsize jarray_size = array_size;
799 jmethodID arraylist_add;
802 if (jarray_size == 0)
807 for (jsize i = 0; i < jarray_size; ++i)
809 jobject
object = ctor(env, array[i] );
814 env->CallBooleanMethod(result, arraylist_add,
object);
Class template jau::function is a general-purpose static-polymorphic function wrapper.
jobject getObject() const noexcept
jclass getClass() const noexcept
Pure virtual JavaAnon, hiding Java JNI details from API, to be implemented by JNI module.
Implementation for JavaAnon, by simply wrapping a JNIGlobalRef instance.
~JavaGlobalObj() noexcept override
JavaGlobalObj & operator=(const JavaGlobalObj &o) noexcept=default
JavaGlobalObj(const JNIGlobalRef &obj, jmethodID mNotifyDeleted_) noexcept
static jobject checkAndGetObject(const JavaAnonRef &shref, const char *file, int line)
JavaGlobalObj(JNIGlobalRef &&obj, jmethodID mNotifyDeleted_) noexcept
jobject getObject() const noexcept
static jobject GetObject(const JavaAnonRef &shref) noexcept
static bool isValid(const JavaAnonRef &shref) noexcept
JavaGlobalObj(jobject obj, jmethodID mNotifyDeleted_) noexcept
const JNIGlobalRef & getJavaObject() const noexcept
std::string toString() const noexcept override
JavaGlobalObj(JavaGlobalObj &&o) noexcept=default
static void check(const JavaAnonRef &shref, const char *file, int line)
JavaGlobalObj(const JavaGlobalObj &o) noexcept=default
static JNIGlobalRef GetJavaObject(const JavaAnonRef &shref) noexcept
JNIGlobalRef getJavaObject() noexcept
static jclass GetClass(const JavaAnonRef &shref) noexcept
jclass getClass() const noexcept
JavaGlobalObj & operator=(JavaGlobalObj &&o) noexcept=default
Sharing the anonymous Java object (JavaAnon), i.e.
A std::shared_ptr<T> storage instance to be copied from and released into a java object's long native...
void null_check2() const
Throws an exception if either this instances shared_ptr<T> storage or the managed object reference is...
void null_check1() const
Throws an exception if this instances shared_ptr<T> storage is nullptr.
bool is_null() const noexcept
Returns true if either this instances shared_ptr<T> storage or the managed object reference is nullpt...
shared_ptr_ref(jlong nativeInstance, const bool throw_on_nullptr=true)
Constructs a new instance, copying the instance from the given java long nativeInstance value,...
T * operator->()
Provides access to pointer of stored T.
shared_ptr_ref(const std::shared_ptr< T > &ref) noexcept
Constructs a new instance, copying the given std::shared_ptr<T>.
shared_ptr_ref(std::shared_ptr< T > &&ref) noexcept
Constructs a new instance, moving the given std::shared_ptr<T>.
const std::shared_ptr< T > & shared_ptr() const
Provides access to const reference of shared_ptr<T>, r-value.
shared_ptr_ref(const shared_ptr_ref &o) noexcept
Copy constructor.
std::shared_ptr< T > * pointer() noexcept
Provides access to the shared_ptr<T> pointer, l-value of storage.
std::shared_ptr< T > * release() noexcept
Release ownership and returns the shared_ptr<T> storage.
std::string toString() const noexcept
shared_ptr_ref & operator=(shared_ptr_ref &&o) noexcept
Move assignment operator.
T & operator*()
Provides access to reference of stored T.
jlong release_to_jlong() noexcept
Release ownership and return the jlong representation of the shared_ptr<T> storage.
shared_ptr_ref(JNIEnv *env, jobject obj, const bool throw_on_nullptr=true)
Constructs a new instance, copying the instance from the java object's long nativeInstance field.
shared_ptr_ref(T *ptr) noexcept
Constructs a new instance, taking ownership of the given T pointer.
shared_ptr_ref(shared_ptr_ref &&o) noexcept
Move constructor.
shared_ptr_ref() noexcept
Default constructor, nullptr.
void release_into_object(JNIEnv *env, jobject obj)
Deletes the std::shared_ptr<T> storage of the target java object if exists first and writes this inst...
shared_ptr_ref & operator=(const shared_ptr_ref &o)
Assignment operator.
shared_ptr_ref & operator=(const std::shared_ptr< T > &o)
Assignment operator.
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
Like std::for_each() of 'algorithm'.
std::string to_string(const alphabet &v) noexcept
std::basic_string< char, std::char_traits< char >, jau::callocator_sec< char > > secure_string
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
std::shared_ptr< JavaGlobalObj > JavaGlobalObjRef
jint getIntFieldValue(JNIEnv *env, jobject obj, const char *field_name)
void java_exception_check_and_throw(JNIEnv *env, const char *file, int line)
Throws a C++ exception if a java exception occurred, otherwise do nothing.
void print_native_caught_exception_fwd2java(const jau::OutOfMemoryError &e, const char *file, int line)
void raise_java_exception(JNIEnv *env, const std::exception &e, const char *file, int line)
jfieldID getField(JNIEnv *env, jobject obj, const char *field_name, const char *field_signature)
jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T &array, jau::function< jobject(JNIEnv *, const std::shared_ptr< U > &)> ctor)
std::shared_ptr< T > * castInstance(jlong instance, const bool throw_on_nullptr=true)
Returns the cast shared_ptr<T> pointer from the java object's long nativeInstance field.
jclass search_class(JNIEnv *env, JavaUplink &object)
void clearInstance(JNIEnv *env, jobject obj)
Deletes the std::shared_ptr<T> storage of the java object if exists and write nullptr into its long n...
jfieldID getInstanceField(JNIEnv *env, jobject obj)
jobject convert_vector_to_jarraylist(JNIEnv *env, T &array, jau::function< jobject(JNIEnv *, const U &)> ctor)
std::shared_ptr< T > * getInstance(JNIEnv *env, jobject obj, const bool throw_on_nullptr=true)
Returns the cast shared_ptr<T> pointer from the java object's long nativeInstance field.
void setInstance(JNIEnv *env, jobject obj, const std::shared_ptr< T > &t)
Deletes the std::shared_ptr<T> storage of the java object if exists first and writes the given std::s...
std::vector< std::string > convert_jlist_string_to_vector(JNIEnv *env, jobject jlist)
std::string getStringFieldValue(JNIEnv *env, jobject obj, const char *field_name)
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
jlong getLongFieldValue(JNIEnv *env, jobject obj, const char *field_name)
jobject convert_instance_to_jobject(JNIEnv *env, const std::shared_ptr< T > &elem, const char *ctor_prototype, jau::function< jobject(JNIEnv *, jclass, jmethodID, const std::shared_ptr< T > &)> ctor)
void rethrow_and_raise_java_exception_jauimpl(JNIEnv *env, const char *file, int line)
Re-throw current exception and raise respective java exception using any matching function above.
jbyteArray convert_bytes_to_jbytearray(JNIEnv *env, const byte_container_type &data)
jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T &array)
jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, const char *type, bool is_static)
void setObjectRef(JNIEnv *env, jobject obj, T *t, const char *field_name)
jobject getObjectFieldValue(JNIEnv *env, jobject obj, const char *field_name, const char *field_signature)
jobject convert_vector_stringview_to_jarraylist(JNIEnv *env, const std::vector< std::string_view > &array)
bool java_exception_check(JNIEnv *env, const char *file, int line)
Return true if a java exception occurred, otherwise false.
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static)
std::shared_ptr< JavaAnon > JavaAnonRef
jobject convert_vector_string_to_jarraylist(JNIEnv *env, const std::vector< std::string > &array)
T * getObjectRef(JNIEnv *env, jobject obj, const char *field_name)
jau::io::secure_string from_jbytebuffer_to_sstring(JNIEnv *env, jobject jbytebuffer)
jobject get_new_arraylist(JNIEnv *env, jsize size, jmethodID *add)
std::string from_jstring_to_string(JNIEnv *env, jstring str)
bool from_jboolean_to_bool(const jboolean val)
jobject convert_vector_bytes_to_jarraylist(JNIEnv *env, const std::vector< std::vector< uint8_t > > &array)
jclass search_class(JNIEnv *env, const char *clazz_name)
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.