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