* src/vmcore/class.c (class_get_classloader): New function.
[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 != NULL)
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
852
853 /* get_array_class *************************************************************
854
855    Returns the array class with the given name for the given
856    classloader, or NULL if an exception occurred.
857
858    Note: This function does eager loading. 
859
860 *******************************************************************************/
861
862 static classinfo *get_array_class(utf *name,classloader *initloader,
863                                                                                         classloader *defloader,bool link)
864 {
865         classinfo *c;
866         
867         /* lookup this class in the classcache */
868         c = classcache_lookup(initloader,name);
869         if (!c)
870                 c = classcache_lookup_defined(defloader,name);
871
872         if (!c) {
873                 /* we have to create it */
874                 c = class_create_classinfo(name);
875                 c = load_newly_created_array(c,initloader);
876                 if (c == NULL)
877                         return NULL;
878         }
879
880         assert(c);
881         assert(c->state & CLASS_LOADED);
882         assert(c->classloader == defloader);
883
884         if (link && !(c->state & CLASS_LINKED))
885                 if (!link_class(c))
886                         return NULL;
887
888         assert(!link || (c->state & CLASS_LINKED));
889
890         return c;
891 }
892
893
894 /* class_array_of **************************************************************
895
896    Returns an array class with the given component class. The array
897    class is dynamically created if neccessary.
898
899 *******************************************************************************/
900
901 classinfo *class_array_of(classinfo *component, bool link)
902 {
903         classloader       *cl;
904     s4                 namelen;
905     char              *namebuf;
906         utf               *u;
907         classinfo         *c;
908         s4                 dumpsize;
909
910         cl = component->classloader;
911
912         dumpsize = dump_size();
913
914     /* Assemble the array class name */
915     namelen = component->name->blength;
916     
917     if (component->name->text[0] == '[') {
918         /* the component is itself an array */
919         namebuf = DMNEW(char, namelen + 1);
920         namebuf[0] = '[';
921         MCOPY(namebuf + 1, component->name->text, char, namelen);
922         namelen++;
923     }
924         else {
925         /* the component is a non-array class */
926         namebuf = DMNEW(char, namelen + 3);
927         namebuf[0] = '[';
928         namebuf[1] = 'L';
929         MCOPY(namebuf + 2, component->name->text, char, namelen);
930         namebuf[2 + namelen] = ';';
931         namelen += 3;
932     }
933
934         u = utf_new(namebuf, namelen);
935
936         c = get_array_class(u, cl, cl, link);
937
938         dump_release(dumpsize);
939
940         return c;
941 }
942
943
944 /* class_multiarray_of *********************************************************
945
946    Returns an array class with the given dimension and element class.
947    The array class is dynamically created if neccessary.
948
949 *******************************************************************************/
950
951 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
952 {
953     s4 namelen;
954     char *namebuf;
955         s4 dumpsize;
956         classinfo *c;
957
958         dumpsize = dump_size();
959
960         if (dim < 1) {
961                 log_text("Invalid array dimension requested");
962                 assert(0);
963         }
964
965     /* Assemble the array class name */
966     namelen = element->name->blength;
967     
968     if (element->name->text[0] == '[') {
969         /* the element is itself an array */
970         namebuf = DMNEW(char, namelen + dim);
971         memcpy(namebuf + dim, element->name->text, namelen);
972         namelen += dim;
973     }
974     else {
975         /* the element is a non-array class */
976         namebuf = DMNEW(char, namelen + 2 + dim);
977         namebuf[dim] = 'L';
978         memcpy(namebuf + dim + 1, element->name->text, namelen);
979         namelen += (2 + dim);
980         namebuf[namelen - 1] = ';';
981     }
982         memset(namebuf, '[', dim);
983
984         c = get_array_class(utf_new(namebuf, namelen),
985                                                 element->classloader,
986                                                 element->classloader,
987                                                 link);
988
989         dump_release(dumpsize);
990
991         return c;
992 }
993
994
995 /* class_lookup_classref *******************************************************
996
997    Looks up the constant_classref for a given classname in the classref
998    tables of a class.
999
1000    IN:
1001        cls..............the class containing the reference
1002            name.............the name of the class refered to
1003
1004     RETURN VALUE:
1005            a pointer to a constant_classref, or 
1006            NULL if the reference was not found
1007    
1008 *******************************************************************************/
1009
1010 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1011 {
1012         constant_classref *ref;
1013         extra_classref *xref;
1014         int count;
1015
1016         assert(cls);
1017         assert(name);
1018         assert(!cls->classrefcount || cls->classrefs);
1019         
1020         /* first search the main classref table */
1021         count = cls->classrefcount;
1022         ref = cls->classrefs;
1023         for (; count; --count, ++ref)
1024                 if (ref->name == name)
1025                         return ref;
1026
1027         /* next try the list of extra classrefs */
1028         for (xref = cls->extclassrefs; xref; xref = xref->next) {
1029                 if (xref->classref.name == name)
1030                         return &(xref->classref);
1031         }
1032
1033         /* not found */
1034         return NULL;
1035 }
1036
1037
1038 /* class_get_classref **********************************************************
1039
1040    Returns the constant_classref for a given classname.
1041
1042    IN:
1043        cls..............the class containing the reference
1044            name.............the name of the class refered to
1045
1046    RETURN VALUE:
1047        a pointer to a constant_classref (never NULL)
1048
1049    NOTE:
1050        The given name is not checked for validity!
1051    
1052 *******************************************************************************/
1053
1054 constant_classref *class_get_classref(classinfo *cls, utf *name)
1055 {
1056         constant_classref *ref;
1057         extra_classref *xref;
1058
1059         assert(cls);
1060         assert(name);
1061
1062         ref = class_lookup_classref(cls,name);
1063         if (ref)
1064                 return ref;
1065
1066         xref = NEW(extra_classref);
1067         CLASSREF_INIT(xref->classref,cls,name);
1068
1069         xref->next = cls->extclassrefs;
1070         cls->extclassrefs = xref;
1071
1072         return &(xref->classref);
1073 }
1074
1075
1076 /* class_get_self_classref *****************************************************
1077
1078    Returns the constant_classref to the class itself.
1079
1080    IN:
1081        cls..............the class containing the reference
1082
1083    RETURN VALUE:
1084        a pointer to a constant_classref (never NULL)
1085
1086 *******************************************************************************/
1087
1088 constant_classref *class_get_self_classref(classinfo *cls)
1089 {
1090         /* XXX this should be done in a faster way. Maybe always make */
1091         /* the classref of index 0 a self reference.                  */
1092         return class_get_classref(cls,cls->name);
1093 }
1094
1095 /* class_get_classref_multiarray_of ********************************************
1096
1097    Returns an array type reference with the given dimension and element class
1098    reference.
1099
1100    IN:
1101        dim..............the requested dimension
1102                             dim must be in [1;255]. This is NOT checked!
1103            ref..............the component class reference
1104
1105    RETURN VALUE:
1106        a pointer to the class reference for the array type
1107
1108    NOTE:
1109        The referer of `ref` is used as the referer for the new classref.
1110
1111 *******************************************************************************/
1112
1113 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1114 {
1115     s4 namelen;
1116     char *namebuf;
1117         s4 dumpsize;
1118         constant_classref *cr;
1119
1120         assert(ref);
1121         assert(dim >= 1 && dim <= 255);
1122
1123         dumpsize = dump_size();
1124
1125     /* Assemble the array class name */
1126     namelen = ref->name->blength;
1127     
1128     if (ref->name->text[0] == '[') {
1129         /* the element is itself an array */
1130         namebuf = DMNEW(char, namelen + dim);
1131         memcpy(namebuf + dim, ref->name->text, namelen);
1132         namelen += dim;
1133     }
1134     else {
1135         /* the element is a non-array class */
1136         namebuf = DMNEW(char, namelen + 2 + dim);
1137         namebuf[dim] = 'L';
1138         memcpy(namebuf + dim + 1, ref->name->text, namelen);
1139         namelen += (2 + dim);
1140         namebuf[namelen - 1] = ';';
1141     }
1142         memset(namebuf, '[', dim);
1143
1144     cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1145
1146         dump_release(dumpsize);
1147
1148         return cr;
1149 }
1150
1151
1152 /* class_get_classref_component_of *********************************************
1153
1154    Returns the component classref of a given array type reference
1155
1156    IN:
1157        ref..............the array type reference
1158
1159    RETURN VALUE:
1160        a reference to the component class, or
1161            NULL if `ref` is not an object array type reference
1162
1163    NOTE:
1164        The referer of `ref` is used as the referer for the new classref.
1165
1166 *******************************************************************************/
1167
1168 constant_classref *class_get_classref_component_of(constant_classref *ref)
1169 {
1170         s4 namelen;
1171         char *name;
1172         
1173         assert(ref);
1174
1175         name = ref->name->text;
1176         if (*name++ != '[')
1177                 return NULL;
1178         
1179         namelen = ref->name->blength - 1;
1180         if (*name == 'L') {
1181                 name++;
1182                 namelen -= 2;
1183         }
1184         else if (*name != '[') {
1185                 return NULL;
1186         }
1187
1188     return class_get_classref(ref->referer, utf_new(name, namelen));
1189 }
1190
1191
1192 /* class_findmethod ************************************************************
1193         
1194    Searches a 'classinfo' structure for a method having the given name
1195    and descriptor. If descriptor is NULL, it is ignored.
1196
1197 *******************************************************************************/
1198
1199 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1200 {
1201         methodinfo *m;
1202         s4          i;
1203
1204         for (i = 0; i < c->methodscount; i++) {
1205                 m = &(c->methods[i]);
1206
1207                 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1208                         return m;
1209         }
1210
1211         return NULL;
1212 }
1213
1214
1215 /* class_resolvemethod *********************************************************
1216         
1217    Searches a class and it's super classes for a method.
1218
1219    Superinterfaces are *not* searched.
1220
1221 *******************************************************************************/
1222
1223 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1224 {
1225         methodinfo *m;
1226
1227         while (c) {
1228                 m = class_findmethod(c, name, desc);
1229
1230                 if (m)
1231                         return m;
1232
1233                 /* JVM Specification bug: 
1234
1235                    It is important NOT to resolve special <init> and <clinit>
1236                    methods to super classes or interfaces; yet, this is not
1237                    explicited in the specification.  Section 5.4.3.3 should be
1238                    updated appropriately.  */
1239
1240                 if (name == utf_init || name == utf_clinit)
1241                         return NULL;
1242
1243                 c = c->super;
1244         }
1245
1246         return NULL;
1247 }
1248
1249
1250 /* class_resolveinterfacemethod_intern *****************************************
1251
1252    Internally used helper function. Do not use this directly.
1253
1254 *******************************************************************************/
1255
1256 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1257                                                                                                            utf *name, utf *desc)
1258 {
1259         methodinfo *m;
1260         s4          i;
1261
1262         /* try to find the method in the class */
1263
1264         m = class_findmethod(c, name, desc);
1265
1266         if (m != NULL)
1267                 return m;
1268
1269         /* No method found?  Try the super interfaces. */
1270
1271         for (i = 0; i < c->interfacescount; i++) {
1272                 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1273
1274                 if (m != NULL)
1275                         return m;
1276         }
1277
1278         /* no method found */
1279
1280         return NULL;
1281 }
1282
1283
1284 /* class_resolveclassmethod ****************************************************
1285         
1286    Resolves a reference from REFERER to a method with NAME and DESC in
1287    class C.
1288
1289    If the method cannot be resolved the return value is NULL. If
1290    EXCEPT is true *exceptionptr is set, too.
1291
1292 *******************************************************************************/
1293
1294 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1295                                                                          classinfo *referer, bool throwexception)
1296 {
1297         classinfo  *cls;
1298         methodinfo *m;
1299         s4          i;
1300
1301 /*      if (c->flags & ACC_INTERFACE) { */
1302 /*              if (throwexception) */
1303 /*                      *exceptionptr = */
1304 /*                              new_exception(string_java_lang_IncompatibleClassChangeError); */
1305 /*              return NULL; */
1306 /*      } */
1307
1308         /* try class c and its superclasses */
1309
1310         cls = c;
1311
1312         m = class_resolvemethod(cls, name, desc);
1313
1314         if (m != NULL)
1315                 goto found;
1316
1317         /* Try the super interfaces. */
1318
1319         for (i = 0; i < c->interfacescount; i++) {
1320                 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1321
1322                 if (m != NULL)
1323                         goto found;
1324         }
1325         
1326         if (throwexception)
1327                 exceptions_throw_nosuchmethoderror(c, name, desc);
1328
1329         return NULL;
1330
1331  found:
1332         if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1333                 if (throwexception)
1334                         exceptions_throw_abstractmethoderror();
1335
1336                 return NULL;
1337         }
1338
1339         /* XXX check access rights */
1340
1341         return m;
1342 }
1343
1344
1345 /* class_resolveinterfacemethod ************************************************
1346
1347    Resolves a reference from REFERER to a method with NAME and DESC in
1348    interface C.
1349
1350    If the method cannot be resolved the return value is NULL. If
1351    EXCEPT is true *exceptionptr is set, too.
1352
1353 *******************************************************************************/
1354
1355 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1356                                                                                  classinfo *referer, bool throwexception)
1357 {
1358         methodinfo *mi;
1359
1360         if (!(c->flags & ACC_INTERFACE)) {
1361                 if (throwexception)
1362                         exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1363
1364                 return NULL;
1365         }
1366
1367         mi = class_resolveinterfacemethod_intern(c, name, desc);
1368
1369         if (mi != NULL)
1370                 return mi;
1371
1372         /* try class java.lang.Object */
1373
1374         mi = class_findmethod(class_java_lang_Object, name, desc);
1375
1376         if (mi != NULL)
1377                 return mi;
1378
1379         if (throwexception)
1380                 exceptions_throw_nosuchmethoderror(c, name, desc);
1381
1382         return NULL;
1383 }
1384
1385
1386 /* class_findfield *************************************************************
1387         
1388    Searches for field with specified name and type in a classinfo
1389    structure. If no such field is found NULL is returned.
1390
1391 *******************************************************************************/
1392
1393 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1394 {
1395         s4 i;
1396
1397         for (i = 0; i < c->fieldscount; i++)
1398                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1399                         return &(c->fields[i]);
1400
1401         if (c->super != NULL)
1402                 return class_findfield(c->super, name, desc);
1403
1404         return NULL;
1405 }
1406
1407
1408 /* class_findfield_approx ******************************************************
1409         
1410    Searches in 'classinfo'-structure for a field with the specified
1411    name.
1412
1413 *******************************************************************************/
1414  
1415 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1416 {
1417         s4 i;
1418
1419         /* get field index */
1420
1421         i = class_findfield_index_by_name(c, name);
1422
1423         /* field was not found, return */
1424
1425         if (i == -1)
1426                 return NULL;
1427
1428         /* return field address */
1429
1430         return &(c->fields[i]);
1431 }
1432
1433
1434 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1435 {
1436         s4 i;
1437
1438         for (i = 0; i < c->fieldscount; i++) {
1439                 /* compare field names */
1440
1441                 if ((c->fields[i].name == name))
1442                         return i;
1443         }
1444
1445         /* field was not found, raise exception */      
1446
1447         exceptions_throw_nosuchfielderror(c, name);
1448
1449         return -1;
1450 }
1451
1452
1453 /****************** Function: class_resolvefield_int ***************************
1454
1455     This is an internally used helper function. Do not use this directly.
1456
1457         Tries to resolve a field having the given name and type.
1458     If the field cannot be resolved, NULL is returned.
1459
1460 *******************************************************************************/
1461
1462 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1463 {
1464         fieldinfo *fi;
1465         s4         i;
1466
1467         /* search for field in class c */
1468
1469         for (i = 0; i < c->fieldscount; i++) { 
1470                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1471                         return &(c->fields[i]);
1472                 }
1473     }
1474
1475         /* Try super interfaces recursively. */
1476
1477         for (i = 0; i < c->interfacescount; i++) {
1478                 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1479
1480                 if (fi != NULL)
1481                         return fi;
1482         }
1483
1484         /* Try super class. */
1485
1486         if (c->super != NULL)
1487                 return class_resolvefield_int(c->super, name, desc);
1488
1489         /* not found */
1490
1491         return NULL;
1492 }
1493
1494
1495 /********************* Function: class_resolvefield ***************************
1496         
1497         Resolves a reference from REFERER to a field with NAME and DESC in class C.
1498
1499     If the field cannot be resolved the return value is NULL. If EXCEPT is
1500     true *exceptionptr is set, too.
1501
1502 *******************************************************************************/
1503
1504 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1505                                                           classinfo *referer, bool throwexception)
1506 {
1507         fieldinfo *fi;
1508
1509         fi = class_resolvefield_int(c, name, desc);
1510
1511         if (!fi) {
1512                 if (throwexception)
1513                         exceptions_throw_nosuchfielderror(c, name);
1514
1515                 return NULL;
1516         }
1517
1518         /* XXX check access rights */
1519
1520         return fi;
1521 }
1522
1523
1524 /* class_issubclass ************************************************************
1525
1526    Checks if sub is a descendant of super.
1527         
1528 *******************************************************************************/
1529
1530 bool class_issubclass(classinfo *sub, classinfo *super)
1531 {
1532         classinfo *c;
1533
1534         c = sub;
1535
1536         for (;;) {
1537                 /* We reached java/lang/Object and did not find the requested
1538                    super class. */
1539
1540                 if (c == NULL)
1541                         return false;
1542
1543                 /* We found the requested super class. */
1544
1545                 if (c == super)
1546                         return true;
1547
1548                 c = c->super;
1549         }
1550 }
1551
1552
1553 /* class_isanysubclass *********************************************************
1554
1555    Checks a subclass relation between two classes. Implemented
1556    interfaces are interpreted as super classes.
1557
1558    Return value: 1 ... sub is subclass of super
1559                  0 ... otherwise
1560
1561 *******************************************************************************/
1562
1563 bool class_isanysubclass(classinfo *sub, classinfo *super)
1564 {
1565         uint32_t diffval;
1566         bool     result;
1567
1568         /* This is the trivial case. */
1569
1570         if (sub == super)
1571                 return true;
1572
1573         /* Primitive classes are only subclasses of themselves. */
1574
1575         if (class_is_primitive(sub) || class_is_primitive(super))
1576                 return false;
1577
1578         /* Check for interfaces. */
1579
1580         if (super->flags & ACC_INTERFACE) {
1581                 result = (sub->vftbl->interfacetablelength > super->index) &&
1582                         (sub->vftbl->interfacetable[-super->index] != NULL);
1583         }
1584         else {
1585                 /* java.lang.Object is the only super class of any
1586                    interface. */
1587
1588                 if (sub->flags & ACC_INTERFACE)
1589                         return (super == class_java_lang_Object);
1590
1591                 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1592
1593                 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1594                 result  = diffval <= (uint32_t) super->vftbl->diffval;
1595
1596                 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1597         }
1598
1599         return result;
1600 }
1601
1602
1603 /* class_is_primitive **********************************************************
1604
1605    Checks if the given class is a primitive class.
1606
1607 *******************************************************************************/
1608
1609 bool class_is_primitive(classinfo *c)
1610 {
1611         if (c->flags & ACC_CLASS_PRIMITIVE)
1612                 return true;
1613
1614         return false;
1615 }
1616
1617
1618 /* class_is_anonymousclass *****************************************************
1619
1620    Checks if the given class is an anonymous class.
1621
1622 *******************************************************************************/
1623
1624 bool class_is_anonymousclass(classinfo *c)
1625 {
1626         if (c->flags & ACC_CLASS_ANONYMOUS)
1627                 return true;
1628
1629         return false;
1630 }
1631
1632
1633 /* class_is_array **************************************************************
1634
1635    Checks if the given class is an array class.
1636
1637 *******************************************************************************/
1638
1639 bool class_is_array(classinfo *c)
1640 {
1641         if (!(c->state & CLASS_LINKED))
1642                 if (!link_class(c))
1643                         return false;
1644
1645         return (c->vftbl->arraydesc != NULL);
1646 }
1647
1648
1649 /* class_is_interface **********************************************************
1650
1651    Checks if the given class is an interface.
1652
1653 *******************************************************************************/
1654
1655 bool class_is_interface(classinfo *c)
1656 {
1657         if (c->flags & ACC_INTERFACE)
1658                 return true;
1659
1660         return false;
1661 }
1662
1663
1664 /* class_is_localclass *********************************************************
1665
1666    Checks if the given class is a local class.
1667
1668 *******************************************************************************/
1669
1670 bool class_is_localclass(classinfo *c)
1671 {
1672         if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1673                 return true;
1674
1675         return false;
1676 }
1677
1678
1679 /* class_is_memberclass ********************************************************
1680
1681    Checks if the given class is a member class.
1682
1683 *******************************************************************************/
1684
1685 bool class_is_memberclass(classinfo *c)
1686 {
1687         if (c->flags & ACC_CLASS_MEMBER)
1688                 return true;
1689
1690         return false;
1691 }
1692
1693
1694 /* class_get_classloader *******************************************************
1695
1696    Return the classloader of the given class.
1697
1698 *******************************************************************************/
1699
1700 classloader *class_get_classloader(classinfo *c)
1701 {
1702         classloader *cl;
1703
1704         cl = c->classloader;
1705
1706         if (cl == NULL)
1707                 return NULL;
1708
1709 #if defined(ENABLE_HANDLES)
1710         /* The classloader entry itself is the handle. */
1711
1712         return cl;
1713 #else
1714         /* Get the object out of the classloader entry. */
1715         /* Cast to prevent warning (will be removed). */
1716
1717         return (classloader *) cl->object;
1718 #endif
1719 }
1720
1721
1722 /* class_get_superclass ********************************************************
1723
1724    Return the super class of the given class.
1725
1726 *******************************************************************************/
1727
1728 classinfo *class_get_superclass(classinfo *c)
1729 {
1730         /* For interfaces we return NULL. */
1731
1732         if (c->flags & ACC_INTERFACE)
1733                 return NULL;
1734
1735         /* For java/lang/Object, primitive-type and Void classes c->super
1736            is NULL and we return NULL. */
1737
1738         return c->super;
1739 }
1740
1741
1742 /* class_get_componenttype *****************************************************
1743
1744    Return the component class of the given class.  If the given class
1745    is not an array, return NULL.
1746
1747 *******************************************************************************/
1748
1749 classinfo *class_get_componenttype(classinfo *c)
1750 {
1751         classinfo       *component;
1752         arraydescriptor *ad;
1753         
1754         /* XXX maybe we could find a way to do this without linking. */
1755         /* This way should be safe and easy, however.                */
1756
1757         if (!(c->state & CLASS_LINKED))
1758                 if (!link_class(c))
1759                         return NULL;
1760
1761         ad = c->vftbl->arraydesc;
1762         
1763         if (ad == NULL)
1764                 return NULL;
1765         
1766         if (ad->arraytype == ARRAYTYPE_OBJECT)
1767                 component = ad->componentvftbl->class;
1768         else
1769                 component = primitive_class_get_by_type(ad->arraytype);
1770                 
1771         return component;
1772 }
1773
1774
1775 /* class_get_declaredclasses ***************************************************
1776
1777    Return an array of declared classes of the given class.
1778
1779 *******************************************************************************/
1780
1781 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1782 {
1783         classref_or_classinfo  inner;
1784         classref_or_classinfo  outer;
1785         utf                   *outername;
1786         int                    declaredclasscount;  /* number of declared classes */
1787         int                    pos;                     /* current declared class */
1788         java_handle_objectarray_t *oa;               /* array of declared classes */
1789         int                    i;
1790         classinfo             *ic;
1791
1792         declaredclasscount = 0;
1793
1794         if (!class_is_primitive(c) && !class_is_array(c)) {
1795                 /* Determine number of declared classes. */
1796
1797                 for (i = 0; i < c->innerclasscount; i++) {
1798                         /* Get outer-class.  If the inner-class is not a member
1799                            class, the outer-class is NULL. */
1800
1801                         outer = c->innerclass[i].outer_class;
1802
1803                         if (outer.any == NULL)
1804                                 continue;
1805
1806                         /* Check if outer-class is a classref or a real class and
1807                get the class name from the structure. */
1808
1809                         outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1810
1811                         /* Outer class is this class. */
1812
1813                         if ((outername == c->name) &&
1814                                 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1815                                 declaredclasscount++;
1816                 }
1817         }
1818
1819         /* Allocate Class[] and check for OOM. */
1820
1821         oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1822
1823         if (oa == NULL)
1824                 return NULL;
1825
1826         for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1827                 inner = c->innerclass[i].inner_class;
1828                 outer = c->innerclass[i].outer_class;
1829
1830                 /* Get outer-class.  If the inner-class is not a member class,
1831                    the outer-class is NULL. */
1832
1833                 if (outer.any == NULL)
1834                         continue;
1835
1836                 /* Check if outer_class is a classref or a real class and get
1837                    the class name from the structure. */
1838
1839                 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1840
1841                 /* Outer class is this class. */
1842
1843                 if ((outername == c->name) &&
1844                         ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1845
1846                         ic = resolve_classref_or_classinfo_eager(inner, false);
1847
1848                         if (ic == NULL)
1849                                 return NULL;
1850
1851                         if (!(ic->state & CLASS_LINKED))
1852                                 if (!link_class(ic))
1853                                         return NULL;
1854
1855                         LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1856                 }
1857         }
1858
1859         return oa;
1860 }
1861
1862
1863 /* class_get_declaringclass ****************************************************
1864
1865    If the class or interface given is a member of another class,
1866    return the declaring class.  For array and primitive classes return
1867    NULL.
1868
1869 *******************************************************************************/
1870
1871 classinfo *class_get_declaringclass(classinfo *c)
1872 {
1873         classref_or_classinfo  cr;
1874         classinfo             *dc;
1875
1876         /* Get declaring class. */
1877
1878         cr = c->declaringclass;
1879
1880         if (cr.any == NULL)
1881                 return NULL;
1882
1883         /* Resolve the class if necessary. */
1884
1885         if (IS_CLASSREF(cr)) {
1886 /*              dc = resolve_classref_eager(cr.ref); */
1887                 dc = resolve_classref_or_classinfo_eager(cr, true);
1888
1889                 if (dc == NULL)
1890                         return NULL;
1891
1892                 /* Store the resolved class in the class structure. */
1893
1894                 cr.cls = dc;
1895         }
1896
1897         dc = cr.cls;
1898
1899         return dc;
1900 }
1901
1902
1903 /* class_get_enclosingclass ****************************************************
1904
1905    Return the enclosing class for the given class.
1906
1907 *******************************************************************************/
1908
1909 classinfo *class_get_enclosingclass(classinfo *c)
1910 {
1911         classref_or_classinfo  cr;
1912         classinfo             *ec;
1913
1914         /* Get enclosing class. */
1915
1916         cr = c->enclosingclass;
1917
1918         if (cr.any == NULL)
1919                 return NULL;
1920
1921         /* Resolve the class if necessary. */
1922
1923         if (IS_CLASSREF(cr)) {
1924 /*              ec = resolve_classref_eager(cr.ref); */
1925                 ec = resolve_classref_or_classinfo_eager(cr, true);
1926
1927                 if (ec == NULL)
1928                         return NULL;
1929
1930                 /* Store the resolved class in the class structure. */
1931
1932                 cr.cls = ec;
1933         }
1934
1935         ec = cr.cls;
1936
1937         return ec;
1938 }
1939
1940
1941 /* class_get_interfaces ********************************************************
1942
1943    Return an array of interfaces of the given class.
1944
1945 *******************************************************************************/
1946
1947 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1948 {
1949         classinfo                 *ic;
1950         java_handle_objectarray_t *oa;
1951         u4                         i;
1952
1953         if (!(c->state & CLASS_LINKED))
1954                 if (!link_class(c))
1955                         return NULL;
1956
1957         oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1958
1959         if (oa == NULL)
1960                 return NULL;
1961
1962         for (i = 0; i < c->interfacescount; i++) {
1963                 ic = c->interfaces[i];
1964
1965                 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1966         }
1967
1968         return oa;
1969 }
1970
1971
1972 /* class_get_annotations *******************************************************
1973
1974    Return the unparsed declared annotations in an byte array
1975    of the given class (or NULL if there aren't any).
1976
1977 *******************************************************************************/
1978
1979 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1980 {
1981 #if defined(ENABLE_ANNOTATIONS)
1982         return c->annotations;
1983 #else
1984         return NULL;
1985 #endif
1986 }
1987
1988
1989 /* class_get_signature *********************************************************
1990
1991    Return the signature of the given class.  For array and primitive
1992    classes return NULL.
1993
1994 *******************************************************************************/
1995
1996 #if defined(ENABLE_JAVASE)
1997 utf *class_get_signature(classinfo *c)
1998 {
1999         /* For array and primitive classes return NULL. */
2000
2001         if (class_is_array(c) || class_is_primitive(c))
2002                 return NULL;
2003
2004         return c->signature;
2005 }
2006 #endif
2007
2008
2009 /* class_printflags ************************************************************
2010
2011    Prints flags of a class.
2012
2013 *******************************************************************************/
2014
2015 #if !defined(NDEBUG)
2016 void class_printflags(classinfo *c)
2017 {
2018         if (c == NULL) {
2019                 printf("NULL");
2020                 return;
2021         }
2022
2023         if (c->flags & ACC_PUBLIC)       printf(" PUBLIC");
2024         if (c->flags & ACC_PRIVATE)      printf(" PRIVATE");
2025         if (c->flags & ACC_PROTECTED)    printf(" PROTECTED");
2026         if (c->flags & ACC_STATIC)       printf(" STATIC");
2027         if (c->flags & ACC_FINAL)        printf(" FINAL");
2028         if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2029         if (c->flags & ACC_VOLATILE)     printf(" VOLATILE");
2030         if (c->flags & ACC_TRANSIENT)    printf(" TRANSIENT");
2031         if (c->flags & ACC_NATIVE)       printf(" NATIVE");
2032         if (c->flags & ACC_INTERFACE)    printf(" INTERFACE");
2033         if (c->flags & ACC_ABSTRACT)     printf(" ABSTRACT");
2034 }
2035 #endif
2036
2037
2038 /* class_print *****************************************************************
2039
2040    Prints classname plus flags.
2041
2042 *******************************************************************************/
2043
2044 #if !defined(NDEBUG)
2045 void class_print(classinfo *c)
2046 {
2047         if (c == NULL) {
2048                 printf("NULL");
2049                 return;
2050         }
2051
2052         utf_display_printable_ascii(c->name);
2053         class_printflags(c);
2054 }
2055 #endif
2056
2057
2058 /* class_classref_print ********************************************************
2059
2060    Prints classname plus referer class.
2061
2062 *******************************************************************************/
2063
2064 #if !defined(NDEBUG)
2065 void class_classref_print(constant_classref *cr)
2066 {
2067         if (cr == NULL) {
2068                 printf("NULL");
2069                 return;
2070         }
2071
2072         utf_display_printable_ascii(cr->name);
2073         printf("(ref.by ");
2074         if (cr->referer)
2075                 class_print(cr->referer);
2076         else
2077                 printf("NULL");
2078         printf(")");
2079 }
2080 #endif
2081
2082
2083 /* class_println ***************************************************************
2084
2085    Prints classname plus flags and new line.
2086
2087 *******************************************************************************/
2088
2089 #if !defined(NDEBUG)
2090 void class_println(classinfo *c)
2091 {
2092         class_print(c);
2093         printf("\n");
2094 }
2095 #endif
2096
2097
2098 /* class_classref_println ******************************************************
2099
2100    Prints classname plus referer class and new line.
2101
2102 *******************************************************************************/
2103
2104 #if !defined(NDEBUG)
2105 void class_classref_println(constant_classref *cr)
2106 {
2107         class_classref_print(cr);
2108         printf("\n");
2109 }
2110 #endif
2111
2112
2113 /* class_classref_or_classinfo_print *******************************************
2114
2115    Prints classname plus referer class.
2116
2117 *******************************************************************************/
2118
2119 #if !defined(NDEBUG)
2120 void class_classref_or_classinfo_print(classref_or_classinfo c)
2121 {
2122         if (c.any == NULL) {
2123                 printf("(classref_or_classinfo) NULL");
2124                 return;
2125         }
2126         if (IS_CLASSREF(c))
2127                 class_classref_print(c.ref);
2128         else
2129                 class_print(c.cls);
2130 }
2131 #endif
2132
2133
2134 /* class_classref_or_classinfo_println *****************************************
2135
2136    Prints classname plus referer class and a newline.
2137
2138 *******************************************************************************/
2139
2140 void class_classref_or_classinfo_println(classref_or_classinfo c)
2141 {
2142         class_classref_or_classinfo_print(c);
2143         printf("\n");
2144 }
2145
2146
2147 /* class_showconstantpool ******************************************************
2148
2149    Dump the constant pool of the given class to stdout.
2150
2151 *******************************************************************************/
2152
2153 #if !defined(NDEBUG)
2154 void class_showconstantpool (classinfo *c) 
2155 {
2156         u4 i;
2157         voidptr e;
2158
2159         printf ("---- dump of constant pool ----\n");
2160
2161         for (i=0; i<c->cpcount; i++) {
2162                 printf ("#%d:  ", (int) i);
2163                 
2164                 e = c -> cpinfos [i];
2165                 if (e) {
2166                         
2167                         switch (c -> cptags [i]) {
2168                         case CONSTANT_Class:
2169                                 printf ("Classreference -> ");
2170                                 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2171                                 break;
2172                         case CONSTANT_Fieldref:
2173                                 printf ("Fieldref -> ");
2174                                 field_fieldref_print((constant_FMIref *) e);
2175                                 break;
2176                         case CONSTANT_Methodref:
2177                                 printf ("Methodref -> ");
2178                                 method_methodref_print((constant_FMIref *) e);
2179                                 break;
2180                         case CONSTANT_InterfaceMethodref:
2181                                 printf ("InterfaceMethod -> ");
2182                                 method_methodref_print((constant_FMIref *) e);
2183                                 break;
2184                         case CONSTANT_String:
2185                                 printf ("String -> ");
2186                                 utf_display_printable_ascii (e);
2187                                 break;
2188                         case CONSTANT_Integer:
2189                                 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2190                                 break;
2191                         case CONSTANT_Float:
2192                                 printf ("Float -> %f", ((constant_float*)e) -> value);
2193                                 break;
2194                         case CONSTANT_Double:
2195                                 printf ("Double -> %f", ((constant_double*)e) -> value);
2196                                 break;
2197                         case CONSTANT_Long:
2198                                 {
2199                                         u8 v = ((constant_long*)e) -> value;
2200 #if U8_AVAILABLE
2201                                         printf ("Long -> %ld", (long int) v);
2202 #else
2203                                         printf ("Long -> HI: %ld, LO: %ld\n", 
2204                                                         (long int) v.high, (long int) v.low);
2205 #endif 
2206                                 }
2207                                 break;
2208                         case CONSTANT_NameAndType:
2209                                 {
2210                                         constant_nameandtype *cnt = e;
2211                                         printf ("NameAndType: ");
2212                                         utf_display_printable_ascii (cnt->name);
2213                                         printf (" ");
2214                                         utf_display_printable_ascii (cnt->descriptor);
2215                                 }
2216                                 break;
2217                         case CONSTANT_Utf8:
2218                                 printf ("Utf8 -> ");
2219                                 utf_display_printable_ascii (e);
2220                                 break;
2221                         default: 
2222                                 log_text("Invalid type of ConstantPool-Entry");
2223                                 assert(0);
2224                         }
2225                 }
2226
2227                 printf ("\n");
2228         }
2229 }
2230 #endif /* !defined(NDEBUG) */
2231
2232
2233 /* class_showmethods ***********************************************************
2234
2235    Dump info about the fields and methods of the given class to stdout.
2236
2237 *******************************************************************************/
2238
2239 #if !defined(NDEBUG)
2240 void class_showmethods (classinfo *c)
2241 {
2242         s4 i;
2243         
2244         printf("--------- Fields and Methods ----------------\n");
2245         printf("Flags: ");
2246         class_printflags(c);
2247         printf("\n");
2248
2249         printf("This: ");
2250         utf_display_printable_ascii(c->name);
2251         printf("\n");
2252
2253         if (c->super) {
2254                 printf("Super: ");
2255                 utf_display_printable_ascii(c->super->name);
2256                 printf ("\n");
2257         }
2258
2259         printf("Index: %d\n", c->index);
2260         
2261         printf("Interfaces:\n");        
2262         for (i = 0; i < c->interfacescount; i++) {
2263                 printf("   ");
2264                 utf_display_printable_ascii(c->interfaces[i]->name);
2265                 printf (" (%d)\n", c->interfaces[i]->index);
2266         }
2267
2268         printf("Fields:\n");
2269         for (i = 0; i < c->fieldscount; i++)
2270                 field_println(&(c->fields[i]));
2271
2272         printf("Methods:\n");
2273         for (i = 0; i < c->methodscount; i++) {
2274                 methodinfo *m = &(c->methods[i]);
2275
2276                 if (!(m->flags & ACC_STATIC))
2277                         printf("vftblindex: %d   ", m->vftblindex);
2278
2279                 method_println(m);
2280         }
2281
2282         printf ("Virtual function table:\n");
2283         for (i = 0; i < c->vftbl->vftbllength; i++)
2284                 printf ("entry: %d,  %ld\n", i, (long int) (c->vftbl->table[i]));
2285 }
2286 #endif /* !defined(NDEBUG) */
2287
2288
2289 /*
2290  * These are local overrides for various environment variables in Emacs.
2291  * Please do not remove this and leave it at the end of the file, where
2292  * Emacs will automagically detect them.
2293  * ---------------------------------------------------------------------
2294  * Local variables:
2295  * mode: c
2296  * indent-tabs-mode: t
2297  * c-basic-offset: 4
2298  * tab-width: 4
2299  * End:
2300  * vim:noexpandtab:sw=4:ts=4:
2301  */