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