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