forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjni.cc
2904 lines (2503 loc) · 77.6 KB
/
jni.cc
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
// jni.cc - JNI implementation, including the jump table.
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
Free Software Foundation
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
#include <config.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <gcj/cni.h>
#include <jvm.h>
#include <java-assert.h>
#include <jni.h>
#ifdef ENABLE_JVMPI
#include <jvmpi.h>
#endif
#ifdef INTERPRETER
#include <jvmti.h>
#include "jvmti-int.h"
#endif
#include <java/lang/Class.h>
#include <java/lang/ClassLoader.h>
#include <java/lang/Throwable.h>
#include <java/lang/ArrayIndexOutOfBoundsException.h>
#include <java/lang/StringIndexOutOfBoundsException.h>
#include <java/lang/StringBuffer.h>
#include <java/lang/UnsatisfiedLinkError.h>
#include <java/lang/InstantiationException.h>
#include <java/lang/NoSuchFieldError.h>
#include <java/lang/NoSuchMethodError.h>
#include <java/lang/reflect/Constructor.h>
#include <java/lang/reflect/Method.h>
#include <java/lang/reflect/Modifier.h>
#include <java/lang/OutOfMemoryError.h>
#include <java/lang/Integer.h>
#include <java/lang/ThreadGroup.h>
#include <java/lang/Thread.h>
#include <java/lang/IllegalAccessError.h>
#include <java/nio/Buffer.h>
#include <java/nio/DirectByteBufferImpl.h>
#include <java/nio/DirectByteBufferImpl$ReadWrite.h>
#include <java/util/IdentityHashMap.h>
#include <gnu/gcj/RawData.h>
#include <java/lang/ClassNotFoundException.h>
#include <gcj/method.h>
#include <gcj/field.h>
#include <java-interp.h>
#include <java-threads.h>
using namespace gcj;
// This enum is used to select different template instantiations in
// the invocation code.
enum invocation_type
{
normal,
nonvirtual,
static_type,
constructor
};
// Forward declarations.
extern struct JNINativeInterface_ _Jv_JNIFunctions;
extern struct JNIInvokeInterface_ _Jv_JNI_InvokeFunctions;
// Number of slots in the default frame. The VM must allow at least
// 16.
#define FRAME_SIZE 16
// Mark value indicating this is an overflow frame.
#define MARK_NONE 0
// Mark value indicating this is a user frame.
#define MARK_USER 1
// Mark value indicating this is a system frame.
#define MARK_SYSTEM 2
// This structure is used to keep track of local references.
struct _Jv_JNI_LocalFrame
{
// This is one of the MARK_ constants.
unsigned char marker;
// Flag to indicate some locals were allocated.
bool allocated_p;
// Number of elements in frame.
int size;
// The class loader of the JNI method that allocated this frame.
::java::lang::ClassLoader *loader;
// Next frame in chain.
_Jv_JNI_LocalFrame *next;
// The elements. These are allocated using the C "struct hack".
jobject vec[0];
};
// This holds a reference count for all local references.
static java::util::IdentityHashMap *local_ref_table;
// This holds a reference count for all global references.
static java::util::IdentityHashMap *global_ref_table;
// The only VM.
JavaVM *_Jv_the_vm;
#ifdef ENABLE_JVMPI
// The only JVMPI interface description.
static JVMPI_Interface _Jv_JVMPI_Interface;
static jint
jvmpiEnableEvent (jint event_type, void *)
{
switch (event_type)
{
case JVMPI_EVENT_OBJECT_ALLOC:
_Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
break;
case JVMPI_EVENT_THREAD_START:
_Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
break;
case JVMPI_EVENT_THREAD_END:
_Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
break;
default:
return JVMPI_NOT_AVAILABLE;
}
return JVMPI_SUCCESS;
}
static jint
jvmpiDisableEvent (jint event_type, void *)
{
switch (event_type)
{
case JVMPI_EVENT_OBJECT_ALLOC:
_Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
break;
default:
return JVMPI_NOT_AVAILABLE;
}
return JVMPI_SUCCESS;
}
#endif
void
_Jv_JNI_Init (void)
{
local_ref_table = new java::util::IdentityHashMap;
global_ref_table = new java::util::IdentityHashMap;
#ifdef ENABLE_JVMPI
_Jv_JVMPI_Interface.version = 1;
_Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
_Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
_Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
_Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
_Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
#endif
}
// Tell the GC that a certain pointer is live.
static void
mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
{
JvSynchronize sync (ref_table);
using namespace java::lang;
Integer *refcount = (Integer *) ref_table->get (obj);
jint val = (refcount == NULL) ? 0 : refcount->intValue ();
// FIXME: what about out of memory error?
ref_table->put (obj, new Integer (val + 1));
}
// Unmark a pointer.
static void
unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
{
JvSynchronize sync (ref_table);
using namespace java::lang;
Integer *refcount = (Integer *) ref_table->get (obj);
JvAssert (refcount);
jint val = refcount->intValue () - 1;
JvAssert (val >= 0);
if (val == 0)
ref_table->remove (obj);
else
// FIXME: what about out of memory error?
ref_table->put (obj, new Integer (val));
}
// "Unwrap" some random non-reference type. This exists to simplify
// other template functions.
template<typename T>
static T
unwrap (T val)
{
return val;
}
// Unwrap a weak reference, if required.
template<typename T>
static T *
unwrap (T *obj)
{
using namespace gnu::gcj::runtime;
// We can compare the class directly because JNIWeakRef is `final'.
// Doing it this way is much faster.
if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
return obj;
JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
return reinterpret_cast<T *> (wr->get ());
}
jobject
_Jv_UnwrapJNIweakReference (jobject obj)
{
return unwrap (obj);
}
static jobject JNICALL
_Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
{
// This seems weird but I think it is correct.
obj = unwrap (obj);
mark_for_gc (obj, global_ref_table);
return obj;
}
static void JNICALL
_Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
{
// This seems weird but I think it is correct.
obj = unwrap (obj);
// NULL is ok here -- the JNI specification doesn't say so, but this
// is a no-op.
if (! obj)
return;
unmark_for_gc (obj, global_ref_table);
}
static void JNICALL
_Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
{
_Jv_JNI_LocalFrame *frame;
// This seems weird but I think it is correct.
obj = unwrap (obj);
// NULL is ok here -- the JNI specification doesn't say so, but this
// is a no-op.
if (! obj)
return;
for (frame = env->locals; frame != NULL; frame = frame->next)
{
for (int i = 0; i < frame->size; ++i)
{
if (frame->vec[i] == obj)
{
frame->vec[i] = NULL;
unmark_for_gc (obj, local_ref_table);
return;
}
}
// Don't go past a marked frame.
JvAssert (frame->marker == MARK_NONE);
}
JvAssert (0);
}
static jint JNICALL
_Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
{
// It is easier to just always allocate a new frame of the requested
// size. This isn't the most efficient thing, but for now we don't
// care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
_Jv_JNI_LocalFrame *frame;
try
{
frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
+ size * sizeof (jobject));
}
catch (jthrowable t)
{
env->ex = t;
return JNI_ERR;
}
frame->marker = MARK_NONE;
frame->size = size;
frame->allocated_p = false;
memset (&frame->vec[0], 0, size * sizeof (jobject));
frame->loader = env->locals->loader;
frame->next = env->locals;
env->locals = frame;
return 0;
}
static jint JNICALL
_Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
{
jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
if (r < 0)
return r;
// The new frame is on top.
env->locals->marker = MARK_USER;
return 0;
}
static jobject JNICALL
_Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
{
// This seems weird but I think it is correct.
obj = unwrap (obj);
// Try to find an open slot somewhere in the topmost frame.
_Jv_JNI_LocalFrame *frame = env->locals;
bool done = false, set = false;
for (; frame != NULL && ! done; frame = frame->next)
{
for (int i = 0; i < frame->size; ++i)
{
if (frame->vec[i] == NULL)
{
set = true;
done = true;
frame->vec[i] = obj;
frame->allocated_p = true;
break;
}
}
// If we found a slot, or if the frame we just searched is the
// mark frame, then we are done.
if (done || frame == NULL || frame->marker != MARK_NONE)
break;
}
if (! set)
{
// No slots, so we allocate a new frame. According to the spec
// we could just die here. FIXME: return value.
_Jv_JNI_EnsureLocalCapacity (env, 16);
// We know the first element of the new frame will be ok.
env->locals->vec[0] = obj;
env->locals->allocated_p = true;
}
mark_for_gc (obj, local_ref_table);
return obj;
}
static jobject JNICALL
_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
{
_Jv_JNI_LocalFrame *rf = env->locals;
bool done = false;
while (rf != NULL && ! done)
{
for (int i = 0; i < rf->size; ++i)
if (rf->vec[i] != NULL)
unmark_for_gc (rf->vec[i], local_ref_table);
// If the frame we just freed is the marker frame, we are done.
done = (rf->marker == stop);
_Jv_JNI_LocalFrame *n = rf->next;
// When N==NULL, we've reached the reusable bottom_locals, and we must
// not free it. However, we must be sure to clear all its elements.
if (n == NULL)
{
if (rf->allocated_p)
memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
rf->allocated_p = false;
rf = NULL;
break;
}
_Jv_Free (rf);
rf = n;
}
// Update the local frame information.
env->locals = rf;
return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
}
static jobject JNICALL
_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
{
return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
}
// Make sure an array's type is compatible with the type of the
// destination.
template<typename T>
static bool
_Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
{
jclass klass = array->getClass()->getComponentType();
if (__builtin_expect (klass != K, false))
{
env->ex = new java::lang::IllegalAccessError ();
return false;
}
else
return true;
}
// Pop a `system' frame from the stack. This is `extern "C"' as it is
// used by the compiler.
extern "C" void
_Jv_JNI_PopSystemFrame (JNIEnv *env)
{
// Only enter slow path when we're not at the bottom, or there have been
// allocations. Usually this is false and we can just null out the locals
// field.
if (__builtin_expect ((env->locals->next
|| env->locals->allocated_p), false))
_Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
else
env->locals = NULL;
#ifdef INTERPRETER
if (__builtin_expect (env->ex != NULL, false))
{
jthrowable t = env->ex;
env->ex = NULL;
if (JVMTI_REQUESTED_EVENT (Exception))
_Jv_ReportJVMTIExceptionThrow (t);
throw t;
}
#endif
}
template<typename T> T extract_from_jvalue(jvalue const & t);
template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
// This function is used from other template functions. It wraps the
// return value appropriately; we specialize it so that object returns
// are turned into local references.
template<typename T>
static T
wrap_value (JNIEnv *, T value)
{
return value;
}
// This specialization is used for jobject, jclass, jstring, jarray,
// etc.
template<typename R, typename T>
static T *
wrap_value (JNIEnv *env, T *value)
{
return (value == NULL
? value
: (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
}
static jint JNICALL
_Jv_JNI_GetVersion (JNIEnv *)
{
return JNI_VERSION_1_4;
}
static jclass JNICALL
_Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
const jbyte *buf, jsize bufLen)
{
try
{
loader = unwrap (loader);
jstring sname = JvNewStringUTF (name);
jbyteArray bytes = JvNewByteArray (bufLen);
jbyte *elts = elements (bytes);
memcpy (elts, buf, bufLen * sizeof (jbyte));
java::lang::ClassLoader *l
= reinterpret_cast<java::lang::ClassLoader *> (loader);
jclass result = l->defineClass (sname, bytes, 0, bufLen);
return (jclass) wrap_value (env, result);
}
catch (jthrowable t)
{
env->ex = t;
return NULL;
}
}
static jclass JNICALL
_Jv_JNI_FindClass (JNIEnv *env, const char *name)
{
// FIXME: assume that NAME isn't too long.
int len = strlen (name);
char s[len + 1];
for (int i = 0; i <= len; ++i)
s[i] = (name[i] == '/') ? '.' : name[i];
jclass r = NULL;
try
{
// This might throw an out of memory exception.
jstring n = JvNewStringUTF (s);
java::lang::ClassLoader *loader = NULL;
if (env->locals->loader != NULL)
loader = env->locals->loader;
if (loader == NULL)
{
// FIXME: should use getBaseClassLoader, but we don't have that
// yet.
loader = java::lang::ClassLoader::getSystemClassLoader ();
}
r = loader->loadClass (n);
_Jv_InitClass (r);
}
catch (jthrowable t)
{
env->ex = t;
}
return (jclass) wrap_value (env, r);
}
static jclass JNICALL
_Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
{
return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
}
static jboolean JNICALL
_Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
{
return unwrap (clazz2)->isAssignableFrom (unwrap (clazz1));
}
static jint JNICALL
_Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
{
// We check in case the user did some funky cast.
obj = unwrap (obj);
JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
env->ex = obj;
return 0;
}
static jint JNICALL
_Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
{
using namespace java::lang::reflect;
clazz = unwrap (clazz);
JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
int r = JNI_OK;
try
{
JArray<jclass> *argtypes
= (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
NULL);
jclass *elts = elements (argtypes);
elts[0] = &java::lang::String::class$;
Constructor *cons = clazz->getConstructor (argtypes);
jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
NULL);
jobject *velts = elements (values);
velts[0] = JvNewStringUTF (message);
jobject obj = cons->newInstance (values);
env->ex = reinterpret_cast<jthrowable> (obj);
}
catch (jthrowable t)
{
env->ex = t;
r = JNI_ERR;
}
return r;
}
static jthrowable JNICALL
_Jv_JNI_ExceptionOccurred (JNIEnv *env)
{
return (jthrowable) wrap_value (env, env->ex);
}
static void JNICALL
_Jv_JNI_ExceptionDescribe (JNIEnv *env)
{
if (env->ex != NULL)
env->ex->printStackTrace();
}
static void JNICALL
_Jv_JNI_ExceptionClear (JNIEnv *env)
{
env->ex = NULL;
}
static jboolean JNICALL
_Jv_JNI_ExceptionCheck (JNIEnv *env)
{
return env->ex != NULL;
}
static void JNICALL
_Jv_JNI_FatalError (JNIEnv *, const char *message)
{
JvFail (message);
}
static jboolean JNICALL
_Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
{
return unwrap (obj1) == unwrap (obj2);
}
static jobject JNICALL
_Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
{
jobject obj = NULL;
using namespace java::lang::reflect;
try
{
clazz = unwrap (clazz);
JvAssert (clazz && ! clazz->isArray ());
if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
env->ex = new java::lang::InstantiationException ();
else
obj = _Jv_AllocObject (clazz);
}
catch (jthrowable t)
{
env->ex = t;
}
return wrap_value (env, obj);
}
static jclass JNICALL
_Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
{
obj = unwrap (obj);
JvAssert (obj);
return (jclass) wrap_value (env, obj->getClass());
}
static jboolean JNICALL
_Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
{
return unwrap (clazz)->isInstance(unwrap (obj));
}
//
// This section concerns method invocation.
//
template<jboolean is_static>
static jmethodID JNICALL
_Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
const char *name, const char *sig)
{
try
{
clazz = unwrap (clazz);
_Jv_InitClass (clazz);
_Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
// FIXME: assume that SIG isn't too long.
int len = strlen (sig);
char s[len + 1];
for (int i = 0; i <= len; ++i)
s[i] = (sig[i] == '/') ? '.' : sig[i];
_Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
JvAssert (! clazz->isPrimitive());
using namespace java::lang::reflect;
while (clazz != NULL)
{
jint count = JvNumMethods (clazz);
jmethodID meth = JvGetFirstMethod (clazz);
for (jint i = 0; i < count; ++i)
{
if (((is_static && Modifier::isStatic (meth->accflags))
|| (! is_static && ! Modifier::isStatic (meth->accflags)))
&& _Jv_equalUtf8Consts (meth->name, name_u)
&& _Jv_equalUtf8Consts (meth->signature, sig_u))
return meth;
meth = meth->getNextMethod();
}
clazz = clazz->getSuperclass ();
}
java::lang::StringBuffer *name_sig =
new java::lang::StringBuffer (JvNewStringUTF (name));
name_sig->append ((jchar) ' ');
name_sig->append (JvNewStringUTF (s));
env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
}
catch (jthrowable t)
{
env->ex = t;
}
return NULL;
}
// This is a helper function which turns a va_list into an array of
// `jvalue's. It needs signature information in order to do its work.
// The array of values must already be allocated.
static void
array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
{
jclass *arg_elts = elements (arg_types);
for (int i = 0; i < arg_types->length; ++i)
{
// Here we assume that sizeof(int) >= sizeof(jint), because we
// use `int' when decoding the varargs. Likewise for
// float, and double. Also we assume that sizeof(jlong) >=
// sizeof(int), i.e. that jlong values are not further
// promoted.
JvAssert (sizeof (int) >= sizeof (jint));
JvAssert (sizeof (jlong) >= sizeof (int));
JvAssert (sizeof (double) >= sizeof (jfloat));
JvAssert (sizeof (double) >= sizeof (jdouble));
if (arg_elts[i] == JvPrimClass (byte))
values[i].b = (jbyte) va_arg (vargs, int);
else if (arg_elts[i] == JvPrimClass (short))
values[i].s = (jshort) va_arg (vargs, int);
else if (arg_elts[i] == JvPrimClass (int))
values[i].i = (jint) va_arg (vargs, int);
else if (arg_elts[i] == JvPrimClass (long))
values[i].j = (jlong) va_arg (vargs, jlong);
else if (arg_elts[i] == JvPrimClass (float))
values[i].f = (jfloat) va_arg (vargs, double);
else if (arg_elts[i] == JvPrimClass (double))
values[i].d = (jdouble) va_arg (vargs, double);
else if (arg_elts[i] == JvPrimClass (boolean))
values[i].z = (jboolean) va_arg (vargs, int);
else if (arg_elts[i] == JvPrimClass (char))
values[i].c = (jchar) va_arg (vargs, int);
else
{
// An object.
values[i].l = unwrap (va_arg (vargs, jobject));
}
}
}
// This can call any sort of method: virtual, "nonvirtual", static, or
// constructor.
template<typename T, invocation_type style>
static T JNICALL
_Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
jmethodID id, va_list vargs)
{
obj = unwrap (obj);
klass = unwrap (klass);
jclass decl_class = klass ? klass : obj->getClass ();
JvAssert (decl_class != NULL);
jclass return_type;
JArray<jclass> *arg_types;
try
{
_Jv_GetTypesFromSignature (id, decl_class,
&arg_types, &return_type);
jvalue args[arg_types->length];
array_from_valist (args, arg_types, vargs);
// For constructors we need to pass the Class we are instantiating.
if (style == constructor)
return_type = klass;
jvalue result;
_Jv_CallAnyMethodA (obj, return_type, id,
style == constructor,
style == normal,
arg_types, args, &result);
return wrap_value (env, extract_from_jvalue<T>(result));
}
catch (jthrowable t)
{
env->ex = t;
}
return wrap_value (env, (T) 0);
}
template<typename T, invocation_type style>
static T JNICALL
_Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
jmethodID method, ...)
{
va_list args;
T result;
va_start (args, method);
result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
va_end (args);
return result;
}
template<typename T, invocation_type style>
static T JNICALL
_Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
jmethodID id, const jvalue *args)
{
obj = unwrap (obj);
klass = unwrap (klass);
jclass decl_class = klass ? klass : obj->getClass ();
JvAssert (decl_class != NULL);
jclass return_type;
JArray<jclass> *arg_types;
try
{
_Jv_GetTypesFromSignature (id, decl_class,
&arg_types, &return_type);
// For constructors we need to pass the Class we are instantiating.
if (style == constructor)
return_type = klass;
// Unwrap arguments as required. Eww.
jclass *type_elts = elements (arg_types);
jvalue arg_copy[arg_types->length];
for (int i = 0; i < arg_types->length; ++i)
{
if (type_elts[i]->isPrimitive ())
arg_copy[i] = args[i];
else
arg_copy[i].l = unwrap (args[i].l);
}
jvalue result;
_Jv_CallAnyMethodA (obj, return_type, id,
style == constructor,
style == normal,
arg_types, arg_copy, &result);
return wrap_value (env, extract_from_jvalue<T>(result));
}
catch (jthrowable t)
{
env->ex = t;
}
return wrap_value (env, (T) 0);
}
template<invocation_type style>
static void JNICALL
_Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
jmethodID id, va_list vargs)
{
obj = unwrap (obj);
klass = unwrap (klass);
jclass decl_class = klass ? klass : obj->getClass ();
JvAssert (decl_class != NULL);
jclass return_type;
JArray<jclass> *arg_types;
try
{
_Jv_GetTypesFromSignature (id, decl_class,
&arg_types, &return_type);
jvalue args[arg_types->length];
array_from_valist (args, arg_types, vargs);
// For constructors we need to pass the Class we are instantiating.
if (style == constructor)
return_type = klass;
_Jv_CallAnyMethodA (obj, return_type, id,
style == constructor,
style == normal,
arg_types, args, NULL);
}
catch (jthrowable t)
{
env->ex = t;
}
}
template<invocation_type style>
static void JNICALL
_Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
jmethodID method, ...)
{
va_list args;
va_start (args, method);
_Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
va_end (args);
}
template<invocation_type style>
static void JNICALL
_Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
jmethodID id, const jvalue *args)
{
jclass decl_class = klass ? klass : obj->getClass ();
JvAssert (decl_class != NULL);
jclass return_type;
JArray<jclass> *arg_types;
try
{
_Jv_GetTypesFromSignature (id, decl_class,
&arg_types, &return_type);
// Unwrap arguments as required. Eww.
jclass *type_elts = elements (arg_types);
jvalue arg_copy[arg_types->length];
for (int i = 0; i < arg_types->length; ++i)
{
if (type_elts[i]->isPrimitive ())
arg_copy[i] = args[i];
else
arg_copy[i].l = unwrap (args[i].l);
}
_Jv_CallAnyMethodA (obj, return_type, id,
style == constructor,
style == normal,
arg_types, args, NULL);
}