-
Notifications
You must be signed in to change notification settings - Fork 29
/
jnipp.cpp
1658 lines (1344 loc) · 49.4 KB
/
jnipp.cpp
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1
// Windows Dependencies
# include <windows.h>
#else
// UNIX Dependencies
# include <dlfcn.h>
# include <unistd.h>
# include <tuple>
# include <stdlib.h>
#endif
// External Dependencies
#include <jni.h>
// Standard Dependencies
#include <atomic>
#include <string>
#include <vector>
// Local Dependencies
#include "jnipp.h"
namespace jni
{
// Static Variables
static std::atomic_bool isVm(false);
static JavaVM* javaVm = nullptr;
static bool getEnv(JavaVM *vm, JNIEnv **env) {
return vm->GetEnv((void **)env, JNI_VERSION_1_2) == JNI_OK;
}
static bool isAttached(JavaVM *vm) {
JNIEnv *env = nullptr;
return getEnv(vm, &env);
}
/**
Maintains the lifecycle of a JNIEnv.
*/
class ScopedEnv final
{
public:
ScopedEnv() noexcept : _vm(nullptr), _env(nullptr), _attached(false) {}
~ScopedEnv();
// Caution - throws if VM is nullptr!
void init(JavaVM* vm);
JNIEnv* get() const noexcept { return _env; }
private:
// Instance Variables
JavaVM* _vm;
JNIEnv* _env;
bool _attached; ///< Manually attached, as opposed to already attached.
};
ScopedEnv::~ScopedEnv()
{
if (_vm && _attached)
_vm->DetachCurrentThread();
}
void ScopedEnv::init(JavaVM* vm)
{
if (_env != nullptr)
return;
if (vm == nullptr)
throw InitializationException("JNI not initialized");
if (!getEnv(vm, &_env))
{
#ifdef __ANDROID__
if (vm->AttachCurrentThread(&_env, nullptr) != 0)
#else
if (vm->AttachCurrentThread((void**)&_env, nullptr) != 0)
#endif
throw InitializationException("Could not attach JNI to thread");
_attached = true;
}
_vm = vm;
}
/*
Helper Functions
*/
#ifdef _WIN32
static bool fileExists(const std::string& filePath)
{
DWORD attr = ::GetFileAttributesA(filePath.c_str());
return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY);
}
#else
/**
Convert from a UTF-16 Java string to a UTF-32 string.
*/
std::wstring toWString(const jchar* str, jsize length)
{
std::wstring result;
result.reserve(length);
for (jsize i = 0; i < length; ++i)
{
wchar_t ch = str[i];
// Check for a two-segment character.
if (ch >= wchar_t(0xD800) && ch <= wchar_t(0xDBFF)) {
if (i + 1 >= length)
break;
// Create a single, 32-bit character.
ch = (ch - wchar_t(0xD800)) << 10;
ch += str[i++] - wchar_t(0x1DC00);
}
result += ch;
}
return result;
}
/**
Convert from a UTF-32 string to a UTF-16 Java string.
*/
std::vector<jchar> toJString(const wchar_t* str, size_t length)
{
std::vector<jchar> result;
result.reserve(length * 2); // Worst case scenario.
for (size_t i = 0; i < length; ++i)
{
wchar_t ch = str[i];
// Check for multi-byte UTF-16 character.
if (ch > wchar_t(0xFFFF)) {
ch -= uint32_t(0x10000);
// Add the first of the two-segment character.
result.push_back(jchar(0xD800 + (ch >> 10)));
ch = wchar_t(0xDC00) + (ch & 0x03FF);
}
result.push_back(jchar(ch));
}
return result;
}
#endif // _WIN32
static ScopedEnv &scopedEnvInstance() noexcept
{
static thread_local ScopedEnv env;
return env;
}
// may return nullptr, beware!
JNIEnv *env_noexcept() noexcept
{
ScopedEnv& env = scopedEnvInstance();
if (env.get() != nullptr && !isAttached(javaVm))
{
// we got detached, so clear it.
// will be re-populated from static javaVm below.
env = ScopedEnv{};
}
if (env.get() == nullptr && javaVm != nullptr)
{
env.init(javaVm);
}
return env.get();
}
JNIEnv* env()
{
JNIEnv *ret = env_noexcept();
if (ret == nullptr)
{
throw InitializationException("JNI not initialized");
}
return ret;
}
static jclass findClass(const char* name)
{
jclass ref = env()->FindClass(name);
if (ref == nullptr)
{
env()->ExceptionClear();
throw NameResolutionException(name);
}
return ref;
}
static void handleJavaExceptions()
{
JNIEnv* env = jni::env();
jthrowable exception = env->ExceptionOccurred();
if (exception != nullptr)
{
Object obj(exception, Object::Temporary);
env->ExceptionClear();
std::string msg = obj.call<std::string>("toString");
throw InvocationException(msg.c_str());
}
}
static std::string toString(jobject handle, bool deleteLocal = true)
{
std::string result;
if (handle != nullptr)
{
JNIEnv* env = jni::env();
const char* chars = env->GetStringUTFChars(jstring(handle), nullptr);
result.assign(chars, env->GetStringUTFLength(jstring(handle)));
env->ReleaseStringUTFChars(jstring(handle), chars);
if (deleteLocal)
env->DeleteLocalRef(handle);
}
return result;
}
static std::wstring toWString(jobject handle, bool deleteLocal = true)
{
std::wstring result;
if (handle != nullptr)
{
JNIEnv* env = jni::env();
const jchar* chars = env->GetStringChars(jstring(handle), nullptr);
#ifdef _WIN32
result.assign((const wchar_t*) chars, env->GetStringLength(jstring(handle)));
#else
result = toWString(chars, env->GetStringLength(jstring(handle)));
#endif
env->ReleaseStringChars(jstring(handle), chars);
if (deleteLocal)
env->DeleteLocalRef(handle);
}
return result;
}
/*
Stand-alone Function Implementations
*/
void init(JNIEnv* env)
{
bool expected = false;
if (isVm.compare_exchange_strong(expected, true))
{
if (javaVm == nullptr && env->GetJavaVM(&javaVm) != 0)
throw InitializationException("Could not acquire Java VM");
}
}
void init(JavaVM* vm) {
bool expected = false;
if (isVm.compare_exchange_strong(expected, true))
{
javaVm = vm;
}
}
/*
Object Implementation
*/
Object::Object() noexcept : _handle(nullptr), _class(nullptr), _isGlobal(false)
{
}
Object::Object(const Object& other) : _handle(nullptr), _class(nullptr), _isGlobal(!other.isNull())
{
if (!other.isNull())
_handle = env()->NewGlobalRef(other._handle);
}
Object::Object(Object&& other) noexcept : _handle(other._handle), _class(other._class), _isGlobal(other._isGlobal)
{
other._handle = nullptr;
other._class = nullptr;
other._isGlobal = false;
}
Object::Object(jobject ref, int scopeFlags) : _handle(ref), _class(nullptr), _isGlobal((scopeFlags & Temporary) == 0)
{
if (!_isGlobal)
return;
JNIEnv* env = jni::env();
_handle = env->NewGlobalRef(ref);
if (scopeFlags & DeleteLocalInput)
env->DeleteLocalRef(ref);
}
Object::~Object() noexcept
{
JNIEnv* env = jni::env_noexcept();
if (env == nullptr)
{
// Better be empty. Cannot do anything useful.
return;
}
if (_isGlobal && _handle != nullptr)
env->DeleteGlobalRef(_handle);
if (_class != nullptr)
env->DeleteGlobalRef(_class);
}
Object& Object::operator=(const Object& other)
{
if (_handle != other._handle)
{
JNIEnv* env = jni::env();
// Ditch the old reference.
if (_isGlobal)
env->DeleteGlobalRef(_handle);
if (_class != nullptr)
env->DeleteGlobalRef(_class);
// Assign the new reference.
if ((_isGlobal = !other.isNull()) != false)
_handle = env->NewGlobalRef(other._handle);
_class = nullptr;
}
return *this;
}
bool Object::operator==(const Object& other) const
{
return env()->IsSameObject(_handle, other._handle) != JNI_FALSE;
}
Object& Object::operator=(Object&& other)
{
if (_handle != other._handle)
{
JNIEnv* env = jni::env();
// Ditch the old reference.
if (_isGlobal)
env->DeleteGlobalRef(_handle);
if (_class != nullptr)
env->DeleteGlobalRef(_class);
// Assign the new reference.
_handle = other._handle;
_isGlobal = other._isGlobal;
_class = other._class;
other._handle = nullptr;
other._isGlobal = false;
other._class = nullptr;
}
return *this;
}
bool Object::isNull() const noexcept
{
return _handle == nullptr || env()->IsSameObject(_handle, nullptr);
}
void Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<void> const&) const
{
env()->CallVoidMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
}
bool Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<bool> const&) const
{
auto result = env()->CallBooleanMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result != 0;
}
bool Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<bool> const&) const
{
return env()->GetBooleanField(_handle, field) != 0;
}
template <> void Object::set(field_t field, const bool& value)
{
env()->SetBooleanField(_handle, field, value);
}
byte_t Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<byte_t> const&) const
{
auto result = env()->CallByteMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
wchar_t Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<wchar_t> const&) const
{
auto result = env()->CallCharMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
short Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<short> const&) const
{
auto result = env()->CallShortMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
int Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<int> const&) const
{
auto result = env()->CallIntMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
long long Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<long long> const&) const
{
auto result = env()->CallLongMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
float Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<float> const&) const
{
auto result = env()->CallFloatMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
double Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<double> const&) const
{
auto result = env()->CallDoubleMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return result;
}
std::string Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<std::string> const&) const
{
auto result = env()->CallObjectMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return toString(result);
}
std::wstring Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<std::wstring> const&) const
{
auto result = env()->CallObjectMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return toWString(result);
}
jni::Object Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jni::Object> const&) const
{
auto result = env()->CallObjectMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return Object(result, DeleteLocalInput);
}
jarray Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jarray> const&) const
{
auto result = env()->CallObjectMethodA(_handle, method, (jvalue*) args);
handleJavaExceptions();
return (jarray)result;
}
byte_t Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<byte_t> const&) const
{
return env()->GetByteField(_handle, field);
}
wchar_t Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<wchar_t> const&) const
{
return env()->GetCharField(_handle, field);
}
short Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<short> const&) const
{
return env()->GetShortField(_handle, field);
}
int Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<int> const&) const
{
return env()->GetIntField(_handle, field);
}
long long Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<long long> const&) const
{
return env()->GetLongField(_handle, field);
}
float Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<float> const&) const
{
return env()->GetFloatField(_handle, field);
}
double Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<double> const&) const
{
return env()->GetDoubleField(_handle, field);
}
std::string Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<std::string> const&) const
{
return toString(env()->GetObjectField(_handle, field));
}
std::wstring Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<std::wstring> const&) const
{
return toWString(env()->GetObjectField(_handle, field));
}
Object Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<Object> const&) const
{
return Object(env()->GetObjectField(_handle, field), DeleteLocalInput);
}
template <> void Object::set(field_t field, const byte_t& value)
{
env()->SetByteField(_handle, field, value);
}
template <> void Object::set(field_t field, const wchar_t& value)
{
env()->SetCharField(_handle, field, value);
}
template <> void Object::set(field_t field, const short& value)
{
env()->SetShortField(_handle, field, value);
}
template <> void Object::set(field_t field, const int& value)
{
env()->SetIntField(_handle, field, value);
}
template <> void Object::set(field_t field, const long long& value)
{
env()->SetLongField(_handle, field, value);
}
template <> void Object::set(field_t field, const float& value)
{
env()->SetFloatField(_handle, field, value);
}
template <> void Object::set(field_t field, const double& value)
{
env()->SetDoubleField(_handle, field, value);
}
template <> void Object::set(field_t field, const std::string& value)
{
JNIEnv* env = jni::env();
jobject handle = env->NewStringUTF(value.c_str());
env->SetObjectField(_handle, field, handle);
env->DeleteLocalRef(handle);
}
template <> void Object::set(field_t field, const std::wstring& value)
{
JNIEnv* env = jni::env();
#ifdef _WIN32
jobject handle = env->NewString((const jchar*) value.c_str(), jsize(value.length()));
#else
auto jstr = toJString(value.c_str(), value.length());
jobject handle = env->NewString(jstr.data(), jsize(jstr.size()));
#endif
env->SetObjectField(_handle, field, handle);
env->DeleteLocalRef(handle);
}
template <> void Object::set(field_t field, const wchar_t* const& value)
{
JNIEnv* env = jni::env();
#ifdef _WIN32
jobject handle = env->NewString((const jchar*) value, jsize(std::wcslen(value)));
#else
auto jstr = toJString(value, std::wcslen(value));
jobject handle = env->NewString(jstr.data(), jsize(jstr.size()));
#endif
env->SetObjectField(_handle, field, handle);
env->DeleteLocalRef(handle);
}
template <> void Object::set(field_t field, const char* const& value)
{
JNIEnv* env = jni::env();
jobject handle = env->NewStringUTF(value);
env->SetObjectField(_handle, field, handle);
env->DeleteLocalRef(handle);
}
template <> void Object::set(field_t field, const Object& value)
{
env()->SetObjectField(_handle, field, value.getHandle());
}
template <> void Object::set(field_t field, const Object* const& value)
{
env()->SetObjectField(_handle, field, value ? value->getHandle() : nullptr);
}
jclass Object::getClass() const
{
if (_class == nullptr)
{
JNIEnv* env = jni::env();
jclass classRef = env->GetObjectClass(_handle);
_class = jclass(env->NewGlobalRef(classRef));
env->DeleteLocalRef(classRef);
}
return _class;
}
method_t Object::getMethod(const char* name, const char* signature) const
{
return Class(getClass(), Temporary).getMethod(name, signature);
}
method_t Object::getMethod(const char* nameAndSignature) const
{
return Class(getClass(), Temporary).getMethod(nameAndSignature);
}
field_t Object::getField(const char* name, const char* signature) const
{
return Class(getClass(), Temporary).getField(name, signature);
}
jobject Object::makeLocalReference() const
{
if (isNull())
return nullptr;
return env()->NewLocalRef(_handle);
}
/*
Class Implementation
*/
Class::Class(const char* name) : Object(findClass(name), DeleteLocalInput)
{
}
Class::Class(jclass ref, int scopeFlags) : Object(ref, scopeFlags)
{
}
Object Class::newInstance() const
{
method_t constructor = getMethod("<init>", "()V");
jobject obj = env()->NewObject(getHandle(), constructor);
handleJavaExceptions();
return Object(obj, Object::DeleteLocalInput);
}
field_t Class::getField(const char* name, const char* signature) const
{
jfieldID id = env()->GetFieldID(getHandle(), name, signature);
if (id == nullptr)
throw NameResolutionException(name);
return id;
}
field_t Class::getStaticField(const char* name, const char* signature) const
{
jfieldID id = env()->GetStaticFieldID(getHandle(), name, signature);
if (id == nullptr)
throw NameResolutionException(name);
return id;
}
method_t Class::getMethod(const char* name, const char* signature) const
{
jmethodID id = env()->GetMethodID(getHandle(), name, signature);
if (id == nullptr)
throw NameResolutionException(name);
return id;
}
method_t Class::getMethod(const char* nameAndSignature) const
{
jmethodID id = nullptr;
const char* sig = std::strchr(nameAndSignature, '(');
if (sig != nullptr)
return getMethod(std::string(nameAndSignature, sig - nameAndSignature).c_str(), sig);
if (id == nullptr)
throw NameResolutionException(nameAndSignature);
return id;
}
method_t Class::getStaticMethod(const char* name, const char* signature) const
{
jmethodID id = env()->GetStaticMethodID(getHandle(), name, signature);
if (id == nullptr)
throw NameResolutionException(name);
return id;
}
method_t Class::getStaticMethod(const char* nameAndSignature) const
{
jmethodID id = nullptr;
const char* sig = std::strchr(nameAndSignature, '(');
if (sig != nullptr)
return getStaticMethod(std::string(nameAndSignature, sig - nameAndSignature).c_str(), sig);
if (id == nullptr)
throw NameResolutionException(nameAndSignature);
return id;
}
Class Class::getParent() const
{
return Class(env()->GetSuperclass(getHandle()), DeleteLocalInput);
}
std::string Class::getName() const
{
return Object::call<std::string>("getName");
}
template <> bool Class::get(field_t field) const
{
return env()->GetStaticBooleanField(getHandle(), field) != 0;
}
template <> byte_t Class::get(field_t field) const
{
return env()->GetStaticByteField(getHandle(), field);
}
template <> wchar_t Class::get(field_t field) const
{
return env()->GetStaticCharField(getHandle(), field);
}
template <> short Class::get(field_t field) const
{
return env()->GetStaticShortField(getHandle(), field);
}
template <> int Class::get(field_t field) const
{
return env()->GetStaticIntField(getHandle(), field);
}
template <> long long Class::get(field_t field) const
{
return env()->GetStaticLongField(getHandle(), field);
}
template <> float Class::get(field_t field) const
{
return env()->GetStaticFloatField(getHandle(), field);
}
template <> double Class::get(field_t field) const
{
return env()->GetStaticDoubleField(getHandle(), field);
}
template <> std::string Class::get(field_t field) const
{
return toString(env()->GetStaticObjectField(getHandle(), field));
}
template <> std::wstring Class::get(field_t field) const
{
return toWString(env()->GetStaticObjectField(getHandle(), field));
}
template <> Object Class::get(field_t field) const
{
return Object(env()->GetStaticObjectField(getHandle(), field), DeleteLocalInput);
}
template <> void Class::set(field_t field, const bool& value)
{
env()->SetStaticBooleanField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const byte_t& value)
{
env()->SetStaticByteField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const wchar_t& value)
{
env()->SetStaticCharField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const short& value)
{
env()->SetStaticShortField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const int& value)
{
env()->SetStaticIntField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const long long& value)
{
env()->SetStaticLongField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const float& value)
{
env()->SetStaticFloatField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const double& value)
{
env()->SetStaticDoubleField(getHandle(), field, value);
}
template <> void Class::set(field_t field, const Object& value)
{
env()->SetStaticObjectField(getHandle(), field, value.getHandle());
}
template <> void Class::set(field_t field, const Object* const& value)
{
env()->SetStaticObjectField(getHandle(), field, value ? value->getHandle() : nullptr);
}
template <> void Class::set(field_t field, const std::string& value)
{
JNIEnv* env = jni::env();
jobject handle = env->NewStringUTF(value.c_str());
env->SetStaticObjectField(getHandle(), field, handle);
env->DeleteLocalRef(handle);
}
template <> void Class::set(field_t field, const std::wstring& value)
{
JNIEnv* env = jni::env();
#ifdef _WIN32
jobject handle = env->NewString((const jchar*) value.c_str(), jsize(value.length()));
#else
auto jstr = toJString(value.c_str(), value.length());
jobject handle = env->NewString(jstr.data(), jsize(jstr.size()));
#endif
env->SetStaticObjectField(getHandle(), field, handle);
env->DeleteLocalRef(handle);
}
template <> void Class::callStaticMethod(method_t method, internal::value_t* args) const
{
env()->CallStaticVoidMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
}
template <> bool Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticBooleanMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result != 0;
}
template <> byte_t Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticByteMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> wchar_t Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticCharMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> short Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticShortMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> int Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticIntMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> long long Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticLongMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> float Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticFloatMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> double Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticDoubleMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result;
}
template <> std::string Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticObjectMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return toString(result);
}
template <> std::wstring Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticObjectMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return toWString(result);
}
template <> jni::Object Class::callStaticMethod(method_t method, internal::value_t* args) const
{
auto result = env()->CallStaticObjectMethodA(getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return Object(result, DeleteLocalInput);
}
template <> void Class::callExactMethod(jobject obj, method_t method, internal::value_t* args) const
{
env()->CallNonvirtualVoidMethodA(obj, getHandle(), method, (jvalue*) args);
handleJavaExceptions();
}
template <> bool Class::callExactMethod(jobject obj, method_t method, internal::value_t* args) const
{
auto result = env()->CallNonvirtualBooleanMethodA(obj, getHandle(), method, (jvalue*) args);
handleJavaExceptions();
return result != 0;
}