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