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 8230 2007-07-25 08:23:10Z 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;
86 /* system exception classes required in cacao */
88 classinfo *class_java_lang_Throwable;
89 classinfo *class_java_lang_Error;
90 classinfo *class_java_lang_LinkageError;
91 classinfo *class_java_lang_NoClassDefFoundError;
92 classinfo *class_java_lang_OutOfMemoryError;
93 classinfo *class_java_lang_VirtualMachineError;
95 #if defined(WITH_CLASSPATH_GNU)
96 classinfo *class_java_lang_VMThrowable;
99 classinfo *class_java_lang_Exception;
100 classinfo *class_java_lang_ClassCastException;
101 classinfo *class_java_lang_ClassNotFoundException;
103 #if defined(ENABLE_JAVASE)
104 classinfo *class_java_lang_Void;
106 classinfo *class_java_lang_Boolean;
107 classinfo *class_java_lang_Byte;
108 classinfo *class_java_lang_Character;
109 classinfo *class_java_lang_Short;
110 classinfo *class_java_lang_Integer;
111 classinfo *class_java_lang_Long;
112 classinfo *class_java_lang_Float;
113 classinfo *class_java_lang_Double;
116 /* some runtime exception */
118 classinfo *class_java_lang_NullPointerException;
121 /* some classes which may be used more often */
123 #if defined(ENABLE_JAVASE)
124 classinfo *class_java_lang_StackTraceElement;
125 classinfo *class_java_lang_reflect_Constructor;
126 classinfo *class_java_lang_reflect_Field;
127 classinfo *class_java_lang_reflect_Method;
128 classinfo *class_java_security_PrivilegedAction;
129 classinfo *class_java_util_Vector;
131 classinfo *arrayclass_java_lang_Object;
135 /* pseudo classes for the typechecker */
137 classinfo *pseudo_class_Arraystub;
138 classinfo *pseudo_class_Null;
139 classinfo *pseudo_class_New;
142 /* class_set_packagename *******************************************************
144 Derive the package name from the class name and store it in the struct.
146 *******************************************************************************/
148 void class_set_packagename(classinfo *c)
150 char *p = UTF_END(c->name) - 1;
151 char *start = c->name->text;
153 /* set the package name */
154 /* classes in the unnamed package keep packagename == NULL */
156 if (c->name->text[0] == '[') {
157 /* set packagename of arrays to the element's package */
159 for (; *start == '['; start++);
161 /* skip the 'L' in arrays of references */
165 for (; (p > start) && (*p != '/'); --p);
167 c->packagename = utf_new(start, p - start);
170 for (; (p > start) && (*p != '/'); --p);
172 c->packagename = utf_new(start, p - start);
177 /* class_create_classinfo ******************************************************
179 Create a new classinfo struct. The class name is set to the given utf *,
180 most other fields are initialized to zero.
182 Note: classname may be NULL. In this case a not-yet-named classinfo is
183 created. The name must be filled in later and class_set_packagename
184 must be called after that.
186 *******************************************************************************/
188 classinfo *class_create_classinfo(utf *classname)
192 #if defined(ENABLE_STATISTICS)
194 size_classinfo += sizeof(classinfo);
197 /* we use a safe name for temporarily unnamed classes */
199 if (classname == NULL)
200 classname = utf_not_named_yet;
204 log_message_utf("Creating class: ", classname);
207 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
209 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
210 /*c=NEW(classinfo);*/
213 /* Set the header.vftbl of all loaded classes to the one of
214 java.lang.Class, so Java code can use a class as object. */
216 if (class_java_lang_Class != NULL)
217 if (class_java_lang_Class->vftbl != NULL)
218 c->object.header.vftbl = class_java_lang_Class->vftbl;
220 #if defined(ENABLE_JAVASE)
221 /* check if the class is a reference class and flag it */
223 if (classname == utf_java_lang_ref_SoftReference) {
224 c->flags |= ACC_CLASS_REFERENCE_SOFT;
226 else if (classname == utf_java_lang_ref_WeakReference) {
227 c->flags |= ACC_CLASS_REFERENCE_WEAK;
229 else if (classname == utf_java_lang_ref_PhantomReference) {
230 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
234 if (classname != utf_not_named_yet)
235 class_set_packagename(c);
237 LOCK_INIT_OBJECT_LOCK(&c->object.header);
243 /* class_postset_header_vftbl **************************************************
245 Set the header.vftbl of all classes created before java.lang.Class
246 was linked. This is necessary that Java code can use a class as
249 *******************************************************************************/
251 void class_postset_header_vftbl(void)
255 classcache_name_entry *nmen;
256 classcache_class_entry *clsen;
258 assert(class_java_lang_Class);
260 for (slot = 0; slot < hashtable_classcache.size; slot++) {
261 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
263 for (; nmen; nmen = nmen->hashlink) {
264 /* iterate over all class entries */
266 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
269 /* now set the the vftbl */
271 if (c->object.header.vftbl == NULL)
272 c->object.header.vftbl = class_java_lang_Class->vftbl;
278 /* class_define ****************************************************************
280 Calls the loader and defines a class in the VM.
282 *******************************************************************************/
284 classinfo *class_define(utf *name, java_objectheader *cl, int32_t length, const uint8_t *data)
291 /* check if this class has already been defined */
293 c = classcache_lookup_defined_or_initiated(cl, name);
296 exceptions_throw_linkageerror("duplicate class definition: ", c);
301 /* create a new classinfo struct */
303 c = class_create_classinfo(name);
305 #if defined(ENABLE_STATISTICS)
308 if (opt_getloadingtime)
312 /* build a classbuffer with the given data */
314 cb = NEW(classbuffer);
321 /* preset the defining classloader */
325 /* load the class from this buffer */
327 r = load_class_from_classbuffer(cb);
331 FREE(cb, classbuffer);
333 #if defined(ENABLE_STATISTICS)
336 if (opt_getloadingtime)
341 /* If return value is NULL, we had a problem and the class is
342 not loaded. Now free the allocated memory, otherwise we
343 could run into a DOS. */
350 /* Store the newly defined class in the class cache. This call
351 also checks whether a class of the same name has already been
352 defined by the same defining loader, and if so, replaces the
353 newly created class by the one defined earlier. */
355 /* Important: The classinfo given to classcache_store must be
356 fully prepared because another thread may return
357 this pointer after the lookup at to top of this
358 function directly after the class cache lock has
361 c = classcache_store(cl, c, true);
367 /* class_load_attribute_sourcefile *********************************************
369 SourceFile_attribute {
370 u2 attribute_name_index;
375 *******************************************************************************/
377 static bool class_load_attribute_sourcefile(classbuffer *cb)
388 /* check buffer size */
390 if (!suck_check_classbuffer_size(cb, 4 + 2))
393 /* check attribute length */
395 attribute_length = suck_u4(cb);
397 if (attribute_length != 2) {
398 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
402 /* there can be no more than one SourceFile attribute */
404 if (c->sourcefile != NULL) {
405 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
411 sourcefile_index = suck_u2(cb);
412 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
414 if (sourcefile == NULL)
417 /* store sourcefile */
419 c->sourcefile = sourcefile;
425 /* class_load_attribute_enclosingmethod ****************************************
427 EnclosingMethod_attribute {
428 u2 attribute_name_index;
434 *******************************************************************************/
436 #if defined(ENABLE_JAVASE)
437 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
443 classref_or_classinfo cr;
444 constant_nameandtype *cn;
450 /* check buffer size */
452 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
455 /* check attribute length */
457 attribute_length = suck_u4(cb);
459 if (attribute_length != 4) {
460 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
464 /* there can be no more than one EnclosingMethod attribute */
466 if (c->enclosingmethod != NULL) {
467 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
471 /* get class index */
473 class_index = suck_u2(cb);
474 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
476 /* get method index */
478 method_index = suck_u2(cb);
479 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
481 /* store info in classinfo */
483 c->enclosingclass.any = cr.any;
484 c->enclosingmethod = cn;
488 #endif /* defined(ENABLE_JAVASE) */
491 /* class_load_attributes *******************************************************
493 Read attributes from ClassFile.
496 u2 attribute_name_index;
498 u1 info[attribute_length];
501 InnerClasses_attribute {
502 u2 attribute_name_index;
506 *******************************************************************************/
508 bool class_load_attributes(classbuffer *cb)
513 u2 attribute_name_index;
518 /* get attributes count */
520 if (!suck_check_classbuffer_size(cb, 2))
523 attributes_count = suck_u2(cb);
525 for (i = 0; i < attributes_count; i++) {
526 /* get attribute name */
528 if (!suck_check_classbuffer_size(cb, 2))
531 attribute_name_index = suck_u2(cb);
533 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
535 if (attribute_name == NULL)
538 if (attribute_name == utf_InnerClasses) {
541 if (c->innerclass != NULL) {
542 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
546 if (!suck_check_classbuffer_size(cb, 4 + 2))
549 /* skip attribute length */
552 /* number of records */
553 c->innerclasscount = suck_u2(cb);
555 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
558 /* allocate memory for innerclass structure */
559 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
561 for (j = 0; j < c->innerclasscount; j++) {
562 /* The innerclass structure contains a class with an encoded
563 name, its defining scope, its simple name and a bitmask of
564 the access flags. If an inner class is not a member, its
565 outer_class is NULL, if a class is anonymous, its name is
568 innerclassinfo *info = c->innerclass + j;
570 info->inner_class.ref =
571 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
572 info->outer_class.ref =
573 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
575 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
576 info->flags = suck_u2(cb);
579 else if (attribute_name == utf_SourceFile) {
582 if (!class_load_attribute_sourcefile(cb))
585 #if defined(ENABLE_JAVASE)
586 else if (attribute_name == utf_EnclosingMethod) {
587 /* EnclosingMethod */
589 if (!class_load_attribute_enclosingmethod(cb))
592 else if (attribute_name == utf_Signature) {
595 if (!loader_load_attribute_signature(cb, &(c->signature)))
599 /* XXX We can't do a release with that enabled */
601 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
602 /* RuntimeVisibleAnnotations */
604 if (!annotation_load_attribute_runtimevisibleannotations(cb))
610 /* unknown attribute */
612 if (!loader_skip_attribute_body(cb))
621 /* class_freepool **************************************************************
623 Frees all resources used by this classes Constant Pool.
625 *******************************************************************************/
627 static void class_freecpool(classinfo *c)
633 if (c->cptags && c->cpinfos) {
634 for (idx = 0; idx < c->cpcount; idx++) {
635 tag = c->cptags[idx];
636 info = c->cpinfos[idx];
640 case CONSTANT_Fieldref:
641 case CONSTANT_Methodref:
642 case CONSTANT_InterfaceMethodref:
643 FREE(info, constant_FMIref);
645 case CONSTANT_Integer:
646 FREE(info, constant_integer);
649 FREE(info, constant_float);
652 FREE(info, constant_long);
654 case CONSTANT_Double:
655 FREE(info, constant_double);
657 case CONSTANT_NameAndType:
658 FREE(info, constant_nameandtype);
666 MFREE(c->cptags, u1, c->cpcount);
669 MFREE(c->cpinfos, voidptr, c->cpcount);
673 /* class_getconstant ***********************************************************
675 Retrieves the value at position 'pos' of the constantpool of a
676 class. If the type of the value is other than 'ctype', an error is
679 *******************************************************************************/
681 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
683 /* check index and type of constantpool entry */
684 /* (pos == 0 is caught by type comparison) */
686 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
687 exceptions_throw_classformaterror(c, "Illegal constant pool index");
691 return c->cpinfos[pos];
695 /* innerclass_getconstant ******************************************************
697 Like class_getconstant, but if cptags is ZERO, null is returned.
699 *******************************************************************************/
701 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
703 /* invalid position in constantpool */
705 if (pos >= c->cpcount) {
706 exceptions_throw_classformaterror(c, "Illegal constant pool index");
710 /* constantpool entry of type 0 */
712 if (c->cptags[pos] == 0)
715 /* check type of constantpool entry */
717 if (c->cptags[pos] != ctype) {
718 exceptions_throw_classformaterror(c, "Illegal constant pool index");
722 return c->cpinfos[pos];
726 /* class_free ******************************************************************
728 Frees all resources used by the class.
730 *******************************************************************************/
732 void class_free(classinfo *c)
740 MFREE(c->interfaces, classinfo*, c->interfacescount);
743 for (i = 0; i < c->fieldscount; i++)
744 field_free(&(c->fields[i]));
745 #if defined(ENABLE_CACAO_GC)
746 MFREE(c->fields, fieldinfo, c->fieldscount);
751 for (i = 0; i < c->methodscount; i++)
752 method_free(&(c->methods[i]));
753 MFREE(c->methods, methodinfo, c->methodscount);
756 if ((v = c->vftbl) != NULL) {
758 mem_free(v->arraydesc,sizeof(arraydescriptor));
760 for (i = 0; i < v->interfacetablelength; i++) {
761 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
763 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
765 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
766 sizeof(methodptr*) * (v->interfacetablelength -
767 (v->interfacetablelength > 0));
768 v = (vftbl_t*) (((methodptr*) v) -
769 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
774 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
776 /* if (c->classvftbl)
777 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
783 /* get_array_class *************************************************************
785 Returns the array class with the given name for the given
786 classloader, or NULL if an exception occurred.
788 Note: This function does eager loading.
790 *******************************************************************************/
792 static classinfo *get_array_class(utf *name,java_objectheader *initloader,
793 java_objectheader *defloader,bool link)
797 /* lookup this class in the classcache */
798 c = classcache_lookup(initloader,name);
800 c = classcache_lookup_defined(defloader,name);
803 /* we have to create it */
804 c = class_create_classinfo(name);
805 c = load_newly_created_array(c,initloader);
811 assert(c->state & CLASS_LOADED);
812 assert(c->classloader == defloader);
814 if (link && !(c->state & CLASS_LINKED))
818 assert(!link || (c->state & CLASS_LINKED));
824 /* class_array_of **************************************************************
826 Returns an array class with the given component class. The array
827 class is dynamically created if neccessary.
829 *******************************************************************************/
831 classinfo *class_array_of(classinfo *component, bool link)
833 java_objectheader *cl;
840 cl = component->classloader;
842 dumpsize = dump_size();
844 /* Assemble the array class name */
845 namelen = component->name->blength;
847 if (component->name->text[0] == '[') {
848 /* the component is itself an array */
849 namebuf = DMNEW(char, namelen + 1);
851 MCOPY(namebuf + 1, component->name->text, char, namelen);
855 /* the component is a non-array class */
856 namebuf = DMNEW(char, namelen + 3);
859 MCOPY(namebuf + 2, component->name->text, char, namelen);
860 namebuf[2 + namelen] = ';';
864 u = utf_new(namebuf, namelen);
866 c = get_array_class(u, cl, cl, link);
868 dump_release(dumpsize);
874 /* class_multiarray_of *********************************************************
876 Returns an array class with the given dimension and element class.
877 The array class is dynamically created if neccessary.
879 *******************************************************************************/
881 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
888 dumpsize = dump_size();
891 log_text("Invalid array dimension requested");
895 /* Assemble the array class name */
896 namelen = element->name->blength;
898 if (element->name->text[0] == '[') {
899 /* the element is itself an array */
900 namebuf = DMNEW(char, namelen + dim);
901 memcpy(namebuf + dim, element->name->text, namelen);
905 /* the element is a non-array class */
906 namebuf = DMNEW(char, namelen + 2 + dim);
908 memcpy(namebuf + dim + 1, element->name->text, namelen);
909 namelen += (2 + dim);
910 namebuf[namelen - 1] = ';';
912 memset(namebuf, '[', dim);
914 c = get_array_class(utf_new(namebuf, namelen),
915 element->classloader,
916 element->classloader,
919 dump_release(dumpsize);
925 /* class_lookup_classref *******************************************************
927 Looks up the constant_classref for a given classname in the classref
931 cls..............the class containing the reference
932 name.............the name of the class refered to
935 a pointer to a constant_classref, or
936 NULL if the reference was not found
938 *******************************************************************************/
940 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
942 constant_classref *ref;
943 extra_classref *xref;
948 assert(!cls->classrefcount || cls->classrefs);
950 /* first search the main classref table */
951 count = cls->classrefcount;
952 ref = cls->classrefs;
953 for (; count; --count, ++ref)
954 if (ref->name == name)
957 /* next try the list of extra classrefs */
958 for (xref = cls->extclassrefs; xref; xref = xref->next) {
959 if (xref->classref.name == name)
960 return &(xref->classref);
968 /* class_get_classref **********************************************************
970 Returns the constant_classref for a given classname.
973 cls..............the class containing the reference
974 name.............the name of the class refered to
977 a pointer to a constant_classref (never NULL)
980 The given name is not checked for validity!
982 *******************************************************************************/
984 constant_classref *class_get_classref(classinfo *cls, utf *name)
986 constant_classref *ref;
987 extra_classref *xref;
992 ref = class_lookup_classref(cls,name);
996 xref = NEW(extra_classref);
997 CLASSREF_INIT(xref->classref,cls,name);
999 xref->next = cls->extclassrefs;
1000 cls->extclassrefs = xref;
1002 return &(xref->classref);
1006 /* class_get_self_classref *****************************************************
1008 Returns the constant_classref to the class itself.
1011 cls..............the class containing the reference
1014 a pointer to a constant_classref (never NULL)
1016 *******************************************************************************/
1018 constant_classref *class_get_self_classref(classinfo *cls)
1020 /* XXX this should be done in a faster way. Maybe always make */
1021 /* the classref of index 0 a self reference. */
1022 return class_get_classref(cls,cls->name);
1025 /* class_get_classref_multiarray_of ********************************************
1027 Returns an array type reference with the given dimension and element class
1031 dim..............the requested dimension
1032 dim must be in [1;255]. This is NOT checked!
1033 ref..............the component class reference
1036 a pointer to the class reference for the array type
1039 The referer of `ref` is used as the referer for the new classref.
1041 *******************************************************************************/
1043 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1048 constant_classref *cr;
1051 assert(dim >= 1 && dim <= 255);
1053 dumpsize = dump_size();
1055 /* Assemble the array class name */
1056 namelen = ref->name->blength;
1058 if (ref->name->text[0] == '[') {
1059 /* the element is itself an array */
1060 namebuf = DMNEW(char, namelen + dim);
1061 memcpy(namebuf + dim, ref->name->text, namelen);
1065 /* the element is a non-array class */
1066 namebuf = DMNEW(char, namelen + 2 + dim);
1068 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1069 namelen += (2 + dim);
1070 namebuf[namelen - 1] = ';';
1072 memset(namebuf, '[', dim);
1074 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1076 dump_release(dumpsize);
1082 /* class_get_classref_component_of *********************************************
1084 Returns the component classref of a given array type reference
1087 ref..............the array type reference
1090 a reference to the component class, or
1091 NULL if `ref` is not an object array type reference
1094 The referer of `ref` is used as the referer for the new classref.
1096 *******************************************************************************/
1098 constant_classref *class_get_classref_component_of(constant_classref *ref)
1105 name = ref->name->text;
1109 namelen = ref->name->blength - 1;
1114 else if (*name != '[') {
1118 return class_get_classref(ref->referer, utf_new(name, namelen));
1122 /* class_findmethod ************************************************************
1124 Searches a 'classinfo' structure for a method having the given name
1125 and descriptor. If descriptor is NULL, it is ignored.
1127 *******************************************************************************/
1129 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1134 for (i = 0; i < c->methodscount; i++) {
1135 m = &(c->methods[i]);
1137 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1145 /* class_resolvemethod *********************************************************
1147 Searches a class and it's super classes for a method.
1149 Superinterfaces are *not* searched.
1151 *******************************************************************************/
1153 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1158 m = class_findmethod(c, name, desc);
1163 /* JVM Specification bug:
1165 It is important NOT to resolve special <init> and <clinit>
1166 methods to super classes or interfaces; yet, this is not
1167 explicited in the specification. Section 5.4.3.3 should be
1168 updated appropriately. */
1170 if (name == utf_init || name == utf_clinit)
1180 /* class_resolveinterfacemethod_intern *****************************************
1182 Internally used helper function. Do not use this directly.
1184 *******************************************************************************/
1186 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1187 utf *name, utf *desc)
1192 /* try to find the method in the class */
1194 m = class_findmethod(c, name, desc);
1199 /* no method found? try the superinterfaces */
1201 for (i = 0; i < c->interfacescount; i++) {
1202 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1209 /* no method found */
1215 /* class_resolveclassmethod ****************************************************
1217 Resolves a reference from REFERER to a method with NAME and DESC in
1220 If the method cannot be resolved the return value is NULL. If
1221 EXCEPT is true *exceptionptr is set, too.
1223 *******************************************************************************/
1225 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1226 classinfo *referer, bool throwexception)
1232 /* if (c->flags & ACC_INTERFACE) { */
1233 /* if (throwexception) */
1234 /* *exceptionptr = */
1235 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1239 /* try class c and its superclasses */
1243 m = class_resolvemethod(cls, name, desc);
1248 /* try the superinterfaces */
1250 for (i = 0; i < c->interfacescount; i++) {
1251 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1259 exceptions_throw_nosuchmethoderror(c, name, desc);
1264 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1266 exceptions_throw_abstractmethoderror();
1271 /* XXX check access rights */
1277 /* class_resolveinterfacemethod ************************************************
1279 Resolves a reference from REFERER to a method with NAME and DESC in
1282 If the method cannot be resolved the return value is NULL. If
1283 EXCEPT is true *exceptionptr is set, too.
1285 *******************************************************************************/
1287 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1288 classinfo *referer, bool throwexception)
1292 if (!(c->flags & ACC_INTERFACE)) {
1294 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1299 mi = class_resolveinterfacemethod_intern(c, name, desc);
1304 /* try class java.lang.Object */
1306 mi = class_findmethod(class_java_lang_Object, name, desc);
1312 exceptions_throw_nosuchmethoderror(c, name, desc);
1318 /* class_findfield *************************************************************
1320 Searches for field with specified name and type in a classinfo
1321 structure. If no such field is found NULL is returned.
1323 *******************************************************************************/
1325 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1329 for (i = 0; i < c->fieldscount; i++)
1330 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1331 return &(c->fields[i]);
1334 return class_findfield(c->super.cls, name, desc);
1340 /* class_findfield_approx ******************************************************
1342 Searches in 'classinfo'-structure for a field with the specified
1345 *******************************************************************************/
1347 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1351 /* get field index */
1353 i = class_findfield_index_by_name(c, name);
1355 /* field was not found, return */
1360 /* return field address */
1362 return &(c->fields[i]);
1366 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1370 for (i = 0; i < c->fieldscount; i++) {
1371 /* compare field names */
1373 if ((c->fields[i].name == name))
1377 /* field was not found, raise exception */
1379 exceptions_throw_nosuchfielderror(c, name);
1385 /****************** Function: class_resolvefield_int ***************************
1387 This is an internally used helper function. Do not use this directly.
1389 Tries to resolve a field having the given name and type.
1390 If the field cannot be resolved, NULL is returned.
1392 *******************************************************************************/
1394 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1399 /* search for field in class c */
1401 for (i = 0; i < c->fieldscount; i++) {
1402 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1403 return &(c->fields[i]);
1407 /* try superinterfaces recursively */
1409 for (i = 0; i < c->interfacescount; i++) {
1410 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1415 /* try superclass */
1418 return class_resolvefield_int(c->super.cls, name, desc);
1426 /********************* Function: class_resolvefield ***************************
1428 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1430 If the field cannot be resolved the return value is NULL. If EXCEPT is
1431 true *exceptionptr is set, too.
1433 *******************************************************************************/
1435 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1436 classinfo *referer, bool throwexception)
1440 fi = class_resolvefield_int(c, name, desc);
1444 exceptions_throw_nosuchfielderror(c, name);
1449 /* XXX check access rights */
1455 /* class_issubclass ************************************************************
1457 Checks if sub is a descendant of super.
1459 *******************************************************************************/
1461 bool class_issubclass(classinfo *sub, classinfo *super)
1470 sub = sub->super.cls;
1475 /* class_isanysubclass *********************************************************
1477 Checks a subclass relation between two classes. Implemented
1478 interfaces are interpreted as super classes.
1480 Return value: 1 ... sub is subclass of super
1483 *******************************************************************************/
1485 bool class_isanysubclass(classinfo *sub, classinfo *super)
1487 castinfo classvalues;
1491 /* This is the trivial case. */
1496 /* Primitive classes are only subclasses of themselves. */
1498 if (class_is_primitive(sub) || class_is_primitive(super))
1501 /* Check for interfaces. */
1503 if (super->flags & ACC_INTERFACE) {
1504 result = (sub->vftbl->interfacetablelength > super->index) &&
1505 (sub->vftbl->interfacetable[-super->index] != NULL);
1508 /* java.lang.Object is the only super class of any
1511 if (sub->flags & ACC_INTERFACE)
1512 return (super == class_java_lang_Object);
1514 ASM_GETCLASSVALUES_ATOMIC(super->vftbl, sub->vftbl, &classvalues);
1516 diffval = classvalues.sub_baseval - classvalues.super_baseval;
1517 result = diffval <= (u4) classvalues.super_diffval;
1524 /* class_is_primitive **********************************************************
1526 Checks if the given class is a primitive class.
1528 *******************************************************************************/
1530 bool class_is_primitive(classinfo *c)
1532 if (c->flags & ACC_CLASS_PRIMITIVE)
1539 /* class_is_array **************************************************************
1541 Checks if the given class is an array class.
1543 *******************************************************************************/
1545 bool class_is_array(classinfo *c)
1547 if (!(c->state & CLASS_LINKED))
1551 return (c->vftbl->arraydesc != NULL);
1555 /* class_is_interface **********************************************************
1557 Checks if the given class is an interface.
1559 *******************************************************************************/
1561 bool class_is_interface(classinfo *c)
1563 if (c->flags & ACC_INTERFACE)
1570 /* class_get_superclass ********************************************************
1572 Return the super class of the given class. If the super-field is a
1573 class-reference, resolve it and store it in the classinfo.
1575 *******************************************************************************/
1577 classinfo *class_get_superclass(classinfo *c)
1581 /* For java.lang.Object, primitive and Void classes we return
1584 if (c->super.any == NULL)
1587 /* For interfaces we also return NULL. */
1589 if (c->flags & ACC_INTERFACE)
1592 /* We may have to resolve the super class reference. */
1594 if (IS_CLASSREF(c->super)) {
1595 super = resolve_classref_or_classinfo_eager(c->super, true);
1600 /* Store the resolved super class in the class structure. */
1602 c->super.cls = super;
1605 return c->super.cls;
1609 /* class_get_declaringclass ****************************************************
1611 If the class or interface given is a member of another class,
1612 return the declaring class. For array and primitive classes return
1615 *******************************************************************************/
1617 classinfo *class_get_declaringclass(classinfo *c)
1619 classref_or_classinfo innercr;
1621 classref_or_classinfo outercr;
1625 /* return NULL for arrayclasses and primitive classes */
1627 if (class_is_primitive(c) || (c->name->text[0] == '['))
1630 /* no innerclasses exist */
1632 if (c->innerclasscount == 0)
1635 for (i = 0; i < c->innerclasscount; i++) {
1636 /* Check if inner_class is a classref or a real class and get
1637 the class name from the structure. */
1639 innercr = c->innerclass[i].inner_class;
1641 innername = IS_CLASSREF(innercr) ?
1642 innercr.ref->name : innercr.cls->name;
1644 /* Is the current innerclass this class? */
1646 if (innername == c->name) {
1647 /* Maybe the outer class is not loaded yet. */
1649 outercr = c->innerclass[i].outer_class;
1651 outer = resolve_classref_or_classinfo_eager(outercr, false);
1656 if (!(outer->state & CLASS_LINKED))
1657 if (!link_class(outer))
1668 /* class_get_signature *********************************************************
1670 Return the signature of the given class. For array and primitive
1671 classes return NULL.
1673 *******************************************************************************/
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;
1686 /* class_printflags ************************************************************
1688 Prints flags of a class.
1690 *******************************************************************************/
1692 #if !defined(NDEBUG)
1693 void class_printflags(classinfo *c)
1700 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1701 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1702 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1703 if (c->flags & ACC_STATIC) printf(" STATIC");
1704 if (c->flags & ACC_FINAL) printf(" FINAL");
1705 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1706 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1707 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1708 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1709 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1710 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1715 /* class_print *****************************************************************
1717 Prints classname plus flags.
1719 *******************************************************************************/
1721 #if !defined(NDEBUG)
1722 void class_print(classinfo *c)
1729 utf_display_printable_ascii(c->name);
1730 class_printflags(c);
1735 /* class_classref_print ********************************************************
1737 Prints classname plus referer class.
1739 *******************************************************************************/
1741 #if !defined(NDEBUG)
1742 void class_classref_print(constant_classref *cr)
1749 utf_display_printable_ascii(cr->name);
1752 class_print(cr->referer);
1760 /* class_println ***************************************************************
1762 Prints classname plus flags and new line.
1764 *******************************************************************************/
1766 #if !defined(NDEBUG)
1767 void class_println(classinfo *c)
1775 /* class_classref_println ******************************************************
1777 Prints classname plus referer class and new line.
1779 *******************************************************************************/
1781 #if !defined(NDEBUG)
1782 void class_classref_println(constant_classref *cr)
1784 class_classref_print(cr);
1790 /* class_classref_or_classinfo_print *******************************************
1792 Prints classname plus referer class.
1794 *******************************************************************************/
1796 #if !defined(NDEBUG)
1797 void class_classref_or_classinfo_print(classref_or_classinfo c)
1799 if (c.any == NULL) {
1800 printf("(classref_or_classinfo) NULL");
1804 class_classref_print(c.ref);
1811 /* class_classref_or_classinfo_println *****************************************
1813 Prints classname plus referer class and a newline.
1815 *******************************************************************************/
1817 void class_classref_or_classinfo_println(classref_or_classinfo c)
1819 class_classref_or_classinfo_println(c);
1824 /* class_showconstantpool ******************************************************
1826 Dump the constant pool of the given class to stdout.
1828 *******************************************************************************/
1830 #if !defined(NDEBUG)
1831 void class_showconstantpool (classinfo *c)
1836 printf ("---- dump of constant pool ----\n");
1838 for (i=0; i<c->cpcount; i++) {
1839 printf ("#%d: ", (int) i);
1841 e = c -> cpinfos [i];
1844 switch (c -> cptags [i]) {
1845 case CONSTANT_Class:
1846 printf ("Classreference -> ");
1847 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
1849 case CONSTANT_Fieldref:
1850 printf ("Fieldref -> ");
1851 field_fieldref_print((constant_FMIref *) e);
1853 case CONSTANT_Methodref:
1854 printf ("Methodref -> ");
1855 method_methodref_print((constant_FMIref *) e);
1857 case CONSTANT_InterfaceMethodref:
1858 printf ("InterfaceMethod -> ");
1859 method_methodref_print((constant_FMIref *) e);
1861 case CONSTANT_String:
1862 printf ("String -> ");
1863 utf_display_printable_ascii (e);
1865 case CONSTANT_Integer:
1866 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
1868 case CONSTANT_Float:
1869 printf ("Float -> %f", ((constant_float*)e) -> value);
1871 case CONSTANT_Double:
1872 printf ("Double -> %f", ((constant_double*)e) -> value);
1876 u8 v = ((constant_long*)e) -> value;
1878 printf ("Long -> %ld", (long int) v);
1880 printf ("Long -> HI: %ld, LO: %ld\n",
1881 (long int) v.high, (long int) v.low);
1885 case CONSTANT_NameAndType:
1887 constant_nameandtype *cnt = e;
1888 printf ("NameAndType: ");
1889 utf_display_printable_ascii (cnt->name);
1891 utf_display_printable_ascii (cnt->descriptor);
1895 printf ("Utf8 -> ");
1896 utf_display_printable_ascii (e);
1899 log_text("Invalid type of ConstantPool-Entry");
1907 #endif /* !defined(NDEBUG) */
1910 /* class_showmethods ***********************************************************
1912 Dump info about the fields and methods of the given class to stdout.
1914 *******************************************************************************/
1916 #if !defined(NDEBUG)
1917 void class_showmethods (classinfo *c)
1921 printf("--------- Fields and Methods ----------------\n");
1923 class_printflags(c);
1927 utf_display_printable_ascii(c->name);
1932 utf_display_printable_ascii(c->super.cls->name);
1936 printf("Index: %d\n", c->index);
1938 printf("Interfaces:\n");
1939 for (i = 0; i < c->interfacescount; i++) {
1941 utf_display_printable_ascii(c->interfaces[i].cls->name);
1942 printf (" (%d)\n", c->interfaces[i].cls->index);
1945 printf("Fields:\n");
1946 for (i = 0; i < c->fieldscount; i++)
1947 field_println(&(c->fields[i]));
1949 printf("Methods:\n");
1950 for (i = 0; i < c->methodscount; i++) {
1951 methodinfo *m = &(c->methods[i]);
1953 if (!(m->flags & ACC_STATIC))
1954 printf("vftblindex: %d ", m->vftblindex);
1959 printf ("Virtual function table:\n");
1960 for (i = 0; i < c->vftbl->vftbllength; i++)
1961 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
1963 #endif /* !defined(NDEBUG) */
1967 * These are local overrides for various environment variables in Emacs.
1968 * Please do not remove this and leave it at the end of the file, where
1969 * Emacs will automagically detect them.
1970 * ---------------------------------------------------------------------
1973 * indent-tabs-mode: t
1977 * vim:noexpandtab:sw=4:ts=4: