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