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);
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);
179 const jobject
obj =
static_cast<const JavaGlobalObj*
>(shref.get())->getObject();
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);
191 const jobject
obj =
static_cast<const JavaGlobalObj*
>(shref.get())->getObject();
192 if(
nullptr ==
obj ) {
198 if(
nullptr == shref || 0 == shref.use_count() ) {
201 const jobject
obj =
static_cast<const JavaGlobalObj*
>(shref.get())->getObject();
202 if(
nullptr ==
obj ) {
208 : javaObjectRef(
obj), mNotifyDeleted(mNotifyDeleted_) { }
211 : javaObjectRef(
obj), mNotifyDeleted(mNotifyDeleted_) { }
214 : javaObjectRef(std::move(
obj)), mNotifyDeleted(mNotifyDeleted_) { }
231 jobject
getObject() const noexcept {
return javaObjectRef.getObject(); }
233 jclass
getClass() const noexcept {
return javaObjectRef.getClass(); }
237 return static_cast<JavaGlobalObj*
>(shref.get())->getJavaObject();
241 return static_cast<JavaGlobalObj*
>(shref.get())->getObject();
258 template <
typename T>
261 jlong jobj = env->GetLongField(
obj,
getField(env,
obj, field_name,
"J"));
263 return reinterpret_cast<T *
>(jobj);
266 template <
typename T>
269 jlong jobj =
reinterpret_cast<jlong
>(t);
270 env->SetLongField(
obj,
getField(env,
obj, field_name,
"J"), jobj);
284 template <
typename T>
285 std::shared_ptr<T> *
castInstance(jlong instance,
const bool throw_on_nullptr=
true)
287 std::shared_ptr<T> * ref_ptr =
reinterpret_cast<std::shared_ptr<T> *
>(instance);
288 if( throw_on_nullptr ) {
289 if (
nullptr == ref_ptr) {
307 template <
typename T>
308 std::shared_ptr<T>*
getInstance(JNIEnv *env, jobject
obj,
const bool throw_on_nullptr=
true) {
311 std::shared_ptr<T>* ref_ptr =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
312 if( throw_on_nullptr ) {
313 if (
nullptr == ref_ptr) {
316 if (
nullptr == *ref_ptr) {
332 template <
typename T>
338 const jlong instance = (jlong) (intptr_t) &t;
343 const jlong nativeInstance = env->GetLongField(
obj, instance_field);
345 std::shared_ptr<T>* other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
346 if(
nullptr != other ) {
350 env->SetLongField(
obj, instance_field, instance);
363 template <
typename T>
368 const jlong nativeInstance = env->GetLongField(
obj, instance_field);
370 std::shared_ptr<T>* other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
371 if(
nullptr != other ) {
375 env->SetLongField(
obj, instance_field, 0);
391 template <
typename T>
394 std::shared_ptr<T>* ref_ptr;
397 std::shared_ptr<T>* ref_ptr_ = ref_ptr;
402 static jlong get_instance(JNIEnv *env, jobject
obj) {
403 if(
nullptr !=
obj ) {
411 static jlong get_instance(JNIEnv *env, jobject
obj, jfieldID instance_field) {
412 if(
nullptr !=
obj ) {
413 const jlong res = env->GetLongField(
obj, instance_field);
429 : ref_ptr(
new std::shared_ptr<T>( o.shared_ptr() ) )
434 : ref_ptr( o.ref_ptr )
442 if(
nullptr != ref_ptr ) {
445 ref_ptr =
new std::shared_ptr<T>( o.
shared_ptr() );
453 if(
nullptr != ref_ptr ) {
462 if(
nullptr != ref_ptr ) {
479 : ref_ptr(
new std::shared_ptr<T>( std::move(ref) ) )
484 if(
nullptr != ref_ptr ) {
487 ref_ptr =
new std::shared_ptr<T>( o );
498 if (
nullptr == ref_ptr) {
508 if (
nullptr == ref_ptr) {
511 if (
nullptr == *ref_ptr) {
531 std::shared_ptr<T> * other =
reinterpret_cast<std::shared_ptr<T> *
>(nativeInstance);
532 if(
nullptr != other &&
nullptr != *other ) {
535 if( throw_on_nullptr ) {
563 const std::shared_ptr<T>* res = ref_ptr;
574 const jlong res = (jlong) (intptr_t)ref_ptr;
594 if(
nullptr ==
obj ) {
600 std::shared_ptr<T> * other =
reinterpret_cast<std::shared_ptr<T> *
>( get_instance(env,
obj, instance_field) );
601 if(
nullptr != other ) {
614 return nullptr == ref_ptr ||
nullptr == *ref_ptr;
642 return *(ref_ptr->get());
653 return ref_ptr->get();
658 ", obj "+ (
nullptr != ref_ptr ?
jau::to_hexstring(ref_ptr->get()) :
"null" ) +
"]";
666 template <
typename T>
668 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<T>&)> ctor)
670 jclass clazz =
search_class(env, T::java_class().c_str());
671 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
673 jobject
object = ctor(env, clazz, clazz_ctor, elem);
683 template <
typename T>
685 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<T>&)> ctor,
686 const std::shared_ptr<T>& elem)
688 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
690 jobject
object = ctor(env, clazz, clazz_ctor, elem);
700 template <
typename T>
703 nsize_t array_size = array.size();
705 jmethodID arraylist_add;
708 if (0 == array_size) {
712 jau::for_each(array.begin(), array.end(), [&](
typename T::value_type & elem){
713 JavaAnonRef objref = elem->getJavaObject();
714 if ( nullptr == objref ) {
715 throw InternalError(
"JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE);
722 template <
typename T,
typename U>
724 const char *ctor_prototype,
jau::function<jobject(JNIEnv*, jclass, jmethodID,
const std::shared_ptr<U>&)> ctor)
726 const size_t array_size = array.size();
727 if( array_size > std::numeric_limits<jsize>::max() ) {
729 " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()),
E_FILE_LINE);
731 const jsize jarray_size = array_size;
733 jmethodID arraylist_add;
736 if (jarray_size == 0)
741 jclass clazz =
search_class(env, U::java_class().c_str());
742 jmethodID clazz_ctor =
search_method(env, clazz,
"<init>", ctor_prototype,
false);
744 for (jsize i = 0; i < jarray_size; ++i)
746 jobject
object = ctor(env, clazz, clazz_ctor, array[i] );
751 env->CallBooleanMethod(result, arraylist_add,
object);
757 template <
typename T,
typename U>
760 const size_t array_size = array.size();
761 if( array_size > std::numeric_limits<jsize>::max() ) {
763 " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()),
E_FILE_LINE);
765 const jsize jarray_size = array_size;
767 jmethodID arraylist_add;
770 if (jarray_size == 0)
775 for (jsize i = 0; i < jarray_size; ++i)
777 jobject
object = ctor(env, array[i] );
782 env->CallBooleanMethod(result, arraylist_add,
object);
788 template <
typename T,
typename U>
791 const size_t array_size = array.size();
792 if( array_size > std::numeric_limits<jsize>::max() ) {
794 " exceeds max jsize "+std::to_string(std::numeric_limits<jsize>::max()),
E_FILE_LINE);
796 const jsize jarray_size = array_size;
798 jmethodID arraylist_add;
801 if (jarray_size == 0)
806 for (jsize i = 0; i < jarray_size; ++i)
808 jobject
object = ctor(env, array[i] );
813 env->CallBooleanMethod(result, arraylist_add,
object);
Class template jau::function is a general-purpose static-polymorphic function wrapper.
Pure virtual JavaAnon, hiding Java JNI details from API, to be implemented by JNI module.
~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.
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,...
shared_ptr_ref(const std::shared_ptr< T > &ref)
Constructs a new instance, copying the given std::shared_ptr<T>.
T * operator->()
Provides access to pointer of stored 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(T *ptr)
Constructs a new instance, taking ownership of the given T pointer.
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(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::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.
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.
jfieldID getField(JNIEnv *env, jobject obj, const char *field_name, const char *field_signature)
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.
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)
void print_native_caught_exception_fwd2java(const jau::ExceptionBase &e, const char *file, int line)
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)
void raise_java_exception(JNIEnv *env, const jau::ExceptionBase &e, const char *file, int line)
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, const bool skipLeading0x=false) noexcept
Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.