summaryrefslogtreecommitdiffstats
path: root/include/jau/jni/helper_jni.hpp
blob: c37adf27c95952e7163df30a8f1fd57daf9ce92d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020, 2022 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef JAU_HELPER_JNI_HPP_
#define JAU_HELPER_JNI_HPP_

#include <vector>
#include <memory>
#include <functional>
#include <jni.h>

#include <jau/java_uplink.hpp>
#include <jau/basic_types.hpp>
#include <jau/darray.hpp>
#include <jau/basic_algos.hpp>

#include <jau/jni/jni_mem.hpp>

namespace jau {

    /** \addtogroup JavaVM
     *
     *  @{
     */

    //
    // C++ <-> java exceptions
    //

    /**
     * Return true if a java exception occurred, otherwise false.
     * <p>
     * In case of an exception, the information might be logged to stderr.
     * </p>
     * <p>
     * In case of an exception, user shall release resourced in their JNI code
     * and leave immediately.
     * </p>
     */
    bool java_exception_check(JNIEnv *env, const char* file, int line);

    /**
     * Throws a C++ exception if a java exception occurred, otherwise do nothing.
     * <p>
     * In case of an exception, the information might be logged to stderr.
     * </p>
     * <p>
     * In case of an exception and hence thrown C++ exception,
     * might want to catch all and handle it via {@link #rethrow_and_raise_java_exception(JNIEnv*)}.
     * </p>
     */
    void java_exception_check_and_throw(JNIEnv *env, const char* file, int line);

    void print_native_caught_exception_fwd2java(const jau::OutOfMemoryError &e, const char* file, int line);
    void print_native_caught_exception_fwd2java(const jau::RuntimeException &e, const char* file, int line);
    void print_native_caught_exception_fwd2java(const std::exception &e, const char* file, int line);
    void print_native_caught_exception_fwd2java(const std::string &msg, const char* file, int line);
    void print_native_caught_exception_fwd2java(const char * cmsg, const char* file, int line);

    void raise_java_exception(JNIEnv *env, const std::exception &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const std::runtime_error &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::RuntimeException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::InternalError &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::NullPointerException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::IllegalArgumentException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const std::invalid_argument &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::IllegalStateException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::UnsupportedOperationException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::IndexOutOfBoundsException &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const std::bad_alloc &e, const char* file, int line);
    void raise_java_exception(JNIEnv *env, const jau::OutOfMemoryError &e, const char* file, int line);

    /**
     * Re-throw current exception and raise respective java exception
     * using any matching function above.
     */
    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.
     */
    #define rethrow_and_raise_java_exception_jau(E) rethrow_and_raise_java_exception_jauimpl((E), __FILE__, __LINE__)
    // inline void rethrow_and_raise_java_exception_jau(JNIEnv *env) { rethrow_and_raise_java_exception_jauimpl(env, __FILE__, __LINE__); }

    //
    // Basic
    //

    jfieldID getField(JNIEnv *env, jobject obj, const char* field_name, const char* field_signature);
    inline jfieldID getInstanceField(JNIEnv *env, jobject obj) {
        return getField(env, obj, "nativeInstance", "J");
    }
    jobject getObjectFieldValue(JNIEnv *env, jobject obj, const char* field_name, const char* field_signature);
    std::string getStringFieldValue(JNIEnv *env, jobject obj, const char* field_name);
    jlong getLongFieldValue(JNIEnv *env, jobject obj, const char* field_name);

    jclass search_class(JNIEnv *env, const char *clazz_name);
    jclass search_class(JNIEnv *env, jobject obj);
    jclass search_class(JNIEnv *env, JavaUplink &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,
                            const char *type, bool is_static);

    bool from_jboolean_to_bool(jboolean val);
    std::string from_jstring_to_string(JNIEnv *env, jstring str);
    jstring from_string_to_jstring(JNIEnv *env, const std::string & str);

    jobject get_new_arraylist(JNIEnv *env, jsize size, jmethodID *add);

    jobject convert_vector_string_to_jarraylist(JNIEnv *env, const std::vector<std::string>& array);
    std::vector<std::string> convert_jlist_string_to_vector(JNIEnv *env, jobject jlist);

    //
    // C++ JavaAnon implementation
    //

