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