1 /* src/vmcore/class.c - class related functions
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: class.c 8321 2007-08-16 11:37:25Z michi $
41 #include "mm/memory.h"
43 #include "native/llni.h"
45 #include "threads/lock-common.h"
47 #include "toolbox/logging.h"
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/global.h"
52 #include "vm/resolve.h"
54 #include "vm/jit/asmpart.h"
56 #include "vmcore/class.h"
57 #include "vmcore/classcache.h"
58 #include "vmcore/linker.h"
59 #include "vmcore/loader.h"
60 #include "vmcore/options.h"
62 #if defined(ENABLE_STATISTICS)
63 # include "vmcore/statistics.h"
66 #include "vmcore/suck.h"
67 #include "vmcore/utf8.h"
70 /* global variables ***********************************************************/
72 /* frequently used classes ****************************************************/
74 /* important system classes */
76 classinfo *class_java_lang_Object;
77 classinfo *class_java_lang_Class;
78 classinfo *class_java_lang_ClassLoader;
79 classinfo *class_java_lang_Cloneable;
80 classinfo *class_java_lang_SecurityManager;
81 classinfo *class_java_lang_String;
82 classinfo *class_java_lang_System;
83 classinfo *class_java_lang_Thread;
84 classinfo *class_java_lang_ThreadGroup;
85 classinfo *class_java_lang_VMSystem;
86 classinfo *class_java_lang_VMThread;
87 classinfo *class_java_io_Serializable;
89 #if defined(WITH_CLASSPATH_SUN)
90 classinfo *class_sun_reflect_MagicAccessorImpl;
93 /* system exception classes required in cacao */
95 classinfo *class_java_lang_Throwable;
96 classinfo *class_java_lang_Error;
97 classinfo *class_java_lang_LinkageError;
98 classinfo *class_java_lang_NoClassDefFoundError;
99 classinfo *class_java_lang_OutOfMemoryError;
100 classinfo *class_java_lang_VirtualMachineError;
102 #if defined(WITH_CLASSPATH_GNU)
103 classinfo *class_java_lang_VMThrowable;
106 classinfo *class_java_lang_Exception;
107 classinfo *class_java_lang_ClassCastException;
108 classinfo *class_java_lang_ClassNotFoundException;
110 #if defined(ENABLE_JAVASE)
111 classinfo *class_java_lang_Void;
113 classinfo *class_java_lang_Boolean;
114 classinfo *class_java_lang_Byte;
115 classinfo *class_java_lang_Character;
116 classinfo *class_java_lang_Short;
117 classinfo *class_java_lang_Integer;
118 classinfo *class_java_lang_Long;
119 classinfo *class_java_lang_Float;
120 classinfo *class_java_lang_Double;
123 /* some runtime exception */
125 classinfo *class_java_lang_NullPointerException;
128 /* some classes which may be used more often */
130 #if defined(ENABLE_JAVASE)
131 classinfo *class_java_lang_StackTraceElement;
132 classinfo *class_java_lang_reflect_Constructor;
133 classinfo *class_java_lang_reflect_Field;
134 classinfo *class_java_lang_reflect_Method;
135 classinfo *class_java_security_PrivilegedAction;
136 classinfo *class_java_util_Vector;
138 classinfo *arrayclass_java_lang_Object;
140 #if defined(ENABLE_ANNOTATIONS)
141 classinfo *class_sun_reflect_ConstantPool;
142 classinfo *class_sun_reflect_annotation_AnnotationParser;
147 /* pseudo classes for the typechecker */
149 classinfo *pseudo_class_Arraystub;
150 classinfo *pseudo_class_Null;
151 classinfo *pseudo_class_New;
154 /* class_set_packagename *******************************************************
156 Derive the package name from the class name and store it in the struct.
158 *******************************************************************************/
160 void class_set_packagename(classinfo *c)
162 char *p = UTF_END(c->name) - 1;
163 char *start = c->name->text;
165 /* set the package name */
166 /* classes in the unnamed package keep packagename == NULL */
168 if (c->name->text[0] == '[') {
169 /* set packagename of arrays to the element's package */
171 for (; *start == '['; start++);
173 /* skip the 'L' in arrays of references */
177 for (; (p > start) && (*p != '/'); --p);
179 c->packagename = utf_new(start, p - start);
182 for (; (p > start) && (*p != '/'); --p);
184 c->packagename = utf_new(start, p - start);
189 /* class_create_classinfo ******************************************************
191 Create a new classinfo struct. The class name is set to the given utf *,
192 most other fields are initialized to zero.
194 Note: classname may be NULL. In this case a not-yet-named classinfo is
195 created. The name must be filled in later and class_set_packagename
196 must be called after that.
198 *******************************************************************************/
200 classinfo *class_create_classinfo(utf *classname)
204 #if defined(ENABLE_STATISTICS)
206 size_classinfo += sizeof(classinfo);
209 /* we use a safe name for temporarily unnamed classes */
211 if (classname == NULL)
212 classname = utf_not_named_yet;
216 log_message_utf("Creating class: ", classname);
219 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
221 #if defined(ENABLE_GC_CACAO)
222 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
224 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
225 /*c=NEW(classinfo);*/
229 /* Set the header.vftbl of all loaded classes to the one of
230 java.lang.Class, so Java code can use a class as object. */
232 if (class_java_lang_Class != NULL)
233 if (class_java_lang_Class->vftbl != NULL)
234 c->object.header.vftbl = class_java_lang_Class->vftbl;
236 #if defined(ENABLE_JAVASE)
237 /* check if the class is a reference class and flag it */
239 if (classname == utf_java_lang_ref_SoftReference) {
240 c->flags |= ACC_CLASS_REFERENCE_SOFT;
242 else if (classname == utf_java_lang_ref_WeakReference) {
243 c->flags |= ACC_CLASS_REFERENCE_WEAK;
245 else if (classname == utf_java_lang_ref_PhantomReference) {
246 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
250 if (classname != utf_not_named_yet)
251 class_set_packagename(c);
253 LOCK_INIT_OBJECT_LOCK(&c->object.header);
259 /* class_postset_header_vftbl **************************************************
261 Set the header.vftbl of all classes created before java.lang.Class
262 was linked. This is necessary that Java code can use a class as
265 *******************************************************************************/
267 void class_postset_header_vftbl(void)
271 classcache_name_entry *nmen;
272 classcache_class_entry *clsen;
274 assert(class_java_lang_Class);
276 for (slot = 0; slot < hashtable_classcache.size; slot++) {
277 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
279 for (; nmen; nmen = nmen->hashlink) {
280 /* iterate over all class entries */
282 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
285 /* now set the the vftbl */
287 if (c->object.header.vftbl == NULL)
288 c->object.header.vftbl = class_java_lang_Class->vftbl;
294 /* class_define ****************************************************************
296 Calls the loader and defines a class in the VM.
298 *******************************************************************************/
300 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
307 /* check if this class has already been defined */
309 c = classcache_lookup_defined_or_initiated(cl, name);
312 exceptions_throw_linkageerror("duplicate class definition: ", c);
317 /* create a new classinfo struct */
319 c = class_create_classinfo(name);
321 #if defined(ENABLE_STATISTICS)
324 if (opt_getloadingtime)
328 /* build a classbuffer with the given data */
330 cb = NEW(classbuffer);
337 /* preset the defining classloader */
341 /* load the class from this buffer */
343 r = load_class_from_classbuffer(cb);
347 FREE(cb, classbuffer);
349 #if defined(ENABLE_STATISTICS)
352 if (opt_getloadingtime)
357 /* If return value is NULL, we had a problem and the class is
358 not loaded. Now free the allocated memory, otherwise we
359 could run into a DOS. */
366 /* Store the newly defined class in the class cache. This call
367 also checks whether a class of the same name has already been
368 defined by the same defining loader, and if so, replaces the
369 newly created class by the one defined earlier. */
371 /* Important: The classinfo given to classcache_store must be
372 fully prepared because another thread may return
373 this pointer after the lookup at to top of this
374 function directly after the class cache lock has
377 c = classcache_store(cl, c, true);
383 /* class_load_attribute_sourcefile *********************************************
385 SourceFile_attribute {
386 u2 attribute_name_index;
391 *******************************************************************************/
393 static bool class_load_attribute_sourcefile(classbuffer *cb)
404 /* check buffer size */
406 if (!suck_check_classbuffer_size(cb, 4 + 2))
409 /* check attribute length */
411 attribute_length = suck_u4(cb);
413 if (attribute_length != 2) {
414 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
418 /* there can be no more than one SourceFile attribute */
420 if (c->sourcefile != NULL) {
421 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
427 sourcefile_index = suck_u2(cb);
428 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
430 if (sourcefile == NULL)
433 /* store sourcefile */
435 c->sourcefile = sourcefile;
441 /* class_load_attribute_enclosingmethod ****************************************
443 EnclosingMethod_attribute {
444 u2 attribute_name_index;
450 *******************************************************************************/
452 #if defined(ENABLE_JAVASE)
453 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
459 classref_or_classinfo cr;
460 constant_nameandtype *cn;
466 /* check buffer size */
468 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
471 /* check attribute length */
473 attribute_length = suck_u4(cb);
475 if (attribute_length != 4) {
476 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
480 /* there can be no more than one EnclosingMethod attribute */
482 if (c->enclosingmethod != NULL) {
483 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
487 /* get class index */
489 class_index = suck_u2(cb);
490 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
492 /* get method index */
494 method_index = suck_u2(cb);
495 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
497 /* store info in classinfo */
499 c->enclosingclass.any = cr.any;
500 c->enclosingmethod = cn;
504 #endif /* defined(ENABLE_JAVASE) */
507 /* class_load_attributes *******************************************************
509 Read attributes from ClassFile.
512 u2 attribute_name_index;
514 u1 info[attribute_length];
517 InnerClasses_attribute {
518 u2 attribute_name_index;
522 *******************************************************************************/
524 bool class_load_attributes(classbuffer *cb)
529 u2 attribute_name_index;
534 /* get attributes count */
536 if (!suck_check_classbuffer_size(cb, 2))
539 attributes_count = suck_u2(cb);
541 for (i = 0; i < attributes_count; i++) {
542 /* get attribute name */
544 if (!suck_check_classbuffer_size(cb, 2))
547 attribute_name_index = suck_u2(cb);
549 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
551 if (attribute_name == NULL)
554 if (attribute_name == utf_InnerClasses) {
557 if (c->innerclass != NULL) {
558 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
562 if (!suck_check_classbuffer_size(cb, 4 + 2))
565 /* skip attribute length */
568 /* number of records */
569 c->innerclasscount = suck_u2(cb);
571 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
574 /* allocate memory for innerclass structure */
575 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
577 for (j = 0; j < c->innerclasscount; j++) {
578 /* The innerclass structure contains a class with an encoded
579 name, its defining scope, its simple name and a bitmask of
580 the access flags. If an inner class is not a member, its
581 outer_class is NULL, if a class is anonymous, its name is
584 innerclassinfo *info = c->innerclass + j;
586 info->inner_class.ref =
587 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
588 info->outer_class.ref =
589 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
591 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
592 info->flags = suck_u2(cb);
595 else if (attribute_name == utf_SourceFile) {
598 if (!class_load_attribute_sourcefile(cb))
601 #if defined(ENABLE_JAVASE)
602 else if (attribute_name == utf_EnclosingMethod) {
603 /* EnclosingMethod */
605 if (!class_load_attribute_enclosingmethod(cb))
608 else if (attribute_name == utf_Signature) {
611 if (!loader_load_attribute_signature(cb, &(c->signature)))
616 #if defined(ENABLE_ANNOTATIONS)
617 /* XXX We can't do a release with that enabled */
619 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
620 /* RuntimeVisibleAnnotations */
621 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
624 /* XXX RuntimeInvisibleAnnotations should only be loaded
625 * (or returned to Java) if some commandline options says so.
626 * Currently there is no such option available in cacao,
627 * therefore I load them allways (for testing purpose).
628 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
629 * generated if you tell javac to do so. So in most cases
630 * there won't be any.
632 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
633 /* RuntimeInvisibleAnnotations */
634 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
640 /* unknown attribute */
642 if (!loader_skip_attribute_body(cb))
651 /* class_freepool **************************************************************
653 Frees all resources used by this classes Constant Pool.
655 *******************************************************************************/
657 static void class_freecpool(classinfo *c)
663 if (c->cptags && c->cpinfos) {
664 for (idx = 0; idx < c->cpcount; idx++) {
665 tag = c->cptags[idx];
666 info = c->cpinfos[idx];
670 case CONSTANT_Fieldref:
671 case CONSTANT_Methodref:
672 case CONSTANT_InterfaceMethodref:
673 FREE(info, constant_FMIref);
675 case CONSTANT_Integer:
676 FREE(info, constant_integer);
679 FREE(info, constant_float);
682 FREE(info, constant_long);
684 case CONSTANT_Double:
685 FREE(info, constant_double);
687 case CONSTANT_NameAndType:
688 FREE(info, constant_nameandtype);
696 MFREE(c->cptags, u1, c->cpcount);
699 MFREE(c->cpinfos, voidptr, c->cpcount);
703 /* class_getconstant ***********************************************************
705 Retrieves the value at position 'pos' of the constantpool of a
706 class. If the type of the value is other than 'ctype', an error is
709 *******************************************************************************/
711 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
713 /* check index and type of constantpool entry */
714 /* (pos == 0 is caught by type comparison) */
716 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
717 exceptions_throw_classformaterror(c, "Illegal constant pool index");
721 return c->cpinfos[pos];
725 /* innerclass_getconstant ******************************************************
727 Like class_getconstant, but if cptags is ZERO, null is returned.
729 *******************************************************************************/
731 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
733 /* invalid position in constantpool */
735 if (pos >= c->cpcount) {
736 exceptions_throw_classformaterror(c, "Illegal constant pool index");
740 /* constantpool entry of type 0 */
742 if (c->cptags[pos] == 0)
745 /* check type of constantpool entry */
747 if (c->cptags[pos] != ctype) {
748 exceptions_throw_classformaterror(c, "Illegal constant pool index");
752 return c->cpinfos[pos];
756 /* class_free ******************************************************************
758 Frees all resources used by the class.
760 *******************************************************************************/
762 void class_free(classinfo *c)
770 MFREE(c->interfaces, classinfo*, c->interfacescount);
773 for (i = 0; i < c->fieldscount; i++)
774 field_free(&(c->fields[i]));
775 #if defined(ENABLE_CACAO_GC)
776 MFREE(c->fields, fieldinfo, c->fieldscount);
781 for (i = 0; i < c->methodscount; i++)
782 method_free(&(c->methods[i]));
783 MFREE(c->methods, methodinfo, c->methodscount);
786 if ((v = c->vftbl) != NULL) {
788 mem_free(v->arraydesc,sizeof(arraydescriptor));
790 for (i = 0; i < v->interfacetablelength; i++) {
791 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
793 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
795 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
796 sizeof(methodptr*) * (v->interfacetablelength -
797 (v->interfacetablelength > 0));
798 v = (vftbl_t*) (((methodptr*) v) -
799 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
804 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
806 /* if (c->classvftbl)
807 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
811 #if defined(ENABLE_ANNOTATIONS)
812 annotation_bytearray_free(c->annotations);
814 annotation_bytearrays_free(c->method_annotations);
815 annotation_bytearrays_free(c->method_parameterannotations);
816 annotation_bytearrays_free(c->method_annotationdefaults);
818 annotation_bytearrays_free(c->field_annotations);
823 /* get_array_class *************************************************************
825 Returns the array class with the given name for the given
826 classloader, or NULL if an exception occurred.
828 Note: This function does eager loading.
830 *******************************************************************************/
832 static classinfo *get_array_class(utf *name,classloader *initloader,
833 classloader *defloader,bool link)
837 /* lookup this class in the classcache */
838 c = classcache_lookup(initloader,name);
840 c = classcache_lookup_defined(defloader,name);
843 /* we have to create it */
844 c = class_create_classinfo(name);
845 c = load_newly_created_array(c,initloader);
851 assert(c->state & CLASS_LOADED);
852 assert(c->classloader == defloader);
854 if (link && !(c->state & CLASS_LINKED))
858 assert(!link || (c->state & CLASS_LINKED));
864 /* class_array_of **************************************************************
866 Returns an array class with the given component class. The array
867 class is dynamically created if neccessary.
869 *******************************************************************************/
871 classinfo *class_array_of(classinfo *component, bool link)
880 cl = component->classloader;
882 dumpsize = dump_size();
884 /* Assemble the array class name */
885 namelen = component->name->blength;
887 if (component->name->text[0] == '[') {
888 /* the component is itself an array */
889 namebuf = DMNEW(char, namelen + 1);
891 MCOPY(namebuf + 1, component->name->text, char, namelen);
895 /* the component is a non-array class */
896 namebuf = DMNEW(char, namelen + 3);
899 MCOPY(namebuf + 2, component->name->text, char, namelen);
900 namebuf[2 + namelen] = ';';
904 u = utf_new(namebuf, namelen);
906 c = get_array_class(u, cl, cl, link);
908 dump_release(dumpsize);
914 /* class_multiarray_of *********************************************************
916 Returns an array class with the given dimension and element class.
917 The array class is dynamically created if neccessary.
919 *******************************************************************************/
921 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
928 dumpsize = dump_size();
931 log_text("Invalid array dimension requested");
935 /* Assemble the array class name */
936 namelen = element->name->blength;
938 if (element->name->text[0] == '[') {
939 /* the element is itself an array */
940 namebuf = DMNEW(char, namelen + dim);
941 memcpy(namebuf + dim, element->name->text, namelen);
945 /* the element is a non-array class */
946 namebuf = DMNEW(char, namelen + 2 + dim);
948 memcpy(namebuf + dim + 1, element->name->text, namelen);
949 namelen += (2 + dim);
950 namebuf[namelen - 1] = ';';
952 memset(namebuf, '[', dim);
954 c = get_array_class(utf_new(namebuf, namelen),
955 element->classloader,
956 element->classloader,
959 dump_release(dumpsize);
965 /* class_lookup_classref *******************************************************
967 Looks up the constant_classref for a given classname in the classref
971 cls..............the class containing the reference
972 name.............the name of the class refered to
975 a pointer to a constant_classref, or
976 NULL if the reference was not found
978 *******************************************************************************/
980 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
982 constant_classref *ref;
983 extra_classref *xref;
988 assert(!cls->classrefcount || cls->classrefs);
990 /* first search the main classref table */
991 count = cls->classrefcount;
992 ref = cls->classrefs;
993 for (; count; --count, ++ref)
994 if (ref->name == name)
997 /* next try the list of extra classrefs */
998 for (xref = cls->extclassrefs; xref; xref = xref->next) {
999 if (xref->classref.name == name)
1000 return &(xref->classref);
1008 /* class_get_classref **********************************************************
1010 Returns the constant_classref for a given classname.
1013 cls..............the class containing the reference
1014 name.............the name of the class refered to
1017 a pointer to a constant_classref (never NULL)
1020 The given name is not checked for validity!
1022 *******************************************************************************/
1024 constant_classref *class_get_classref(classinfo *cls, utf *name)
1026 constant_classref *ref;
1027 extra_classref *xref;
1032 ref = class_lookup_classref(cls,name);
1036 xref = NEW(extra_classref);
1037 CLASSREF_INIT(xref->classref,cls,name);
1039 xref->next = cls->extclassrefs;
1040 cls->extclassrefs = xref;
1042 return &(xref->classref);
1046 /* class_get_self_classref *****************************************************
1048 Returns the constant_classref to the class itself.
1051 cls..............the class containing the reference
1054 a pointer to a constant_classref (never NULL)
1056 *******************************************************************************/
1058 constant_classref *class_get_self_classref(classinfo *cls)
1060 /* XXX this should be done in a faster way. Maybe always make */
1061 /* the classref of index 0 a self reference. */
1062 return class_get_classref(cls,cls->name);
1065 /* class_get_classref_multiarray_of ********************************************
1067 Returns an array type reference with the given dimension and element class
1071 dim..............the requested dimension
1072 dim must be in [1;255]. This is NOT checked!
1073 ref..............the component class reference
1076 a pointer to the class reference for the array type
1079 The referer of `ref` is used as the referer for the new classref.
1081 *******************************************************************************/
1083 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1088 constant_classref *cr;
1091 assert(dim >= 1 && dim <= 255);
1093 dumpsize = dump_size();
1095 /* Assemble the array class name */
1096 namelen = ref->name->blength;
1098 if (ref->name->text[0] == '[') {
1099 /* the element is itself an array */
1100 namebuf = DMNEW(char, namelen + dim);
1101 memcpy(namebuf + dim, ref->name->text, namelen);
1105 /* the element is a non-array class */
1106 namebuf = DMNEW(char, namelen + 2 + dim);
1108 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1109 namelen += (2 + dim);
1110 namebuf[namelen - 1] = ';';
1112 memset(namebuf, '[', dim);
1114 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1116 dump_release(dumpsize);
1122 /* class_get_classref_component_of *********************************************
1124 Returns the component classref of a given array type reference
1127 ref..............the array type reference
1130 a reference to the component class, or
1131 NULL if `ref` is not an object array type reference
1134 The referer of `ref` is used as the referer for the new classref.
1136 *******************************************************************************/
1138 constant_classref *class_get_classref_component_of(constant_classref *ref)
1145 name = ref->name->text;
1149 namelen = ref->name->blength - 1;
1154 else if (*name != '[') {
1158 return class_get_classref(ref->referer, utf_new(name, namelen));
1162 /* class_findmethod ************************************************************
1164 Searches a 'classinfo' structure for a method having the given name
1165 and descriptor. If descriptor is NULL, it is ignored.
1167 *******************************************************************************/
1169 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1174 for (i = 0; i < c->methodscount; i++) {
1175 m = &(c->methods[i]);
1177 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1185 /* class_resolvemethod *********************************************************
1187 Searches a class and it's super classes for a method.
1189 Superinterfaces are *not* searched.
1191 *******************************************************************************/
1193 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1198 m = class_findmethod(c, name, desc);
1203 /* JVM Specification bug:
1205 It is important NOT to resolve special <init> and <clinit>
1206 methods to super classes or interfaces; yet, this is not
1207 explicited in the specification. Section 5.4.3.3 should be
1208 updated appropriately. */
1210 if (name == utf_init || name == utf_clinit)
1220 /* class_resolveinterfacemethod_intern *****************************************
1222 Internally used helper function. Do not use this directly.
1224 *******************************************************************************/
1226 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1227 utf *name, utf *desc)
1232 /* try to find the method in the class */
1234 m = class_findmethod(c, name, desc);
1239 /* no method found? try the superinterfaces */
1241 for (i = 0; i < c->interfacescount; i++) {
1242 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1249 /* no method found */
1255 /* class_resolveclassmethod ****************************************************
1257 Resolves a reference from REFERER to a method with NAME and DESC in
1260 If the method cannot be resolved the return value is NULL. If
1261 EXCEPT is true *exceptionptr is set, too.
1263 *******************************************************************************/
1265 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1266 classinfo *referer, bool throwexception)
1272 /* if (c->flags & ACC_INTERFACE) { */
1273 /* if (throwexception) */
1274 /* *exceptionptr = */
1275 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1279 /* try class c and its superclasses */
1283 m = class_resolvemethod(cls, name, desc);
1288 /* try the superinterfaces */
1290 for (i = 0; i < c->interfacescount; i++) {
1291 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1299 exceptions_throw_nosuchmethoderror(c, name, desc);
1304 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1306 exceptions_throw_abstractmethoderror();
1311 /* XXX check access rights */
1317 /* class_resolveinterfacemethod ************************************************
1319 Resolves a reference from REFERER to a method with NAME and DESC in
1322 If the method cannot be resolved the return value is NULL. If
1323 EXCEPT is true *exceptionptr is set, too.
1325 *******************************************************************************/
1327 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1328 classinfo *referer, bool throwexception)
1332 if (!(c->flags & ACC_INTERFACE)) {
1334 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1339 mi = class_resolveinterfacemethod_intern(c, name, desc);
1344 /* try class java.lang.Object */
1346 mi = class_findmethod(class_java_lang_Object, name, desc);
1352 exceptions_throw_nosuchmethoderror(c, name, desc);
1358 /* class_findfield *************************************************************
1360 Searches for field with specified name and type in a classinfo
1361 structure. If no such field is found NULL is returned.
1363 *******************************************************************************/
1365 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1369 for (i = 0; i < c->fieldscount; i++)
1370 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1371 return &(c->fields[i]);
1374 return class_findfield(c->super.cls, name, desc);
1380 /* class_findfield_approx ******************************************************
1382 Searches in 'classinfo'-structure for a field with the specified
1385 *******************************************************************************/
1387 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1391 /* get field index */
1393 i = class_findfield_index_by_name(c, name);
1395 /* field was not found, return */
1400 /* return field address */
1402 return &(c->fields[i]);
1406 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1410 for (i = 0; i < c->fieldscount; i++) {
1411 /* compare field names */
1413 if ((c->fields[i].name == name))
1417 /* field was not found, raise exception */
1419 exceptions_throw_nosuchfielderror(c, name);
1425 /****************** Function: class_resolvefield_int ***************************
1427 This is an internally used helper function. Do not use this directly.
1429 Tries to resolve a field having the given name and type.
1430 If the field cannot be resolved, NULL is returned.
1432 *******************************************************************************/
1434 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1439 /* search for field in class c */
1441 for (i = 0; i < c->fieldscount; i++) {
1442 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1443 return &(c->fields[i]);
1447 /* try superinterfaces recursively */
1449 for (i = 0; i < c->interfacescount; i++) {
1450 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1455 /* try superclass */
1458 return class_resolvefield_int(c->super.cls, name, desc);
1466 /********************* Function: class_resolvefield ***************************
1468 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1470 If the field cannot be resolved the return value is NULL. If EXCEPT is
1471 true *exceptionptr is set, too.
1473 *******************************************************************************/
1475 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1476 classinfo *referer, bool throwexception)
1480 fi = class_resolvefield_int(c, name, desc);
1484 exceptions_throw_nosuchfielderror(c, name);
1489 /* XXX check access rights */
1495 /* class_resolve_superclass ****************************************************
1497 Resolves the super class reference of the given class if necessary.
1499 *******************************************************************************/
1501 static classinfo *class_resolve_superclass(classinfo *c)
1505 if (c->super.any == NULL)
1508 /* Do we have a super class reference or is it already
1511 if (IS_CLASSREF(c->super)) {
1512 super = resolve_classref_or_classinfo_eager(c->super, true);
1517 /* Store the resolved super class in the class structure. */
1519 c->super.cls = super;
1522 return c->super.cls;
1526 /* class_issubclass ************************************************************
1528 Checks if sub is a descendant of super.
1530 *******************************************************************************/
1532 bool class_issubclass(classinfo *sub, classinfo *super)
1541 sub = class_resolve_superclass(sub);
1546 /* class_isanysubclass *********************************************************
1548 Checks a subclass relation between two classes. Implemented
1549 interfaces are interpreted as super classes.
1551 Return value: 1 ... sub is subclass of super
1554 *******************************************************************************/
1556 bool class_isanysubclass(classinfo *sub, classinfo *super)
1561 /* This is the trivial case. */
1566 /* Primitive classes are only subclasses of themselves. */
1568 if (class_is_primitive(sub) || class_is_primitive(super))
1571 /* Check for interfaces. */
1573 if (super->flags & ACC_INTERFACE) {
1574 result = (sub->vftbl->interfacetablelength > super->index) &&
1575 (sub->vftbl->interfacetable[-super->index] != NULL);
1578 /* java.lang.Object is the only super class of any
1581 if (sub->flags & ACC_INTERFACE)
1582 return (super == class_java_lang_Object);
1584 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1586 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1587 result = diffval <= (uint32_t) super->vftbl->diffval;
1589 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1596 /* class_is_primitive **********************************************************
1598 Checks if the given class is a primitive class.
1600 *******************************************************************************/
1602 bool class_is_primitive(classinfo *c)
1604 if (c->flags & ACC_CLASS_PRIMITIVE)
1611 /* class_is_array **************************************************************
1613 Checks if the given class is an array class.
1615 *******************************************************************************/
1617 bool class_is_array(classinfo *c)
1619 if (!(c->state & CLASS_LINKED))
1623 return (c->vftbl->arraydesc != NULL);
1627 /* class_is_interface **********************************************************
1629 Checks if the given class is an interface.
1631 *******************************************************************************/
1633 bool class_is_interface(classinfo *c)
1635 if (c->flags & ACC_INTERFACE)
1642 /* class_get_superclass ********************************************************
1644 Return the super class of the given class. If the super-field is a
1645 class-reference, resolve it and store it in the classinfo.
1647 *******************************************************************************/
1649 classinfo *class_get_superclass(classinfo *c)
1653 /* For java.lang.Object, primitive and Void classes we return
1656 if (c->super.any == NULL)
1659 /* For interfaces we also return NULL. */
1661 if (c->flags & ACC_INTERFACE)
1664 /* We may have to resolve the super class reference. */
1666 super = class_resolve_superclass(c);
1672 /* class_get_declaredclasses ***************************************************
1674 Return an array of declared classes of the given class.
1676 *******************************************************************************/
1678 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1680 classref_or_classinfo inner;
1681 classref_or_classinfo outer;
1683 int declaredclasscount; /* number of declared classes */
1684 int pos; /* current declared class */
1685 java_handle_objectarray_t *oa; /* array of declared classes */
1689 declaredclasscount = 0;
1691 if (!class_is_primitive(c) && !class_is_array(c)) {
1692 /* Determine number of declared classes. */
1694 for (i = 0; i < c->innerclasscount; i++) {
1695 outer = c->innerclass[i].outer_class;
1697 /* Check if outer_class is a classref or a real class and
1698 get the class name from the structure. */
1700 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1702 /* Outer class is this class. */
1704 if ((outername == c->name) &&
1705 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1706 declaredclasscount++;
1710 /* Allocate Class[] and check for OOM. */
1712 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1717 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1718 inner = c->innerclass[i].inner_class;
1719 outer = c->innerclass[i].outer_class;
1721 /* Check if outer_class is a classref or a real class and get
1722 the class name from the structure. */
1724 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1726 /* Outer class is this class. */
1728 if ((outername == c->name) &&
1729 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1731 ic = resolve_classref_or_classinfo_eager(inner, false);
1736 if (!(ic->state & CLASS_LINKED))
1737 if (!link_class(ic))
1740 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1748 /* class_get_declaringclass ****************************************************
1750 If the class or interface given is a member of another class,
1751 return the declaring class. For array and primitive classes return
1754 *******************************************************************************/
1756 classinfo *class_get_declaringclass(classinfo *c)
1758 classref_or_classinfo innercr;
1760 classref_or_classinfo outercr;
1764 /* return NULL for arrayclasses and primitive classes */
1766 if (class_is_primitive(c) || (c->name->text[0] == '['))
1769 /* no innerclasses exist */
1771 if (c->innerclasscount == 0)
1774 for (i = 0; i < c->innerclasscount; i++) {
1775 /* Check if inner_class is a classref or a real class and get
1776 the class name from the structure. */
1778 innercr = c->innerclass[i].inner_class;
1780 innername = IS_CLASSREF(innercr) ?
1781 innercr.ref->name : innercr.cls->name;
1783 /* Is the current innerclass this class? */
1785 if (innername == c->name) {
1786 /* Maybe the outer class is not loaded yet. */
1788 outercr = c->innerclass[i].outer_class;
1790 outer = resolve_classref_or_classinfo_eager(outercr, false);
1795 if (!(outer->state & CLASS_LINKED))
1796 if (!link_class(outer))
1807 /* class_get_interfaces ********************************************************
1809 Return an array of interfaces of the given class.
1811 *******************************************************************************/
1813 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1816 java_handle_objectarray_t *oa;
1819 if (!(c->state & CLASS_LINKED))
1823 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1828 for (i = 0; i < c->interfacescount; i++) {
1829 ic = c->interfaces[i].cls;
1831 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1838 /* class_get_signature *********************************************************
1840 Return the signature of the given class. For array and primitive
1841 classes return NULL.
1843 *******************************************************************************/
1845 #if defined(ENABLE_JAVASE)
1846 utf *class_get_signature(classinfo *c)
1848 /* For array and primitive classes return NULL. */
1850 if (class_is_array(c) || class_is_primitive(c))
1853 return c->signature;
1858 /* class_printflags ************************************************************
1860 Prints flags of a class.
1862 *******************************************************************************/
1864 #if !defined(NDEBUG)
1865 void class_printflags(classinfo *c)
1872 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1873 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1874 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1875 if (c->flags & ACC_STATIC) printf(" STATIC");
1876 if (c->flags & ACC_FINAL) printf(" FINAL");
1877 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1878 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1879 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1880 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1881 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1882 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1887 /* class_print *****************************************************************
1889 Prints classname plus flags.
1891 *******************************************************************************/
1893 #if !defined(NDEBUG)
1894 void class_print(classinfo *c)
1901 utf_display_printable_ascii(c->name);
1902 class_printflags(c);
1907 /* class_classref_print ********************************************************
1909 Prints classname plus referer class.
1911 *******************************************************************************/
1913 #if !defined(NDEBUG)
1914 void class_classref_print(constant_classref *cr)
1921 utf_display_printable_ascii(cr->name);
1924 class_print(cr->referer);
1932 /* class_println ***************************************************************
1934 Prints classname plus flags and new line.
1936 *******************************************************************************/
1938 #if !defined(NDEBUG)
1939 void class_println(classinfo *c)
1947 /* class_classref_println ******************************************************
1949 Prints classname plus referer class and new line.
1951 *******************************************************************************/
1953 #if !defined(NDEBUG)
1954 void class_classref_println(constant_classref *cr)
1956 class_classref_print(cr);
1962 /* class_classref_or_classinfo_print *******************************************
1964 Prints classname plus referer class.
1966 *******************************************************************************/
1968 #if !defined(NDEBUG)
1969 void class_classref_or_classinfo_print(classref_or_classinfo c)
1971 if (c.any == NULL) {
1972 printf("(classref_or_classinfo) NULL");
1976 class_classref_print(c.ref);
1983 /* class_classref_or_classinfo_println *****************************************
1985 Prints classname plus referer class and a newline.
1987 *******************************************************************************/
1989 void class_classref_or_classinfo_println(classref_or_classinfo c)
1991 class_classref_or_classinfo_println(c);
1996 /* class_showconstantpool ******************************************************
1998 Dump the constant pool of the given class to stdout.
2000 *******************************************************************************/
2002 #if !defined(NDEBUG)
2003 void class_showconstantpool (classinfo *c)
2008 printf ("---- dump of constant pool ----\n");
2010 for (i=0; i<c->cpcount; i++) {
2011 printf ("#%d: ", (int) i);
2013 e = c -> cpinfos [i];
2016 switch (c -> cptags [i]) {
2017 case CONSTANT_Class:
2018 printf ("Classreference -> ");
2019 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2021 case CONSTANT_Fieldref:
2022 printf ("Fieldref -> ");
2023 field_fieldref_print((constant_FMIref *) e);
2025 case CONSTANT_Methodref:
2026 printf ("Methodref -> ");
2027 method_methodref_print((constant_FMIref *) e);
2029 case CONSTANT_InterfaceMethodref:
2030 printf ("InterfaceMethod -> ");
2031 method_methodref_print((constant_FMIref *) e);
2033 case CONSTANT_String:
2034 printf ("String -> ");
2035 utf_display_printable_ascii (e);
2037 case CONSTANT_Integer:
2038 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2040 case CONSTANT_Float:
2041 printf ("Float -> %f", ((constant_float*)e) -> value);
2043 case CONSTANT_Double:
2044 printf ("Double -> %f", ((constant_double*)e) -> value);
2048 u8 v = ((constant_long*)e) -> value;
2050 printf ("Long -> %ld", (long int) v);
2052 printf ("Long -> HI: %ld, LO: %ld\n",
2053 (long int) v.high, (long int) v.low);
2057 case CONSTANT_NameAndType:
2059 constant_nameandtype *cnt = e;
2060 printf ("NameAndType: ");
2061 utf_display_printable_ascii (cnt->name);
2063 utf_display_printable_ascii (cnt->descriptor);
2067 printf ("Utf8 -> ");
2068 utf_display_printable_ascii (e);
2071 log_text("Invalid type of ConstantPool-Entry");
2079 #endif /* !defined(NDEBUG) */
2082 /* class_showmethods ***********************************************************
2084 Dump info about the fields and methods of the given class to stdout.
2086 *******************************************************************************/
2088 #if !defined(NDEBUG)
2089 void class_showmethods (classinfo *c)
2093 printf("--------- Fields and Methods ----------------\n");
2095 class_printflags(c);
2099 utf_display_printable_ascii(c->name);
2104 utf_display_printable_ascii(c->super.cls->name);
2108 printf("Index: %d\n", c->index);
2110 printf("Interfaces:\n");
2111 for (i = 0; i < c->interfacescount; i++) {
2113 utf_display_printable_ascii(c->interfaces[i].cls->name);
2114 printf (" (%d)\n", c->interfaces[i].cls->index);
2117 printf("Fields:\n");
2118 for (i = 0; i < c->fieldscount; i++)
2119 field_println(&(c->fields[i]));
2121 printf("Methods:\n");
2122 for (i = 0; i < c->methodscount; i++) {
2123 methodinfo *m = &(c->methods[i]);
2125 if (!(m->flags & ACC_STATIC))
2126 printf("vftblindex: %d ", m->vftblindex);
2131 printf ("Virtual function table:\n");
2132 for (i = 0; i < c->vftbl->vftbllength; i++)
2133 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2135 #endif /* !defined(NDEBUG) */
2139 * These are local overrides for various environment variables in Emacs.
2140 * Please do not remove this and leave it at the end of the file, where
2141 * Emacs will automagically detect them.
2142 * ---------------------------------------------------------------------
2145 * indent-tabs-mode: t
2149 * vim:noexpandtab:sw=4:ts=4: