classinfo: changed type of super and interfaces to classref_or_classinfo
[cacao.git] / src / native / vm / VMClass.c
1 /* src/native/vm/VMClass.c - java/lang/VMClass
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: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: VMClass.c 2186 2005-04-02 00:43:25Z edwin $
33
34 */
35
36
37 #include <string.h>
38
39 #include "types.h"
40 #include "mm/memory.h"
41 #include "native/jni.h"
42 #include "native/native.h"
43 #include "native/include/java_lang_Class.h"
44 #include "native/include/java_lang_ClassLoader.h"
45 #include "native/include/java_lang_Object.h"
46 #include "native/include/java_lang_VMClass.h"
47 #include "native/include/java_lang_reflect_Constructor.h"
48 #include "native/include/java_lang_reflect_Field.h"
49 #include "native/include/java_lang_reflect_Method.h"
50 #include "native/include/java_security_ProtectionDomain.h"
51 #include "toolbox/logging.h"
52 #include "vm/builtin.h"
53 #include "vm/exceptions.h"
54 #include "vm/global.h"
55 #include "vm/loader.h"
56 #include "vm/stringlocal.h"
57 #include "vm/tables.h"
58
59
60 /* for selecting public members */
61 #define MEMBER_PUBLIC  0
62
63
64 /*
65  * Class:     java/lang/VMClass
66  * Method:    forName
67  * Signature: (Ljava/lang/String;)Ljava/lang/Class;
68  */
69 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClass_forName(JNIEnv *env, jclass clazz, java_lang_String *s)
70 {
71         return NULL;
72
73         /* XXX TWISTI: we currently use the classpath default implementation, maybe 
74            we change this someday to a faster native version */
75 #if 0
76         classinfo *c;
77         utf       *u;
78
79         /* illegal argument */
80
81         if (!s)
82                 return NULL;
83         
84         /* create utf string in which '.' is replaced by '/' */
85
86         u = javastring_toutf(s, true);
87
88         /* create a new class, ... */
89
90         c = class_new(u);
91
92         /* try to load, ... */
93
94         if (!load_class_bootstrap(c)) {
95                 classinfo *xclass;
96
97                 xclass = (*exceptionptr)->vftbl->class;
98
99                 /* if the exception is a NoClassDefFoundError, we replace it with a
100                    ClassNotFoundException, otherwise return the exception */
101
102                 if (xclass == class_java_lang_NoClassDefFoundError) {
103                         /* clear exceptionptr, because builtin_new checks for 
104                            ExceptionInInitializerError */
105                         *exceptionptr = NULL;
106
107                         *exceptionptr =
108                                 new_exception_javastring(string_java_lang_ClassNotFoundException, s);
109                 }
110
111             return NULL;
112         }
113
114         /* link, ... */
115
116         if (!link_class(c))
117                 return NULL;
118         
119         /* ...and initialize it */
120
121         if (!class_init(c))
122                 return NULL;
123
124         use_class_as_object(c);
125
126         return (java_lang_Class *) c;
127 #endif
128 }
129
130
131 /*
132  * Class:     java/lang/VMClass
133  * Method:    getClassLoader
134  * Signature: ()Ljava/lang/ClassLoader;
135  */
136 JNIEXPORT java_lang_ClassLoader* JNICALL Java_java_lang_VMClass_getClassLoader(JNIEnv *env, jclass clazz, java_lang_Class *that)
137 {  
138         return (java_lang_ClassLoader *) ((classinfo *) that)->classloader;
139 }
140
141
142 /*
143  * Class:     java_lang_VMClass
144  * Method:    getComponentType
145  * Signature: ()Ljava/lang/Class;
146  */
147 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClass_getComponentType(JNIEnv *env, jclass clazz, java_lang_Class *that)
148 {
149     classinfo *thisclass = (classinfo *) that;
150     classinfo *c = NULL;
151     arraydescriptor *desc;
152     
153     if ((desc = thisclass->vftbl->arraydesc) != NULL) {
154         if (desc->arraytype == ARRAYTYPE_OBJECT)
155             c = desc->componentvftbl->class;
156         else
157             c = primitivetype_table[desc->arraytype].class_primitive;
158         
159         /* set vftbl */
160                 use_class_as_object(c);
161     }
162     
163     return (java_lang_Class *) c;
164 }
165
166
167 /*
168  * Class:     java/lang/VMClass
169  * Method:    getDeclaredConstructors
170  * Signature: (Z)[Ljava/lang/reflect/Constructor;
171  */
172 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMClass_getDeclaredConstructors(JNIEnv *env, jclass clazz, java_lang_Class *that, s4 public_only)
173 {
174   
175     classinfo *c = (classinfo *) that;
176     java_objectheader *o;
177     classinfo *class_constructor;
178     java_objectarray *array_constructor;     /* result: array of Method-objects */
179     java_objectarray *exceptiontypes;   /* the exceptions thrown by the method */
180     methodinfo *m;                      /* the current method to be represented */    
181     int public_methods = 0;             /* number of public methods of the class */
182     int pos = 0;
183     int i;
184
185     /* determine number of constructors */
186     for (i = 0; i < c->methodscount; i++) 
187                 if (((c->methods[i].flags & ACC_PUBLIC) || !public_only) && 
188                         (c->methods[i].name == utf_init))
189                         public_methods++;
190
191     class_constructor = class_new(utf_new_char("java/lang/reflect/Constructor"));
192
193         if (!class_constructor->loaded)
194                 load_class_bootstrap(class_constructor);
195
196         if (!class_constructor->linked)
197                 link_class(class_constructor);
198
199     array_constructor = builtin_anewarray(public_methods, class_constructor);
200
201     if (!array_constructor) 
202                 return NULL;
203
204     for (i = 0; i < c->methodscount; i++) 
205                 if ((c->methods[i].flags & ACC_PUBLIC) || !public_only){
206                         m = &c->methods[i];         
207                         if (m->name != utf_init)
208                                 continue;
209
210                         o = native_new_and_init(class_constructor);     
211                         array_constructor->data[pos++] = o;
212
213                         /* array of exceptions declared to be thrown, information not available !! */
214                         exceptiontypes = builtin_anewarray(0, class_java_lang_Class);
215
216                         /*          class_showconstantpool(class_constructor);*/
217                         /* initialize instance fields */
218                         /*          ((java_lang_reflect_Constructor*)o)->flag=(m->flags & (ACC_PRIVATE | ACC_PUBLIC | ACC_PROTECTED));*/
219                         setfield_critical(class_constructor,o,"clazz",          "Ljava/lang/Class;",  jobject, (jobject) c /*this*/);
220                         setfield_critical(class_constructor,o,"slot",           "I",                 jint,    i); 
221                         /*          setfield_critical(class_constructor,o,"flag",           "I",                     jint,    (m->flags & (ACC_PRIVATE | 
222                                         ACC_PUBLIC | ACC_PROTECTED))); */
223                         setfield_critical(class_constructor,o,"exceptionTypes", "[Ljava/lang/Class;", jobject, (jobject) exceptiontypes);
224             setfield_critical(class_constructor,o,"parameterTypes", "[Ljava/lang/Class;", jobject, (jobject) get_parametertypes(m));
225         }            
226     
227         return array_constructor;
228 }
229
230
231 /*
232  * Class:     java_lang_VMClass
233  * Method:    getDeclaredClasses
234  * Signature: (Z)[Ljava/lang/Class;
235  */
236 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMClass_getDeclaredClasses(JNIEnv *env, jclass clazz, java_lang_Class *that, s4 publicOnly)
237 {
238 #if defined(__GNUC__)
239 #warning fix the public only case
240 #endif
241         classinfo *c = (classinfo *) that;
242         int pos = 0;                /* current declared class */
243         int declaredclasscount = 0; /* number of declared classes */
244         java_objectarray *result;   /* array of declared classes */
245         int notPublicOnly = !publicOnly;
246         int i;
247
248         if (!that)
249                 return NULL;
250
251         /*printf("PublicOnly: %d\n",publicOnly);*/
252         if (!Java_java_lang_VMClass_isPrimitive(env, clazz, (java_lang_Class *) c) && (c->name->text[0] != '[')) {
253                 /* determine number of declared classes */
254                 for (i = 0; i < c->innerclasscount; i++) {
255                         if ( (c->innerclass[i].outer_class == c) && (notPublicOnly || (c->innerclass[i].flags & ACC_PUBLIC)))
256                                 /* outer class is this class */
257                                 declaredclasscount++;
258                 }
259         }
260
261         /*class_showmethods(c); */
262
263         result = builtin_anewarray(declaredclasscount, class_java_lang_Class);          
264
265         for (i = 0; i < c->innerclasscount; i++) {
266                 classinfo *inner = c->innerclass[i].inner_class;
267                 classinfo *outer = c->innerclass[i].outer_class;
268                 
269                 if ((outer == c) && (notPublicOnly || (inner->flags & ACC_PUBLIC))) {
270                         /* outer class is this class, store innerclass in array */
271                         use_class_as_object(inner);
272                         result->data[pos++] = (java_objectheader *) inner;
273                 }
274         }
275
276         return result;
277 }
278
279
280 /*
281  * Class:     java/lang/Class
282  * Method:    getDeclaringClass
283  * Signature: ()Ljava/lang/Class;
284  */
285 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClass_getDeclaringClass(JNIEnv *env, jclass clazz, struct java_lang_Class *that)
286 {
287 #if defined(__GNUC__)
288 #warning fixme
289 #endif
290         classinfo *c = (classinfo *) that;
291
292         if (that && !Java_java_lang_VMClass_isPrimitive(env, clazz,that) && (c->name->text[0] != '[')) {
293                 int i;
294
295                 if (c->innerclasscount == 0)  /* no innerclasses exist */
296                         return NULL;
297     
298                 for (i = 0; i < c->innerclasscount; i++) {
299                         classinfo *inner =  c->innerclass[i].inner_class;
300                         classinfo *outer =  c->innerclass[i].outer_class;
301       
302                         if (inner == c) {
303                                 /* innerclass is this class */
304                                 use_class_as_object(outer);
305                                 return (java_lang_Class *) outer;
306                         }
307                 }
308         }
309
310         /* return NULL for arrayclasses and primitive classes */
311         return NULL;
312 }
313
314
315 java_lang_reflect_Field* cacao_getField0(JNIEnv *env, java_lang_Class *that, java_lang_String *name, s4 public_only)
316 {
317     classinfo *c;
318     fieldinfo *f;               /* the field to be represented */
319     java_lang_reflect_Field *o; /* result: field-object */
320     int idx;
321
322     /* create Field object */
323 /*      c = (classinfo *) loader_load(utf_new_char("java/lang/reflect/Field")); */
324     c = class_new(utf_new_char("java/lang/reflect/Field"));
325     o = (java_lang_reflect_Field *) native_new_and_init(c);
326
327     /* get fieldinfo entry */
328     idx = class_findfield_index_approx((classinfo *) that, javastring_toutf(name, false));
329
330     if (idx < 0) {
331             *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
332             return NULL;
333         }
334
335     f = &(((classinfo *) that)->fields[idx]);
336     if (f) {
337                 if (public_only && !(f->flags & ACC_PUBLIC)) {
338                         /* field is not public  and public only had been requested*/
339                         *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
340                         return NULL;
341                 }
342
343                 /* initialize instance fields */
344                 setfield_critical(c,o,"declaringClass",          "Ljava/lang/Class;",  jobject, (jobject) that /*this*/);
345                 /*      ((java_lang_reflect_Field*)(o))->flag=f->flags;*/
346                 /* save type in slot-field for faster processing */
347                 /* setfield_critical(c,o,"flag",           "I",             jint,    (jint) f->flags); */
348                 /*o->flag = f->flags;*/
349                 setfield_critical(c,o,"slot",           "I",                jint,    (jint) idx);  
350                 setfield_critical(c,o,"name",           "Ljava/lang/String;", jstring, (jstring) name);
351                 /*setfield_critical(c,o,"type",           "Ljava/lang/Class;",  jclass,  fieldtype);*/
352
353                 return o;
354     }
355
356     return NULL;
357 }
358
359
360 /*
361  * Class:     java_lang_VMClass
362  * Method:    getDeclaredFields
363  * Signature: (Z)[Ljava/lang/reflect/Field;
364  */
365 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMClass_getDeclaredFields(JNIEnv *env, jclass clazz, java_lang_Class *that, s4 public_only)
366 {
367     classinfo *c = (classinfo *) that;
368     classinfo *class_field;
369     java_objectarray *array_field; /* result: array of field-objects */
370     int public_fields = 0;         /* number of elements in field-array */
371     int pos = 0;
372     int i;
373
374     /* determine number of fields */
375     for (i = 0; i < c->fieldscount; i++) 
376                 if ((c->fields[i].flags & ACC_PUBLIC) || (!public_only))
377                         public_fields++;
378
379 /*      class_field = loader_load(utf_new_char("java/lang/reflect/Field")); */
380     class_field = class_new(utf_new_char("java/lang/reflect/Field"));
381
382     if (!class_field) 
383                 return NULL;
384
385     /* create array of fields */
386     array_field = builtin_anewarray(public_fields, class_field);
387
388     /* creation of array failed */
389     if (!array_field) 
390                 return NULL;
391
392     /* get the fields and store in the array */    
393     for (i = 0; i < c->fieldscount; i++) 
394                 if ( (c->fields[i].flags & ACC_PUBLIC) || (!public_only))
395                         array_field->data[pos++] = 
396                                 (java_objectheader *) cacao_getField0(env,
397                                            that, (java_lang_String *) javastring_new(c->fields[i].name),public_only);
398     return array_field;
399 }
400
401
402 /*
403  * Class:     java/lang/Class
404  * Method:    getInterfaces
405  * Signature: ()[Ljava/lang/Class;
406  */
407 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMClass_getInterfaces(JNIEnv *env, jclass clazz, java_lang_Class *that)
408 {
409         classinfo *c = (classinfo *) that;
410         u4 i;
411         java_objectarray *a;
412
413         a = builtin_anewarray(c->interfacescount, class_java_lang_Class);
414
415         if (!a)
416                 return NULL;
417
418         for (i = 0; i < c->interfacescount; i++) {
419                 use_class_as_object(c->interfaces[i].cls);
420
421                 a->data[i] = (java_objectheader *) c->interfaces[i].cls;
422         }
423
424         return a;
425 }
426
427
428 java_lang_reflect_Method* cacao_getMethod0(JNIEnv *env, java_lang_Class *that, java_lang_String *name, java_objectarray *types, s4 which)
429 {
430     classinfo *c; 
431     classinfo *clazz = (classinfo *) that;
432     java_lang_reflect_Method* o;         /* result: Method-object */ 
433     java_objectarray *exceptiontypes;    /* the exceptions thrown by the method */
434     methodinfo *m;                       /* the method to be represented */
435
436 /*      c = (classinfo *) loader_load(utf_new_char("java/lang/reflect/Method")); */
437     c = class_new(utf_new_char("java/lang/reflect/Method"));
438     o = (java_lang_reflect_Method *) native_new_and_init(c);
439
440     /* find the method */
441     m = class_resolvemethod_approx(clazz, 
442                                                                    javastring_toutf(name, false),
443                                                                    create_methodsig(types,0)
444                                                                    );
445
446     if (!m || (which == MEMBER_PUBLIC && !(m->flags & ACC_PUBLIC))) {
447                 /* no apropriate method was found */
448                 *exceptionptr = new_exception(string_java_lang_NoSuchMethodException);
449                 return NULL;
450         }
451    
452     /* array of exceptions declared to be thrown, information not available!  */
453     exceptiontypes = builtin_anewarray(0, class_java_lang_Class);
454
455     /* initialize instance fields */
456
457     setfield_critical(c, o, "clazz",          "Ljava/lang/Class;",  jobject,
458                                           (jobject) clazz /*this*/);
459
460     setfield_critical(c, o, "parameterTypes", "[Ljava/lang/Class;", jobject,
461                                           (jobject) types);
462
463     setfield_critical(c, o, "exceptionTypes", "[Ljava/lang/Class;", jobject,
464                                           (jobject) exceptiontypes);
465
466     setfield_critical(c, o, "name",           "Ljava/lang/String;", jstring,
467                                           (jobject) javastring_new(m->name));
468
469     setfield_critical(c, o, "modifiers",      "I",                  jint,
470                                           m->flags);
471
472     setfield_critical(c, o, "slot",           "I",                  jint,
473                                           0); 
474
475     setfield_critical(c, o, "returnType",     "Ljava/lang/Class;",  jclass,
476                                           get_returntype(m));
477
478     return o;
479 }
480
481
482 /*
483  * Class:     java_lang_VMClass
484  * Method:    getDeclaredMethods
485  * Signature: (Z)[Ljava/lang/reflect/Method;
486  */
487 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMClass_getDeclaredMethods(JNIEnv *env, jclass clazz, java_lang_Class *that, s4 public_only)
488 {
489     classinfo *c = (classinfo *) that;    
490     java_objectheader *o;
491     classinfo *class_method;
492     java_objectarray *array_method;     /* result: array of Method-objects */
493     java_objectarray *exceptiontypes;   /* the exceptions thrown by the method */
494     methodinfo *m;                      /* the current method to be represented */    
495     int public_methods = 0;             /* number of public methods of the class */
496     int pos = 0;
497     int i;
498
499 /*      class_method = (classinfo*) loader_load(utf_new_char ("java/lang/reflect/Method")); */
500     class_method = class_new(utf_new_char("java/lang/reflect/Method"));
501
502     if (!class_method) 
503                 return NULL;
504
505         /* JOWENN: array classes do not declare methods according to mauve test. It should be considered, if 
506            we should return to my old clone method overriding instead of declaring it as a member function */
507         if (Java_java_lang_VMClass_isArray(env, clazz,that)) {
508                 return builtin_anewarray(0, class_method);
509         }
510
511
512     /* determine number of methods */
513     for (i = 0; i < c->methodscount; i++) 
514                 if ((((c->methods[i].flags & ACC_PUBLIC)) || (!public_only)) && 
515                         (!
516                          ((c->methods[i].name == utf_init) ||
517                           (c->methods[i].name == utf_clinit) )
518                          )) public_methods++;
519
520         /*      
521                 class_showmethods(class_method);
522                 panic("JOWENN");
523         */
524     
525
526     array_method = builtin_anewarray(public_methods, class_method);
527
528     if (!array_method) 
529                 return NULL;
530
531     for (i = 0; i < c->methodscount; i++) 
532                 if (((c->methods[i].flags & ACC_PUBLIC) || (!public_only)) && 
533                         (!
534                          ((c->methods[i].name == utf_init) ||
535                           (c->methods[i].name == utf_clinit) )
536                          )) {
537
538                         m = &c->methods[i];         
539                         o = native_new_and_init(class_method);     
540                         array_method->data[pos++] = o;
541
542                         /* array of exceptions declared to be thrown, information not available !! */
543                         exceptiontypes = builtin_anewarray (0, class_java_lang_Class);
544
545
546                         /* initialize instance fields */
547                         /*          ((java_lang_reflect_Method*)o)->flag=(m->flags & 
548                                         (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_ABSTRACT | ACC_STATIC | ACC_FINAL |
549                                         ACC_SYNCHRONIZED | ACC_NATIVE | ACC_STRICT)
550                                         );*/
551                         setfield_critical(class_method,o,"declaringClass",          "Ljava/lang/Class;",  jobject, (jobject) c /*this*/);
552                         setfield_critical(class_method,o,"name",           "Ljava/lang/String;", jstring, (jobject) javastring_new(m->name));
553                         /*          setfield_critical(class_method,o,"flag",      "I",               jint,   (m->flags &
554                                         (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_ABSTRACT | ACC_STATIC | ACC_FINAL |
555                                         ACC_SYNCHRONIZED | ACC_NATIVE | ACC_STRICT)));*/
556                         setfield_critical(class_method,o,"slot",           "I",              jint,    i); 
557                         /*          setfield_critical(class_method,o,"returnType",     "Ljava/lang/Class;",  jclass,  get_returntype(m));
558                                         setfield_critical(class_method,o,"exceptionTypes", "[Ljava/lang/Class;", jobject, (jobject) exceptiontypes);
559                                         setfield_critical(class_method,o,"parameterTypes", "[Ljava/lang/Class;", jobject, (jobject) get_parametertypes(m));*/
560         }            
561
562     return array_method;
563 }
564
565
566 /*
567  * Class:     java/lang/Class
568  * Method:    getModifiers
569  * Signature: ()I
570  */
571 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_getModifiers(JNIEnv *env, jclass clazz, java_lang_Class *that)
572 {
573         classinfo *c = (classinfo *) that;
574         return c->flags;
575 }
576
577
578 /*
579  * Class:     java/lang/Class
580  * Method:    getName
581  * Signature: ()Ljava/lang/String;
582  */
583 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMClass_getName(JNIEnv *env, jclass clazz, java_lang_Class* that)
584 {
585         u4 i;
586         classinfo *c = (classinfo *) that;
587         java_lang_String *s = (java_lang_String *) javastring_new(c->name);
588
589         if (!s)
590                 return NULL;
591
592         /* return string where '/' is replaced by '.' */
593         for (i = 0; i < s->value->header.size; i++) {
594                 if (s->value->data[i] == '/')
595                         s->value->data[i] = '.';
596         }
597
598         return s;
599         
600 }
601
602
603 /*
604  * Class:     java/lang/VMClass
605  * Method:    getBeautifiedName
606  * Signature: (Ljava/lang/Class;)Ljava/lang/String;
607  */
608 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMClass_getBeautifiedName(JNIEnv *env, jclass clazz, java_lang_Class *par1)
609 {
610     u4 dimCnt;
611     classinfo *c = (classinfo *) (par1);
612
613     char *utf__ptr = c->name->text;      /* current position in utf-text */
614     char **utf_ptr = &utf__ptr;
615     char *desc_end = utf_end(c->name);   /* points behind utf string     */
616     java_lang_String *s;
617     char *str = NULL;
618     s4   len;
619     s4   i;
620     
621 #if 0
622     log_text("Java_java_lang_VMClass_getBeautifiedName");
623     utf_display(c->name);
624     log_text("beautifying");
625 #endif
626     dimCnt=0;
627     while ( *utf_ptr != desc_end ) {
628                 if (utf_nextu2(utf_ptr)=='[') dimCnt++;
629                 else break;
630     }
631     utf__ptr = (*utf_ptr) - 1;
632
633     len = 0;
634
635 #if 0   
636     log_text("------>");
637     utf_display(c->name);
638     log_text("<------");
639 #endif 
640
641     if (((*utf_ptr) + 1) == desc_end) {
642             for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
643                         if (primitivetype_table[i].typesig == (*utf__ptr)) {
644                                 len = dimCnt * 2 + strlen(primitivetype_table[i].name);
645                                 str = MNEW(char, len + 1);
646                                 strcpy(str, primitivetype_table[i].name);
647                                 break;
648                         }
649             }
650     }
651
652     if (len == 0) {
653                 if (dimCnt>0) {
654                         len = dimCnt + strlen(c->name->text) - 2;
655                         str = MNEW(char, len + 1);
656                         strncpy(str, ++utf__ptr, len - 2 * dimCnt);        
657                 } else {
658                         len = strlen(c->name->text);
659                         str = MNEW(char, len + 1);
660                         strncpy(str, utf__ptr, len);       
661                 }
662                 
663     }   
664
665     dimCnt = len - 2 * dimCnt;
666     str[len] = 0;
667     for (i = len - 1; i >= dimCnt; i = i - 2) {
668                 str[i] = ']';
669                 str[i - 1] = '[';
670     }
671
672     s = javastring_new(utf_new_char(str));
673     MFREE(str, char, len + 1);
674
675     if (!s) return NULL;
676
677         /* return string where '/' is replaced by '.' */
678         for (i = 0; i < s->value->header.size; i++) {
679                 if (s->value->data[i] == '/') s->value->data[i] = '.';
680         }
681         
682         return s;
683 }
684
685
686
687 /*
688  * Class:     java/lang/Class
689  * Method:    getSuperclass
690  * Signature: ()Ljava/lang/Class;
691  */
692 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClass_getSuperclass(JNIEnv *env, jclass clazz, java_lang_Class *that)
693 {
694         classinfo *cl = (classinfo *) that;
695         classinfo *c = cl->super.cls;
696
697         if (!c)
698                 return NULL;
699
700         use_class_as_object(c);
701
702         return (java_lang_Class *) c;
703 }
704
705
706 /*
707  * Class:     java/lang/Class
708  * Method:    isArray
709  * Signature: ()Z
710  */
711 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_isArray(JNIEnv *env, jclass clazz, java_lang_Class *that)
712 {
713     classinfo *c = (classinfo *) that;
714
715     return c->vftbl->arraydesc != NULL;
716 }
717
718
719 /*
720  * Class:     java/lang/Class
721  * Method:    isAssignableFrom
722  * Signature: (Ljava/lang/Class;)Z
723  */
724 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_isAssignableFrom(JNIEnv *env, jclass clazz, java_lang_Class *that, java_lang_Class *sup)
725 {
726         if (!sup) {
727                 *exceptionptr = new_nullpointerexception();
728                 return 0;
729         }
730
731         /* XXX this may be wrong for array classes */
732         return builtin_isanysubclass((classinfo*)sup, (classinfo*)that);
733
734 }
735
736
737 /*
738  * Class:     java/lang/Class
739  * Method:    isInstance
740  * Signature: (Ljava/lang/Object;)Z
741  */
742 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_isInstance(JNIEnv *env, jclass clazz, java_lang_Class *that, java_lang_Object *obj)
743 {
744         return builtin_instanceof((java_objectheader*)obj, (classinfo*)that);
745 }
746
747
748 /*
749  * Class:     java/lang/Class
750  * Method:    isInterface
751  * Signature: ()Z
752  */
753 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_isInterface(JNIEnv *env, jclass clazz, java_lang_Class *that)
754 {
755         classinfo *c = (classinfo *) that;
756
757         if (c->flags & ACC_INTERFACE)
758                 return true;
759
760         return false;
761 }
762
763
764 /*
765  * Class:     java/lang/Class
766  * Method:    isPrimitive
767  * Signature: ()Z
768  */
769 JNIEXPORT s4 JNICALL Java_java_lang_VMClass_isPrimitive(JNIEnv *env, jclass clazz, java_lang_Class *that)
770 {
771         int i;
772         classinfo *c = (classinfo *) that;
773
774         /* search table of primitive classes */
775         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
776                 if (primitivetype_table[i].class_primitive == c)
777                         return true;
778
779         return false;
780 }
781
782
783 /*
784  * Class:     java/lang/VMClass
785  * Method:    initialize
786  * Signature: ()V
787  */
788 JNIEXPORT void JNICALL Java_java_lang_VMClass_initialize(JNIEnv *env, jclass clazz, java_lang_Class *c)
789 {
790         classinfo *ci;
791
792         ci = (classinfo *) c;
793
794         /* initialize class */
795         if (!ci->initialized)
796                 /* No need to check return value, because class_init already sets the */
797                 /* exception pointer. */
798                 (void) class_init(ci);
799 }
800
801
802 /*
803  * Class:     java/lang/VMClass
804  * Method:    loadArrayClass
805  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/Class;
806  */
807 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClass_loadArrayClass(JNIEnv *env, jclass clazz, java_lang_String *name, java_lang_ClassLoader *classloader)
808 {
809         classinfo *c;
810         utf       *u;
811
812         /* create utf string with `.' replaced by `/' */
813
814         u = javastring_toutf(name, true);
815
816         /* class_new "loads" the array class */
817
818         c = class_new(u);
819
820         /* set the classloader */
821
822         c->classloader = (java_objectheader*) classloader; /* XXX is this correct? */
823
824         use_class_as_object(c);
825
826         return (java_lang_Class *) c;
827 }
828
829
830 /*
831  * Class:     java/lang/VMClass
832  * Method:    throwException
833  * Signature: (Ljava/lang/Throwable;)V
834  */
835 JNIEXPORT void JNICALL Java_java_lang_VMClass_throwException(JNIEnv *env, jclass clazz, java_lang_Throwable *t)
836 {
837         *exceptionptr = (java_objectheader *) t;
838 }
839
840
841 /*
842  * These are local overrides for various environment variables in Emacs.
843  * Please do not remove this and leave it at the end of the file, where
844  * Emacs will automagically detect them.
845  * ---------------------------------------------------------------------
846  * Local variables:
847  * mode: c
848  * indent-tabs-mode: t
849  * c-basic-offset: 4
850  * tab-width: 4
851  * End:
852  */