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