-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_jni.hpp
811 lines (738 loc) · 29.3 KB
/
meta_jni.hpp
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#pragma once
#ifdef _WIN32
#include <Windows.h>
#elif defined(__linux__)
#include <pthread.h>
#endif
#include <jni.h>
#include <string_view>
#include <type_traits>
#include <memory>
#include <vector>
#include <mutex>
#include <shared_mutex>
#include <cstdint>
#include <functional>
#ifdef NDEBUG
#define assertm(exp, msg) ;
#else
#include <iostream>
#define assertm(exp, msg) if (!exp) { std::cout << msg << '\n'; abort(); }
#endif
#define BEGIN_KLASS_DEF(unobf_klass_name, obf_klass_name) struct unobf_klass_name##_members; using unobf_klass_name = jni::klass<obf_klass_name, unobf_klass_name##_members>; struct unobf_klass_name##_members : public jni::empty_members { unobf_klass_name##_members(jclass owner_klass, jobject object_instance, bool is_global_ref) : jni::empty_members(owner_klass, object_instance, is_global_ref) {}
#define END_KLASS_DEF() };
#define BEGIN_KLASS_DEF_EX(unobf_klass_name, obf_klass_name, inherit_from) struct unobf_klass_name##_members; using unobf_klass_name = jni::klass<obf_klass_name, unobf_klass_name##_members>; struct unobf_klass_name##_members : public inherit_from##_members { unobf_klass_name##_members(jclass owner_klass, jobject object_instance, bool is_global_ref) : inherit_from##_members(owner_klass, object_instance, is_global_ref) {}
#define KLASS_DECLARATION(unobf_klass_name, obf_klass_name) struct unobf_klass_name##_members; using unobf_klass_name = jni::klass<obf_klass_name, unobf_klass_name##_members>;
#define BEGIN_KLASS_MEMBERS_EX(unobf_klass_name, inherit_from) struct unobf_klass_name##_members : public inherit_from##_members { unobf_klass_name##_members(jclass owner_klass, jobject object_instance, bool is_global_ref) : inherit_from##_members(owner_klass, object_instance, is_global_ref) {}
#define BEGIN_KLASS_MEMBERS(unobf_klass_name) BEGIN_KLASS_MEMBERS_EX(unobf_klass_name, jni::empty)
#define END_KLASS_MEMBERS() };
namespace jni
{
inline uint32_t _tls_index = 0;
inline std::vector<jobject> _refs_to_delete{};
inline std::mutex _refs_to_delete_mutex{};
inline std::function<jclass(const char* class_name)> _custom_find_class{};
inline JNIEnv* get_env()
{
if (!_tls_index) return nullptr;
#ifdef _WIN32
return (JNIEnv*)TlsGetValue(_tls_index);
#elif __linux__
return (JNIEnv*)pthread_getspecific(_tls_index);
#endif
}
inline void set_thread_env(JNIEnv* new_env)
{
if (get_env()) return;
#ifdef _WIN32
TlsSetValue(_tls_index, new_env);
#elif __linux__
pthread_setspecific(_tls_index, new_env);
#endif
}
inline void init()
{
if (_tls_index) return;
#ifdef _WIN32
_tls_index = TlsAlloc();
#elif __linux__
pthread_key_create(&_tls_index, nullptr);
#endif
assertm(_tls_index, "tls index allocation failed");
}
inline void shutdown() //needs to be called on exit, library unusable after this
{
if (!get_env()) return;
{
std::lock_guard lock{ _refs_to_delete_mutex }; //shouldn't be necessary, every jni calls should be stopped before calling jni::destroy_cache
for (jobject object : _refs_to_delete)
{
if (!object) continue;
get_env()->DeleteGlobalRef(object);
}
_custom_find_class = {}; // destroy in case the custom find class stores a classloader reference
}
#ifdef _WIN32
TlsFree(_tls_index);
#elif __linux__
pthread_key_delete(_tls_index);
#endif
}
inline void set_custom_find_class(std::function<jclass(const char* class_name)> find_class)
{
_custom_find_class = find_class;
}
template<size_t N>
struct string_litteral
{
constexpr string_litteral(const char(&str)[N])
{
std::copy_n(str, N, value);
}
constexpr operator const char* () const
{
return value;
}
constexpr operator std::string_view() const
{
return value;
}
char value[N];
};
template<string_litteral... strs> inline constexpr auto concat()
{
constexpr std::size_t size = ((sizeof(strs.value) - 1) + ...); //-1 to not include null terminator (dumb)
char concatenated[size + 1] = { '\0' }; //+1 for null terminator
auto append = [i = 0, &concatenated](auto const& s) mutable
{
for (int n = 0; n < sizeof(s.value) - 1; ++n) concatenated[i++] = s.value[n]; //-1 to not include null terminator
};
(append(strs), ...);
concatenated[size] = '\0';
return string_litteral(concatenated);
}
template<typename klass_type> struct jclass_cache
{
inline static std::shared_mutex mutex{};
inline static jclass value = nullptr;
};
template<typename klass_type> inline jclass get_cached_jclass() //findClass
{
JNIEnv* env = get_env();
if (!env) return nullptr;
jclass& cached = jclass_cache<klass_type>::value;
{
std::shared_lock shared_lock{ jclass_cache<klass_type>::mutex };
if (cached) return cached;
}
jclass local = env->FindClass(klass_type::get_name());
if (env->ExceptionCheck())
env->ExceptionClear();
jclass found = (jclass)env->NewGlobalRef(local);
if (!found && _custom_find_class)
found = (jclass)env->NewGlobalRef(_custom_find_class(klass_type::get_name()));
assertm(found, (const char*)(concat<"failed to find class: ", klass_type::get_name()>()));
{
std::unique_lock unique_lock{ jclass_cache<klass_type>::mutex };
cached = found;
}
{
std::lock_guard lock{ _refs_to_delete_mutex };
_refs_to_delete.push_back(found);
}
return found;
}
class object_wrapper
{
public:
object_wrapper(jobject object_instance, bool is_global_ref) :
object_instance((is_global_ref && object_instance ? get_env()->NewGlobalRef(object_instance) : object_instance)),
is_global_ref(is_global_ref)
{
}
object_wrapper(const object_wrapper& other) :
object_wrapper(other.object_instance, other.is_global_ref)
{
}
virtual ~object_wrapper()
{
if (is_global_ref)
clear_ref();
}
object_wrapper& operator=(const object_wrapper& other) //operator = keeps the current ref type
{
if (is_global_ref)
{
jobject old_instance = object_instance; // set before deleting, eg if operator= is called on itself or on an object_wrapper with the same object_instance
object_instance = (other.object_instance ? get_env()->NewGlobalRef(other.object_instance) : nullptr);
if (old_instance) get_env()->DeleteGlobalRef(old_instance);
}
else
object_instance = other.object_instance;
return *this;
}
bool operator==(const object_wrapper& other) const
{
return is_same_object(other);
}
bool is_same_object(const object_wrapper& other) const
{
return get_env()->IsSameObject(object_instance, other.object_instance) == JNI_TRUE;
}
template<typename klass_type>
bool is_instance_of() const
{
return get_env()->IsInstanceOf(object_instance, get_cached_jclass<klass_type>()) == JNI_TRUE;
}
void clear_ref()
{
if (!object_instance) return;
if (is_global_ref && get_env())
get_env()->DeleteGlobalRef(object_instance);
object_instance = nullptr;
}
operator jobject() const
{
return this->object_instance;
}
operator bool() const
{
return this->object_instance;
}
bool is_global() const
{
return is_global_ref;
}
jobject object_instance;
private:
bool is_global_ref; //global refs aren't destroyed on PopLocalFrame, and can be shared between threads
};
struct empty_members : public object_wrapper
{
empty_members(jclass owner_klass, jobject object_instance, bool is_global_ref) :
object_wrapper(object_instance, is_global_ref),
owner_klass(owner_klass)
{
}
jclass owner_klass;
};
template<typename T, typename... U> inline constexpr bool is_any_of_type = (std::is_same_v<T, U> || ...);
template<typename T> inline constexpr bool is_jni_primitive_type = is_any_of_type<T, jboolean, jbyte, jchar, jshort, jint, jfloat, jlong, jdouble>;
enum is_static_t : bool
{
STATIC = true,
NOT_STATIC = false
};
template<class T> inline constexpr auto get_signature_for_type()
{
if constexpr (std::is_void_v<T>)
return string_litteral("V");
if constexpr (!is_jni_primitive_type<T> && !std::is_void_v<T>)
return T::get_signature();
if constexpr (std::is_same_v<jboolean, T>)
return string_litteral("Z");
if constexpr (std::is_same_v<jbyte, T>)
return string_litteral("B");
if constexpr (std::is_same_v<jchar, T>)
return string_litteral("C");
if constexpr (std::is_same_v<jshort, T>)
return string_litteral("S");
if constexpr (std::is_same_v<jint, T>)
return string_litteral("I");
if constexpr (std::is_same_v<jfloat, T>)
return string_litteral("F");
if constexpr (std::is_same_v<jlong, T>)
return string_litteral("J");
if constexpr (std::is_same_v<jdouble, T>)
return string_litteral("D");
}
template<class array_element_type>
class array : public object_wrapper
{
public:
array(jobject object_instance, bool is_global_ref = false) :
object_wrapper(object_instance, is_global_ref)
{
}
array& operator=(const array& other) //operator= is not inherited by default
{
object_wrapper::operator=(other);
return *this;
}
std::vector<array_element_type> to_vector() const
{
jsize length = get_length();
std::vector<array_element_type> vector{};
vector.reserve(length);
if constexpr (!is_jni_primitive_type<array_element_type>)
{
for (jsize i = 0; i < length; ++i)
vector.push_back( array_element_type(get_env()->GetObjectArrayElement((jobjectArray)object_instance, i)) );
}
if constexpr (std::is_same_v<jboolean, array_element_type>)
{
std::unique_ptr<jboolean[]> buffer = std::make_unique<jboolean[]>(length);
get_env()->GetBooleanArrayRegion((jbooleanArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jbyte, array_element_type>)
{
std::unique_ptr<jbyte[]> buffer = std::make_unique<jbyte[]>(length);
get_env()->GetByteArrayRegion((jbyteArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jchar, array_element_type>)
{
std::unique_ptr<jchar[]> buffer = std::make_unique<jchar[]>(length);
get_env()->GetCharArrayRegion((jcharArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jshort, array_element_type>)
{
std::unique_ptr<jshort[]> buffer = std::make_unique<jshort[]>(length);
get_env()->GetShortArrayRegion((jshortArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jint, array_element_type>)
{
std::unique_ptr<jint[]> buffer = std::make_unique<jint[]>(length);
get_env()->GetIntArrayRegion((jintArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jfloat, array_element_type>)
{
std::unique_ptr<jfloat[]> buffer = std::make_unique<jfloat[]>(length);
get_env()->GetFloatArrayRegion((jfloatArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jlong, array_element_type>)
{
std::unique_ptr<jlong[]> buffer = std::make_unique<jlong[]>(length);
get_env()->GetLongArrayRegion((jlongArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
if constexpr (std::is_same_v<jdouble, array_element_type>)
{
std::unique_ptr<jdouble[]> buffer = std::make_unique<jdouble[]>(length);
get_env()->GetDoubleArrayRegion((jdoubleArray)object_instance, 0, length, buffer.get());
vector.insert(vector.begin(), buffer.get(), buffer.get() + length);
}
return vector;
}
jsize get_length() const
{
return get_env()->GetArrayLength((jarray)object_instance);
}
static constexpr auto get_signature()
{
return concat<"[", get_signature_for_type<array_element_type>()>();
}
static constexpr auto get_name() //this is used for FindClass
{
return get_signature();
}
static array create(const std::vector<array_element_type>& values)
{
jobject object = nullptr;
if constexpr (!is_jni_primitive_type<array_element_type>)
{
object = get_env()->NewObjectArray((jsize)values.size(), get_cached_jclass<array_element_type>(), nullptr);
for (jsize i = 0; i < values.size(); ++i)
get_env()->SetObjectArrayElement((jobjectArray)object, i, (jobject)values[i]);
}
if constexpr (std::is_same_v<jboolean, array_element_type>)
{
object = get_env()->NewBooleanArray((jsize)values.size());
get_env()->SetBooleanArrayRegion((jbooleanArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jbyte, array_element_type>)
{
object = get_env()->NewByteArray((jsize)values.size());
get_env()->SetByteArrayRegion((jbyteArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jchar, array_element_type>)
{
object = get_env()->NewCharArray((jsize)values.size());
get_env()->SetCharArrayRegion((jcharArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jshort, array_element_type>)
{
object = get_env()->NewShortArray((jsize)values.size());
get_env()->SetShortArrayRegion((jshortArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jint, array_element_type>)
{
object = get_env()->NewIntArray((jsize)values.size());
get_env()->SetIntArrayRegion((jintArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jfloat, array_element_type>)
{
object = get_env()->NewFloatArray((jsize)values.size());
get_env()->SetFloatArrayRegion((jfloatArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jlong, array_element_type>)
{
object = get_env()->NewLongArray((jsize)values.size());
get_env()->SetLongArrayRegion((jlongArray)object, 0, (jsize)values.size(), values.data());
}
if constexpr (std::is_same_v<jdouble, array_element_type>)
{
object = get_env()->NewDoubleArray((jsize)values.size());
get_env()->SetDoubleArrayRegion((jdoubleArray)object, 0, (jsize)values.size(), values.data());
}
return array(object);
}
};
template<typename field_type, string_litteral field_name, is_static_t is_static = NOT_STATIC>
class field
{
public:
field(const empty_members& m) :
m(m)
{
if (id) return;
if constexpr (is_static)
id = get_env()->GetStaticFieldID(m.owner_klass, get_name(), get_signature());
if constexpr (!is_static)
id = get_env()->GetFieldID(m.owner_klass, get_name(), get_signature());
assertm(id, (const char*)(concat<"failed to find fieldID: ", get_name(), " ", get_signature()>()));
}
field(const field& other) = delete; // make sure field won't be copied (we store a empty_members reference which must not be copied)
field& operator=(const field_type& new_value)
{
set(new_value);
return *this;
}
void set(const field_type& new_value)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return;
if constexpr (!is_jni_primitive_type<field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticObjectField(m.owner_klass, id, (jobject)new_value);
if constexpr (!is_static)
return get_env()->SetObjectField(m.object_instance, id, (jobject)new_value);
}
if constexpr (std::is_same_v<jboolean, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticBooleanField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetBooleanField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jbyte, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticByteField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetByteField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jchar, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticCharField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetCharField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jshort, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticShortField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetShortField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jint, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticIntField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetIntField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jfloat, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticFloatField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetFloatField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jlong, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticLongField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetLongField(m.object_instance, id, new_value);
}
if constexpr (std::is_same_v<jdouble, field_type>)
{
if constexpr (is_static)
return get_env()->SetStaticDoubleField(m.owner_klass, id, new_value);
if constexpr (!is_static)
return get_env()->SetDoubleField(m.object_instance, id, new_value);
}
}
auto get() const
{
if constexpr (!is_jni_primitive_type<field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return field_type(nullptr);
if constexpr (is_static)
return field_type(get_env()->GetStaticObjectField(m.owner_klass, id));
if constexpr (!is_static)
return field_type(get_env()->GetObjectField(m.object_instance, id));
}
if constexpr (std::is_same_v<jboolean, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jboolean(JNI_FALSE);
if constexpr (is_static)
return get_env()->GetStaticBooleanField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetBooleanField(m.object_instance, id);
}
if constexpr (std::is_same_v<jbyte, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jbyte(0);
if constexpr (is_static)
return get_env()->GetStaticByteField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetByteField(m.object_instance, id);
}
if constexpr (std::is_same_v<jchar, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jchar(0);
if constexpr (is_static)
return get_env()->GetStaticCharField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetCharField(m.object_instance, id);
}
if constexpr (std::is_same_v<jshort, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jshort(0);
if constexpr (is_static)
return get_env()->GetStaticShortField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetShortField(m.object_instance, id);
}
if constexpr (std::is_same_v<jint, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jint(0);
if constexpr (is_static)
return get_env()->GetStaticIntField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetIntField(m.object_instance, id);
}
if constexpr (std::is_same_v<jfloat, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jfloat(0.f);
if constexpr (is_static)
return get_env()->GetStaticFloatField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetFloatField(m.object_instance, id);
}
if constexpr (std::is_same_v<jlong, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jlong(0LL);
if constexpr (is_static)
return get_env()->GetStaticLongField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetLongField(m.object_instance, id);
}
if constexpr (std::is_same_v<jdouble, field_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jdouble(0.0);
if constexpr (is_static)
return get_env()->GetStaticDoubleField(m.owner_klass, id);
if constexpr (!is_static)
return get_env()->GetDoubleField(m.object_instance, id);
}
}
operator field_type() const
{
return get();
}
static constexpr auto get_name()
{
return field_name;
}
static constexpr auto get_signature()
{
return get_signature_for_type<field_type>();
}
static constexpr bool is_field_static()
{
return is_static;
}
operator jfieldID() const
{
return id;
}
private:
const empty_members& m;
inline static jfieldID id = nullptr;
};
template<typename method_return_type, string_litteral method_name, is_static_t is_static = NOT_STATIC, class... method_parameters_type>
class method
{
public:
method(const empty_members& m) :
m(m)
{
if (id) return;
if constexpr (is_static)
id = get_env()->GetStaticMethodID(m.owner_klass, get_name(), get_signature());
if constexpr (!is_static)
id = get_env()->GetMethodID(m.owner_klass, get_name(), get_signature());
assertm(id, (const char*)(concat<"failed to find methodID: ", get_name(), " ", get_signature()>()));
}
method(const method& other) = delete; // make sure method won't be copied (we store a empty_members reference which must not be copied)
auto operator()(const method_parameters_type&... method_parameters) const
{
return call(method_parameters...);
}
auto call(const method_parameters_type&... method_parameters) const
{
if constexpr (std::is_void_v<method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return;
if constexpr (is_static)
get_env()->CallStaticVoidMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
get_env()->CallVoidMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
return;
}
if constexpr (!is_jni_primitive_type<method_return_type> && !std::is_void_v<method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return method_return_type(nullptr);
if constexpr (is_static)
return method_return_type(get_env()->CallStaticObjectMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...));
if constexpr (!is_static)
return method_return_type(get_env()->CallObjectMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...));
}
if constexpr (std::is_same_v<jboolean, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jboolean(JNI_FALSE);
if constexpr (is_static)
return get_env()->CallStaticBooleanMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallBooleanMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jbyte, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jbyte(0);
if constexpr (is_static)
return get_env()->CallStaticByteMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallByteMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jchar, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jchar(0);
if constexpr (is_static)
return get_env()->CallStaticCharMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallCharMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jshort, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jshort(0);
if constexpr (is_static)
return get_env()->CallStaticShortMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallShortMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jint, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jint(0);
if constexpr (is_static)
return get_env()->CallStaticIntMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallIntMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jfloat, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jfloat(0.f);
if constexpr (is_static)
return get_env()->CallStaticFloatMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallFloatMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jlong, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jlong(0LL);
if constexpr (is_static)
return get_env()->CallStaticLongMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallLongMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
if constexpr (std::is_same_v<jdouble, method_return_type>)
{
if (!id || !m.owner_klass || (!is_static && !m.object_instance)) return jdouble(0.0);
if constexpr (is_static)
return get_env()->CallStaticDoubleMethod(m.owner_klass, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
if constexpr (!is_static)
return get_env()->CallDoubleMethod(m.object_instance, id, std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...);
}
}
operator jmethodID() const
{
return id;
}
static constexpr auto get_name()
{
return method_name;
}
static constexpr auto get_signature()
{
return concat<"(", get_signature_for_type<method_parameters_type>()..., ")", get_signature_for_type<method_return_type>()>();
}
static constexpr bool is_method_static()
{
return is_static;
}
private:
const empty_members& m;
inline static jmethodID id;
};
template<class... method_parameters_type>
using constructor = method<void, "<init>", jni::NOT_STATIC, method_parameters_type...>;
template<string_litteral class_name, class members_type>
class klass : public members_type
{
public:
klass(jobject object_instance = nullptr, bool is_global_ref = false) :
members_type(get_cached_jclass<klass>(), object_instance, is_global_ref) // be careful order of initialization matters
{
}
klass(const klass& other) : klass(other.object_instance, other.is_global()) {} // very important to not copy jni::field and method
klass& operator=(const klass& other) //operator= is not inherited by default
{
object_wrapper::operator=(other);
return *this;
}
template<class... method_parameters_type>
static klass new_object(jni::constructor<method_parameters_type...> members_type::*constructor, const method_parameters_type&... method_parameters) // tbh I was just playing with member pointers
{
klass tmp{}; //lmao
return klass{jni::get_env()->NewObject(get_cached_jclass<klass>(), jmethodID(tmp.*constructor), std::conditional_t<is_jni_primitive_type<method_parameters_type>, method_parameters_type, jobject>(method_parameters)...)};
}
static constexpr auto get_name()
{
return class_name;
}
static constexpr auto get_signature()
{
return concat<"L", class_name, ";">();
}
};
class frame
{
public:
frame(jint capacity = 16)
{
get_env()->PushLocalFrame(capacity);
}
~frame()
{
get_env()->PopLocalFrame(nullptr);
}
};
}