    /**
     * Implementation for JavaAnon,
     * by simply wrapping a JNIGlobalRef instance.
     */
    class JavaGlobalObj : public JavaAnon {
        private:
            JNIGlobalRef javaObjectRef;
            jmethodID  mNotifyDeleted;

        public:
            static inline void check(const JavaAnonRef& shref, const char* file, int line) {
                if( nullptr == shref ) {
                    throw RuntimeException("JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
                }
                if( 0 == shref.use_count() ) { // safe-guard for concurrent dtor
                    throw RuntimeException("JavaGlobalObj::check: Empty shared-JavaAnonObj", file, line);
                }
                const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject();
                if( nullptr == obj ) {
                    throw RuntimeException("JavaGlobalObj::check: Null object", file, line);
                }
            }
            static inline jobject checkAndGetObject(const JavaAnonRef& shref, const char* file, int line) {
                if( nullptr == shref ) {
                    throw RuntimeException("JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
                }
                if( 0 == shref.use_count() ) { // safe-guard for concurrent dtor
                    throw RuntimeException("JavaGlobalObj::check: Empty shared-JavaAnonObj", file, line);
                }
                const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject();
                if( nullptr == obj ) {
                    throw RuntimeException("JavaGlobalObj::check: Null object", file, line);
                }
                return obj;
            }
            static bool isValid(const JavaAnonRef& shref) noexcept {
                if( nullptr == shref || 0 == shref.use_count() ) {
                    return false;
                }
                const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject();
                if( nullptr == obj ) {
                    return false;
                }
                return true;
            }
            JavaGlobalObj(jobject obj, jmethodID mNotifyDeleted_) noexcept
            : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted_) { }

            JavaGlobalObj(const JNIGlobalRef& obj, jmethodID mNotifyDeleted_) noexcept
            : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted_) { }

            JavaGlobalObj(JNIGlobalRef && obj, jmethodID mNotifyDeleted_) noexcept
            : javaObjectRef(std::move(obj)), mNotifyDeleted(mNotifyDeleted_) { }

            JavaGlobalObj(const JavaGlobalObj &o) noexcept = default;
            JavaGlobalObj(JavaGlobalObj &&o) noexcept = default;
            JavaGlobalObj& operator=(const JavaGlobalObj &o) noexcept = default;
            JavaGlobalObj& operator=(JavaGlobalObj &&o) noexcept = default;

            ~JavaGlobalObj() noexcept override;

            std::string toString() const noexcept override {
                const uint64_t ref = (uint64_t)(void*)javaObjectRef.getObject();
                return "JavaGlobalObj["+to_hexstring(ref)+"]";
            }

            const JNIGlobalRef & getJavaObject() const noexcept { return javaObjectRef; }
            JNIGlobalRef getJavaObject() noexcept { return javaObjectRef; }

            /* Provides access to the stored GlobalRef as an jobject. */
            jobject getObject() const noexcept { return javaObjectRef.getObject(); }
            /* Provides access to the stored GlobalRef as a jclass. */
            jclass getClass() const noexcept { return javaObjectRef.getClass(); }

            /* Provides access to the stored GlobalRef as an getJavaObject. */
            static JNIGlobalRef GetJavaObject(const JavaAnonRef & shref) noexcept {
                return static_cast<JavaGlobalObj*>(shref.get())->getJavaObject();
            }
            /* Provides access to the stored GlobalRef as an jobject. */
            static jobject GetObject(const JavaAnonRef & shref) noexcept {
                return static_cast<JavaGlobalObj*>(shref.get())->getObject();
            }

            /* Provides access to the stored GlobalRef as a jclass. */
            static jclass GetClass(const JavaAnonRef & shref) noexcept {
                return static_cast<JavaGlobalObj*>(shref.get())->getClass();
            }
    };
    typedef std::shared_ptr<JavaGlobalObj> JavaGlobalObjRef;

    //
    // C++ JavaAnon <-> java access, all generic
    //
    // We prefer using `std::shared_ptr<T>` instead of a `naked pointer`,
    // this way we automatically preserve the native instance lifecycle while within a JNI method.
    //

    template <typename T>
    T *getObjectRef(JNIEnv *env, jobject obj, const char* field_name)
    {
        jlong jobj = env->GetLongField(obj, getField(env, obj, field_name, "J"));
        java_exception_check_and_throw(env, E_FILE_LINE);
        return reinterpret_cast<T *>(jobj);
    }

    template <typename T>
    void setObjectRef(JNIEnv *env, jobject obj, T *t, const char* field_name)
    {
        jlong jobj = reinterpret_cast<jlong>(t);
        env->SetLongField(obj, getField(env, obj, field_name, "J"), jobj);
        java_exception_check_and_throw(env, E_FILE_LINE);
    }

    /**
     * Returns the cast `shared_ptr<T>` pointer from the java object's `long nativeInstance` field.
     *
     * If `throw_on_nullptr` is true, throws an exception if the shared_ptr<T> pointer is nullptr.
     *
     * @tparam T
     * @param instance
     * @param throw_on_nullptr
     * @return
     */
    template <typename T>
    std::shared_ptr<T> * castInstance(jlong instance, const bool throw_on_nullptr=true)
    {
        std::shared_ptr<T> * ref_ptr = reinterpret_cast<std::shared_ptr<T> *>(instance);
        if( throw_on_nullptr ) {
            if (nullptr == ref_ptr) {
                throw jau::RuntimeException("null reference store", E_FILE_LINE);
            }
        }
        return ref_ptr;
    }

    /**
     * Returns the cast `shared_ptr<T>` pointer from the java object's `long nativeInstance` field.
     *
     * If `throw_on_nullptr` is true, throws an exception if either the shared_ptr<T> pointer or
     * its managed object reference is nullptr.
     *
     * @tparam T
     * @param env
     * @param obj
     * @param throw_on_nullptr if true, throws exception if instance reference is nullptr (default). Otherwise not.
     */
    template <typename T>
    std::shared_ptr<T>* getInstance(JNIEnv *env, jobject obj, const bool throw_on_nullptr=true) {
        const jlong nativeInstance = env->GetLongField(obj, getInstanceField(env, obj));
        java_exception_check_and_throw(env, E_FILE_LINE);
        std::shared_ptr<T>* ref_ptr = reinterpret_cast<std::shared_ptr<T> *>(nativeInstance);
        if( throw_on_nullptr ) {
            if (nullptr == ref_ptr) {
                throw jau::RuntimeException("null reference store", E_FILE_LINE);
            }
            if (nullptr == *ref_ptr) {
                throw jau::RuntimeException("null reference", E_FILE_LINE);
            }
        }
        return ref_ptr;
    }

    /**
     * Deletes the `std::shared_ptr<T>` storage of the java object if exists first
     * and writes the given `std::shared_ptr<T>` storage pointer into its `long nativeInstance` field.
     *
     * @tparam T
     * @param env
     * @param obj
     * @param t
     */
    template <typename T>
    void setInstance(JNIEnv *env, jobject obj, const std::shared_ptr<T>& t)
    {
         if (t == nullptr) {
             throw jau::RuntimeException("Trying to create null object", E_FILE_LINE);
         }
         const jlong instance = (jlong) (intptr_t) &t;

         jfieldID instance_field = getInstanceField(env, obj);
         java_exception_check_and_throw(env, E_FILE_LINE);
         {
             const jlong nativeInstance = env->GetLongField(obj, instance_field);
             java_exception_check_and_throw(env, E_FILE_LINE);
             std::shared_ptr<T>* other = reinterpret_cast<std::shared_ptr<T> *>(nativeInstance);
             if( nullptr != other ) {
                 delete other;
             }
         }
         env->SetLongField(obj, instance_field, instance);
         java_exception_check_and_throw(env, E_FILE_LINE);
    }


    /**
     * Deletes the `std::shared_ptr<T>` storage of the java object if exists
     * and write `nullptr` into its `long nativeInstance` field.
     *
     * @tparam T
     * @param env
     * @param obj
     */
    template <typename T>
    void clearInstance(JNIEnv *env, jobject obj) {
        jfieldID instance_field = getInstanceField(env, obj);
        java_exception_check_and_throw(env, E_FILE_LINE);
        {
            const jlong nativeInstance = env->GetLongField(obj, instance_field);
            java_exception_check_and_throw(env, E_FILE_LINE);
            std::shared_ptr<T>* other = reinterpret_cast<std::shared_ptr<T> *>(nativeInstance);
            if( nullptr != other ) {
                delete other;
            }
        }
        env->SetLongField(obj, instance_field, 0);
        java_exception_check_and_throw(env, E_FILE_LINE);
    }

