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 8237 2007-07-27 16:15:29Z twisti $
41 #include "mm/memory.h"
43 #include "threads/lock-common.h"
45 #include "toolbox/logging.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/resolve.h"
51 #include "vm/jit/asmpart.h"
53 #include "vmcore/class.h"
54 #include "vmcore/classcache.h"
55 #include "vmcore/loader.h"
56 #include "vmcore/options.h"
58 #if defined(ENABLE_STATISTICS)
59 # include "vmcore/statistics.h"
62 #include "vmcore/suck.h"
63 #include "vmcore/utf8.h"
66 /* global variables ***********************************************************/
68 /* frequently used classes ****************************************************/
70 /* important system classes */
72 classinfo *class_java_lang_Object;
73 classinfo *class_java_lang_Class;
74 classinfo *class_java_lang_ClassLoader;
75 classinfo *class_java_lang_Cloneable;
76 classinfo *class_java_lang_SecurityManager;
77 classinfo *class_java_lang_String;
78 classinfo *class_java_lang_System;
79 classinfo *class_java_lang_Thread;
80 classinfo *class_java_lang_ThreadGroup;
81 classinfo *class_java_lang_VMSystem;
82 classinfo *class_java_lang_VMThread;
83 classinfo *class_java_io_Serializable;
85 #if defined(WITH_CLASSPATH_SUN)
86 classinfo *class_sun_reflect_MagicAccessorImpl;
89 /* system exception classes required in cacao */
91 classinfo *class_java_lang_Throwable;
92 classinfo *class_java_lang_Error;
93 classinfo *class_java_lang_LinkageError;
94 classinfo *class_java_lang_NoClassDefFoundError;
95 classinfo *class_java_lang_OutOfMemoryError;
96 classinfo *class_java_lang_VirtualMachineError;
98 #if defined(WITH_CLASSPATH_GNU)
99 classinfo *class_java_lang_VMThrowable;
102 classinfo *class_java_lang_Exception;
103 classinfo *class_java_lang_ClassCastException;
104 classinfo *class_java_lang_ClassNotFoundException;
106 #if defined(ENABLE_JAVASE)
107 classinfo *class_java_lang_Void;
109 classinfo *class_java_lang_Boolean;
110 classinfo *class_java_lang_Byte;
111 classinfo *class_java_lang_Character;
112 classinfo *class_java_lang_Short;
113 classinfo *class_java_lang_Integer;
114 classinfo *class_java_lang_Long;
115 classinfo *class_java_lang_Float;
116 classinfo *class_java_lang_Double;
119 /* some runtime exception */
121 classinfo *class_java_lang_NullPointerException;
124 /* some classes which may be used more often */
126 #if defined(ENABLE_JAVASE)
127 classinfo *class_java_lang_StackTraceElement;
128 classinfo *class_java_lang_reflect_Constructor;
129 classinfo *class_java_lang_reflect_Field;
130 classinfo *class_java_lang_reflect_Method;
131 classinfo *class_java_security_PrivilegedAction;
132 classinfo *class_java_util_Vector;
134 classinfo *arrayclass_java_lang_Object;
138 /* pseudo classes for the typechecker */
140 classinfo *pseudo_class_Arraystub;
141 classinfo *pseudo_class_Null;
142 classinfo *pseudo_class_New;
145 /* class_set_packagename *******************************************************
147 Derive the package name from the class name and store it in the struct.
149 *******************************************************************************/
151 void class_set_packagename(classinfo *c)
153 char *p = UTF_END(c->name) - 1;
154 char *start = c->name->text;
156 /* set the package name */
157 /* classes in the unnamed package keep packagename == NULL */
159 if (c->name->text[0] == '[') {
160 /* set packagename of arrays to the element's package */
162 for (; *start == '['; start++);
164 /* skip the 'L' in arrays of references */
168 for (; (p > start) && (*p != '/'); --p);
170 c->packagename = utf_new(start, p - start);
173 for (; (p > start) && (*p != '/'); --p);
175 c->packagename = utf_new(start, p - start);
180 /* class_create_classinfo ******************************************************
182 Create a new classinfo struct. The class name is set to the given utf *,
183 most other fields are initialized to zero.
185 Note: classname may be NULL. In this case a not-yet-named classinfo is
186 created. The name must be filled in later and class_set_packagename
187 must be called after that.
189 *******************************************************************************/
191 classinfo *class_create_classinfo(utf *classname)
195 #if defined(ENABLE_STATISTICS)
197 size_classinfo += sizeof(classinfo);
200 /* we use a safe name for temporarily unnamed classes */
202 if (classname == NULL)
203 classname = utf_not_named_yet;
207 log_message_utf("Creating class: ", classname);
210 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
212 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
213 /*c=NEW(classinfo);*/
216 /* Set the header.vftbl of all loaded classes to the one of
217 java.lang.Class, so Java code can use a class as object. */
219 if (class_java_lang_Class != NULL)
220 if (class_java_lang_Class->vftbl != NULL)
221 c->object.header.vftbl = class_java_lang_Class->vftbl;
223 #if defined(ENABLE_JAVASE)
224 /* check if the class is a reference class and flag it */
226 if (classname == utf_java_lang_ref_SoftReference) {
227 c->flags |= ACC_CLASS_REFERENCE_SOFT;
229 else if (classname == utf_java_lang_ref_WeakReference) {
230 c->flags |= ACC_CLASS_REFERENCE_WEAK;
232 else if (classname == utf_java_lang_ref_PhantomReference) {
233 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
237 if (classname != utf_not_named_yet)
238 class_set_packagename(c);
240 LOCK_INIT_OBJECT_LOCK(&c->object.header);
246 /* class_postset_header_vftbl **************************************************
248 Set the header.vftbl of all classes created before java.lang.Class
249 was linked. This is necessary that Java code can use a class as
252 *******************************************************************************/
254 void class_postset_header_vftbl(void)
258 classcache_name_entry *nmen;
259 classcache_class_entry *clsen;
261 assert(class_java_lang_Class);
263 for (slot = 0; slot < hashtable_classcache.size; slot++) {
264 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
266 for (; nmen; nmen = nmen->hashlink) {
267 /* iterate over all class entries */
269 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
272 /* now set the the vftbl */
274 if (c->object.header.vftbl == NULL)
275 c->object.header.vftbl = class_java_lang_Class->vftbl;
281 /* class_define ****************************************************************
283 Calls the loader and defines a class in the VM.
285 *******************************************************************************/
287 classinfo *class_define(utf *name, java_objectheader *cl, int32_t length, const uint8_t *data)
294 /* check if this class has already been defined */
296 c = classcache_lookup_defined_or_initiated(cl, name);
299 exceptions_throw_linkageerror("duplicate class definition: ", c);
304 /* create a new classinfo struct */
306 c = class_create_classinfo(name);
308 #if defined(ENABLE_STATISTICS)
311 if (opt_getloadingtime)
315 /* build a classbuffer with the given data */
317 cb = NEW(classbuffer);
324 /* preset the defining classloader */
328 /* load the class from this buffer */
330 r = load_class_from_classbuffer(cb);
334 FREE(cb, classbuffer);
336 #if defined(ENABLE_STATISTICS)
339 if (opt_getloadingtime)
344 /* If return value is NULL, we had a problem and the class is
345 not loaded. Now free the allocated memory, otherwise we
346 could run into a DOS. */
353 /* Store the newly defined class in the class cache. This call
354 also checks whether a class of the same name has already been
355 defined by the same defining loader, and if so, replaces the
356 newly created class by the one defined earlier. */
358 /* Important: The classinfo given to classcache_store must be
359 fully prepared because another thread may return
360 this pointer after the lookup at to top of this
361 function directly after the class cache lock has
364 c = classcache_store(cl, c, true);
370 /* class_load_attribute_sourcefile *********************************************
372 SourceFile_attribute {
373 u2 attribute_name_index;
378 *******************************************************************************/
380 static bool class_load_attribute_sourcefile(classbuffer *cb)
391 /* check buffer size */
393 if (!suck_check_classbuffer_size(cb, 4 + 2))
396 /* check attribute length */
398 attribute_length = suck_u4(cb);
400 if (attribute_length != 2) {
401 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
405 /* there can be no more than one SourceFile attribute */
407 if (c->sourcefile != NULL) {
408 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
414 sourcefile_index = suck_u2(cb);
415 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
417 if (sourcefile == NULL)
420 /* store sourcefile */
422 c->sourcefile = sourcefile;
428 /* class_load_attribute_enclosingmethod ****************************************
430 EnclosingMethod_attribute {
431 u2 attribute_name_index;
437 *******************************************************************************/
439 #if defined(ENABLE_JAVASE)
440 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
446 classref_or_classinfo cr;
447 constant_nameandtype *cn;
453 /* check buffer size */
455 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
458 /* check attribute length */
460 attribute_length = suck_u4(cb);
462 if (attribute_length != 4) {
463 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
467 /* there can be no more than one EnclosingMethod attribute */
469 if (c->enclosingmethod != NULL) {
470 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
474 /* get class index */
476 class_index = suck_u2(cb);
477 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
479 /* get method index */
481 method_index = suck_u2(cb);
482 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
484 /* store info in classinfo */
486 c->enclosingclass.any = cr.any;
487 c->enclosingmethod = cn;
491 #endif /* defined(ENABLE_JAVASE) */
494 /* class_load_attributes *******************************************************
496 Read attributes from ClassFile.
499 u2 attribute_name_index;
501 u1 info[attribute_length];
504 InnerClasses_attribute {
505 u2 attribute_name_index;
509 *******************************************************************************/
511 bool class_load_attributes(classbuffer *cb)
516 u2 attribute_name_index;
521 /* get attributes count */
523 if (!suck_check_classbuffer_size(cb, 2))
526 attributes_count = suck_u2(cb);
528 for (i = 0; i < attributes_count; i++) {
529 /* get attribute name */
531 if (!suck_check_classbuffer_size(cb, 2))
534 attribute_name_index = suck_u2(cb);
536 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
538 if (attribute_name == NULL)
541 if (attribute_name == utf_InnerClasses) {
544 if (c->innerclass != NULL) {
545 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
549 if (!suck_check_classbuffer_size(cb, 4 + 2))
552 /* skip attribute length */
555 /* number of records */
556 c->innerclasscount = suck_u2(cb);
558 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
561 /* allocate memory for innerclass structure */
562 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
564 for (j = 0; j < c->innerclasscount; j++) {
565 /* The innerclass structure contains a class with an encoded
566 name, its defining scope, its simple name and a bitmask of
567 the access flags. If an inner class is not a member, its
568 outer_class is NULL, if a class is anonymous, its name is
571 innerclassinfo *info = c->innerclass + j;
573 info->inner_class.ref =
574 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
575 info->outer_class.ref =
576 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
578 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
579 info->flags = suck_u2(cb);
582 else if (attribute_name == utf_SourceFile) {
585 if (!class_load_attribute_sourcefile(cb))
588 #if defined(ENABLE_JAVASE)
589 else if (attribute_name == utf_EnclosingMethod) {
590 /* EnclosingMethod */
592 if (!class_load_attribute_enclosingmethod(cb))
595 else if (attribute_name == utf_Signature) {
598 if (!loader_load_attribute_signature(cb, &(c->signature)))
602 /* XXX We can't do a release with that enabled */
604 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
605 /* RuntimeVisibleAnnotations */
607 if (!annotation_load_attribute_runtimevisibleannotations(cb))
613 /* unknown attribute */
615 if (!loader_skip_attribute_body(cb))
624 /* class_freepool **************************************************************
626 Frees all resources used by this classes Constant Pool.
628 *******************************************************************************/
630 static void class_freecpool(classinfo *c)
636 if (c->cptags && c->cpinfos) {
637 for (idx = 0; idx < c->cpcount; idx++) {
638 tag = c->cptags[idx];
639 info = c->cpinfos[idx];
643 case CONSTANT_Fieldref:
644 case CONSTANT_Methodref:
645 case CONSTANT_InterfaceMethodref:
646 FREE(info, constant_FMIref);
648 case CONSTANT_Integer:
649 FREE(info, constant_integer);
652 FREE(info, constant_float);
655 FREE(info, constant_long);
657 case CONSTANT_Double:
658 FREE(info, constant_double);
660 case CONSTANT_NameAndType:
661 FREE(info, constant_nameandtype);
669 MFREE(c->cptags, u1, c->cpcount);
672 MFREE(c->cpinfos, voidptr, c->cpcount);
676 /* class_getconstant ***********************************************************
678 Retrieves the value at position 'pos' of the constantpool of a
679 class. If the type of the value is other than 'ctype', an error is
682 *******************************************************************************/
684 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
686 /* check index and type of constantpool entry */
687 /* (pos == 0 is caught by type comparison) */
689 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
690 exceptions_throw_classformaterror(c, "Illegal constant pool index");
694 return c->cpinfos[pos];
698 /* innerclass_getconstant ******************************************************
700 Like class_getconstant, but if cptags is ZERO, null is returned.
702 *******************************************************************************/
704 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
706 /* invalid position in constantpool */
708 if (pos >= c->cpcount) {
709 exceptions_throw_classformaterror(c, "Illegal constant pool index");
713 /* constantpool entry of type 0 */
715 if (c->cptags[pos] == 0)
718 /* check type of constantpool entry */
720 if (c->cptags[pos] != ctype) {
721 exceptions_throw_classformaterror(c, "Illegal constant pool index");
725 return c->cpinfos[pos];
729 /* class_free ******************************************************************
731 Frees all resources used by the class.
733 *******************************************************************************/
735 void class_free(classinfo *c)
743 MFREE(c->interfaces, classinfo*, c->interfacescount);
746 for (i = 0; i < c->fieldscount; i++)
747 field_free(&(c->fields[i]));
748 #if defined(ENABLE_CACAO_GC)
749 MFREE(c->fields, fieldinfo, c->fieldscount);
754 for (i = 0; i < c->methodscount; i++)
755 method_free(&(c->methods[i]));
756 MFREE(c->methods, methodinfo, c->methodscount);
759 if ((v = c->vftbl) != NULL) {
761 mem_free(v->arraydesc,sizeof(arraydescriptor));
763 for (i = 0; i < v->interfacetablelength; i++) {
764 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
766 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
768 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
769 sizeof(methodptr*) * (v->interfacetablelength -
770 (v->interfacetablelength > 0));
771 v = (vftbl_t*) (((methodptr*) v) -
772 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
777 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
779 /* if (c->classvftbl)
780 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
786 /* get_array_class *************************************************************
788 Returns the array class with the given name for the given
789 classloader, or NULL if an exception occurred.
791 Note: This function does eager loading.
793 *******************************************************************************/
795 static classinfo *get_array_class(utf *name,java_objectheader *initloader,
796 java_objectheader *defloader,bool link)
800 /* lookup this class in the classcache */
801 c = classcache_lookup(initloader,name);
803 c = classcache_lookup_defined(defloader,name);
806 /* we have to create it */
807 c = class_create_classinfo(name);
808 c = load_newly_created_array(c,initloader);
814 assert(c->state & CLASS_LOADED);
815 assert(c->classloader == defloader);
817 if (link && !(c->state & CLASS_LINKED))
821 assert(!link || (c->state & CLASS_LINKED));
827 /* class_array_of **************************************************************
829 Returns an array class with the given component class. The array
830 class is dynamically created if neccessary.
832 *******************************************************************************/
834 classinfo *class_array_of(classinfo *component, bool link)
836 java_objectheader *cl;
843 cl = component->classloader;
845 dumpsize = dump_size();
847 /* Assemble the array class name */
848 namelen = component->name->blength;
850 if (component->name->text[0] == '[') {
851 /* the component is itself an array */
852 namebuf = DMNEW(char, namelen + 1);
854 MCOPY(namebuf + 1, component->name->text, char, namelen);
858 /* the component is a non-array class */
859 namebuf = DMNEW(char, namelen + 3);
862 MCOPY(namebuf + 2, component->name->text, char, namelen);
863 namebuf[2 + namelen] = ';';
867 u = utf_new(namebuf, namelen);
869 c = get_array_class(u, cl, cl, link);
871 dump_release(dumpsize);
877 /* class_multiarray_of *********************************************************
879 Returns an array class with the given dimension and element class.
880 The array class is dynamically created if neccessary.
882 *******************************************************************************/
884 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
891 dumpsize = dump_size();
894 log_text("Invalid array dimension requested");
898 /* Assemble the array class name */
899 namelen = element->name->blength;
901 if (element->name->text[0] == '[') {
902 /* the element is itself an array */
903 namebuf = DMNEW(char, namelen + dim);
904 memcpy(namebuf + dim, element->name->text, namelen);
908 /* the element is a non-array class */
909 namebuf = DMNEW(char, namelen + 2 + dim);
911 memcpy(namebuf + dim + 1, element->name->text, namelen);
912 namelen += (2 + dim);
913 namebuf[namelen - 1] = ';';
915 memset(namebuf, '[', dim);
917 c = get_array_class(utf_new(namebuf, namelen),
918 element->classloader,
919 element->classloader,
922 dump_release(dumpsize);
928 /* class_lookup_classref *******************************************************
930 Looks up the constant_classref for a given classname in the classref
934 cls..............the class containing the reference
935 name.............the name of the class refered to
938 a pointer to a constant_classref, or
939 NULL if the reference was not found
941 *******************************************************************************/
943 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
945 constant_classref *ref;
946 extra_classref *xref;
951 assert(!cls->classrefcount || cls->classrefs);
953 /* first search the main classref table */
954 count = cls->classrefcount;
955 ref = cls->classrefs;
956 for (; count; --count, ++ref)
957 if (ref->name == name)
960 /* next try the list of extra classrefs */
961 for (xref = cls->extclassrefs; xref; xref = xref->next) {
962 if (xref->classref.name == name)
963 return &(xref->classref);
971 /* class_get_classref **********************************************************
973 Returns the constant_classref for a given classname.
976 cls..............the class containing the reference
977 name.............the name of the class refered to
980 a pointer to a constant_classref (never NULL)
983 The given name is not checked for validity!
985 *******************************************************************************/
987 constant_classref *class_get_classref(classinfo *cls, utf *name)
989 constant_classref *ref;
990 extra_classref *xref;
995 ref = class_lookup_classref(cls,name);
999 xref = NEW(extra_classref);
1000 CLASSREF_INIT(xref->classref,cls,name);
1002 xref->next = cls->extclassrefs;
1003 cls->extclassrefs = xref;
1005 return &(xref->classref);
1009 /* class_get_self_classref *****************************************************
1011 Returns the constant_classref to the class itself.
1014 cls..............the class containing the reference
1017 a pointer to a constant_classref (never NULL)
1019 *******************************************************************************/
1021 constant_classref *class_get_self_classref(classinfo *cls)
1023 /* XXX this should be done in a faster way. Maybe always make */
1024 /* the classref of index 0 a self reference. */
1025 return class_get_classref(cls,cls->name);
1028 /* class_get_classref_multiarray_of ********************************************
1030 Returns an array type reference with the given dimension and element class
1034 dim..............the requested dimension
1035 dim must be in [1;255]. This is NOT checked!
1036 ref..............the component class reference
1039 a pointer to the class reference for the array type
1042 The referer of `ref` is used as the referer for the new classref.
1044 *******************************************************************************/
1046 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1051 constant_classref *cr;
1054 assert(dim >= 1 && dim <= 255);
1056 dumpsize = dump_size();
1058 /* Assemble the array class name */
1059 namelen = ref->name->blength;
1061 if (ref->name->text[0] == '[') {
1062 /* the element is itself an array */
1063 namebuf = DMNEW(char, namelen + dim);
1064 memcpy(namebuf + dim, ref->name->text, namelen);
1068 /* the element is a non-array class */
1069 namebuf = DMNEW(char, namelen + 2 + dim);
1071 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1072 namelen += (2 + dim);
1073 namebuf[namelen - 1] = ';';
1075 memset(namebuf, '[', dim);
1077 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1079 dump_release(dumpsize);
1085 /* class_get_classref_component_of *********************************************
1087 Returns the component classref of a given array type reference
1090 ref..............the array type reference
1093 a reference to the component class, or
1094 NULL if `ref` is not an object array type reference
1097 The referer of `ref` is used as the referer for the new classref.
1099 *******************************************************************************/
1101 constant_classref *class_get_classref_component_of(constant_classref *ref)
1108 name = ref->name->text;
1112 namelen = ref->name->blength - 1;
1117 else if (*name != '[') {
1121 return class_get_classref(ref->referer, utf_new(name, namelen));
1125 /* class_findmethod ************************************************************
1127 Searches a 'classinfo' structure for a method having the given name
1128 and descriptor. If descriptor is NULL, it is ignored.
1130 *******************************************************************************/
1132 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1137 for (i = 0; i < c->methodscount; i++) {
1138 m = &(c->methods[i]);
1140 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1148 /* class_resolvemethod *********************************************************
1150 Searches a class and it's super classes for a method.
1152 Superinterfaces are *not* searched.
1154 *******************************************************************************/
1156 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1161 m = class_findmethod(c, name, desc);
1166 /* JVM Specification bug:
1168 It is important NOT to resolve special <init> and <clinit>
1169 methods to super classes or interfaces; yet, this is not
1170 explicited in the specification. Section 5.4.3.3 should be
1171 updated appropriately. */
1173 if (name == utf_init || name == utf_clinit)
1183 /* class_resolveinterfacemethod_intern *****************************************
1185 Internally used helper function. Do not use this directly.
1187 *******************************************************************************/
1189 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1190 utf *name, utf *desc)
1195 /* try to find the method in the class */
1197 m = class_findmethod(c, name, desc);
1202 /* no method found? try the superinterfaces */
1204 for (i = 0; i < c->interfacescount; i++) {
1205 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1212 /* no method found */
1218 /* class_resolveclassmethod ****************************************************
1220 Resolves a reference from REFERER to a method with NAME and DESC in
1223 If the method cannot be resolved the return value is NULL. If
1224 EXCEPT is true *exceptionptr is set, too.
1226 *******************************************************************************/
1228 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1229 classinfo *referer, bool throwexception)
1235 /* if (c->flags & ACC_INTERFACE) { */
1236 /* if (throwexception) */
1237 /* *exceptionptr = */
1238 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1242 /* try class c and its superclasses */
1246 m = class_resolvemethod(cls, name, desc);
1251 /* try the superinterfaces */
1253 for (i = 0; i < c->interfacescount; i++) {
1254 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1262 exceptions_throw_nosuchmethoderror(c, name, desc);
1267 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1269 exceptions_throw_abstractmethoderror();
1274 /* XXX check access rights */
1280 /* class_resolveinterfacemethod ************************************************
1282 Resolves a reference from REFERER to a method with NAME and DESC in
1285 If the method cannot be resolved the return value is NULL. If
1286 EXCEPT is true *exceptionptr is set, too.
1288 *******************************************************************************/
1290 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1291 classinfo *referer, bool throwexception)
1295 if (!(c->flags & ACC_INTERFACE)) {
1297 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1302 mi = class_resolveinterfacemethod_intern(c, name, desc);
1307 /* try class java.lang.Object */
1309 mi = class_findmethod(class_java_lang_Object, name, desc);
1315 exceptions_throw_nosuchmethoderror(c, name, desc);
1321 /* class_findfield *************************************************************
1323 Searches for field with specified name and type in a classinfo
1324 structure. If no such field is found NULL is returned.
1326 *******************************************************************************/
1328 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1332 for (i = 0; i < c->fieldscount; i++)
1333 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1334 return &(c->fields[i]);
1337 return class_findfield(c->super.cls, name, desc);
1343 /* class_findfield_approx ******************************************************
1345 Searches in 'classinfo'-structure for a field with the specified
1348 *******************************************************************************/
1350 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1354 /* get field index */
1356 i = class_findfield_index_by_name(c, name);
1358 /* field was not found, return */
1363 /* return field address */
1365 return &(c->fields[i]);
1369 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1373 for (i = 0; i < c->fieldscount; i++) {
1374 /* compare field names */
1376 if ((c->fields[i].name == name))
1380 /* field was not found, raise exception */
1382 exceptions_throw_nosuchfielderror(c, name);
1388 /****************** Function: class_resolvefield_int ***************************
1390 This is an internally used helper function. Do not use this directly.
1392 Tries to resolve a field having the given name and type.
1393 If the field cannot be resolved, NULL is returned.
1395 *******************************************************************************/
1397 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1402 /* search for field in class c */
1404 for (i = 0; i < c->fieldscount; i++) {
1405 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1406 return &(c->fields[i]);
1410 /* try superinterfaces recursively */
1412 for (i = 0; i < c->interfacescount; i++) {
1413 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1418 /* try superclass */
1421 return class_resolvefield_int(c->super.cls, name, desc);
1429 /********************* Function: class_resolvefield ***************************
1431 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1433 If the field cannot be resolved the return value is NULL. If EXCEPT is
1434 true *exceptionptr is set, too.
1436 *******************************************************************************/
1438 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1439 classinfo *referer, bool throwexception)
1443 fi = class_resolvefield_int(c, name, desc);
1447 exceptions_throw_nosuchfielderror(c, name);
1452 /* XXX check access rights */
1458 /* class_issubclass ************************************************************
1460 Checks if sub is a descendant of super.
1462 *******************************************************************************/
1464 bool class_issubclass(classinfo *sub, classinfo *super)
1466 for (; sub != NULL; sub = class_get_superclass(sub))
1474 /* class_isanysubclass *********************************************************
1476 Checks a subclass relation between two classes. Implemented
1477 interfaces are interpreted as super classes.
1479 Return value: 1 ... sub is subclass of super
1482 *******************************************************************************/
1484 bool class_isanysubclass(classinfo *sub, classinfo *super)
1486 castinfo classvalues;
1490 /* This is the trivial case. */
1495 /* Primitive classes are only subclasses of themselves. */
1497 if (class_is_primitive(sub) || class_is_primitive(super))
1500 /* Check for interfaces. */
1502 if (super->flags & ACC_INTERFACE) {
1503 result = (sub->vftbl->interfacetablelength > super->index) &&
1504 (sub->vftbl->interfacetable[-super->index] != NULL);
1507 /* java.lang.Object is the only super class of any
1510 if (sub->flags & ACC_INTERFACE)
1511 return (super == class_java_lang_Object);
1513 ASM_GETCLASSVALUES_ATOMIC(super->vftbl, sub->vftbl, &classvalues);
1515 diffval = classvalues.sub_baseval - classvalues.super_baseval;
1516 result = diffval <= (u4) classvalues.super_diffval;
1523 /* class_is_primitive **********************************************************
1525 Checks if the given class is a primitive class.
1527 *******************************************************************************/
1529 bool class_is_primitive(classinfo *c)
1531 if (c->flags & ACC_CLASS_PRIMITIVE)
1538 /* class_is_array **************************************************************
1540 Checks if the given class is an array class.
1542 *******************************************************************************/
1544 bool class_is_array(classinfo *c)
1546 if (!(c->state & CLASS_LINKED))
1550 return (c->vftbl->arraydesc != NULL);
1554 /* class_is_interface **********************************************************
1556 Checks if the given class is an interface.
1558 *******************************************************************************/
1560 bool class_is_interface(classinfo *c)
1562 if (c->flags & ACC_INTERFACE)
1569 /* class_get_superclass ********************************************************
1571 Return the super class of the given class. If the super-field is a
1572 class-reference, resolve it and store it in the classinfo.
1574 *******************************************************************************/
1576 classinfo *class_get_superclass(classinfo *c)
1580 /* For java.lang.Object, primitive and Void classes we return
1583 if (c->super.any == NULL)
1586 /* For interfaces we also return NULL. */
1588 if (c->flags & ACC_INTERFACE)
1591 /* We may have to resolve the super class reference. */
1593 if (IS_CLASSREF(c->super)) {
1594 super = resolve_classref_or_classinfo_eager(c->super, true);
1599 /* Store the resolved super class in the class structure. */
1601 c->super.cls = super;
1604 return c->super.cls;
1608 /* class_get_declaringclass ****************************************************
1610 If the class or interface given is a member of another class,
1611 return the declaring class. For array and primitive classes return
1614 *******************************************************************************/
1616 classinfo *class_get_declaringclass(classinfo *c)
1618 classref_or_classinfo innercr;
1620 classref_or_classinfo outercr;
1624 /* return NULL for arrayclasses and primitive classes */
1626 if (class_is_primitive(c) || (c->name->text[0] == '['))
1629 /* no innerclasses exist */
1631 if (c->innerclasscount == 0)
1634 for (i = 0; i < c->innerclasscount; i++) {
1635 /* Check if inner_class is a classref or a real class and get
1636 the class name from the structure. */
1638 innercr = c->innerclass[i].inner_class;
1640 innername = IS_CLASSREF(innercr) ?
1641 innercr.ref->name : innercr.cls->name;
1643 /* Is the current innerclass this class? */
1645 if (innername == c->name) {
1646 /* Maybe the outer class is not loaded yet. */
1648 outercr = c->innerclass[i].outer_class;
1650 outer = resolve_classref_or_classinfo_eager(outercr, false);
1655 if (!(outer->state & CLASS_LINKED))
1656 if (!link_class(outer))
1667 /* class_get_signature *********************************************************
1669 Return the signature of the given class. For array and primitive
1670 classes return NULL.
1672 *******************************************************************************/
1674 #if defined(ENABLE_JAVASE)
1675 utf *class_get_signature(classinfo *c)
1677 /* For array and primitive classes return NULL. */
1679 if (class_is_array(c) || class_is_primitive(c))
1682 return c->signature;
1687 /* class_printflags ************************************************************
1689 Prints flags of a class.
1691 *******************************************************************************/
1693 #if !defined(NDEBUG)
1694 void class_printflags(classinfo *c)
1701 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1702 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1703 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1704 if (c->flags & ACC_STATIC) printf(" STATIC");
1705 if (c->flags & ACC_FINAL) printf(" FINAL");
1706 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1707 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1708 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1709 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1710 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1711 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1716 /* class_print *****************************************************************
1718 Prints classname plus flags.
1720 *******************************************************************************/
1722 #if !defined(NDEBUG)
1723 void class_print(classinfo *c)
1730 utf_display_printable_ascii(c->name);
1731 class_printflags(c);
1736 /* class_classref_print ********************************************************
1738 Prints classname plus referer class.
1740 *******************************************************************************/
1742 #if !defined(NDEBUG)
1743 void class_classref_print(constant_classref *cr)
1750 utf_display_printable_ascii(cr->name);
1753 class_print(cr->referer);
1761 /* class_println ***************************************************************
1763 Prints classname plus flags and new line.
1765 *******************************************************************************/
1767 #if !defined(NDEBUG)
1768 void class_println(classinfo *c)
1776 /* class_classref_println ******************************************************
1778 Prints classname plus referer class and new line.
1780 *******************************************************************************/
1782 #if !defined(NDEBUG)
1783 void class_classref_println(constant_classref *cr)
1785 class_classref_print(cr);
1791 /* class_classref_or_classinfo_print *******************************************
1793 Prints classname plus referer class.
1795 *******************************************************************************/
1797 #if !defined(NDEBUG)
1798 void class_classref_or_classinfo_print(classref_or_classinfo c)
1800 if (c.any == NULL) {
1801 printf("(classref_or_classinfo) NULL");
1805 class_classref_print(c.ref);
1812 /* class_classref_or_classinfo_println *****************************************
1814 Prints classname plus referer class and a newline.
1816 *******************************************************************************/
1818 void class_classref_or_classinfo_println(classref_or_classinfo c)
1820 class_classref_or_classinfo_println(c);
1825 /* class_showconstantpool ******************************************************
1827 Dump the constant pool of the given class to stdout.
1829 *******************************************************************************/
1831 #if !defined(NDEBUG)
1832 void class_showconstantpool (classinfo *c)
1837 printf ("---- dump of constant pool ----\n");
1839 for (i=0; i<c->cpcount; i++) {
1840 printf ("#%d: ", (int) i);
1842 e = c -> cpinfos [i];
1845 switch (c -> cptags [i]) {
1846 case CONSTANT_Class:
1847 printf ("Classreference -> ");
1848 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
1850 case CONSTANT_Fieldref:
1851 printf ("Fieldref -> ");
1852 field_fieldref_print((constant_FMIref *) e);
1854 case CONSTANT_Methodref:
1855 printf ("Methodref -> ");
1856 method_methodref_print((constant_FMIref *) e);
1858 case CONSTANT_InterfaceMethodref:
1859 printf ("InterfaceMethod -> ");
1860 method_methodref_print((constant_FMIref *) e);
1862 case CONSTANT_String:
1863 printf ("String -> ");
1864 utf_display_printable_ascii (e);
1866 case CONSTANT_Integer:
1867 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
1869 case CONSTANT_Float:
1870 printf ("Float -> %f", ((constant_float*)e) -> value);
1872 case CONSTANT_Double:
1873 printf ("Double -> %f", ((constant_double*)e) -> value);
1877 u8 v = ((constant_long*)e) -> value;
1879 printf ("Long -> %ld", (long int) v);
1881 printf ("Long -> HI: %ld, LO: %ld\n",
1882 (long int) v.high, (long int) v.low);
1886 case CONSTANT_NameAndType:
1888 constant_nameandtype *cnt = e;
1889 printf ("NameAndType: ");
1890 utf_display_printable_ascii (cnt->name);
1892 utf_display_printable_ascii (cnt->descriptor);
1896 printf ("Utf8 -> ");
1897 utf_display_printable_ascii (e);
1900 log_text("Invalid type of ConstantPool-Entry");
1908 #endif /* !defined(NDEBUG) */
1911 /* class_showmethods ***********************************************************
1913 Dump info about the fields and methods of the given class to stdout.
1915 *******************************************************************************/
1917 #if !defined(NDEBUG)
1918 void class_showmethods (classinfo *c)
1922 printf("--------- Fields and Methods ----------------\n");
1924 class_printflags(c);
1928 utf_display_printable_ascii(c->name);
1933 utf_display_printable_ascii(c->super.cls->name);
1937 printf("Index: %d\n", c->index);
1939 printf("Interfaces:\n");
1940 for (i = 0; i < c->interfacescount; i++) {
1942 utf_display_printable_ascii(c->interfaces[i].cls->name);
1943 printf (" (%d)\n", c->interfaces[i].cls->index);
1946 printf("Fields:\n");
1947 for (i = 0; i < c->fieldscount; i++)
1948 field_println(&(c->fields[i]));
1950 printf("Methods:\n");
1951 for (i = 0; i < c->methodscount; i++) {
1952 methodinfo *m = &(c->methods[i]);
1954 if (!(m->flags & ACC_STATIC))
1955 printf("vftblindex: %d ", m->vftblindex);
1960 printf ("Virtual function table:\n");
1961 for (i = 0; i < c->vftbl->vftbllength; i++)
1962 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
1964 #endif /* !defined(NDEBUG) */
1968 * These are local overrides for various environment variables in Emacs.
1969 * Please do not remove this and leave it at the end of the file, where
1970 * Emacs will automagically detect them.
1971 * ---------------------------------------------------------------------
1974 * indent-tabs-mode: t
1978 * vim:noexpandtab:sw=4:ts=4: