get_parametertypes:
[cacao.git] / src / native / native.c
1 /* src/native/native.c - table of native functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28             Roman Obermaisser
29             Andreas Krall
30
31    Changes: Christian Thalinger
32
33    $Id: native.c 2597 2005-06-08 11:18:01Z twisti $
34
35 */
36
37
38 #include <assert.h>
39
40 #include "config.h"
41 #include "types.h"
42 #include "cacao/cacao.h"
43 #include "mm/memory.h"
44 #include "native/jni.h"
45 #include "native/native.h"
46 #include "native/include/java_lang_Throwable.h"
47 #include "toolbox/logging.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/loader.h"
52 #include "vm/options.h"
53 #include "vm/resolve.h"
54 #include "vm/stringlocal.h"
55 #include "vm/tables.h"
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/jit.h"
58
59
60 /* include table of native functions ******************************************/
61
62 #include "native/include/java_lang_Cloneable.h"
63 #include "native/include/java_util_Properties.h"
64 #include "native/include/java_io_InputStream.h"
65 #include "native/include/java_io_PrintStream.h"
66
67 #include "native/include/gnu_classpath_VMStackWalker.h"
68 #include "native/include/gnu_classpath_VMSystemProperties.h"
69 #include "native/include/java_lang_Class.h"
70 #include "native/include/java_lang_Object.h"
71 #include "native/include/java_lang_VMClass.h"
72 #include "native/include/java_lang_VMClassLoader.h"
73 #include "native/include/java_lang_VMObject.h"
74 #include "native/include/java_lang_VMRuntime.h"
75 #include "native/include/java_lang_VMString.h"
76 #include "native/include/java_lang_VMSystem.h"
77 #include "native/include/java_lang_VMThread.h"
78 #include "native/include/java_lang_VMThrowable.h"
79 #include "native/include/java_lang_reflect_Constructor.h"
80 #include "native/include/java_lang_reflect_Field.h"
81 #include "native/include/java_lang_reflect_Proxy.h"
82 #include "native/include/java_lang_reflect_Method.h"
83 #include "native/include/java_security_VMAccessController.h"
84
85 #if defined(STATIC_CLASSPATH)
86
87 /* these are required to prevent compiler warnings */
88
89 #include "native/include/java_net_DatagramPacket.h"
90 #include "native/include/java_net_InetAddress.h"
91 #include "native/include/java_net_SocketImpl.h"
92
93 #include "native/include/gnu_java_net_PlainDatagramSocketImpl.h"
94 #include "native/include/gnu_java_net_PlainSocketImpl.h"
95 #include "native/include/gnu_java_nio_PipeImpl.h"
96 #include "native/include/gnu_java_nio_channels_FileChannelImpl.h"
97 #include "native/include/gnu_java_nio_charset_iconv_IconvEncoder.h"
98 #include "native/include/gnu_java_nio_charset_iconv_IconvDecoder.h"
99 #include "native/include/java_lang_VMProcess.h"
100 #include "native/include/java_nio_MappedByteBufferImpl.h"
101 #include "native/include/java_nio_channels_spi_SelectorProvider.h"
102
103 /* now include the native table */
104
105 #include "native/nativetable.inc"
106
107 #else /* defined(STATIC_CLASSPATH) */
108
109 /* Ensure that symbols for functions implemented within CACAO are used and    */
110 /* exported to dlopen.                                                        */
111
112 static functionptr dummynativetable[] = {
113         (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
114
115         (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
116
117         (functionptr) Java_java_lang_VMClass_isInstance,
118         (functionptr) Java_java_lang_VMClass_isAssignableFrom,
119         (functionptr) Java_java_lang_VMClass_isInterface,
120         (functionptr) Java_java_lang_VMClass_isPrimitive,
121         (functionptr) Java_java_lang_VMClass_getName,
122         (functionptr) Java_java_lang_VMClass_getSuperclass,
123         (functionptr) Java_java_lang_VMClass_getInterfaces,
124         (functionptr) Java_java_lang_VMClass_getComponentType,
125         (functionptr) Java_java_lang_VMClass_getModifiers,
126         (functionptr) Java_java_lang_VMClass_getDeclaringClass,
127         (functionptr) Java_java_lang_VMClass_getDeclaredClasses,
128         (functionptr) Java_java_lang_VMClass_getDeclaredFields,
129         (functionptr) Java_java_lang_VMClass_getDeclaredMethods,
130         (functionptr) Java_java_lang_VMClass_getDeclaredConstructors,
131         (functionptr) Java_java_lang_VMClass_getClassLoader,
132         (functionptr) Java_java_lang_VMClass_forName,
133         (functionptr) Java_java_lang_VMClass_isArray,
134         (functionptr) Java_java_lang_VMClass_initialize,
135         (functionptr) Java_java_lang_VMClass_loadArrayClass,
136         (functionptr) Java_java_lang_VMClass_throwException,
137
138         (functionptr) Java_java_lang_VMClassLoader_defineClass,
139         (functionptr) Java_java_lang_VMClassLoader_resolveClass,
140         (functionptr) Java_java_lang_VMClassLoader_loadClass,
141         (functionptr) Java_java_lang_VMClassLoader_getPrimitiveClass,
142         (functionptr) Java_java_lang_VMClassLoader_nativeGetResources,
143
144         (functionptr) Java_java_lang_VMObject_getClass,
145         (functionptr) Java_java_lang_VMObject_clone,
146         (functionptr) Java_java_lang_VMObject_notify,
147         (functionptr) Java_java_lang_VMObject_notifyAll,
148         (functionptr) Java_java_lang_VMObject_wait,
149
150         (functionptr) Java_java_lang_VMRuntime_availableProcessors,
151         (functionptr) Java_java_lang_VMRuntime_freeMemory,
152         (functionptr) Java_java_lang_VMRuntime_totalMemory,
153         (functionptr) Java_java_lang_VMRuntime_maxMemory,
154         (functionptr) Java_java_lang_VMRuntime_gc,
155         (functionptr) Java_java_lang_VMRuntime_runFinalization,
156         (functionptr) Java_java_lang_VMRuntime_runFinalizationForExit,
157         (functionptr) Java_java_lang_VMRuntime_traceInstructions,
158         (functionptr) Java_java_lang_VMRuntime_traceMethodCalls,
159         (functionptr) Java_java_lang_VMRuntime_runFinalizersOnExit,
160         (functionptr) Java_java_lang_VMRuntime_exit,
161         (functionptr) Java_java_lang_VMRuntime_nativeLoad,
162         (functionptr) Java_java_lang_VMRuntime_mapLibraryName,
163
164         (functionptr) Java_java_lang_VMString_intern,
165
166         (functionptr) Java_java_lang_VMSystem_arraycopy,
167         (functionptr) Java_java_lang_VMSystem_identityHashCode,
168
169         (functionptr) Java_java_lang_VMThread_start,
170         (functionptr) Java_java_lang_VMThread_interrupt,
171         (functionptr) Java_java_lang_VMThread_isInterrupted,
172         (functionptr) Java_java_lang_VMThread_suspend,
173         (functionptr) Java_java_lang_VMThread_resume,
174         (functionptr) Java_java_lang_VMThread_nativeSetPriority,
175         (functionptr) Java_java_lang_VMThread_nativeStop,
176         (functionptr) Java_java_lang_VMThread_currentThread,
177         (functionptr) Java_java_lang_VMThread_yield,
178         (functionptr) Java_java_lang_VMThread_interrupted,
179         (functionptr) Java_java_lang_VMThread_holdsLock,
180
181         (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
182         (functionptr) Java_java_lang_VMThrowable_getStackTrace,
183
184         (functionptr) Java_java_lang_reflect_Constructor_getModifiers,
185         (functionptr) Java_java_lang_reflect_Constructor_constructNative,
186
187         (functionptr) Java_java_lang_reflect_Field_getModifiers,
188         (functionptr) Java_java_lang_reflect_Field_getType,
189         (functionptr) Java_java_lang_reflect_Field_get,
190         (functionptr) Java_java_lang_reflect_Field_getBoolean,
191         (functionptr) Java_java_lang_reflect_Field_getByte,
192         (functionptr) Java_java_lang_reflect_Field_getChar,
193         (functionptr) Java_java_lang_reflect_Field_getShort,
194         (functionptr) Java_java_lang_reflect_Field_getInt,
195         (functionptr) Java_java_lang_reflect_Field_getLong,
196         (functionptr) Java_java_lang_reflect_Field_getFloat,
197         (functionptr) Java_java_lang_reflect_Field_getDouble,
198         (functionptr) Java_java_lang_reflect_Field_set,
199         (functionptr) Java_java_lang_reflect_Field_setBoolean,
200         (functionptr) Java_java_lang_reflect_Field_setByte,
201         (functionptr) Java_java_lang_reflect_Field_setChar,
202         (functionptr) Java_java_lang_reflect_Field_setShort,
203         (functionptr) Java_java_lang_reflect_Field_setInt,
204         (functionptr) Java_java_lang_reflect_Field_setLong,
205         (functionptr) Java_java_lang_reflect_Field_setFloat,
206         (functionptr) Java_java_lang_reflect_Field_setDouble,
207
208         (functionptr) Java_java_lang_reflect_Proxy_getProxyClass0,
209         (functionptr) Java_java_lang_reflect_Proxy_getProxyData0,
210         (functionptr) Java_java_lang_reflect_Proxy_generateProxyClass0,
211
212         (functionptr) Java_java_lang_reflect_Method_getModifiers,
213         (functionptr) Java_java_lang_reflect_Method_getReturnType,
214         (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
215         (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
216         (functionptr) Java_java_lang_reflect_Method_invokeNative,
217
218         (functionptr) Java_java_security_VMAccessController_getStack,
219 };
220
221 #endif /* defined(STATIC_CLASSPATH) */
222
223
224 /************* use classinfo structure as java.lang.Class object **************/
225
226 void use_class_as_object(classinfo *c) 
227 {
228         if (!c->classvftbl) {
229                 /* is the class loaded */
230                 if (!c->loaded) {
231 /*                      if (!class_load(c)) */
232 /*                              throw_exception_exit(); */
233                         log_text("use_class_as_object: class_load should not happen");
234                         assert(0);
235                 }
236
237                 /* is the class linked */
238                 if (!c->linked)
239                         if (!link_class(c))
240                                 throw_exception_exit();
241
242                 assert(class_java_lang_Class);
243
244                 c->header.vftbl = class_java_lang_Class->vftbl;
245                 c->classvftbl = true;
246         }
247 }
248
249
250 /************************** tables for methods ********************************/
251
252 #undef JOWENN_DEBUG
253 #undef JOWENN_DEBUG1
254
255 #ifdef STATIC_CLASSPATH
256 #define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
257
258 /* table for fast string comparison */
259 static nativecompref nativecomptable[NATIVETABLESIZE];
260
261 /* string comparsion table initialized */
262 static bool nativecompdone = false;
263 #endif
264
265
266 /* XXX don't define this in a header file!!! */
267
268 static struct nativeCall nativeCalls[] =
269 {
270 #include "nativecalls.inc"
271 };
272
273 #define NATIVECALLSSIZE    (sizeof(nativeCalls) / sizeof(struct nativeCall))
274
275 struct nativeCompCall nativeCompCalls[NATIVECALLSSIZE];
276
277
278 /* native_loadclasses **********************************************************
279
280    Load classes required for native methods.
281
282 *******************************************************************************/
283
284 bool native_init(void)
285 {
286 #if !defined(STATIC_CLASSPATH)
287         void *p;
288
289         /* We need to access the dummy native table, not only to remove a warning */
290         /* but to be sure that the table is not optimized away (gcc does this     */
291         /* since 3.4).                                                            */
292         p = &dummynativetable;
293 #endif
294
295         /* everything's ok */
296
297         return true;
298 }
299
300
301 /*********************** Function: native_findfunction *************************
302
303         Looks up a method (must have the same class name, method name, descriptor
304         and 'static'ness) and returns a function pointer to it.
305         Returns: function pointer or NULL (if there is no such method)
306
307         Remark: For faster operation, the names/descriptors are converted from C
308                 strings to Unicode the first time this function is called.
309
310 *******************************************************************************/
311
312 functionptr native_findfunction(utf *cname, utf *mname, 
313                                                                 utf *desc, bool isstatic)
314 {
315 #ifdef STATIC_CLASSPATH
316         int i;
317         /* entry of table for fast string comparison */
318         struct nativecompref *n;
319         /* for warning message if no function is found */
320         char *buffer;                   
321         int buffer_len;
322
323         isstatic = isstatic ? true : false;
324         
325         if (!nativecompdone) {
326                 for (i = 0; i < NATIVETABLESIZE; i++) {
327                         nativecomptable[i].classname  = 
328                                 utf_new_char(nativetable[i].classname);
329                         nativecomptable[i].methodname = 
330                                 utf_new_char(nativetable[i].methodname);
331                         nativecomptable[i].descriptor = 
332                                 utf_new_char(nativetable[i].descriptor);
333                         nativecomptable[i].isstatic   = 
334                                 nativetable[i].isstatic;
335                         nativecomptable[i].func       = 
336                                 nativetable[i].func;
337                 }
338                 nativecompdone = true;
339         }
340
341 #ifdef JOWENN_DEBUG
342         buffer_len = 
343                 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
344         
345         buffer = MNEW(char, buffer_len);
346
347         strcpy(buffer, "searching matching function in native table:");
348         utf_sprint(buffer+strlen(buffer), mname);
349         strcpy(buffer+strlen(buffer), ": ");
350         utf_sprint(buffer+strlen(buffer), desc);
351         strcpy(buffer+strlen(buffer), " for class ");
352         utf_sprint(buffer+strlen(buffer), cname);
353
354         log_text(buffer);       
355
356         MFREE(buffer, char, buffer_len);
357 #endif
358                 
359         for (i = 0; i < NATIVETABLESIZE; i++) {
360                 n = &(nativecomptable[i]);
361
362                 if (cname == n->classname && mname == n->methodname &&
363                     desc == n->descriptor && isstatic == n->isstatic)
364                         return n->func;
365 #ifdef JOWENN_DEBUG
366                         else {
367                                 if (cname == n->classname && mname == n->methodname )  log_text("static and descriptor mismatch");
368                         
369                                 else {
370                                         buffer_len = 
371                                           utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
372         
373                                         buffer = MNEW(char, buffer_len);
374
375                                         strcpy(buffer, "comparing with:");
376                                         utf_sprint(buffer+strlen(buffer), n->methodname);
377                                         strcpy (buffer+strlen(buffer), ": ");
378                                         utf_sprint(buffer+strlen(buffer), n->descriptor);
379                                         strcpy(buffer+strlen(buffer), " for class ");
380                                         utf_sprint(buffer+strlen(buffer), n->classname);
381
382                                         log_text(buffer);       
383
384                                         MFREE(buffer, char, buffer_len);
385                                 }
386                         } 
387 #endif
388         }
389
390                 
391         /* no function was found, display warning */
392
393         buffer_len = 
394                 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
395
396         buffer = MNEW(char, buffer_len);
397
398         strcpy(buffer, "warning: native function ");
399         utf_sprint(buffer + strlen(buffer), mname);
400         strcpy(buffer + strlen(buffer), ": ");
401         utf_sprint(buffer + strlen(buffer), desc);
402         strcpy(buffer + strlen(buffer), " not found in class ");
403         utf_sprint(buffer + strlen(buffer), cname);
404
405         log_text(buffer);       
406
407         MFREE(buffer, char, buffer_len);
408
409 /*      exit(1); */
410
411         /* keep compiler happy */
412         return NULL;
413 #else
414 /* dynamic classpath */
415   return 0;
416 #endif
417 }
418
419
420 /****************** function class_findfield_approx ****************************
421         
422         searches in 'classinfo'-structure for a field with the
423         specified name
424
425 *******************************************************************************/
426  
427 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
428 {
429         s4 i;
430
431         for (i = 0; i < c->fieldscount; i++) {
432                 /* compare field names */
433                 if ((c->fields[i].name == name))
434                         return &(c->fields[i]);
435         }
436
437         /* field was not found, raise exception */      
438         *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
439
440         return NULL;
441 }
442
443
444 s4 class_findfield_index_approx(classinfo *c, utf *name)
445 {
446         s4 i;
447
448         for (i = 0; i < c->fieldscount; i++) {
449                 /* compare field names */
450                 if ((c->fields[i].name == name))
451                         return i;
452         }
453
454         /* field was not found, raise exception */      
455         *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
456
457         return -1;
458 }
459
460
461 /* native_new_and_init *********************************************************
462
463    Creates a new object on the heap and calls the initializer.
464    Returns the object pointer or NULL if memory is exhausted.
465                         
466 *******************************************************************************/
467
468 java_objectheader *native_new_and_init(classinfo *c)
469 {
470         methodinfo *m;
471         java_objectheader *o;
472
473         if (!c)
474                 return *exceptionptr;
475
476         /* create object */
477
478         o = builtin_new(c);
479         
480         if (!o)
481                 return NULL;
482
483         /* find initializer */
484
485         m = class_findmethod(c, utf_init, utf_void__void);
486                                                       
487         /* initializer not found */
488
489         if (!m)
490                 return o;
491
492         /* call initializer */
493
494         asm_calljavafunction(m, o, NULL, NULL, NULL);
495
496         return o;
497 }
498
499
500 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
501 {
502         methodinfo *m;
503         java_objectheader *o;
504
505         if (!c)
506                 return *exceptionptr;
507
508         /* create object */
509
510         o = builtin_new(c);
511
512         if (!o)
513                 return NULL;
514
515         /* find initializer */
516
517         m = class_resolveclassmethod(c,
518                                                                  utf_init,
519                                                                  utf_java_lang_String__void,
520                                                                  NULL,
521                                                                  true);
522
523         /* initializer not found */
524
525         if (!m)
526                 return NULL;
527
528         /* call initializer */
529
530         asm_calljavafunction(m, o, s, NULL, NULL);
531
532         return o;
533 }
534
535
536 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
537 {
538         methodinfo *m;
539         java_objectheader *o;
540
541         if (!c)
542                 return *exceptionptr;
543
544         /* create object */
545
546         o = builtin_new(c);
547         
548         if (!o)
549                 return NULL;
550
551         /* find initializer */
552
553         m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
554
555         /* initializer not found  */
556
557         if (!m)
558                 return NULL;
559
560         /* call initializer */
561
562         asm_calljavafunction(m, o, (void *) (ptrint) i, NULL, NULL);
563
564         return o;
565 }
566
567
568 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
569 {
570         methodinfo *m;
571         java_objectheader *o;
572
573         if (!c)
574                 return *exceptionptr;
575
576         /* create object */
577
578         o = builtin_new(c);
579         
580         if (!o)
581                 return NULL;
582
583         /* find initializer */
584
585         m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
586                                                       
587         /* initializer not found */
588
589         if (!m)
590                 return NULL;
591
592         /* call initializer */
593
594         asm_calljavafunction(m, o, t, NULL, NULL);
595
596         return o;
597 }
598
599
600 void copy_vftbl(vftbl_t **dest, vftbl_t *src)
601 {
602     *dest = src;
603 #if 0
604     /* XXX this kind of copying does not work (in the general
605      * case). The interface tables would have to be copied, too. I
606      * don't see why we should make a copy anyway. -Edwin
607      */
608         *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
609         memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
610         memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
611 #endif
612 }
613
614
615 /******************************************************************************************                                                                                                             
616
617         creates method signature (excluding return type) from array of 
618         class-objects representing the parameters of the method 
619
620 *******************************************************************************************/
621
622
623 utf *create_methodsig(java_objectarray* types, char *retType)
624 {
625     char *buffer;       /* buffer for building the desciptor */
626     char *pos;          /* current position in buffer */
627     utf *result;        /* the method signature */
628     u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
629     u4 i, j;
630  
631     if (!types) return NULL;
632
633     /* determine required buffer-size */    
634     for (i = 0; i < types->header.size; i++) {
635                 classinfo *c = (classinfo *) types->data[i];
636                 buffer_size  = buffer_size + c->name->blength + 2;
637     }
638
639     if (retType) buffer_size += strlen(retType);
640
641     /* allocate buffer */
642     buffer = MNEW(char, buffer_size);
643     pos    = buffer;
644     
645     /* method-desciptor starts with parenthesis */
646     *pos++ = '(';
647
648     for (i = 0; i < types->header.size; i++) {
649                 char ch;           
650
651                 /* current argument */
652             classinfo *c = (classinfo *) types->data[i];
653
654             /* current position in utf-text */
655             char *utf_ptr = c->name->text; 
656             
657             /* determine type of argument */
658             if ((ch = utf_nextu2(&utf_ptr)) == '[') {
659                 /* arrayclass */
660                 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
661                                 *pos++ = *utf_ptr; /* copy text */
662                         }
663
664             } else {            
665                         /* check for primitive types */
666                         for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
667                                 char *utf_pos   = utf_ptr - 1;
668                                 char *primitive = primitivetype_table[j].wrapname;
669
670                                 /* compare text */
671                                 while (utf_pos < utf_end(c->name)) {
672                                         if (*utf_pos++ != *primitive++) goto nomatch;
673                                 }
674
675                                 /* primitive type found */
676                                 *pos++ = primitivetype_table[j].typesig;
677                                 goto next_type;
678
679                         nomatch:
680                                 ;
681                         }
682
683                         /* no primitive type and no arrayclass, so must be object */
684                         *pos++ = 'L';
685
686                         /* copy text */
687                         for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
688                                 *pos++ = *utf_ptr;
689                         }
690
691                         *pos++ = ';';
692
693                 next_type:
694                         ;
695                 }  
696     }       
697
698     *pos++ = ')';
699
700     if (retType) {
701                 for (i = 0; i < strlen(retType); i++) {
702                         *pos++ = retType[i];
703                 }
704     }
705
706     /* create utf-string */
707     result = utf_new(buffer, (pos - buffer));
708     MFREE(buffer, char, buffer_size);
709
710     return result;
711 }
712
713
714 /* get_parametertypes **********************************************************
715
716    Use the descriptor of a method to generate a java/lang/Class array
717    which contains the classes of the parametertypes of the method.
718
719 *******************************************************************************/
720
721 java_objectarray *get_parametertypes(methodinfo *m) 
722 {
723         methoddesc       *md;
724         typedesc         *paramtypes;
725         s4                paramcount;
726     java_objectarray *result;
727         s4                i;
728
729         md = m->parseddesc;
730
731         /* is the descriptor fully parsed? */
732
733         if (!m->parseddesc->params)
734                 if (!descriptor_params_from_paramtypes(md, m->flags))
735                         return NULL;
736
737         paramtypes = md->paramtypes;
738         paramcount = md->paramcount;
739
740         /* skip `this' pointer */
741
742         if (!(m->flags & ACC_STATIC)) {
743                 paramtypes++;
744                 paramcount--;
745         }
746
747         /* create class-array */
748
749         result = builtin_anewarray(paramcount, class_java_lang_Class);
750
751     /* get classes */
752
753         for (i = 0; i < paramcount; i++) {
754                 if (!resolve_class_from_typedesc(&paramtypes[i], false,
755                                                                                  (classinfo **) &result->data[i]))
756                         return NULL;
757
758                 use_class_as_object((classinfo *) result->data[i]);
759         }
760
761         return result;
762 }
763
764
765 /* get_exceptiontypes **********************************************************
766
767    get the exceptions which can be thrown by a method
768
769 *******************************************************************************/
770
771 java_objectarray* get_exceptiontypes(methodinfo *m)
772 {
773     u2 excount;
774     u2 i;
775     java_objectarray *result;
776         classinfo *cls;
777
778         excount = m->thrownexceptionscount;
779
780     /* create class-array */
781         assert(class_java_lang_Class);
782
783     result = builtin_anewarray(excount, class_java_lang_Class);
784
785     for (i = 0; i < excount; i++) {
786                 if (!resolve_classref_or_classinfo(NULL,m->thrownexceptions[i],resolveEager,false,&cls))
787                         return NULL; /* exception */
788                 use_class_as_object(cls);
789                 result->data[i] = (java_objectheader *)cls;
790     }
791
792     return result;
793 }
794
795
796 /******************************************************************************************
797
798         get the returntype class of a method
799
800 *******************************************************************************************/
801
802 classinfo *get_returntype(methodinfo *m) 
803 {
804         classinfo *cls;
805
806         if (!resolve_class_from_typedesc(&(m->parseddesc->returntype),false,&cls))
807                 return NULL; /* exception */
808
809         use_class_as_object(cls);
810         return cls;
811 }
812
813
814 /*****************************************************************************/
815 /*****************************************************************************/
816
817
818 /*--------------------------------------------------------*/
819 void printNativeCall(nativeCall nc) {
820   int i,j;
821
822   printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
823   for (i=0; i<nc.methCnt; i++) {  
824       printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
825
826     for (j=0; j<nc.callCnt[i]; j++) {  
827         printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
828         nc.methods[i].methodCalls[j].classname, 
829         nc.methods[i].methodCalls[j].methodname, 
830         nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
831       }
832     }
833   printf("-+++++--------------------\n");fflush(stdout);
834 }
835
836 /*--------------------------------------------------------*/
837 void printCompNativeCall(nativeCompCall nc) {
838   int i,j;
839   printf("printCompNativeCall BEGIN\n");fflush(stdout); 
840   printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
841   utf_display(nc.classname); fflush(stdout);
842   
843   for (i=0; i<nc.methCnt; i++) {  
844     printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
845     utf_display(nc.methods[i].methodname); fflush(stdout);
846     utf_display(nc.methods[i].descriptor);fflush(stdout);
847     printf("\n");fflush(stdout);
848
849     for (j=0; j<nc.callCnt[i]; j++) {  
850       printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
851         utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
852         utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
853         utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
854         printf("\n");fflush(stdout);
855       }
856     }
857 printf("---------------------\n");fflush(stdout);
858 }
859
860
861 /*--------------------------------------------------------*/
862 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d ) 
863 {
864     int i = 0;
865     int j = 0;
866     int cnt = 0;
867     classMeth mc;
868     mc.i_class = i;
869     mc.j_method = j;
870     mc.methCnt = cnt;
871
872     return mc;
873 }
874
875 /*--------------------------------------------------------*/
876 nativeCall* findNativeClassCalls(char *aclassname ) {
877 int i;
878
879 for (i=0;i<NATIVECALLSSIZE; i++) {
880    /* convert table to utf later to speed up search */ 
881    if (strcmp(nativeCalls[i].classname, aclassname) == 0) 
882         return &nativeCalls[i];
883    }
884
885 return NULL;
886 }
887 /*--------------------------------------------------------*/
888 /*--------------------------------------------------------*/
889 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
890   int i,j;
891
892
893   ncc->classname = utf_new_char(nc.classname); 
894   ncc->methCnt = nc.methCnt;
895   
896   for (i=0; i<nc.methCnt; i++) {  
897     ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
898     ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
899     ncc->callCnt[i] = nc.callCnt[i];
900
901     for (j=0; j<nc.callCnt[i]; j++) {  
902
903         ncc->methods[i].methodCalls[j].classname  = utf_new_char(nc.methods[i].methodCalls[j].classname);
904
905         if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
906           ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
907           ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
908           }
909         else {
910           ncc->methods[i].methodCalls[j].methodname = NULL;
911           ncc->methods[i].methodCalls[j].descriptor = NULL;
912           }
913       }
914     }
915 }
916
917
918
919 /*--------------------------------------------------------*/
920
921 bool natcall2utf(bool natcallcompdone) {
922 int i;
923
924 if (natcallcompdone) 
925         return true;
926
927 for (i=0;i<NATIVECALLSSIZE; i++) {
928    utfNativeCall  (nativeCalls[i], &nativeCompCalls[i]);  
929    }
930
931 return true;
932 }
933
934 /*--------------------------------------------------------*/
935
936
937 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start)
938 {
939         /* XXX platform dependend */
940
941         java_objectarray *tmpArray;
942         int i;
943         classinfo **current;
944         classinfo *c;
945         size_t size;
946
947         size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
948
949         /*printf("end %p, start %p, size %ld\n",end,start,size);*/
950
951         if (size > 0) {
952                 if (*start == class_java_lang_SecurityManager) {
953                         size--;
954                         start--;
955                 }
956         }
957
958         tmpArray = builtin_anewarray(size, class_java_lang_Class);
959
960         for(i = 0, current = start; i < size; i++, current--) {
961                 c = *current;
962                 /*              printf("%d\n",i);
963                 utf_display(c->name);*/
964                 use_class_as_object(c);
965                 tmpArray->data[i] = (java_objectheader *) c;
966         }
967
968         return tmpArray;
969 }
970
971
972 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start)
973 {
974         /* XXX platform dependend */
975
976         int i;
977         classinfo **current;
978         classinfo *c;
979         classinfo *privilegedAction;
980         size_t size;
981
982         size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
983
984         /*      log_text("builtin_asm_getclassloader");
985         printf("end %p, start %p, size %ld\n",end,start,size);*/
986
987         if (!class_java_lang_SecurityManager)
988                 if (!load_class_bootstrap(utf_new_char("java/lang/SecurityManager"),&class_java_lang_SecurityManager))
989                         return NULL;
990
991         if (size > 0) {
992                 if (*start == class_java_lang_SecurityManager) {
993                         size--;
994                         start--;
995                 }
996         }
997
998         if (!load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"),&privilegedAction))
999                 return NULL;
1000
1001         for(i = 0, current = start; i < size; i++, current--) {
1002                 c = *current;
1003
1004                 if (c == privilegedAction)
1005                         return NULL;
1006
1007                 if (c->classloader)
1008                         return (java_lang_ClassLoader *) c->classloader;
1009         }
1010
1011         return NULL;
1012 }
1013
1014
1015 /*
1016  * These are local overrides for various environment variables in Emacs.
1017  * Please do not remove this and leave it at the end of the file, where
1018  * Emacs will automagically detect them.
1019  * ---------------------------------------------------------------------
1020  * Local variables:
1021  * mode: c
1022  * indent-tabs-mode: t
1023  * c-basic-offset: 4
1024  * tab-width: 4
1025  * End:
1026  */