    /**
     * A `std::shared_ptr<T>` storage instance to be copied from and released into a java object's `long nativeInstance` field.
     *
     * An instance holds a shared_ptr<T> storage pointer for a managed object T.
     *
     * Using a `shared_ptr<T>` copy increments its reference counter and prohibits its destruction while in use.
     *
     * We prefer using `std::shared_ptr<T>` instead of a `naked pointer`,
     * this way we automatically preserve the native instance lifecycle while within a JNI method.
     *
     * @tparam T the managed object type
     */
    template <typename T>
    class shared_ptr_ref {
        private:
            std::shared_ptr<T>* ref_ptr;

            void safe_delete() {
                std::shared_ptr<T>* ref_ptr_ = ref_ptr;
                ref_ptr = nullptr;
                delete ref_ptr_;
            }

            static jlong get_instance(JNIEnv *env, jobject obj) {
                if( nullptr != obj ) {
                    const jlong res = env->GetLongField(obj, getInstanceField(env, obj));
                    java_exception_check_and_throw(env, E_FILE_LINE);
                    return res;
                } else {
                    return 0;
                }
            }
            static jlong get_instance(JNIEnv *env, jobject obj, jfieldID instance_field) {
                if( nullptr != obj ) {
                    const jlong res = env->GetLongField(obj, instance_field);
                    java_exception_check_and_throw(env, E_FILE_LINE);
                    return res;
                } else {
                    return 0;
                }
            }

        public:
            /** Default constructor, nullptr */
            shared_ptr_ref() noexcept
            : ref_ptr( new std::shared_ptr<T>() )
            { }

            /** Copy constructor */
            shared_ptr_ref(const shared_ptr_ref& o) noexcept
            : ref_ptr( new std::shared_ptr<T>( o.shared_ptr() ) )
            { }

            /** Move constructor. */
            shared_ptr_ref(shared_ptr_ref&& o) noexcept
            : ref_ptr( o.ref_ptr )
            {
                o.ref_ptr = nullptr;
            }

            /** Assignment operator. */
            shared_ptr_ref& operator=(const shared_ptr_ref& o) {
                if( nullptr != ref_ptr ) {
                    *ref_ptr = o.shared_ptr();
                } else {
                    ref_ptr = new std::shared_ptr<T>( o.shared_ptr() );
                }
                return *this;
            }

            /** Move assignment operator. */
            shared_ptr_ref& operator=(shared_ptr_ref&& o) {
                if( nullptr != ref_ptr ) {
                    safe_delete();
                }
                ref_ptr = o.ref_ptr;
                o.ref_ptr = nullptr;
                return *this;
            }

            ~shared_ptr_ref() {
                if( nullptr != ref_ptr ) {
                    safe_delete();
                }
            }

            /** Constructs a new instance, taking ownership of the given T pointer. */
            shared_ptr_ref(T * ptr) noexcept
            : ref_ptr( new std::shared_ptr<T>( ptr ) )
            { }

            /** Constructs a new instance, copying the given std::shared_ptr<T>. */
            shared_ptr_ref(const std::shared_ptr<T>& ref) noexcept
            : ref_ptr( new std::shared_ptr<T>( ref ) )
            { }

            /** Constructs a new instance, moving the given std::shared_ptr<T>. */
            shared_ptr_ref(std::shared_ptr<T>&& ref) noexcept
            : ref_ptr( new std::shared_ptr<T>( std::move(ref) ) )
            { }

            /** Assignment operator. */
            shared_ptr_ref& operator=(const std::shared_ptr<T>& o) {
                if( nullptr != ref_ptr ) {
                    *ref_ptr = o;
                } else {
                    ref_ptr = new std::shared_ptr<T>( o );
                }
                return *this;
            }

            /**
             * Throws an exception if this instances shared_ptr<T> storage is nullptr.
             *
             * The managed object reference may be nullptr.
             */
            void null_check1() const {
                if (nullptr == ref_ptr) {
                    throw jau::RuntimeException("null reference store", E_FILE_LINE);
                }
            }

