@@ -49,6 +49,7 @@ int main()
49
49
50
50
static Testable<jni::jclass> classValue;
51
51
static Testable<jni::jobject> objectValue;
52
+ static Testable<jni::jstring> stringValue;
52
53
53
54
env.functions ->FindClass = [] (JNIEnv*, const char * name) -> jclass
54
55
{
@@ -80,6 +81,9 @@ int main()
80
81
jni::Object<Test> object { objectValue.Ptr () };
81
82
object.NewGlobalRef (env);
82
83
84
+ jni::String string { stringValue.Ptr () };
85
+ string.NewGlobalRef (env);
86
+
83
87
84
88
// / Constructor
85
89
@@ -140,8 +144,10 @@ int main()
140
144
141
145
static Testable<jni::jfieldID> booleanFieldID;
142
146
static Testable<jni::jfieldID> objectFieldID;
147
+ static Testable<jni::jfieldID> stringFieldID;
143
148
static const char * booleanFieldName = " boolean" ;
144
149
static const char * objectFieldName = " object" ;
150
+ static const char * stringFieldName = " string" ;
145
151
146
152
env.functions ->GetStaticFieldID = [] (JNIEnv*, jclass, const char * name, const char * sig) -> jfieldID
147
153
{
@@ -150,11 +156,20 @@ int main()
150
156
assert (sig == std::string (" Z" ));
151
157
return jni::Unwrap (booleanFieldID.Ptr ());
152
158
}
153
- else
159
+ else if (name == objectFieldName)
154
160
{
155
161
assert (sig == std::string (" Lmapbox/com/Test;" ));
156
162
return jni::Unwrap (objectFieldID.Ptr ());
157
163
}
164
+ else if (name == stringFieldName)
165
+ {
166
+ assert (sig == std::string (" Ljava/lang/String;" ));
167
+ return jni::Unwrap (stringFieldID.Ptr ());
168
+ }
169
+ else
170
+ {
171
+ abort ();
172
+ }
158
173
};
159
174
160
175
env.functions ->GetStaticBooleanField = [] (JNIEnv*, jclass clazz, jfieldID field) -> jboolean
@@ -208,8 +223,18 @@ int main()
208
223
env.functions ->GetObjectField = [] (JNIEnv*, jobject obj, jfieldID field) -> jobject
209
224
{
210
225
assert (obj == jni::Unwrap (objectValue.Ptr ()));
211
- assert (field == jni::Unwrap (objectFieldID.Ptr ()));
212
- return jni::Unwrap (objectValue.Ptr ());
226
+ if (field == jni::Unwrap (objectFieldID.Ptr ()))
227
+ {
228
+ return jni::Unwrap (objectValue.Ptr ());
229
+ }
230
+ else if (field == jni::Unwrap (stringFieldID.Ptr ()))
231
+ {
232
+ return jni::Unwrap (stringValue.Ptr ());
233
+ }
234
+ else
235
+ {
236
+ abort ();
237
+ }
213
238
};
214
239
215
240
env.functions ->SetBooleanField = [] (JNIEnv*, jobject obj, jfieldID field, jboolean value)
@@ -222,16 +247,31 @@ int main()
222
247
env.functions ->SetObjectField = [] (JNIEnv*, jobject obj, jfieldID field, jobject value)
223
248
{
224
249
assert (obj == jni::Unwrap (objectValue.Ptr ()));
225
- assert (field == jni::Unwrap (objectFieldID.Ptr ()));
226
- assert (value == jni::Unwrap (objectValue.Ptr ()));
250
+ if (field == jni::Unwrap (objectFieldID.Ptr ()))
251
+ {
252
+ assert (value == jni::Unwrap (objectValue.Ptr ()));
253
+ }
254
+ else if (field == jni::Unwrap (stringFieldID.Ptr ()))
255
+ {
256
+ assert (value == jni::Unwrap (stringValue.Ptr ()));
257
+ }
258
+ else
259
+ {
260
+ abort ();
261
+ }
227
262
};
228
263
229
264
auto booleanField = testClass.GetField <jni::jboolean>(env, booleanFieldName);
230
265
auto objectField = testClass.GetField <jni::Object<Test>>(env, objectFieldName);
266
+ auto stringField = testClass.GetField <jni::String>(env, stringFieldName);
267
+
231
268
assert (object.Get (env, booleanField) == true );
232
269
assert (object.Get (env, objectField).Get () == object.Get ());
270
+ assert (object.Get (env, stringField).Get () == string.Get ());
271
+
233
272
object.Set (env, booleanField, jni::jni_false);
234
273
object.Set (env, objectField, object);
274
+ object.Set (env, stringField, string);
235
275
236
276
237
277
// / StaticMethod
@@ -616,22 +656,20 @@ int main()
616
656
617
657
// / Make
618
658
619
- static Testable<jni::jstring> stringValue;
620
-
621
659
env.functions ->NewString = [] (JNIEnv*, const jchar*, jsize) -> jstring
622
660
{
623
661
return jni::Unwrap (stringValue.Ptr ());
624
662
};
625
663
626
- env.functions ->GetStringLength = [] (JNIEnv*, jstring string ) -> jsize
664
+ env.functions ->GetStringLength = [] (JNIEnv*, jstring str ) -> jsize
627
665
{
628
- assert (string == jni::Unwrap (stringValue.Ptr ()));
666
+ assert (str == jni::Unwrap (stringValue.Ptr ()));
629
667
return 5 ;
630
668
};
631
669
632
- env.functions ->GetStringRegion = [] (JNIEnv*, jstring string , jsize start, jsize len, jchar* buf)
670
+ env.functions ->GetStringRegion = [] (JNIEnv*, jstring str , jsize start, jsize len, jchar* buf)
633
671
{
634
- assert (string == jni::Unwrap (stringValue.Ptr ()));
672
+ assert (str == jni::Unwrap (stringValue.Ptr ()));
635
673
assert (start == 0 );
636
674
assert (len == 5 );
637
675
std::u16string (u" hello" ).copy (jni::Wrap<char16_t *>(buf), jni::Wrap<std::size_t >(len));
0 commit comments