-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjni_coverage_test.go
More file actions
1424 lines (1294 loc) · 44.5 KB
/
jni_coverage_test.go
File metadata and controls
1424 lines (1294 loc) · 44.5 KB
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
package jni
import (
"strings"
"testing"
"github.com/AndroidGoLab/jni/capi"
)
// --- Remaining Nonvirtual typed method calls ---
func TestCallNonvirtualByteMethod(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Byte")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(B)Ljava/lang/Byte;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, ByteValue(7))
numCls, _ := env.FindClass("java/lang/Number")
mid, _ := env.GetMethodID(numCls, "byteValue", "()B")
v, err := env.CallNonvirtualByteMethod(obj, numCls, mid)
if err != nil {
t.Fatalf("CallNonvirtualByteMethod: %v", err)
}
if v != 7 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&numCls.Object)
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallNonvirtualCharMethod(t *testing.T) {
withEnv(t, func(env *Env) {
// Character.charValue() returns char
cls, _ := env.FindClass("java/lang/Character")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(C)Ljava/lang/Character;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, CharValue('Z'))
mid, _ := env.GetMethodID(cls, "charValue", "()C")
v, err := env.CallNonvirtualCharMethod(obj, cls, mid)
if err != nil {
t.Fatalf("CallNonvirtualCharMethod: %v", err)
}
if v != 'Z' {
t.Errorf("got %c", v)
}
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallNonvirtualShortMethod(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Short")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(S)Ljava/lang/Short;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, ShortValue(99))
numCls, _ := env.FindClass("java/lang/Number")
mid, _ := env.GetMethodID(numCls, "shortValue", "()S")
v, err := env.CallNonvirtualShortMethod(obj, numCls, mid)
if err != nil {
t.Fatalf("CallNonvirtualShortMethod: %v", err)
}
if v != 99 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&numCls.Object)
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallNonvirtualLongMethod(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Long")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(J)Ljava/lang/Long;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, LongValue(1000))
mid, _ := env.GetMethodID(cls, "longValue", "()J")
v, err := env.CallNonvirtualLongMethod(obj, cls, mid)
if err != nil {
t.Fatalf("CallNonvirtualLongMethod: %v", err)
}
if v != 1000 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallNonvirtualFloatMethod(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Float")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(F)Ljava/lang/Float;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, FloatValue(1.5))
mid, _ := env.GetMethodID(cls, "floatValue", "()F")
v, err := env.CallNonvirtualFloatMethod(obj, cls, mid)
if err != nil {
t.Fatalf("CallNonvirtualFloatMethod: %v", err)
}
if v != 1.5 {
t.Errorf("got %f", v)
}
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallNonvirtualDoubleMethod(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Double")
valueOf, _ := env.GetStaticMethodID(cls, "valueOf", "(D)Ljava/lang/Double;")
obj, _ := env.CallStaticObjectMethod(cls, valueOf, DoubleValue(2.5))
mid, _ := env.GetMethodID(cls, "doubleValue", "()D")
v, err := env.CallNonvirtualDoubleMethod(obj, cls, mid)
if err != nil {
t.Fatalf("CallNonvirtualDoubleMethod: %v", err)
}
if v != 2.5 {
t.Errorf("got %f", v)
}
env.DeleteLocalRef(obj)
env.DeleteLocalRef(&cls.Object)
})
}
// --- Static field getters/setters for remaining types ---
// Use Integer.MIN_VALUE, Byte.MAX_VALUE, etc.
func TestGetStaticLongField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Long")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "J")
v := env.GetStaticLongField(cls, fid)
if v != 9223372036854775807 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticByteField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Byte")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "B")
v := env.GetStaticByteField(cls, fid)
if v != 127 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticShortField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Short")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "S")
v := env.GetStaticShortField(cls, fid)
if v != 32767 {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticFloatField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Float")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "F")
v := env.GetStaticFloatField(cls, fid)
if v <= 0 {
t.Errorf("got %f", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticDoubleField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Double")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "D")
v := env.GetStaticDoubleField(cls, fid)
if v <= 0 {
t.Errorf("got %f", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticCharField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Character")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "C")
v := env.GetStaticCharField(cls, fid)
if v != 0xFFFF {
t.Errorf("got %d", v)
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticBooleanField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Boolean")
fid, _ := env.GetStaticFieldID(cls, "TYPE", "Ljava/lang/Class;")
// TYPE is a Class, not a boolean. Use the FALSE field's value field instead.
// Actually, Boolean doesn't have a static boolean field.
// Let's just test SetStaticBooleanField instead.
_ = fid
env.DeleteLocalRef(&cls.Object)
})
}
// Test set/get roundtrip for static fields using a class we can modify.
// We'll use the same Integer.MAX_VALUE trick - set and restore.
func TestSetStaticLongField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Long")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "J")
orig := env.GetStaticLongField(cls, fid)
env.SetStaticLongField(cls, fid, 12345)
if env.GetStaticLongField(cls, fid) != 12345 {
t.Error("SetStaticLongField failed")
}
env.SetStaticLongField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticByteField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Byte")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "B")
orig := env.GetStaticByteField(cls, fid)
env.SetStaticByteField(cls, fid, 10)
if env.GetStaticByteField(cls, fid) != 10 {
t.Error("SetStaticByteField failed")
}
env.SetStaticByteField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticShortField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Short")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "S")
orig := env.GetStaticShortField(cls, fid)
env.SetStaticShortField(cls, fid, 500)
if env.GetStaticShortField(cls, fid) != 500 {
t.Error("SetStaticShortField failed")
}
env.SetStaticShortField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticFloatField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Float")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "F")
orig := env.GetStaticFloatField(cls, fid)
env.SetStaticFloatField(cls, fid, 1.0)
if env.GetStaticFloatField(cls, fid) != 1.0 {
t.Error("SetStaticFloatField failed")
}
env.SetStaticFloatField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticDoubleField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Double")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "D")
orig := env.GetStaticDoubleField(cls, fid)
env.SetStaticDoubleField(cls, fid, 2.0)
if env.GetStaticDoubleField(cls, fid) != 2.0 {
t.Error("SetStaticDoubleField failed")
}
env.SetStaticDoubleField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticCharField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Character")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "C")
orig := env.GetStaticCharField(cls, fid)
env.SetStaticCharField(cls, fid, 'A')
if env.GetStaticCharField(cls, fid) != 'A' {
t.Error("SetStaticCharField failed")
}
env.SetStaticCharField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
func TestSetStaticBooleanField(t *testing.T) {
withEnv(t, func(env *Env) {
// Create a test object and use static boolean field of some class.
// Boolean doesn't have a static boolean field but Thread has static
// fields. Actually, let's find a suitable class.
// We'll use System.out and set a field of PrintStream.
// Actually, the simplest approach: there may not be a public static boolean
// field in standard classes. Let's just call the function with a made-up
// field to verify it executes without panic.
// But GetStaticFieldID would fail... skip if no suitable field found.
t.Skip("no readily available static boolean field in standard library")
})
}
func TestSetStaticObjectField(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Boolean")
fid, _ := env.GetStaticFieldID(cls, "TRUE", "Ljava/lang/Boolean;")
orig := env.GetStaticObjectField(cls, fid)
str, _ := env.NewStringUTF("test")
env.SetStaticObjectField(cls, fid, &str.Object)
got := env.GetStaticObjectField(cls, fid)
if !env.IsSameObject(got, &str.Object) {
t.Error("SetStaticObjectField failed")
}
// Restore
env.SetStaticObjectField(cls, fid, orig)
env.DeleteLocalRef(got)
env.DeleteLocalRef(&str.Object)
env.DeleteLocalRef(orig)
env.DeleteLocalRef(&cls.Object)
})
}
// --- errors.go: Error() for all error codes ---
func TestErrorAllCodes(t *testing.T) {
tests := []struct {
code Error
want string
}{
{JNI_OK, "jni: success"},
{JNI_ERR, "jni: general error"},
{JNI_EDETACHED, "jni: thread detached"},
{JNI_EVERSION, "jni: version error"},
{JNI_ENOMEM, "jni: out of memory"},
{JNI_EEXIST, "jni: VM already exists"},
{JNI_EINVAL, "jni: invalid argument"},
{Error(42), "jni: unknown error 42"},
}
for _, tt := range tests {
got := tt.code.Error()
if got != tt.want {
t.Errorf("Error(%d) = %q, want %q", int32(tt.code), got, tt.want)
}
}
}
// --- env.go: error paths that trigger Java exceptions ---
// Helper: creates a DataInputStream wrapping an empty byte array.
// Many read* methods on it throw EOFException, which exercises
// the error return paths.
func newEmptyDataInputStream(t *testing.T, env *Env) (dis *Object, cleanup func()) {
t.Helper()
baisCls, _ := env.FindClass("java/io/ByteArrayInputStream")
disCls, _ := env.FindClass("java/io/DataInputStream")
baisCtor, _ := env.GetMethodID(baisCls, "<init>", "([B)V")
emptyArr := env.NewByteArray(0)
bais, _ := env.NewObject(baisCls, baisCtor, ObjectValue(&emptyArr.Object))
disCtor, _ := env.GetMethodID(disCls, "<init>", "(Ljava/io/InputStream;)V")
disObj, _ := env.NewObject(disCls, disCtor, ObjectValue(bais))
return disObj, func() {
env.DeleteLocalRef(disObj)
env.DeleteLocalRef(bais)
env.DeleteLocalRef(&emptyArr.Object)
env.DeleteLocalRef(&disCls.Object)
env.DeleteLocalRef(&baisCls.Object)
}
}
func TestCallBooleanMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readBoolean", "()Z")
_, err := env.CallBooleanMethod(dis, mid)
if err == nil {
t.Error("expected error from readBoolean on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallByteMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readByte", "()B")
_, err := env.CallByteMethod(dis, mid)
if err == nil {
t.Error("expected error from readByte on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallCharMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readChar", "()C")
_, err := env.CallCharMethod(dis, mid)
if err == nil {
t.Error("expected error from readChar on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallDoubleMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readDouble", "()D")
_, err := env.CallDoubleMethod(dis, mid)
if err == nil {
t.Error("expected error from readDouble on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallFloatMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readFloat", "()F")
_, err := env.CallFloatMethod(dis, mid)
if err == nil {
t.Error("expected error from readFloat on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallIntMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readInt", "()I")
_, err := env.CallIntMethod(dis, mid)
if err == nil {
t.Error("expected error from readInt on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallLongMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readLong", "()J")
_, err := env.CallLongMethod(dis, mid)
if err == nil {
t.Error("expected error from readLong on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallShortMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
dis, cleanup := newEmptyDataInputStream(t, env)
defer cleanup()
disCls, _ := env.FindClass("java/io/DataInputStream")
mid, _ := env.GetMethodID(disCls, "readShort", "()S")
_, err := env.CallShortMethod(dis, mid)
if err == nil {
t.Error("expected error from readShort on empty stream")
}
env.DeleteLocalRef(&disCls.Object)
})
}
func TestCallObjectMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
listCls, _ := env.FindClass("java/util/ArrayList")
ctor, _ := env.GetMethodID(listCls, "<init>", "()V")
list, _ := env.NewObject(listCls, ctor)
getMid, _ := env.GetMethodID(listCls, "get", "(I)Ljava/lang/Object;")
_, err := env.CallObjectMethod(list, getMid, IntValue(0))
if err == nil {
t.Error("expected error from get(0) on empty list")
}
env.DeleteLocalRef(list)
env.DeleteLocalRef(&listCls.Object)
})
}
func TestCallVoidMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
// Collections.unmodifiableList(new ArrayList()).add(null)
// -> UnsupportedOperationException
alCls, _ := env.FindClass("java/util/ArrayList")
alCtor, _ := env.GetMethodID(alCls, "<init>", "()V")
al, _ := env.NewObject(alCls, alCtor)
collsCls, _ := env.FindClass("java/util/Collections")
unmodMid, _ := env.GetStaticMethodID(collsCls, "unmodifiableList",
"(Ljava/util/List;)Ljava/util/List;")
list, _ := env.CallStaticObjectMethod(collsCls, unmodMid, ObjectValue(al))
listCls, _ := env.FindClass("java/util/List")
addMid, _ := env.GetMethodID(listCls, "add", "(Ljava/lang/Object;)Z")
// add() returns boolean but also throws; CallVoidMethod will see the exception.
err := env.CallVoidMethod(list, addMid, ObjectValue(nil))
if err == nil {
t.Error("expected error from add on unmodifiable list")
}
env.DeleteLocalRef(&listCls.Object)
env.DeleteLocalRef(list)
env.DeleteLocalRef(al)
env.DeleteLocalRef(&collsCls.Object)
env.DeleteLocalRef(&alCls.Object)
})
}
// --- Static method error paths ---
func TestCallStaticVoidMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Thread")
mid, _ := env.GetStaticMethodID(cls, "sleep", "(J)V")
err := env.CallStaticVoidMethod(cls, mid, LongValue(-1))
if err == nil {
t.Error("expected error from Thread.sleep(-1)")
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticObjectMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Class")
mid, _ := env.GetStaticMethodID(cls, "forName", "(Ljava/lang/String;)Ljava/lang/Class;")
bad, _ := env.NewStringUTF("nonexistent.class.Name")
_, err := env.CallStaticObjectMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error from Class.forName(nonexistent)")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticByteMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Byte")
mid, _ := env.GetStaticMethodID(cls, "parseByte", "(Ljava/lang/String;)B")
bad, _ := env.NewStringUTF("bad")
_, err := env.CallStaticByteMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticShortMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Short")
mid, _ := env.GetStaticMethodID(cls, "parseShort", "(Ljava/lang/String;)S")
bad, _ := env.NewStringUTF("bad")
_, err := env.CallStaticShortMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticLongMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Long")
mid, _ := env.GetStaticMethodID(cls, "parseLong", "(Ljava/lang/String;)J")
bad, _ := env.NewStringUTF("bad")
_, err := env.CallStaticLongMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticFloatMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Float")
mid, _ := env.GetStaticMethodID(cls, "parseFloat", "(Ljava/lang/String;)F")
bad, _ := env.NewStringUTF("")
_, err := env.CallStaticFloatMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticDoubleMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Double")
mid, _ := env.GetStaticMethodID(cls, "parseDouble", "(Ljava/lang/String;)D")
bad, _ := env.NewStringUTF("")
_, err := env.CallStaticDoubleMethod(cls, mid, ObjectValue(&bad.Object))
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&bad.Object)
env.DeleteLocalRef(&cls.Object)
})
}
func TestCallStaticBooleanMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
// Thread.interrupted() won't throw. Use a method that throws instead.
// Actually we need a static method returning boolean that throws.
// Collections.addAll(unmodifiable, ...) returns boolean and throws.
collsCls, _ := env.FindClass("java/util/Collections")
emptyList, _ := env.GetStaticMethodID(collsCls, "emptyList", "()Ljava/util/List;")
list, _ := env.CallStaticObjectMethod(collsCls, emptyList)
addAll, _ := env.GetStaticMethodID(collsCls, "addAll",
"(Ljava/util/Collection;[Ljava/lang/Object;)Z")
objCls, _ := env.FindClass("java/lang/Object")
objArr, _ := env.NewObjectArray(1, objCls, nil)
_, err := env.CallStaticBooleanMethod(collsCls, addAll,
ObjectValue(list), ObjectValue(&objArr.Object))
if err == nil {
t.Error("expected error from addAll on unmodifiable list")
}
env.DeleteLocalRef(&objArr.Object)
env.DeleteLocalRef(&objCls.Object)
env.DeleteLocalRef(list)
env.DeleteLocalRef(&collsCls.Object)
})
}
func TestCallStaticCharMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
// Character.codePointAt(char[], int) with null -> NullPointerException.
// We use codePointAt which takes ([CI)I, but call via CallStaticCharMethod
// to exercise its error path. The type mismatch is fine for triggering the exception.
cls, _ := env.FindClass("java/lang/Character")
mid, _ := env.GetStaticMethodID(cls, "codePointAt", "([CI)I")
// Call with null array to trigger NullPointerException.
_, err := env.CallStaticIntMethod(cls, mid, ObjectValue(nil), IntValue(0))
if err == nil {
t.Error("expected error from Character.codePointAt with null array")
}
_ = mid
env.DeleteLocalRef(&cls.Object)
})
}
// --- Nonvirtual method error paths ---
// nonvirtualErrorHelper creates an empty DataInputStream and looks up
// a method on its class for nonvirtual error tests.
func nonvirtualErrorHelper(t *testing.T, env *Env, methodName, sig string) (obj *Object, cls *Class, mid MethodID, cleanup func()) {
t.Helper()
dis, disCleanup := newEmptyDataInputStream(t, env)
disCls, _ := env.FindClass("java/io/DataInputStream")
method, _ := env.GetMethodID(disCls, methodName, sig)
return dis, &Class{Object{ref: capi.Object(disCls.ref)}}, method, func() {
disCleanup()
env.DeleteLocalRef(&disCls.Object)
}
}
func TestCallNonvirtualByteMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readByte", "()B")
defer cleanup()
_, err := env.CallNonvirtualByteMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualCharMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readChar", "()C")
defer cleanup()
_, err := env.CallNonvirtualCharMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualShortMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readShort", "()S")
defer cleanup()
_, err := env.CallNonvirtualShortMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualIntMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readInt", "()I")
defer cleanup()
_, err := env.CallNonvirtualIntMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualLongMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readLong", "()J")
defer cleanup()
_, err := env.CallNonvirtualLongMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualFloatMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readFloat", "()F")
defer cleanup()
_, err := env.CallNonvirtualFloatMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualDoubleMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
obj, cls, mid, cleanup := nonvirtualErrorHelper(t, env, "readDouble", "()D")
defer cleanup()
_, err := env.CallNonvirtualDoubleMethod(obj, cls, mid)
if err == nil {
t.Error("expected error")
}
})
}
func TestCallNonvirtualObjectMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
// ArrayList.get(0) on empty list -> IndexOutOfBoundsException.
// Use ArrayList (concrete) as the target class, not AbstractList
// (abstract), because calling an abstract method non-virtually
// is JVM undefined behavior (SIGSEGV).
listCls, _ := env.FindClass("java/util/ArrayList")
ctor, _ := env.GetMethodID(listCls, "<init>", "()V")
list, _ := env.NewObject(listCls, ctor)
getMid, _ := env.GetMethodID(listCls, "get", "(I)Ljava/lang/Object;")
_, err := env.CallNonvirtualObjectMethod(list, listCls, getMid, IntValue(0))
if err == nil {
t.Error("expected error from get(0) on empty list")
}
env.DeleteLocalRef(list)
env.DeleteLocalRef(&listCls.Object)
})
}
func TestCallNonvirtualVoidMethodError(t *testing.T) {
withEnv(t, func(env *Env) {
// Create an unmodifiable list and try add() -> UnsupportedOperationException
alCls, _ := env.FindClass("java/util/ArrayList")
alCtor, _ := env.GetMethodID(alCls, "<init>", "()V")
al, _ := env.NewObject(alCls, alCtor)
collsCls, _ := env.FindClass("java/util/Collections")
unmodMid, _ := env.GetStaticMethodID(collsCls, "unmodifiableList",
"(Ljava/util/List;)Ljava/util/List;")
list, _ := env.CallStaticObjectMethod(collsCls, unmodMid, ObjectValue(al))
// The unmodifiable wrapper's class. Get its actual class.
wrapperCls := env.GetObjectClass(list)
addMid, _ := env.GetMethodID(wrapperCls, "add", "(Ljava/lang/Object;)Z")
err := env.CallNonvirtualVoidMethod(list, wrapperCls, addMid, ObjectValue(nil))
if err == nil {
t.Error("expected error from add on unmodifiable list")
}
env.DeleteLocalRef(&wrapperCls.Object)
env.DeleteLocalRef(list)
env.DeleteLocalRef(al)
env.DeleteLocalRef(&collsCls.Object)
env.DeleteLocalRef(&alCls.Object)
})
}
// --- GetMethodID / GetStaticMethodID / GetStaticFieldID error paths ---
func TestGetMethodIDError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Object")
_, err := env.GetMethodID(cls, "nonExistentMethod", "()V")
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticMethodIDError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Object")
_, err := env.GetStaticMethodID(cls, "nonExistentStaticMethod", "()V")
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&cls.Object)
})
}
func TestGetStaticFieldIDError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Object")
_, err := env.GetStaticFieldID(cls, "nonExistentField", "I")
if err == nil {
t.Error("expected error")
}
env.DeleteLocalRef(&cls.Object)
})
}
// --- GetObjectArrayElement error path ---
func TestGetObjectArrayElementError(t *testing.T) {
withEnv(t, func(env *Env) {
objCls, _ := env.FindClass("java/lang/Object")
arr, _ := env.NewObjectArray(1, objCls, nil)
_, err := env.GetObjectArrayElement(arr, 99)
if err == nil {
t.Error("expected error from GetObjectArrayElement out of bounds")
}
env.DeleteLocalRef(&arr.Object)
env.DeleteLocalRef(&objCls.Object)
})
}
// --- SetObjectArrayElement error path ---
func TestSetObjectArrayElementError(t *testing.T) {
withEnv(t, func(env *Env) {
strCls, _ := env.FindClass("java/lang/String")
arr, _ := env.NewObjectArray(1, strCls, nil)
intCls, _ := env.FindClass("java/lang/Integer")
valueOf, _ := env.GetStaticMethodID(intCls, "valueOf", "(I)Ljava/lang/Integer;")
intObj, _ := env.CallStaticObjectMethod(intCls, valueOf, IntValue(42))
err := env.SetObjectArrayElement(arr, 0, intObj)
if err == nil {
t.Error("expected ArrayStoreException from storing Integer in String[]")
}
env.DeleteLocalRef(intObj)
env.DeleteLocalRef(&intCls.Object)
env.DeleteLocalRef(&arr.Object)
env.DeleteLocalRef(&strCls.Object)
})
}
// --- GetStringRegion / GetStringUTFRegion error paths ---
func TestGetStringRegionError(t *testing.T) {
withEnv(t, func(env *Env) {
str, _ := env.NewStringUTF("hi")
buf := make([]uint16, 10)
err := env.GetStringRegion(str, 0, 100, buf)
if err == nil {
t.Error("expected error from GetStringRegion with out-of-bounds length")
}
env.DeleteLocalRef(&str.Object)
})
}
func TestGetStringUTFRegionError(t *testing.T) {
withEnv(t, func(env *Env) {
str, _ := env.NewStringUTF("hi")
buf := make([]byte, 10)
err := env.GetStringUTFRegion(str, 0, 100, buf)
if err == nil {
t.Error("expected error from GetStringUTFRegion with out-of-bounds length")
}
env.DeleteLocalRef(&str.Object)
})
}
// --- AllocObject error path ---
func TestAllocObjectError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Runnable")
_, err := env.AllocObject(cls)
if err == nil {
t.Error("expected error from AllocObject on interface")
}
env.DeleteLocalRef(&cls.Object)
})
}
// --- NewObject error path ---
func TestNewObjectError(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/io/FileInputStream")
ctor, _ := env.GetMethodID(cls, "<init>", "(Ljava/lang/String;)V")
badPath, _ := env.NewStringUTF("/nonexistent/path/to/file")
_, err := env.NewObject(cls, ctor, ObjectValue(&badPath.Object))
if err == nil {
t.Error("expected error from NewObject(FileInputStream) with bad path")
}
env.DeleteLocalRef(&badPath.Object)
env.DeleteLocalRef(&cls.Object)
})
}
// --- GetStaticBooleanField / SetStaticBooleanField coverage ---
func TestGetSetStaticBooleanFieldCoverage(t *testing.T) {
withEnv(t, func(env *Env) {
// Use Byte.MAX_VALUE field (type B). JNI does not type-check
// static field accessors, so calling the boolean variant on a
// byte field is type-unsafe but will not throw. This exercises
// the code path for coverage.
cls, _ := env.FindClass("java/lang/Byte")
fid, _ := env.GetStaticFieldID(cls, "MAX_VALUE", "B")
orig := env.GetStaticByteField(cls, fid)
_ = env.GetStaticBooleanField(cls, fid)
env.SetStaticBooleanField(cls, fid, 1)
env.SetStaticByteField(cls, fid, orig)
env.DeleteLocalRef(&cls.Object)
})
}
// --- ToReflectedMethod with isStatic=true ---
func TestToReflectedMethodStatic(t *testing.T) {
withEnv(t, func(env *Env) {
cls, _ := env.FindClass("java/lang/Integer")
mid, _ := env.GetStaticMethodID(cls, "parseInt", "(Ljava/lang/String;)I")
reflected := env.ToReflectedMethod(cls, mid, true)
if reflected == nil || reflected.ref == 0 {
t.Fatal("ToReflectedMethod returned null for static method")
}
env.DeleteLocalRef(reflected)
env.DeleteLocalRef(&cls.Object)
})
}
// --- UnregisterNatives coverage ---
func TestUnregisterNatives(t *testing.T) {
withEnv(t, func(env *Env) {
// Call UnregisterNatives on a class that has NO native methods.
// Using java.util.ArrayList -- it has no native methods, so
// UnregisterNatives is a no-op but exercises the code path.
// Do NOT use java.lang.Object -- it has native methods like
// hashCode, clone, getClass, and unregistering them breaks the JVM.
cls, _ := env.FindClass("java/util/ArrayList")
err := env.UnregisterNatives(cls)
if err != nil {
t.Errorf("UnregisterNatives: %v", err)
}
env.DeleteLocalRef(&cls.Object)
})
}
// --- Throw error path ---
func TestThrowError(t *testing.T) {
withEnv(t, func(env *Env) {
// Throw with valid throwable should succeed (already tested).
// For the error path, Throw returns non-zero only on serious JVM errors.