            /**
             * Throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             */
            void null_check2() const {
                if (nullptr == ref_ptr) {
                    throw jau::RuntimeException("null reference store", E_FILE_LINE);
                }
                if (nullptr == *ref_ptr) {
                    throw jau::RuntimeException("null reference", E_FILE_LINE);
                }
            }

            /**
             * Constructs a new instance, copying the instance from the given java `long nativeInstance` value,
             * representing a java object's shared_ptr<T> storage
             *
             * Using a `shared_ptr<T>` copy increments its reference counter and prohibits its destruction while in use.
             *
             * If `throw_on_nullptr` is true, throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             *
             * @param nativeInstance the jlong representation of another shared_ptr<T> storage
             * @param throw_on_nullptr if true, throws an exception if either this instances shared_ptr<T> storage or the managed object reference is nullptr.
             */
            shared_ptr_ref(jlong nativeInstance, const bool throw_on_nullptr=true)
            : ref_ptr( new std::shared_ptr<T>() )
            {
                std::shared_ptr<T> * other = reinterpret_cast<std::shared_ptr<T> *>(nativeInstance);
                if( nullptr != other  && nullptr != *other ) {
                    *ref_ptr = *other;
                }
                if( throw_on_nullptr ) {
                    null_check2(); // exception if nullptr, even if other shared_ptr instance becomes nullptr @ copy-ctor
                }
            }

            /**
             * Constructs a new instance, copying the instance from the java object's `long nativeInstance` field.
             * representing its shared_ptr<T> storage
             *
             * Using a `shared_ptr<T>` copy increments its reference counter and prohibits its destruction while in use.
             *
             * If `throw_on_nullptr` is true, throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             *
             * @param env denoting the JVM
             * @param obj denoting the java object holding the `long nativeInstance` field, representing its shared_ptr<T> storage. Maybe `nullptr`, see `throw_on_nullptr`.
             * @param throw_on_nullptr if true, throws an exception if either this instances shared_ptr<T> storage or the managed object reference is nullptr.
             */
            shared_ptr_ref(JNIEnv *env, jobject obj, const bool throw_on_nullptr=true)
            : shared_ptr_ref( get_instance(env, obj), throw_on_nullptr )
            { }

            /**
             * Release ownership and returns the shared_ptr<T> storage.
             *
             * This instance shall not be used anymore.
             */
            std::shared_ptr<T>* release() noexcept {
                const std::shared_ptr<T>* res = ref_ptr;
                ref_ptr = nullptr;
                return res;
            }

            /**
             * Release ownership and return the jlong representation of the shared_ptr<T> storage.
             *
             * This instance shall not be used anymore.
             */
            jlong release_to_jlong() noexcept {
                const jlong res = (jlong) (intptr_t)ref_ptr;
                ref_ptr = nullptr;
                return res;
            }

            /**
             * Deletes the `std::shared_ptr<T>` storage of the target java object if exists first
             * and writes this instance's `std::shared_ptr<T>` storage pointer into its `long nativeInstance` field,
             * then releases ownership, see release_to_jlong().
             *
             * This instance shall not be used anymore.
             *
             * Throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             *
             * @param env
             * @param obj the target java object
             */
            void release_into_object(JNIEnv *env, jobject obj) {
                null_check2();
                if( nullptr == obj ) {
                    throw jau::RuntimeException("null target object", E_FILE_LINE);
                }
                jfieldID instance_field = getInstanceField(env, obj);
                java_exception_check_and_throw(env, E_FILE_LINE);
                {
                    std::shared_ptr<T> * other = reinterpret_cast<std::shared_ptr<T> *>( get_instance(env, obj, instance_field) );
                    if( nullptr != other ) {
                        delete other;
                    }
                }
                env->SetLongField(obj, instance_field, release_to_jlong());
                java_exception_check_and_throw(env, E_FILE_LINE);
            }

            /**
             * Returns true if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             */
            bool is_null() const noexcept {
                return nullptr == ref_ptr || nullptr == *ref_ptr;
            }

            /**
             * Provides access to the shared_ptr<T> pointer, l-value of storage.
             */
            std::shared_ptr<T>* pointer() noexcept {
                return ref_ptr;
            }

