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