Merged revisions 8321-8342 via svnmerge from
[cacao.git] / src / vmcore / class.c
1 /* src/vmcore/class.c - class related functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: class.c 8343 2007-08-17 21:39:32Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "vm/types.h"
38
39 #include "arch.h"
40
41 #include "mm/memory.h"
42
43 #include "native/llni.h"
44
45 #include "threads/lock-common.h"
46
47 #include "toolbox/logging.h"
48
49 #include "vm/array.h"
50 #include "vm/builtin.h"
51 #include "vm/exceptions.h"
52 #include "vm/global.h"
53 #include "vm/resolve.h"
54
55 #include "vm/jit/asmpart.h"
56
57 #include "vmcore/class.h"
58 #include "vmcore/classcache.h"
59 #include "vmcore/linker.h"
60 #include "vmcore/loader.h"
61 #include "vmcore/options.h"
62
63 #if defined(ENABLE_STATISTICS)
64 # include "vmcore/statistics.h"
65 #endif
66
67 #include "vmcore/suck.h"
68 #include "vmcore/utf8.h"
69
70
71 /* global variables ***********************************************************/
72
73 /* frequently used classes ****************************************************/
74
75 /* important system classes */
76
77 classinfo *class_java_lang_Object;
78 classinfo *class_java_lang_Class;
79 classinfo *class_java_lang_ClassLoader;
80 classinfo *class_java_lang_Cloneable;
81 classinfo *class_java_lang_SecurityManager;
82 classinfo *class_java_lang_String;
83 classinfo *class_java_lang_System;
84 classinfo *class_java_lang_Thread;
85 classinfo *class_java_lang_ThreadGroup;
86 classinfo *class_java_lang_VMSystem;
87 classinfo *class_java_lang_VMThread;
88 classinfo *class_java_io_Serializable;
89
90 #if defined(WITH_CLASSPATH_SUN)
91 classinfo *class_sun_reflect_MagicAccessorImpl;
92 #endif
93
94 /* system exception classes required in cacao */
95
96 classinfo *class_java_lang_Throwable;
97 classinfo *class_java_lang_Error;
98 classinfo *class_java_lang_LinkageError;
99 classinfo *class_java_lang_NoClassDefFoundError;
100 classinfo *class_java_lang_OutOfMemoryError;
101 classinfo *class_java_lang_VirtualMachineError;
102
103 #if defined(WITH_CLASSPATH_GNU)
104 classinfo *class_java_lang_VMThrowable;
105 #endif
106
107 classinfo *class_java_lang_Exception;
108 classinfo *class_java_lang_ClassCastException;
109 classinfo *class_java_lang_ClassNotFoundException;
110
111 #if defined(ENABLE_JAVASE)
112 classinfo *class_java_lang_Void;
113 #endif
114 classinfo *class_java_lang_Boolean;
115 classinfo *class_java_lang_Byte;
116 classinfo *class_java_lang_Character;
117 classinfo *class_java_lang_Short;
118 classinfo *class_java_lang_Integer;
119 classinfo *class_java_lang_Long;
120 classinfo *class_java_lang_Float;
121 classinfo *class_java_lang_Double;
122
123
124 /* some runtime exception */
125
126 classinfo *class_java_lang_NullPointerException;
127
128
129 /* some classes which may be used more often */
130
131 #if defined(ENABLE_JAVASE)
132 classinfo *class_java_lang_StackTraceElement;
133 classinfo *class_java_lang_reflect_Constructor;
134 classinfo *class_java_lang_reflect_Field;
135 classinfo *class_java_lang_reflect_Method;
136 classinfo *class_java_security_PrivilegedAction;
137 classinfo *class_java_util_Vector;
138
139 classinfo *arrayclass_java_lang_Object;
140
141 #if defined(ENABLE_ANNOTATIONS)
142 classinfo *class_sun_reflect_ConstantPool;
143 classinfo *class_sun_reflect_annotation_AnnotationParser;
144 #endif
145 #endif
146
147
148 /* pseudo classes for the typechecker */
149
150 classinfo *pseudo_class_Arraystub;
151 classinfo *pseudo_class_Null;
152 classinfo *pseudo_class_New;
153
154
155 /* class_set_packagename *******************************************************
156
157    Derive the package name from the class name and store it in the struct.
158
159 *******************************************************************************/
160
161 void class_set_packagename(classinfo *c)
162 {
163         char *p = UTF_END(c->name) - 1;
164         char *start = c->name->text;
165
166         /* set the package name */
167         /* classes in the unnamed package keep packagename == NULL */
168
169         if (c->name->text[0] == '[') {
170                 /* set packagename of arrays to the element's package */
171
172                 for (; *start == '['; start++);
173
174                 /* skip the 'L' in arrays of references */
175                 if (*start == 'L')
176                         start++;
177
178                 for (; (p > start) && (*p != '/'); --p);
179
180                 c->packagename = utf_new(start, p - start);
181
182         } else {
183                 for (; (p > start) && (*p != '/'); --p);
184
185                 c->packagename = utf_new(start, p - start);
186         }
187 }
188
189
190 /* class_create_classinfo ******************************************************
191
192    Create a new classinfo struct. The class name is set to the given utf *,
193    most other fields are initialized to zero.
194
195    Note: classname may be NULL. In this case a not-yet-named classinfo is
196          created. The name must be filled in later and class_set_packagename
197                  must be called after that.
198
199 *******************************************************************************/
200
201 classinfo *class_create_classinfo(utf *classname)
202 {
203         classinfo *c;
204
205 #if defined(ENABLE_STATISTICS)
206         if (opt_stat)
207                 size_classinfo += sizeof(classinfo);
208 #endif
209
210         /* we use a safe name for temporarily unnamed classes */
211
212         if (classname == NULL)
213                 classname = utf_not_named_yet;
214
215 #if !defined(NDEBUG)
216         if (initverbose)
217                 log_message_utf("Creating class: ", classname);
218 #endif
219
220         /* GCNEW_UNCOLLECTABLE clears the allocated memory */
221
222 #if defined(ENABLE_GC_CACAO)
223         c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
224 #else
225         c = GCNEW_UNCOLLECTABLE(classinfo, 1);
226         /*c=NEW(classinfo);*/
227 #endif
228         c->name = classname;
229
230         /* Set the header.vftbl of all loaded classes to the one of
231        java.lang.Class, so Java code can use a class as object. */
232
233         if (class_java_lang_Class != NULL)
234                 if (class_java_lang_Class->vftbl != NULL)
235                         c->object.header.vftbl = class_java_lang_Class->vftbl;
236
237 #if defined(ENABLE_JAVASE)
238         /* check if the class is a reference class and flag it */
239
240         if (classname == utf_java_lang_ref_SoftReference) {
241                 c->flags |= ACC_CLASS_REFERENCE_SOFT;
242         }
243         else if (classname == utf_java_lang_ref_WeakReference) {
244                 c->flags |= ACC_CLASS_REFERENCE_WEAK;
245         }
246         else if (classname == utf_java_lang_ref_PhantomReference) {
247                 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
248         }
249 #endif
250
251         if (classname != utf_not_named_yet)
252                 class_set_packagename(c);
253
254         LOCK_INIT_OBJECT_LOCK(&c->object.header);
255
256         return c;
257 }
258
259
260 /* class_postset_header_vftbl **************************************************
261
262    Set the header.vftbl of all classes created before java.lang.Class
263    was linked.  This is necessary that Java code can use a class as
264    object.
265
266 *******************************************************************************/
267
268 void class_postset_header_vftbl(void)
269 {
270         classinfo *c;
271         u4 slot;
272         classcache_name_entry *nmen;
273         classcache_class_entry *clsen;
274
275         assert(class_java_lang_Class);
276
277         for (slot = 0; slot < hashtable_classcache.size; slot++) {
278                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
279
280                 for (; nmen; nmen = nmen->hashlink) {
281                         /* iterate over all class entries */
282
283                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
284                                 c = clsen->classobj;
285
286                                 /* now set the the vftbl */
287
288                                 if (c->object.header.vftbl == NULL)
289                                         c->object.header.vftbl = class_java_lang_Class->vftbl;
290                         }
291                 }
292         }
293 }
294
295 /* class_define ****************************************************************
296
297    Calls the loader and defines a class in the VM.
298
299 *******************************************************************************/
300
301 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
302 {
303         classinfo   *c;
304         classinfo   *r;
305         classbuffer *cb;
306
307         if (name != NULL) {
308                 /* check if this class has already been defined */
309
310                 c = classcache_lookup_defined_or_initiated(cl, name);
311
312                 if (c != NULL) {
313                         exceptions_throw_linkageerror("duplicate class definition: ", c);
314                         return NULL;
315                 }
316         } 
317
318         /* create a new classinfo struct */
319
320         c = class_create_classinfo(name);
321
322 #if defined(ENABLE_STATISTICS)
323         /* measure time */
324
325         if (opt_getloadingtime)
326                 loadingtime_start();
327 #endif
328
329         /* build a classbuffer with the given data */
330
331         cb = NEW(classbuffer);
332
333         cb->class = c;
334         cb->size  = length;
335         cb->data  = data;
336         cb->pos   = cb->data;
337
338         /* preset the defining classloader */
339
340         c->classloader = cl;
341
342         /* load the class from this buffer */
343
344         r = load_class_from_classbuffer(cb);
345
346         /* free memory */
347
348         FREE(cb, classbuffer);
349
350 #if defined(ENABLE_STATISTICS)
351         /* measure time */
352
353         if (opt_getloadingtime)
354                 loadingtime_stop();
355 #endif
356
357         if (r == NULL) {
358                 /* If return value is NULL, we had a problem and the class is
359                    not loaded.  Now free the allocated memory, otherwise we
360                    could run into a DOS. */
361
362                 class_free(c);
363
364                 return NULL;
365         }
366
367         /* Store the newly defined class in the class cache. This call
368            also checks whether a class of the same name has already been
369            defined by the same defining loader, and if so, replaces the
370            newly created class by the one defined earlier. */
371
372         /* Important: The classinfo given to classcache_store must be
373                       fully prepared because another thread may return
374                       this pointer after the lookup at to top of this
375                       function directly after the class cache lock has
376                       been released. */
377
378         c = classcache_store(cl, c, true);
379
380         return c;
381 }
382
383
384 /* class_load_attribute_sourcefile *********************************************
385
386    SourceFile_attribute {
387        u2 attribute_name_index;
388        u4 attribute_length;
389            u2 sourcefile_index;
390    }
391
392 *******************************************************************************/
393
394 static bool class_load_attribute_sourcefile(classbuffer *cb)
395 {
396         classinfo *c;
397         u4         attribute_length;
398         u2         sourcefile_index;
399         utf       *sourcefile;
400
401         /* get classinfo */
402
403         c = cb->class;
404
405         /* check buffer size */
406
407         if (!suck_check_classbuffer_size(cb, 4 + 2))
408                 return false;
409
410         /* check attribute length */
411
412         attribute_length = suck_u4(cb);
413
414         if (attribute_length != 2) {
415                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
416                 return false;
417         }
418
419         /* there can be no more than one SourceFile attribute */
420
421         if (c->sourcefile != NULL) {
422                 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
423                 return false;
424         }
425
426         /* get sourcefile */
427
428         sourcefile_index = suck_u2(cb);
429         sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
430
431         if (sourcefile == NULL)
432                 return false;
433
434         /* store sourcefile */
435
436         c->sourcefile = sourcefile;
437
438         return true;
439 }
440
441
442 /* class_load_attribute_enclosingmethod ****************************************
443
444    EnclosingMethod_attribute {
445        u2 attribute_name_index;
446        u4 attribute_length;
447            u2 class_index;
448            u2 method_index;
449    }
450
451 *******************************************************************************/
452
453 #if defined(ENABLE_JAVASE)
454 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
455 {
456         classinfo             *c;
457         u4                     attribute_length;
458         u2                     class_index;
459         u2                     method_index;
460         classref_or_classinfo  cr;
461         constant_nameandtype  *cn;
462
463         /* get classinfo */
464
465         c = cb->class;
466
467         /* check buffer size */
468
469         if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
470                 return false;
471
472         /* check attribute length */
473
474         attribute_length = suck_u4(cb);
475
476         if (attribute_length != 4) {
477                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
478                 return false;
479         }
480
481         /* there can be no more than one EnclosingMethod attribute */
482
483         if (c->enclosingmethod != NULL) {
484                 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
485                 return false;
486         }
487
488         /* get class index */
489
490         class_index = suck_u2(cb);
491         cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
492
493         /* get method index */
494
495         method_index = suck_u2(cb);
496         cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
497
498         /* store info in classinfo */
499
500         c->enclosingclass.any = cr.any;
501         c->enclosingmethod    = cn;
502
503         return true;
504 }
505 #endif /* defined(ENABLE_JAVASE) */
506
507
508 /* class_load_attributes *******************************************************
509
510    Read attributes from ClassFile.
511
512    attribute_info {
513        u2 attribute_name_index;
514        u4 attribute_length;
515        u1 info[attribute_length];
516    }
517
518    InnerClasses_attribute {
519        u2 attribute_name_index;
520        u4 attribute_length;
521    }
522
523 *******************************************************************************/
524
525 bool class_load_attributes(classbuffer *cb)
526 {
527         classinfo             *c;
528         uint16_t               attributes_count;
529         uint16_t               attribute_name_index;
530         utf                   *attribute_name;
531         innerclassinfo        *info;
532         classref_or_classinfo  inner;
533         classref_or_classinfo  outer;
534         utf                   *name;
535         uint16_t               flags;
536         int                    i, j;
537
538         c = cb->class;
539
540         /* get attributes count */
541
542         if (!suck_check_classbuffer_size(cb, 2))
543                 return false;
544
545         attributes_count = suck_u2(cb);
546
547         for (i = 0; i < attributes_count; i++) {
548                 /* get attribute name */
549
550                 if (!suck_check_classbuffer_size(cb, 2))
551                         return false;
552
553                 attribute_name_index = suck_u2(cb);
554                 attribute_name =
555                         class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
556
557                 if (attribute_name == NULL)
558                         return false;
559
560                 if (attribute_name == utf_InnerClasses) {
561                         /* InnerClasses */
562
563                         if (c->innerclass != NULL) {
564                                 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
565                                 return false;
566                         }
567                                 
568                         if (!suck_check_classbuffer_size(cb, 4 + 2))
569                                 return false;
570
571                         /* skip attribute length */
572                         suck_u4(cb);
573
574                         /* number of records */
575                         c->innerclasscount = suck_u2(cb);
576
577                         if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
578                                 return false;
579
580                         /* allocate memory for innerclass structure */
581                         c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
582
583                         for (j = 0; j < c->innerclasscount; j++) {
584                                 /* The innerclass structure contains a class with an encoded
585                                    name, its defining scope, its simple name and a bitmask of
586                                    the access flags. */
587                                                                 
588                                 info = c->innerclass + j;
589
590                                 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
591                                 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
592                                 name      = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
593                                 flags     = suck_u2(cb);
594
595                                 /* If the current inner-class is the currently loaded
596                                    class check for some special flags. */
597
598                                 if (inner.ref->name == c->name) {
599                                         /* If an inner-class is not a member, its
600                                            outer-class is NULL. */
601
602                                         if (outer.ref != NULL) {
603                                                 c->flags |= ACC_CLASS_MEMBER;
604
605                                                 /* A member class doesn't have an
606                                                    EnclosingMethod attribute, so set the
607                                                    enclosing-class to be the same as the
608                                                    declaring-class. */
609
610                                                 c->declaringclass = outer;
611                                                 c->enclosingclass = outer;
612                                         }
613
614                                         /* If an inner-class is anonymous, its name is
615                                            NULL. */
616
617                                         if (name == NULL)
618                                                 c->flags |= ACC_CLASS_ANONYMOUS;
619                                 }
620
621                                 info->inner_class = inner;
622                                 info->outer_class = outer;
623                                 info->name        = name;
624                                 info->flags       = flags;
625                         }
626                 }
627                 else if (attribute_name == utf_SourceFile) {
628                         /* SourceFile */
629
630                         if (!class_load_attribute_sourcefile(cb))
631                                 return false;
632                 }
633 #if defined(ENABLE_JAVASE)
634                 else if (attribute_name == utf_EnclosingMethod) {
635                         /* EnclosingMethod */
636
637                         if (!class_load_attribute_enclosingmethod(cb))
638                                 return false;
639                 }
640                 else if (attribute_name == utf_Signature) {
641                         /* Signature */
642
643                         if (!loader_load_attribute_signature(cb, &(c->signature)))
644                                 return false;
645                 }
646 #endif
647
648 #if defined(ENABLE_ANNOTATIONS)
649                 /* XXX We can't do a release with that enabled */
650
651                 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
652                         /* RuntimeVisibleAnnotations */
653                         if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
654                                 return false;
655                 }
656                 /* XXX RuntimeInvisibleAnnotations should only be loaded
657                  * (or returned to Java) if some commandline options says so.
658                  * Currently there is no such option available in cacao,
659                  * therefore I load them allways (for testing purpose).
660                  * Anyway, bytecode for RuntimeInvisibleAnnotations is only
661                  * generated if you tell javac to do so. So in most cases
662                  * there won't be any.
663                  */
664                 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
665                         /* RuntimeInvisibleAnnotations */
666                         if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
667                                 return false;
668                 }
669 #endif
670
671                 else {
672                         /* unknown attribute */
673
674                         if (!loader_skip_attribute_body(cb))
675                                 return false;
676                 }
677         }
678
679         return true;
680 }
681
682
683 /* class_freepool **************************************************************
684
685         Frees all resources used by this classes Constant Pool.
686
687 *******************************************************************************/
688
689 static void class_freecpool(classinfo *c)
690 {
691         u4 idx;
692         u4 tag;
693         voidptr info;
694         
695         if (c->cptags && c->cpinfos) {
696                 for (idx = 0; idx < c->cpcount; idx++) {
697                         tag = c->cptags[idx];
698                         info = c->cpinfos[idx];
699                 
700                         if (info != NULL) {
701                                 switch (tag) {
702                                 case CONSTANT_Fieldref:
703                                 case CONSTANT_Methodref:
704                                 case CONSTANT_InterfaceMethodref:
705                                         FREE(info, constant_FMIref);
706                                         break;
707                                 case CONSTANT_Integer:
708                                         FREE(info, constant_integer);
709                                         break;
710                                 case CONSTANT_Float:
711                                         FREE(info, constant_float);
712                                         break;
713                                 case CONSTANT_Long:
714                                         FREE(info, constant_long);
715                                         break;
716                                 case CONSTANT_Double:
717                                         FREE(info, constant_double);
718                                         break;
719                                 case CONSTANT_NameAndType:
720                                         FREE(info, constant_nameandtype);
721                                         break;
722                                 }
723                         }
724                 }
725         }
726
727         if (c->cptags)
728                 MFREE(c->cptags, u1, c->cpcount);
729
730         if (c->cpinfos)
731                 MFREE(c->cpinfos, voidptr, c->cpcount);
732 }
733
734
735 /* class_getconstant ***********************************************************
736
737    Retrieves the value at position 'pos' of the constantpool of a
738    class. If the type of the value is other than 'ctype', an error is
739    thrown.
740
741 *******************************************************************************/
742
743 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
744 {
745         /* check index and type of constantpool entry */
746         /* (pos == 0 is caught by type comparison) */
747
748         if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
749                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
750                 return NULL;
751         }
752
753         return c->cpinfos[pos];
754 }
755
756
757 /* innerclass_getconstant ******************************************************
758
759    Like class_getconstant, but if cptags is ZERO, null is returned.
760         
761 *******************************************************************************/
762
763 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
764 {
765         /* invalid position in constantpool */
766
767         if (pos >= c->cpcount) {
768                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
769                 return NULL;
770         }
771
772         /* constantpool entry of type 0 */      
773
774         if (c->cptags[pos] == 0)
775                 return NULL;
776
777         /* check type of constantpool entry */
778
779         if (c->cptags[pos] != ctype) {
780                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
781                 return NULL;
782         }
783                 
784         return c->cpinfos[pos];
785 }
786
787
788 /* class_free ******************************************************************
789
790    Frees all resources used by the class.
791
792 *******************************************************************************/
793
794 void class_free(classinfo *c)
795 {
796         s4 i;
797         vftbl_t *v;
798                 
799         class_freecpool(c);
800
801         if (c->interfaces)
802                 MFREE(c->interfaces, classinfo*, c->interfacescount);
803
804         if (c->fields) {
805                 for (i = 0; i < c->fieldscount; i++)
806                         field_free(&(c->fields[i]));
807 #if defined(ENABLE_CACAO_GC)
808                 MFREE(c->fields, fieldinfo, c->fieldscount);
809 #endif
810         }
811         
812         if (c->methods) {
813                 for (i = 0; i < c->methodscount; i++)
814                         method_free(&(c->methods[i]));
815                 MFREE(c->methods, methodinfo, c->methodscount);
816         }
817
818         if ((v = c->vftbl) != NULL) {
819                 if (v->arraydesc)
820                         mem_free(v->arraydesc,sizeof(arraydescriptor));
821                 
822                 for (i = 0; i < v->interfacetablelength; i++) {
823                         MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
824                 }
825                 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
826
827                 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
828                     sizeof(methodptr*) * (v->interfacetablelength -
829                                          (v->interfacetablelength > 0));
830                 v = (vftbl_t*) (((methodptr*) v) -
831                                                 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
832                 mem_free(v, i);
833         }
834
835         if (c->innerclass)
836                 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
837
838         /*      if (c->classvftbl)
839                 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
840         
841 /*      GCFREE(c); */
842
843 #if defined(ENABLE_ANNOTATIONS)
844         annotation_bytearray_free(c->annotations);
845
846         annotation_bytearrays_free(c->method_annotations);
847         annotation_bytearrays_free(c->method_parameterannotations);
848         annotation_bytearrays_free(c->method_annotationdefaults);
849
850         annotation_bytearrays_free(c->field_annotations);
851 #endif
852 }
853
854
855 /* get_array_class *************************************************************
856
857    Returns the array class with the given name for the given
858    classloader, or NULL if an exception occurred.
859
860    Note: This function does eager loading. 
861
862 *******************************************************************************/
863
864 static classinfo *get_array_class(utf *name,classloader *initloader,
865                                                                                         classloader *defloader,bool link)
866 {
867         classinfo *c;
868         
869         /* lookup this class in the classcache */
870         c = classcache_lookup(initloader,name);
871         if (!c)
872                 c = classcache_lookup_defined(defloader,name);
873
874         if (!c) {
875                 /* we have to create it */
876                 c = class_create_classinfo(name);
877                 c = load_newly_created_array(c,initloader);
878                 if (c == NULL)
879                         return NULL;
880         }
881
882         assert(c);
883         assert(c->state & CLASS_LOADED);
884         assert(c->classloader == defloader);
885
886         if (link && !(c->state & CLASS_LINKED))
887                 if (!link_class(c))
888                         return NULL;
889
890         assert(!link || (c->state & CLASS_LINKED));
891
892         return c;
893 }
894
895
896 /* class_array_of **************************************************************
897
898    Returns an array class with the given component class. The array
899    class is dynamically created if neccessary.
900
901 *******************************************************************************/
902
903 classinfo *class_array_of(classinfo *component, bool link)
904 {
905         classloader       *cl;
906     s4                 namelen;
907     char              *namebuf;
908         utf               *u;
909         classinfo         *c;
910         s4                 dumpsize;
911
912         cl = component->classloader;
913
914         dumpsize = dump_size();
915
916     /* Assemble the array class name */
917     namelen = component->name->blength;
918     
919     if (component->name->text[0] == '[') {
920         /* the component is itself an array */
921         namebuf = DMNEW(char, namelen + 1);
922         namebuf[0] = '[';
923         MCOPY(namebuf + 1, component->name->text, char, namelen);
924         namelen++;
925     }
926         else {
927         /* the component is a non-array class */
928         namebuf = DMNEW(char, namelen + 3);
929         namebuf[0] = '[';
930         namebuf[1] = 'L';
931         MCOPY(namebuf + 2, component->name->text, char, namelen);
932         namebuf[2 + namelen] = ';';
933         namelen += 3;
934     }
935
936         u = utf_new(namebuf, namelen);
937
938         c = get_array_class(u, cl, cl, link);
939
940         dump_release(dumpsize);
941
942         return c;
943 }
944
945
946 /* class_multiarray_of *********************************************************
947
948    Returns an array class with the given dimension and element class.
949    The array class is dynamically created if neccessary.
950
951 *******************************************************************************/
952
953 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
954 {
955     s4 namelen;
956     char *namebuf;
957         s4 dumpsize;
958         classinfo *c;
959
960         dumpsize = dump_size();
961
962         if (dim < 1) {
963                 log_text("Invalid array dimension requested");
964                 assert(0);
965         }
966
967     /* Assemble the array class name */
968     namelen = element->name->blength;
969     
970     if (element->name->text[0] == '[') {
971         /* the element is itself an array */
972         namebuf = DMNEW(char, namelen + dim);
973         memcpy(namebuf + dim, element->name->text, namelen);
974         namelen += dim;
975     }
976     else {
977         /* the element is a non-array class */
978         namebuf = DMNEW(char, namelen + 2 + dim);
979         namebuf[dim] = 'L';
980         memcpy(namebuf + dim + 1, element->name->text, namelen);
981         namelen += (2 + dim);
982         namebuf[namelen - 1] = ';';
983     }
984         memset(namebuf, '[', dim);
985
986         c = get_array_class(utf_new(namebuf, namelen),
987                                                 element->classloader,
988                                                 element->classloader,
989                                                 link);
990
991         dump_release(dumpsize);
992
993         return c;
994 }
995
996
997 /* class_lookup_classref *******************************************************
998
999    Looks up the constant_classref for a given classname in the classref
1000    tables of a class.
1001
1002    IN:
1003        cls..............the class containing the reference
1004            name.............the name of the class refered to
1005
1006     RETURN VALUE:
1007            a pointer to a constant_classref, or 
1008            NULL if the reference was not found
1009    
1010 *******************************************************************************/
1011
1012 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1013 {
1014         constant_classref *ref;
1015         extra_classref *xref;
1016         int count;
1017
1018         assert(cls);
1019         assert(name);
1020         assert(!cls->classrefcount || cls->classrefs);
1021         
1022         /* first search the main classref table */
1023         count = cls->classrefcount;
1024         ref = cls->classrefs;
1025         for (; count; --count, ++ref)
1026                 if (ref->name == name)
1027                         return ref;
1028
1029         /* next try the list of extra classrefs */
1030         for (xref = cls->extclassrefs; xref; xref = xref->next) {
1031                 if (xref->classref.name == name)
1032                         return &(xref->classref);
1033         }
1034
1035         /* not found */
1036         return NULL;
1037 }
1038
1039
1040 /* class_get_classref **********************************************************
1041
1042    Returns the constant_classref for a given classname.
1043
1044    IN:
1045        cls..............the class containing the reference
1046            name.............the name of the class refered to
1047
1048    RETURN VALUE:
1049        a pointer to a constant_classref (never NULL)
1050
1051    NOTE:
1052        The given name is not checked for validity!
1053    
1054 *******************************************************************************/
1055
1056 constant_classref *class_get_classref(classinfo *cls, utf *name)
1057 {
1058         constant_classref *ref;
1059         extra_classref *xref;
1060
1061         assert(cls);
1062         assert(name);
1063
1064         ref = class_lookup_classref(cls,name);
1065         if (ref)
1066                 return ref;
1067
1068         xref = NEW(extra_classref);
1069         CLASSREF_INIT(xref->classref,cls,name);
1070
1071         xref->next = cls->extclassrefs;
1072         cls->extclassrefs = xref;
1073
1074         return &(xref->classref);
1075 }
1076
1077
1078 /* class_get_self_classref *****************************************************
1079
1080    Returns the constant_classref to the class itself.
1081
1082    IN:
1083        cls..............the class containing the reference
1084
1085    RETURN VALUE:
1086        a pointer to a constant_classref (never NULL)
1087
1088 *******************************************************************************/
1089
1090 constant_classref *class_get_self_classref(classinfo *cls)
1091 {
1092         /* XXX this should be done in a faster way. Maybe always make */
1093         /* the classref of index 0 a self reference.                  */
1094         return class_get_classref(cls,cls->name);
1095 }
1096
1097 /* class_get_classref_multiarray_of ********************************************
1098
1099    Returns an array type reference with the given dimension and element class
1100    reference.
1101
1102    IN:
1103        dim..............the requested dimension
1104                             dim must be in [1;255]. This is NOT checked!
1105            ref..............the component class reference
1106
1107    RETURN VALUE:
1108        a pointer to the class reference for the array type
1109
1110    NOTE:
1111        The referer of `ref` is used as the referer for the new classref.
1112
1113 *******************************************************************************/
1114
1115 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1116 {
1117     s4 namelen;
1118     char *namebuf;
1119         s4 dumpsize;
1120         constant_classref *cr;
1121
1122         assert(ref);
1123         assert(dim >= 1 && dim <= 255);
1124
1125         dumpsize = dump_size();
1126
1127     /* Assemble the array class name */
1128     namelen = ref->name->blength;
1129     
1130     if (ref->name->text[0] == '[') {
1131         /* the element is itself an array */
1132         namebuf = DMNEW(char, namelen + dim);
1133         memcpy(namebuf + dim, ref->name->text, namelen);
1134         namelen += dim;
1135     }
1136     else {
1137         /* the element is a non-array class */
1138         namebuf = DMNEW(char, namelen + 2 + dim);
1139         namebuf[dim] = 'L';
1140         memcpy(namebuf + dim + 1, ref->name->text, namelen);
1141         namelen += (2 + dim);
1142         namebuf[namelen - 1] = ';';
1143     }
1144         memset(namebuf, '[', dim);
1145
1146     cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1147
1148         dump_release(dumpsize);
1149
1150         return cr;
1151 }
1152
1153
1154 /* class_get_classref_component_of *********************************************
1155
1156    Returns the component classref of a given array type reference
1157
1158    IN:
1159        ref..............the array type reference
1160
1161    RETURN VALUE:
1162        a reference to the component class, or
1163            NULL if `ref` is not an object array type reference
1164
1165    NOTE:
1166        The referer of `ref` is used as the referer for the new classref.
1167
1168 *******************************************************************************/
1169
1170 constant_classref *class_get_classref_component_of(constant_classref *ref)
1171 {
1172         s4 namelen;
1173         char *name;
1174         
1175         assert(ref);
1176
1177         name = ref->name->text;
1178         if (*name++ != '[')
1179                 return NULL;
1180         
1181         namelen = ref->name->blength - 1;
1182         if (*name == 'L') {
1183                 name++;
1184                 namelen -= 2;
1185         }
1186         else if (*name != '[') {
1187                 return NULL;
1188         }
1189
1190     return class_get_classref(ref->referer, utf_new(name, namelen));
1191 }
1192
1193
1194 /* class_findmethod ************************************************************
1195         
1196    Searches a 'classinfo' structure for a method having the given name
1197    and descriptor. If descriptor is NULL, it is ignored.
1198
1199 *******************************************************************************/
1200
1201 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1202 {
1203         methodinfo *m;
1204         s4          i;
1205
1206         for (i = 0; i < c->methodscount; i++) {
1207                 m = &(c->methods[i]);
1208
1209                 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1210                         return m;
1211         }
1212
1213         return NULL;
1214 }
1215
1216
1217 /* class_resolvemethod *********************************************************
1218         
1219    Searches a class and it's super classes for a method.
1220
1221    Superinterfaces are *not* searched.
1222
1223 *******************************************************************************/
1224
1225 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1226 {
1227         methodinfo *m;
1228
1229         while (c) {
1230                 m = class_findmethod(c, name, desc);
1231
1232                 if (m)
1233                         return m;
1234
1235                 /* JVM Specification bug: 
1236
1237                    It is important NOT to resolve special <init> and <clinit>
1238                    methods to super classes or interfaces; yet, this is not
1239                    explicited in the specification.  Section 5.4.3.3 should be
1240                    updated appropriately.  */
1241
1242                 if (name == utf_init || name == utf_clinit)
1243                         return NULL;
1244
1245                 c = c->super.cls;
1246         }
1247
1248         return NULL;
1249 }
1250
1251
1252 /* class_resolveinterfacemethod_intern *****************************************
1253
1254    Internally used helper function. Do not use this directly.
1255
1256 *******************************************************************************/
1257
1258 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1259                                                                                                            utf *name, utf *desc)
1260 {
1261         methodinfo *m;
1262         s4          i;
1263
1264         /* try to find the method in the class */
1265
1266         m = class_findmethod(c, name, desc);
1267
1268         if (m != NULL)
1269                 return m;
1270
1271         /* no method found? try the superinterfaces */
1272
1273         for (i = 0; i < c->interfacescount; i++) {
1274                 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1275                                                                                                         name, desc);
1276
1277                 if (m != NULL)
1278                         return m;
1279         }
1280
1281         /* no method found */
1282
1283         return NULL;
1284 }
1285
1286
1287 /* class_resolveclassmethod ****************************************************
1288         
1289    Resolves a reference from REFERER to a method with NAME and DESC in
1290    class C.
1291
1292    If the method cannot be resolved the return value is NULL. If
1293    EXCEPT is true *exceptionptr is set, too.
1294
1295 *******************************************************************************/
1296
1297 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1298                                                                          classinfo *referer, bool throwexception)
1299 {
1300         classinfo  *cls;
1301         methodinfo *m;
1302         s4          i;
1303
1304 /*      if (c->flags & ACC_INTERFACE) { */
1305 /*              if (throwexception) */
1306 /*                      *exceptionptr = */
1307 /*                              new_exception(string_java_lang_IncompatibleClassChangeError); */
1308 /*              return NULL; */
1309 /*      } */
1310
1311         /* try class c and its superclasses */
1312
1313         cls = c;
1314
1315         m = class_resolvemethod(cls, name, desc);
1316
1317         if (m != NULL)
1318                 goto found;
1319
1320         /* try the superinterfaces */
1321
1322         for (i = 0; i < c->interfacescount; i++) {
1323                 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1324                                                                                                 name, desc);
1325
1326                 if (m != NULL)
1327                         goto found;
1328         }
1329         
1330         if (throwexception)
1331                 exceptions_throw_nosuchmethoderror(c, name, desc);
1332
1333         return NULL;
1334
1335  found:
1336         if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1337                 if (throwexception)
1338                         exceptions_throw_abstractmethoderror();
1339
1340                 return NULL;
1341         }
1342
1343         /* XXX check access rights */
1344
1345         return m;
1346 }
1347
1348
1349 /* class_resolveinterfacemethod ************************************************
1350
1351    Resolves a reference from REFERER to a method with NAME and DESC in
1352    interface C.
1353
1354    If the method cannot be resolved the return value is NULL. If
1355    EXCEPT is true *exceptionptr is set, too.
1356
1357 *******************************************************************************/
1358
1359 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1360                                                                                  classinfo *referer, bool throwexception)
1361 {
1362         methodinfo *mi;
1363
1364         if (!(c->flags & ACC_INTERFACE)) {
1365                 if (throwexception)
1366                         exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1367
1368                 return NULL;
1369         }
1370
1371         mi = class_resolveinterfacemethod_intern(c, name, desc);
1372
1373         if (mi != NULL)
1374                 return mi;
1375
1376         /* try class java.lang.Object */
1377
1378         mi = class_findmethod(class_java_lang_Object, name, desc);
1379
1380         if (mi != NULL)
1381                 return mi;
1382
1383         if (throwexception)
1384                 exceptions_throw_nosuchmethoderror(c, name, desc);
1385
1386         return NULL;
1387 }
1388
1389
1390 /* class_findfield *************************************************************
1391         
1392    Searches for field with specified name and type in a classinfo
1393    structure. If no such field is found NULL is returned.
1394
1395 *******************************************************************************/
1396
1397 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1398 {
1399         s4 i;
1400
1401         for (i = 0; i < c->fieldscount; i++)
1402                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1403                         return &(c->fields[i]);
1404
1405         if (c->super.cls)
1406                 return class_findfield(c->super.cls, name, desc);
1407
1408         return NULL;
1409 }
1410
1411
1412 /* class_findfield_approx ******************************************************
1413         
1414    Searches in 'classinfo'-structure for a field with the specified
1415    name.
1416
1417 *******************************************************************************/
1418  
1419 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1420 {
1421         s4 i;
1422
1423         /* get field index */
1424
1425         i = class_findfield_index_by_name(c, name);
1426
1427         /* field was not found, return */
1428
1429         if (i == -1)
1430                 return NULL;
1431
1432         /* return field address */
1433
1434         return &(c->fields[i]);
1435 }
1436
1437
1438 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1439 {
1440         s4 i;
1441
1442         for (i = 0; i < c->fieldscount; i++) {
1443                 /* compare field names */
1444
1445                 if ((c->fields[i].name == name))
1446                         return i;
1447         }
1448
1449         /* field was not found, raise exception */      
1450
1451         exceptions_throw_nosuchfielderror(c, name);
1452
1453         return -1;
1454 }
1455
1456
1457 /****************** Function: class_resolvefield_int ***************************
1458
1459     This is an internally used helper function. Do not use this directly.
1460
1461         Tries to resolve a field having the given name and type.
1462     If the field cannot be resolved, NULL is returned.
1463
1464 *******************************************************************************/
1465
1466 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1467 {
1468         fieldinfo *fi;
1469         s4         i;
1470
1471         /* search for field in class c */
1472
1473         for (i = 0; i < c->fieldscount; i++) { 
1474                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1475                         return &(c->fields[i]);
1476                 }
1477     }
1478
1479         /* try superinterfaces recursively */
1480
1481         for (i = 0; i < c->interfacescount; i++) {
1482                 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1483                 if (fi)
1484                         return fi;
1485         }
1486
1487         /* try superclass */
1488
1489         if (c->super.cls)
1490                 return class_resolvefield_int(c->super.cls, name, desc);
1491
1492         /* not found */
1493
1494         return NULL;
1495 }
1496
1497
1498 /********************* Function: class_resolvefield ***************************
1499         
1500         Resolves a reference from REFERER to a field with NAME and DESC in class C.
1501
1502     If the field cannot be resolved the return value is NULL. If EXCEPT is
1503     true *exceptionptr is set, too.
1504
1505 *******************************************************************************/
1506
1507 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1508                                                           classinfo *referer, bool throwexception)
1509 {
1510         fieldinfo *fi;
1511
1512         fi = class_resolvefield_int(c, name, desc);
1513
1514         if (!fi) {
1515                 if (throwexception)
1516                         exceptions_throw_nosuchfielderror(c, name);
1517
1518                 return NULL;
1519         }
1520
1521         /* XXX check access rights */
1522
1523         return fi;
1524 }
1525
1526
1527 /* class_resolve_superclass ****************************************************
1528
1529    Resolves the super class reference of the given class if necessary.
1530
1531 *******************************************************************************/
1532
1533 static classinfo *class_resolve_superclass(classinfo *c)
1534 {
1535         classinfo *super;
1536
1537         if (c->super.any == NULL)
1538                 return NULL;
1539
1540         /* Do we have a super class reference or is it already
1541            resolved? */
1542
1543         if (IS_CLASSREF(c->super)) {
1544                 super = resolve_classref_or_classinfo_eager(c->super, true);
1545
1546                 if (super == NULL)
1547                         return NULL;
1548
1549                 /* Store the resolved super class in the class structure. */
1550
1551                 c->super.cls = super;
1552         }
1553
1554         return c->super.cls;
1555 }
1556
1557
1558 /* class_issubclass ************************************************************
1559
1560    Checks if sub is a descendant of super.
1561         
1562 *******************************************************************************/
1563
1564 bool class_issubclass(classinfo *sub, classinfo *super)
1565 {
1566         for (;;) {
1567                 if (sub == NULL)
1568                         return false;
1569
1570                 if (sub == super)
1571                         return true;
1572
1573                 sub = class_resolve_superclass(sub);
1574         }
1575 }
1576
1577
1578 /* class_isanysubclass *********************************************************
1579
1580    Checks a subclass relation between two classes. Implemented
1581    interfaces are interpreted as super classes.
1582
1583    Return value: 1 ... sub is subclass of super
1584                  0 ... otherwise
1585
1586 *******************************************************************************/
1587
1588 bool class_isanysubclass(classinfo *sub, classinfo *super)
1589 {
1590         uint32_t diffval;
1591         bool     result;
1592
1593         /* This is the trivial case. */
1594
1595         if (sub == super)
1596                 return true;
1597
1598         /* Primitive classes are only subclasses of themselves. */
1599
1600         if (class_is_primitive(sub) || class_is_primitive(super))
1601                 return false;
1602
1603         /* Check for interfaces. */
1604
1605         if (super->flags & ACC_INTERFACE) {
1606                 result = (sub->vftbl->interfacetablelength > super->index) &&
1607                         (sub->vftbl->interfacetable[-super->index] != NULL);
1608         }
1609         else {
1610                 /* java.lang.Object is the only super class of any
1611                    interface. */
1612
1613                 if (sub->flags & ACC_INTERFACE)
1614                         return (super == class_java_lang_Object);
1615
1616                 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1617
1618                 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1619                 result  = diffval <= (uint32_t) super->vftbl->diffval;
1620
1621                 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1622         }
1623
1624         return result;
1625 }
1626
1627
1628 /* class_is_primitive **********************************************************
1629
1630    Checks if the given class is a primitive class.
1631
1632 *******************************************************************************/
1633
1634 bool class_is_primitive(classinfo *c)
1635 {
1636         if (c->flags & ACC_CLASS_PRIMITIVE)
1637                 return true;
1638
1639         return false;
1640 }
1641
1642
1643 /* class_is_anonymousclass *****************************************************
1644
1645    Checks if the given class is an anonymous class.
1646
1647 *******************************************************************************/
1648
1649 bool class_is_anonymousclass(classinfo *c)
1650 {
1651         if (c->flags & ACC_CLASS_ANONYMOUS)
1652                 return true;
1653
1654         return false;
1655 }
1656
1657
1658 /* class_is_array **************************************************************
1659
1660    Checks if the given class is an array class.
1661
1662 *******************************************************************************/
1663
1664 bool class_is_array(classinfo *c)
1665 {
1666         if (!(c->state & CLASS_LINKED))
1667                 if (!link_class(c))
1668                         return false;
1669
1670         return (c->vftbl->arraydesc != NULL);
1671 }
1672
1673
1674 /* class_is_interface **********************************************************
1675
1676    Checks if the given class is an interface.
1677
1678 *******************************************************************************/
1679
1680 bool class_is_interface(classinfo *c)
1681 {
1682         if (c->flags & ACC_INTERFACE)
1683                 return true;
1684
1685         return false;
1686 }
1687
1688
1689 /* class_is_localclass *********************************************************
1690
1691    Checks if the given class is a local class.
1692
1693 *******************************************************************************/
1694
1695 bool class_is_localclass(classinfo *c)
1696 {
1697         if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1698                 return true;
1699
1700         return false;
1701 }
1702
1703
1704 /* class_is_memberclass ********************************************************
1705
1706    Checks if the given class is a member class.
1707
1708 *******************************************************************************/
1709
1710 bool class_is_memberclass(classinfo *c)
1711 {
1712         if (c->flags & ACC_CLASS_MEMBER)
1713                 return true;
1714
1715         return false;
1716 }
1717
1718
1719 /* class_get_superclass ********************************************************
1720
1721    Return the super class of the given class.  If the super-field is a
1722    class-reference, resolve it and store it in the classinfo.
1723
1724 *******************************************************************************/
1725
1726 classinfo *class_get_superclass(classinfo *c)
1727 {
1728         classinfo *super;
1729
1730         /* For java.lang.Object, primitive and Void classes we return
1731            NULL. */
1732
1733         if (c->super.any == NULL)
1734                 return NULL;
1735
1736         /* For interfaces we also return NULL. */
1737
1738         if (c->flags & ACC_INTERFACE)
1739                 return NULL;
1740
1741         /* We may have to resolve the super class reference. */
1742
1743         super = class_resolve_superclass(c);
1744
1745         return super;
1746 }
1747
1748
1749 /* class_get_componenttype *****************************************************
1750
1751    Return the component class of the given class.  If the given class
1752    is not an array, return NULL.
1753
1754 *******************************************************************************/
1755
1756 classinfo *class_get_componenttype(classinfo *c)
1757 {
1758         classinfo       *component;
1759         arraydescriptor *ad;
1760         
1761         /* XXX maybe we could find a way to do this without linking. */
1762         /* This way should be safe and easy, however.                */
1763
1764         if (!(c->state & CLASS_LINKED))
1765                 if (!link_class(c))
1766                         return NULL;
1767
1768         ad = c->vftbl->arraydesc;
1769         
1770         if (ad == NULL)
1771                 return NULL;
1772         
1773         if (ad->arraytype == ARRAYTYPE_OBJECT)
1774                 component = ad->componentvftbl->class;
1775         else
1776                 component = primitive_class_get_by_type(ad->arraytype);
1777                 
1778         return component;
1779 }
1780
1781
1782 /* class_get_declaredclasses ***************************************************
1783
1784    Return an array of declared classes of the given class.
1785
1786 *******************************************************************************/
1787
1788 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1789 {
1790         classref_or_classinfo  inner;
1791         classref_or_classinfo  outer;
1792         utf                   *outername;
1793         int                    declaredclasscount;  /* number of declared classes */
1794         int                    pos;                     /* current declared class */
1795         java_handle_objectarray_t *oa;               /* array of declared classes */
1796         int                    i;
1797         classinfo             *ic;
1798
1799         declaredclasscount = 0;
1800
1801         if (!class_is_primitive(c) && !class_is_array(c)) {
1802                 /* Determine number of declared classes. */
1803
1804                 for (i = 0; i < c->innerclasscount; i++) {
1805                         /* Get outer-class.  If the inner-class is not a member
1806                            class, the outer-class is NULL. */
1807
1808                         outer = c->innerclass[i].outer_class;
1809
1810                         if (outer.any == NULL)
1811                                 continue;
1812
1813                         /* Check if outer-class is a classref or a real class and
1814                get the class name from the structure. */
1815
1816                         outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1817
1818                         /* Outer class is this class. */
1819
1820                         if ((outername == c->name) &&
1821                                 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1822                                 declaredclasscount++;
1823                 }
1824         }
1825
1826         /* Allocate Class[] and check for OOM. */
1827
1828         oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1829
1830         if (oa == NULL)
1831                 return NULL;
1832
1833         for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1834                 inner = c->innerclass[i].inner_class;
1835                 outer = c->innerclass[i].outer_class;
1836
1837                 /* Get outer-class.  If the inner-class is not a member class,
1838                    the outer-class is NULL. */
1839
1840                 if (outer.any == NULL)
1841                         continue;
1842
1843                 /* Check if outer_class is a classref or a real class and get
1844                    the class name from the structure. */
1845
1846                 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1847
1848                 /* Outer class is this class. */
1849
1850                 if ((outername == c->name) &&
1851                         ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1852
1853                         ic = resolve_classref_or_classinfo_eager(inner, false);
1854
1855                         if (ic == NULL)
1856                                 return NULL;
1857
1858                         if (!(ic->state & CLASS_LINKED))
1859                                 if (!link_class(ic))
1860                                         return NULL;
1861
1862                         LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1863                 }
1864         }
1865
1866         return oa;
1867 }
1868
1869
1870 /* class_get_declaringclass ****************************************************
1871
1872    If the class or interface given is a member of another class,
1873    return the declaring class.  For array and primitive classes return
1874    NULL.
1875
1876 *******************************************************************************/
1877
1878 classinfo *class_get_declaringclass(classinfo *c)
1879 {
1880         classref_or_classinfo  cr;
1881         classinfo             *dc;
1882
1883         /* Get declaring class. */
1884
1885         cr = c->declaringclass;
1886
1887         if (cr.any == NULL)
1888                 return NULL;
1889
1890         /* Resolve the class if necessary. */
1891
1892         if (IS_CLASSREF(cr)) {
1893 /*              dc = resolve_classref_eager(cr.ref); */
1894                 dc = resolve_classref_or_classinfo_eager(cr, true);
1895
1896                 if (dc == NULL)
1897                         return NULL;
1898
1899                 /* Store the resolved class in the class structure. */
1900
1901                 cr.cls = dc;
1902         }
1903
1904         dc = cr.cls;
1905
1906         return dc;
1907 }
1908
1909
1910 /* class_get_enclosingclass ****************************************************
1911
1912    Return the enclosing class for the given class.
1913
1914 *******************************************************************************/
1915
1916 classinfo *class_get_enclosingclass(classinfo *c)
1917 {
1918         classref_or_classinfo  cr;
1919         classinfo             *ec;
1920
1921         /* Get enclosing class. */
1922
1923         cr = c->enclosingclass;
1924
1925         if (cr.any == NULL)
1926                 return NULL;
1927
1928         /* Resolve the class if necessary. */
1929
1930         if (IS_CLASSREF(cr)) {
1931 /*              ec = resolve_classref_eager(cr.ref); */
1932                 ec = resolve_classref_or_classinfo_eager(cr, true);
1933
1934                 if (ec == NULL)
1935                         return NULL;
1936
1937                 /* Store the resolved class in the class structure. */
1938
1939                 cr.cls = ec;
1940         }
1941
1942         ec = cr.cls;
1943
1944         return ec;
1945 }
1946
1947
1948 /* class_get_interfaces ********************************************************
1949
1950    Return an array of interfaces of the given class.
1951
1952 *******************************************************************************/
1953
1954 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1955 {
1956         classinfo                 *ic;
1957         java_handle_objectarray_t *oa;
1958         u4                         i;
1959
1960         if (!(c->state & CLASS_LINKED))
1961                 if (!link_class(c))
1962                         return NULL;
1963
1964         oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1965
1966         if (oa == NULL)
1967                 return NULL;
1968
1969         for (i = 0; i < c->interfacescount; i++) {
1970                 ic = c->interfaces[i].cls;
1971
1972                 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1973         }
1974
1975         return oa;
1976 }
1977
1978
1979 /* class_get_signature *********************************************************
1980
1981    Return the signature of the given class.  For array and primitive
1982    classes return NULL.
1983
1984 *******************************************************************************/
1985
1986 #if defined(ENABLE_JAVASE)
1987 utf *class_get_signature(classinfo *c)
1988 {
1989         /* For array and primitive classes return NULL. */
1990
1991         if (class_is_array(c) || class_is_primitive(c))
1992                 return NULL;
1993
1994         return c->signature;
1995 }
1996 #endif
1997
1998
1999 /* class_printflags ************************************************************
2000
2001    Prints flags of a class.
2002
2003 *******************************************************************************/
2004
2005 #if !defined(NDEBUG)
2006 void class_printflags(classinfo *c)
2007 {
2008         if (c == NULL) {
2009                 printf("NULL");
2010                 return;
2011         }
2012
2013         if (c->flags & ACC_PUBLIC)       printf(" PUBLIC");
2014         if (c->flags & ACC_PRIVATE)      printf(" PRIVATE");
2015         if (c->flags & ACC_PROTECTED)    printf(" PROTECTED");
2016         if (c->flags & ACC_STATIC)       printf(" STATIC");
2017         if (c->flags & ACC_FINAL)        printf(" FINAL");
2018         if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2019         if (c->flags & ACC_VOLATILE)     printf(" VOLATILE");
2020         if (c->flags & ACC_TRANSIENT)    printf(" TRANSIENT");
2021         if (c->flags & ACC_NATIVE)       printf(" NATIVE");
2022         if (c->flags & ACC_INTERFACE)    printf(" INTERFACE");
2023         if (c->flags & ACC_ABSTRACT)     printf(" ABSTRACT");
2024 }
2025 #endif
2026
2027
2028 /* class_print *****************************************************************
2029
2030    Prints classname plus flags.
2031
2032 *******************************************************************************/
2033
2034 #if !defined(NDEBUG)
2035 void class_print(classinfo *c)
2036 {
2037         if (c == NULL) {
2038                 printf("NULL");
2039                 return;
2040         }
2041
2042         utf_display_printable_ascii(c->name);
2043         class_printflags(c);
2044 }
2045 #endif
2046
2047
2048 /* class_classref_print ********************************************************
2049
2050    Prints classname plus referer class.
2051
2052 *******************************************************************************/
2053
2054 #if !defined(NDEBUG)
2055 void class_classref_print(constant_classref *cr)
2056 {
2057         if (cr == NULL) {
2058                 printf("NULL");
2059                 return;
2060         }
2061
2062         utf_display_printable_ascii(cr->name);
2063         printf("(ref.by ");
2064         if (cr->referer)
2065                 class_print(cr->referer);
2066         else
2067                 printf("NULL");
2068         printf(")");
2069 }
2070 #endif
2071
2072
2073 /* class_println ***************************************************************
2074
2075    Prints classname plus flags and new line.
2076
2077 *******************************************************************************/
2078
2079 #if !defined(NDEBUG)
2080 void class_println(classinfo *c)
2081 {
2082         class_print(c);
2083         printf("\n");
2084 }
2085 #endif
2086
2087
2088 /* class_classref_println ******************************************************
2089
2090    Prints classname plus referer class and new line.
2091
2092 *******************************************************************************/
2093
2094 #if !defined(NDEBUG)
2095 void class_classref_println(constant_classref *cr)
2096 {
2097         class_classref_print(cr);
2098         printf("\n");
2099 }
2100 #endif
2101
2102
2103 /* class_classref_or_classinfo_print *******************************************
2104
2105    Prints classname plus referer class.
2106
2107 *******************************************************************************/
2108
2109 #if !defined(NDEBUG)
2110 void class_classref_or_classinfo_print(classref_or_classinfo c)
2111 {
2112         if (c.any == NULL) {
2113                 printf("(classref_or_classinfo) NULL");
2114                 return;
2115         }
2116         if (IS_CLASSREF(c))
2117                 class_classref_print(c.ref);
2118         else
2119                 class_print(c.cls);
2120 }
2121 #endif
2122
2123
2124 /* class_classref_or_classinfo_println *****************************************
2125
2126    Prints classname plus referer class and a newline.
2127
2128 *******************************************************************************/
2129
2130 void class_classref_or_classinfo_println(classref_or_classinfo c)
2131 {
2132         class_classref_or_classinfo_println(c);
2133         printf("\n");
2134 }
2135
2136
2137 /* class_showconstantpool ******************************************************
2138
2139    Dump the constant pool of the given class to stdout.
2140
2141 *******************************************************************************/
2142
2143 #if !defined(NDEBUG)
2144 void class_showconstantpool (classinfo *c) 
2145 {
2146         u4 i;
2147         voidptr e;
2148
2149         printf ("---- dump of constant pool ----\n");
2150
2151         for (i=0; i<c->cpcount; i++) {
2152                 printf ("#%d:  ", (int) i);
2153                 
2154                 e = c -> cpinfos [i];
2155                 if (e) {
2156                         
2157                         switch (c -> cptags [i]) {
2158                         case CONSTANT_Class:
2159                                 printf ("Classreference -> ");
2160                                 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2161                                 break;
2162                         case CONSTANT_Fieldref:
2163                                 printf ("Fieldref -> ");
2164                                 field_fieldref_print((constant_FMIref *) e);
2165                                 break;
2166                         case CONSTANT_Methodref:
2167                                 printf ("Methodref -> ");
2168                                 method_methodref_print((constant_FMIref *) e);
2169                                 break;
2170                         case CONSTANT_InterfaceMethodref:
2171                                 printf ("InterfaceMethod -> ");
2172                                 method_methodref_print((constant_FMIref *) e);
2173                                 break;
2174                         case CONSTANT_String:
2175                                 printf ("String -> ");
2176                                 utf_display_printable_ascii (e);
2177                                 break;
2178                         case CONSTANT_Integer:
2179                                 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2180                                 break;
2181                         case CONSTANT_Float:
2182                                 printf ("Float -> %f", ((constant_float*)e) -> value);
2183                                 break;
2184                         case CONSTANT_Double:
2185                                 printf ("Double -> %f", ((constant_double*)e) -> value);
2186                                 break;
2187                         case CONSTANT_Long:
2188                                 {
2189                                         u8 v = ((constant_long*)e) -> value;
2190 #if U8_AVAILABLE
2191                                         printf ("Long -> %ld", (long int) v);
2192 #else
2193                                         printf ("Long -> HI: %ld, LO: %ld\n", 
2194                                                         (long int) v.high, (long int) v.low);
2195 #endif 
2196                                 }
2197                                 break;
2198                         case CONSTANT_NameAndType:
2199                                 {
2200                                         constant_nameandtype *cnt = e;
2201                                         printf ("NameAndType: ");
2202                                         utf_display_printable_ascii (cnt->name);
2203                                         printf (" ");
2204                                         utf_display_printable_ascii (cnt->descriptor);
2205                                 }
2206                                 break;
2207                         case CONSTANT_Utf8:
2208                                 printf ("Utf8 -> ");
2209                                 utf_display_printable_ascii (e);
2210                                 break;
2211                         default: 
2212                                 log_text("Invalid type of ConstantPool-Entry");
2213                                 assert(0);
2214                         }
2215                 }
2216
2217                 printf ("\n");
2218         }
2219 }
2220 #endif /* !defined(NDEBUG) */
2221
2222
2223 /* class_showmethods ***********************************************************
2224
2225    Dump info about the fields and methods of the given class to stdout.
2226
2227 *******************************************************************************/
2228
2229 #if !defined(NDEBUG)
2230 void class_showmethods (classinfo *c)
2231 {
2232         s4 i;
2233         
2234         printf("--------- Fields and Methods ----------------\n");
2235         printf("Flags: ");
2236         class_printflags(c);
2237         printf("\n");
2238
2239         printf("This: ");
2240         utf_display_printable_ascii(c->name);
2241         printf("\n");
2242
2243         if (c->super.cls) {
2244                 printf("Super: ");
2245                 utf_display_printable_ascii(c->super.cls->name);
2246                 printf ("\n");
2247         }
2248
2249         printf("Index: %d\n", c->index);
2250         
2251         printf("Interfaces:\n");        
2252         for (i = 0; i < c->interfacescount; i++) {
2253                 printf("   ");
2254                 utf_display_printable_ascii(c->interfaces[i].cls->name);
2255                 printf (" (%d)\n", c->interfaces[i].cls->index);
2256         }
2257
2258         printf("Fields:\n");
2259         for (i = 0; i < c->fieldscount; i++)
2260                 field_println(&(c->fields[i]));
2261
2262         printf("Methods:\n");
2263         for (i = 0; i < c->methodscount; i++) {
2264                 methodinfo *m = &(c->methods[i]);
2265
2266                 if (!(m->flags & ACC_STATIC))
2267                         printf("vftblindex: %d   ", m->vftblindex);
2268
2269                 method_println(m);
2270         }
2271
2272         printf ("Virtual function table:\n");
2273         for (i = 0; i < c->vftbl->vftbllength; i++)
2274                 printf ("entry: %d,  %ld\n", i, (long int) (c->vftbl->table[i]));
2275 }
2276 #endif /* !defined(NDEBUG) */
2277
2278
2279 /*
2280  * These are local overrides for various environment variables in Emacs.
2281  * Please do not remove this and leave it at the end of the file, where
2282  * Emacs will automagically detect them.
2283  * ---------------------------------------------------------------------
2284  * Local variables:
2285  * mode: c
2286  * indent-tabs-mode: t
2287  * c-basic-offset: 4
2288  * tab-width: 4
2289  * End:
2290  * vim:noexpandtab:sw=4:ts=4:
2291  */