            /**
             * Provides access to const reference of shared_ptr<T>, r-value.
             *
             * Throws an exception if this instances shared_ptr<T> storage is nullptr.
             */
            const std::shared_ptr<T>& shared_ptr() const {
                null_check1();
                return *ref_ptr;
            }

            /**
             * Provides access to reference of stored T.
             *
             * Throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             */
            T& operator*() {
                null_check2();
                return *(ref_ptr->get());
            }

            /**
             * Provides access to pointer of stored T.
             *
             * Throws an exception if either this instances shared_ptr<T> storage or
             * the managed object reference is nullptr.
             */
            T* operator->() {
                null_check2();
                return ref_ptr->get();
            }

            std::string toString() const noexcept {
                return "shared_ptr_ref[ ptr "+jau::to_hexstring(ref_ptr)+
                                     ", obj "+ ( nullptr != ref_ptr ? jau::to_hexstring(ref_ptr->get()) : "null" ) + "]";
            }
    };

    //
    // C++ <-> java type mapping
    //

    template <typename T>
    jobject convert_instance_to_jobject(JNIEnv *env, const std::shared_ptr<T>& elem,
            const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, const std::shared_ptr<T>&)> ctor)
    {
        jclass clazz = search_class(env, T::java_class().c_str());
        jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false);

        jobject object = ctor(env, clazz, clazz_ctor, elem);
        if (!object)
        {
            throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE);
        }
        java_exception_check_and_throw(env, E_FILE_LINE);

        return object;
    }

    template <typename T>
    jobject convert_instance_to_jobject(JNIEnv *env, jclass clazz,
            const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, const std::shared_ptr<T>&)> ctor,
            const std::shared_ptr<T>& elem)
    {
        jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false);

        jobject object = ctor(env, clazz, clazz_ctor, elem);
        if (!object)
        {
            throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE);
        }
        java_exception_check_and_throw(env, E_FILE_LINE);

        return object;
    }

    template <typename T>
    jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array)
    {
        nsize_t array_size = array.size();

        jmethodID arraylist_add;
        jobject result = get_new_arraylist(env, (jsize)array_size, &arraylist_add);

        if (0 == array_size) {
            return result;
        }

        jau::for_each(array.begin(), array.end(), [&](typename T::value_type & elem){
            JavaAnonRef objref = elem->getJavaObject();
            if ( nullptr == objref ) {
                throw InternalError("JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE);
            }
            env->CallBooleanMethod(result, arraylist_add, JavaGlobalObj::GetObject(objref));
        });
        return result;
    }

    template <typename T, typename U>
    jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array,
            const char *ctor_prototype, std::function<jobject(JNIEnv*, jclass, jmethodID, const std::shared_ptr<U>&)> ctor)
    {
        unsigned int array_size = array.size();

        jmethodID arraylist_add;
        jobject result = get_new_arraylist(env, array_size, &arraylist_add);

        if (array_size == 0)
        {
            return result;
        }

        jclass clazz = search_class(env, U::java_class().c_str());
        jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false);

        for (unsigned int i = 0; i < array_size; ++i)
        {
            jobject object = ctor(env, clazz, clazz_ctor, array[i] /* const std::shared_ptr<U>& */);
            if (!object)
            {
                throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE);
            }
            env->CallBooleanMethod(result, arraylist_add, object);
            java_exception_check_and_throw(env, E_FILE_LINE);
        }
        return result;
    }

    template <typename T, typename U>
    jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array, std::function<jobject(JNIEnv*, const std::shared_ptr<U>&)> ctor)
    {
        unsigned int array_size = array.size();

        jmethodID arraylist_add;
        jobject result = get_new_arraylist(env, array_size, &arraylist_add);

        if (array_size == 0)
        {
            return result;
        }

        for (unsigned int i = 0; i < array_size; ++i)
        {
            jobject object = ctor(env, array[i] /* const std::shared_ptr<U>& */);
            if (!object)
            {
                throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE);
            }
            env->CallBooleanMethod(result, arraylist_add, object);
            java_exception_check_and_throw(env, E_FILE_LINE);
        }
        return result;
    }

    /**@}*/


} // namespace direct_bt

#endif /* JAU_HELPER_JNI_HPP_ */