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 8140 2007-06-25 13:26:23Z michi $
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"
50 #include "vm/jit/asmpart.h"
52 #include "vmcore/class.h"
53 #include "vmcore/classcache.h"
54 #include "vmcore/loader.h"
55 #include "vmcore/options.h"
57 #if defined(ENABLE_STATISTICS)
58 # include "vmcore/statistics.h"
61 #include "vmcore/suck.h"
62 #include "vmcore/utf8.h"
65 /* global variables ***********************************************************/
67 list_t unlinkedclasses; /* this is only used for eager class */
71 /* frequently used classes ****************************************************/
73 /* important system classes */
75 classinfo *class_java_lang_Object;
76 classinfo *class_java_lang_Class;
77 classinfo *class_java_lang_ClassLoader;
78 classinfo *class_java_lang_Cloneable;
79 classinfo *class_java_lang_SecurityManager;
80 classinfo *class_java_lang_String;
81 classinfo *class_java_lang_System;
82 classinfo *class_java_lang_Thread;
83 classinfo *class_java_lang_ThreadGroup;
84 classinfo *class_java_lang_VMSystem;
85 classinfo *class_java_lang_VMThread;
86 classinfo *class_java_io_Serializable;
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 #if defined(ENABLE_GC_CACAO)
213 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
215 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
216 /*c=NEW(classinfo);*/
220 /* Set the header.vftbl of all loaded classes to the one of
221 java.lang.Class, so Java code can use a class as object. */
223 if (class_java_lang_Class != NULL)
224 if (class_java_lang_Class->vftbl != NULL)
225 c->object.header.vftbl = class_java_lang_Class->vftbl;
227 #if defined(ENABLE_JAVASE)
228 /* check if the class is a reference class and flag it */
230 if (classname == utf_java_lang_ref_SoftReference) {
231 c->flags |= ACC_CLASS_SOFT_REFERENCE;
233 else if (classname == utf_java_lang_ref_WeakReference) {
234 c->flags |= ACC_CLASS_WEAK_REFERENCE;
236 else if (classname == utf_java_lang_ref_PhantomReference) {
237 c->flags |= ACC_CLASS_PHANTOM_REFERENCE;
241 if (classname != utf_not_named_yet)
242 class_set_packagename(c);
244 LOCK_INIT_OBJECT_LOCK(&c->object.header);
250 /* class_postset_header_vftbl **************************************************
252 Set the header.vftbl of all classes created before java.lang.Class
253 was linked. This is necessary that Java code can use a class as
256 *******************************************************************************/
258 void class_postset_header_vftbl(void)
262 classcache_name_entry *nmen;
263 classcache_class_entry *clsen;
265 assert(class_java_lang_Class);
267 for (slot = 0; slot < hashtable_classcache.size; slot++) {
268 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
270 for (; nmen; nmen = nmen->hashlink) {
271 /* iterate over all class entries */
273 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
276 /* now set the the vftbl */
278 if (c->object.header.vftbl == NULL)
279 c->object.header.vftbl = class_java_lang_Class->vftbl;
285 /* class_define ****************************************************************
287 Calls the loader and defines a class in the VM.
289 *******************************************************************************/
291 classinfo *class_define(utf *name, classloader *cl, s4 length, u1 *data)
298 /* check if this class has already been defined */
300 c = classcache_lookup_defined_or_initiated(cl, name);
303 exceptions_throw_linkageerror("duplicate class definition: ", c);
308 /* create a new classinfo struct */
310 c = class_create_classinfo(name);
312 #if defined(ENABLE_STATISTICS)
315 if (opt_getloadingtime)
319 /* build a classbuffer with the given data */
321 cb = NEW(classbuffer);
328 /* preset the defining classloader */
332 /* load the class from this buffer */
334 r = load_class_from_classbuffer(cb);
338 FREE(cb, classbuffer);
340 #if defined(ENABLE_STATISTICS)
343 if (opt_getloadingtime)
348 /* If return value is NULL, we had a problem and the class is
349 not loaded. Now free the allocated memory, otherwise we
350 could run into a DOS. */
357 /* Store the newly defined class in the class cache. This call
358 also checks whether a class of the same name has already been
359 defined by the same defining loader, and if so, replaces the
360 newly created class by the one defined earlier. */
362 /* Important: The classinfo given to classcache_store must be
363 fully prepared because another thread may return
364 this pointer after the lookup at to top of this
365 function directly after the class cache lock has
368 c = classcache_store(cl, c, true);
374 /* class_load_attribute_sourcefile *********************************************
376 SourceFile_attribute {
377 u2 attribute_name_index;
382 *******************************************************************************/
384 static bool class_load_attribute_sourcefile(classbuffer *cb)
395 /* check buffer size */
397 if (!suck_check_classbuffer_size(cb, 4 + 2))
400 /* check attribute length */
402 attribute_length = suck_u4(cb);
404 if (attribute_length != 2) {
405 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
409 /* there can be no more than one SourceFile attribute */
411 if (c->sourcefile != NULL) {
412 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
418 sourcefile_index = suck_u2(cb);
419 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
421 if (sourcefile == NULL)
424 /* store sourcefile */
426 c->sourcefile = sourcefile;
432 /* class_load_attribute_enclosingmethod ****************************************
434 EnclosingMethod_attribute {
435 u2 attribute_name_index;
441 *******************************************************************************/
443 #if defined(ENABLE_JAVASE)
444 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
450 classref_or_classinfo cr;
451 constant_nameandtype *cn;
457 /* check buffer size */
459 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
462 /* check attribute length */
464 attribute_length = suck_u4(cb);
466 if (attribute_length != 4) {
467 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
471 /* there can be no more than one EnclosingMethod attribute */
473 if (c->enclosingmethod != NULL) {
474 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
478 /* get class index */
480 class_index = suck_u2(cb);
481 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
483 /* get method index */
485 method_index = suck_u2(cb);
486 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
488 /* store info in classinfo */
490 c->enclosingclass.any = cr.any;
491 c->enclosingmethod = cn;
495 #endif /* defined(ENABLE_JAVASE) */
498 /* class_load_attributes *******************************************************
500 Read attributes from ClassFile.
503 u2 attribute_name_index;
505 u1 info[attribute_length];
508 InnerClasses_attribute {
509 u2 attribute_name_index;
513 *******************************************************************************/
515 bool class_load_attributes(classbuffer *cb)
520 u2 attribute_name_index;
525 /* get attributes count */
527 if (!suck_check_classbuffer_size(cb, 2))
530 attributes_count = suck_u2(cb);
532 for (i = 0; i < attributes_count; i++) {
533 /* get attribute name */
535 if (!suck_check_classbuffer_size(cb, 2))
538 attribute_name_index = suck_u2(cb);
540 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
542 if (attribute_name == NULL)
545 if (attribute_name == utf_InnerClasses) {
548 if (c->innerclass != NULL) {
549 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
553 if (!suck_check_classbuffer_size(cb, 4 + 2))
556 /* skip attribute length */
559 /* number of records */
560 c->innerclasscount = suck_u2(cb);
562 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
565 /* allocate memory for innerclass structure */
566 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
568 for (j = 0; j < c->innerclasscount; j++) {
569 /* The innerclass structure contains a class with an encoded
570 name, its defining scope, its simple name and a bitmask of
571 the access flags. If an inner class is not a member, its
572 outer_class is NULL, if a class is anonymous, its name is
575 innerclassinfo *info = c->innerclass + j;
577 info->inner_class.ref =
578 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
579 info->outer_class.ref =
580 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
582 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
583 info->flags = suck_u2(cb);
586 else if (attribute_name == utf_SourceFile) {
589 if (!class_load_attribute_sourcefile(cb))
592 #if defined(ENABLE_JAVASE)
593 else if (attribute_name == utf_EnclosingMethod) {
594 /* EnclosingMethod */
596 if (!class_load_attribute_enclosingmethod(cb))
599 else if (attribute_name == utf_Signature) {
602 if (!loader_load_attribute_signature(cb, &(c->signature)))
606 /* XXX We can't do a release with that enabled */
608 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
609 /* RuntimeVisibleAnnotations */
611 if (!annotation_load_attribute_runtimevisibleannotations(cb))
617 /* unknown attribute */
619 if (!loader_skip_attribute_body(cb))
628 /* class_freepool **************************************************************
630 Frees all resources used by this classes Constant Pool.
632 *******************************************************************************/
634 static void class_freecpool(classinfo *c)
640 if (c->cptags && c->cpinfos) {
641 for (idx = 0; idx < c->cpcount; idx++) {
642 tag = c->cptags[idx];
643 info = c->cpinfos[idx];
647 case CONSTANT_Fieldref:
648 case CONSTANT_Methodref:
649 case CONSTANT_InterfaceMethodref:
650 FREE(info, constant_FMIref);
652 case CONSTANT_Integer:
653 FREE(info, constant_integer);
656 FREE(info, constant_float);
659 FREE(info, constant_long);
661 case CONSTANT_Double:
662 FREE(info, constant_double);
664 case CONSTANT_NameAndType:
665 FREE(info, constant_nameandtype);
673 MFREE(c->cptags, u1, c->cpcount);
676 MFREE(c->cpinfos, voidptr, c->cpcount);
680 /* class_getconstant ***********************************************************
682 Retrieves the value at position 'pos' of the constantpool of a
683 class. If the type of the value is other than 'ctype', an error is
686 *******************************************************************************/
688 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
690 /* check index and type of constantpool entry */
691 /* (pos == 0 is caught by type comparison) */
693 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
694 exceptions_throw_classformaterror(c, "Illegal constant pool index");
698 return c->cpinfos[pos];
702 /* innerclass_getconstant ******************************************************
704 Like class_getconstant, but if cptags is ZERO, null is returned.
706 *******************************************************************************/
708 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
710 /* invalid position in constantpool */
712 if (pos >= c->cpcount) {
713 exceptions_throw_classformaterror(c, "Illegal constant pool index");
717 /* constantpool entry of type 0 */
719 if (c->cptags[pos] == 0)
722 /* check type of constantpool entry */
724 if (c->cptags[pos] != ctype) {
725 exceptions_throw_classformaterror(c, "Illegal constant pool index");
729 return c->cpinfos[pos];
733 /* class_free ******************************************************************
735 Frees all resources used by the class.
737 *******************************************************************************/
739 void class_free(classinfo *c)
747 MFREE(c->interfaces, classinfo*, c->interfacescount);
750 for (i = 0; i < c->fieldscount; i++)
751 field_free(&(c->fields[i]));
752 #if defined(ENABLE_CACAO_GC)
753 MFREE(c->fields, fieldinfo, c->fieldscount);
758 for (i = 0; i < c->methodscount; i++)
759 method_free(&(c->methods[i]));
760 MFREE(c->methods, methodinfo, c->methodscount);
763 if ((v = c->vftbl) != NULL) {
765 mem_free(v->arraydesc,sizeof(arraydescriptor));
767 for (i = 0; i < v->interfacetablelength; i++) {
768 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
770 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
772 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
773 sizeof(methodptr*) * (v->interfacetablelength -
774 (v->interfacetablelength > 0));
775 v = (vftbl_t*) (((methodptr*) v) -
776 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
781 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
783 /* if (c->classvftbl)
784 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
790 /* get_array_class *************************************************************
792 Returns the array class with the given name for the given
793 classloader, or NULL if an exception occurred.
795 Note: This function does eager loading.
797 *******************************************************************************/
799 static classinfo *get_array_class(utf *name,classloader *initloader,
800 classloader *defloader,bool link)
804 /* lookup this class in the classcache */
805 c = classcache_lookup(initloader,name);
807 c = classcache_lookup_defined(defloader,name);
810 /* we have to create it */
811 c = class_create_classinfo(name);
812 c = load_newly_created_array(c,initloader);
818 assert(c->state & CLASS_LOADED);
819 assert(c->classloader == defloader);
821 if (link && !(c->state & CLASS_LINKED))
825 assert(!link || (c->state & CLASS_LINKED));
831 /* class_array_of **************************************************************
833 Returns an array class with the given component class. The array
834 class is dynamically created if neccessary.
836 *******************************************************************************/
838 classinfo *class_array_of(classinfo *component, bool link)
847 cl = component->classloader;
849 dumpsize = dump_size();
851 /* Assemble the array class name */
852 namelen = component->name->blength;
854 if (component->name->text[0] == '[') {
855 /* the component is itself an array */
856 namebuf = DMNEW(char, namelen + 1);
858 MCOPY(namebuf + 1, component->name->text, char, namelen);
862 /* the component is a non-array class */
863 namebuf = DMNEW(char, namelen + 3);
866 MCOPY(namebuf + 2, component->name->text, char, namelen);
867 namebuf[2 + namelen] = ';';
871 u = utf_new(namebuf, namelen);
873 c = get_array_class(u, cl, cl, link);
875 dump_release(dumpsize);
881 /* class_multiarray_of *********************************************************
883 Returns an array class with the given dimension and element class.
884 The array class is dynamically created if neccessary.
886 *******************************************************************************/
888 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
895 dumpsize = dump_size();
898 log_text("Invalid array dimension requested");
902 /* Assemble the array class name */
903 namelen = element->name->blength;
905 if (element->name->text[0] == '[') {
906 /* the element is itself an array */
907 namebuf = DMNEW(char, namelen + dim);
908 memcpy(namebuf + dim, element->name->text, namelen);
912 /* the element is a non-array class */
913 namebuf = DMNEW(char, namelen + 2 + dim);
915 memcpy(namebuf + dim + 1, element->name->text, namelen);
916 namelen += (2 + dim);
917 namebuf[namelen - 1] = ';';
919 memset(namebuf, '[', dim);
921 c = get_array_class(utf_new(namebuf, namelen),
922 element->classloader,
923 element->classloader,
926 dump_release(dumpsize);
932 /* class_lookup_classref *******************************************************
934 Looks up the constant_classref for a given classname in the classref
938 cls..............the class containing the reference
939 name.............the name of the class refered to
942 a pointer to a constant_classref, or
943 NULL if the reference was not found
945 *******************************************************************************/
947 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
949 constant_classref *ref;
950 extra_classref *xref;
955 assert(!cls->classrefcount || cls->classrefs);
957 /* first search the main classref table */
958 count = cls->classrefcount;
959 ref = cls->classrefs;
960 for (; count; --count, ++ref)
961 if (ref->name == name)
964 /* next try the list of extra classrefs */
965 for (xref = cls->extclassrefs; xref; xref = xref->next) {
966 if (xref->classref.name == name)
967 return &(xref->classref);
975 /* class_get_classref **********************************************************
977 Returns the constant_classref for a given classname.
980 cls..............the class containing the reference
981 name.............the name of the class refered to
984 a pointer to a constant_classref (never NULL)
987 The given name is not checked for validity!
989 *******************************************************************************/
991 constant_classref *class_get_classref(classinfo *cls, utf *name)
993 constant_classref *ref;
994 extra_classref *xref;
999 ref = class_lookup_classref(cls,name);
1003 xref = NEW(extra_classref);
1004 CLASSREF_INIT(xref->classref,cls,name);
1006 xref->next = cls->extclassrefs;
1007 cls->extclassrefs = xref;
1009 return &(xref->classref);
1013 /* class_get_self_classref *****************************************************
1015 Returns the constant_classref to the class itself.
1018 cls..............the class containing the reference
1021 a pointer to a constant_classref (never NULL)
1023 *******************************************************************************/
1025 constant_classref *class_get_self_classref(classinfo *cls)
1027 /* XXX this should be done in a faster way. Maybe always make */
1028 /* the classref of index 0 a self reference. */
1029 return class_get_classref(cls,cls->name);
1032 /* class_get_classref_multiarray_of ********************************************
1034 Returns an array type reference with the given dimension and element class
1038 dim..............the requested dimension
1039 dim must be in [1;255]. This is NOT checked!
1040 ref..............the component class reference
1043 a pointer to the class reference for the array type
1046 The referer of `ref` is used as the referer for the new classref.
1048 *******************************************************************************/
1050 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1055 constant_classref *cr;
1058 assert(dim >= 1 && dim <= 255);
1060 dumpsize = dump_size();
1062 /* Assemble the array class name */
1063 namelen = ref->name->blength;
1065 if (ref->name->text[0] == '[') {
1066 /* the element is itself an array */
1067 namebuf = DMNEW(char, namelen + dim);
1068 memcpy(namebuf + dim, ref->name->text, namelen);
1072 /* the element is a non-array class */
1073 namebuf = DMNEW(char, namelen + 2 + dim);
1075 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1076 namelen += (2 + dim);
1077 namebuf[namelen - 1] = ';';
1079 memset(namebuf, '[', dim);
1081 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1083 dump_release(dumpsize);
1089 /* class_get_classref_component_of *********************************************
1091 Returns the component classref of a given array type reference
1094 ref..............the array type reference
1097 a reference to the component class, or
1098 NULL if `ref` is not an object array type reference
1101 The referer of `ref` is used as the referer for the new classref.
1103 *******************************************************************************/
1105 constant_classref *class_get_classref_component_of(constant_classref *ref)
1112 name = ref->name->text;
1116 namelen = ref->name->blength - 1;
1121 else if (*name != '[') {
1125 return class_get_classref(ref->referer, utf_new(name, namelen));
1129 /* class_findmethod ************************************************************
1131 Searches a 'classinfo' structure for a method having the given name
1132 and descriptor. If descriptor is NULL, it is ignored.
1134 *******************************************************************************/
1136 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1141 for (i = 0; i < c->methodscount; i++) {
1142 m = &(c->methods[i]);
1144 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1152 /* class_resolvemethod *********************************************************
1154 Searches a class and it's super classes for a method.
1156 Superinterfaces are *not* searched.
1158 *******************************************************************************/
1160 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1165 m = class_findmethod(c, name, desc);
1170 /* JVM Specification bug:
1172 It is important NOT to resolve special <init> and <clinit>
1173 methods to super classes or interfaces; yet, this is not
1174 explicited in the specification. Section 5.4.3.3 should be
1175 updated appropriately. */
1177 if (name == utf_init || name == utf_clinit)
1187 /* class_resolveinterfacemethod_intern *****************************************
1189 Internally used helper function. Do not use this directly.
1191 *******************************************************************************/
1193 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1194 utf *name, utf *desc)
1199 /* try to find the method in the class */
1201 m = class_findmethod(c, name, desc);
1206 /* no method found? try the superinterfaces */
1208 for (i = 0; i < c->interfacescount; i++) {
1209 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1216 /* no method found */
1222 /* class_resolveclassmethod ****************************************************
1224 Resolves a reference from REFERER to a method with NAME and DESC in
1227 If the method cannot be resolved the return value is NULL. If
1228 EXCEPT is true *exceptionptr is set, too.
1230 *******************************************************************************/
1232 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1233 classinfo *referer, bool throwexception)
1239 /* if (c->flags & ACC_INTERFACE) { */
1240 /* if (throwexception) */
1241 /* *exceptionptr = */
1242 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1246 /* try class c and its superclasses */
1250 m = class_resolvemethod(cls, name, desc);
1255 /* try the superinterfaces */
1257 for (i = 0; i < c->interfacescount; i++) {
1258 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1266 exceptions_throw_nosuchmethoderror(c, name, desc);
1271 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1273 exceptions_throw_abstractmethoderror();
1278 /* XXX check access rights */
1284 /* class_resolveinterfacemethod ************************************************
1286 Resolves a reference from REFERER to a method with NAME and DESC in
1289 If the method cannot be resolved the return value is NULL. If
1290 EXCEPT is true *exceptionptr is set, too.
1292 *******************************************************************************/
1294 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1295 classinfo *referer, bool throwexception)
1299 if (!(c->flags & ACC_INTERFACE)) {
1301 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1306 mi = class_resolveinterfacemethod_intern(c, name, desc);
1311 /* try class java.lang.Object */
1313 mi = class_findmethod(class_java_lang_Object, name, desc);
1319 exceptions_throw_nosuchmethoderror(c, name, desc);
1325 /* class_findfield *************************************************************
1327 Searches for field with specified name and type in a classinfo
1328 structure. If no such field is found NULL is returned.
1330 *******************************************************************************/
1332 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1336 for (i = 0; i < c->fieldscount; i++)
1337 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1338 return &(c->fields[i]);
1341 return class_findfield(c->super.cls, name, desc);
1347 /* class_findfield_approx ******************************************************
1349 Searches in 'classinfo'-structure for a field with the specified
1352 *******************************************************************************/
1354 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1358 /* get field index */
1360 i = class_findfield_index_by_name(c, name);
1362 /* field was not found, return */
1367 /* return field address */
1369 return &(c->fields[i]);
1373 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1377 for (i = 0; i < c->fieldscount; i++) {
1378 /* compare field names */
1380 if ((c->fields[i].name == name))
1384 /* field was not found, raise exception */
1386 exceptions_throw_nosuchfielderror(c, name);
1392 /****************** Function: class_resolvefield_int ***************************
1394 This is an internally used helper function. Do not use this directly.
1396 Tries to resolve a field having the given name and type.
1397 If the field cannot be resolved, NULL is returned.
1399 *******************************************************************************/
1401 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1406 /* search for field in class c */
1408 for (i = 0; i < c->fieldscount; i++) {
1409 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1410 return &(c->fields[i]);
1414 /* try superinterfaces recursively */
1416 for (i = 0; i < c->interfacescount; i++) {
1417 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1422 /* try superclass */
1425 return class_resolvefield_int(c->super.cls, name, desc);
1433 /********************* Function: class_resolvefield ***************************
1435 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1437 If the field cannot be resolved the return value is NULL. If EXCEPT is
1438 true *exceptionptr is set, too.
1440 *******************************************************************************/
1442 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1443 classinfo *referer, bool throwexception)
1447 fi = class_resolvefield_int(c, name, desc);
1451 exceptions_throw_nosuchfielderror(c, name);
1456 /* XXX check access rights */
1462 /* class_issubclass ************************************************************
1464 Checks if sub is a descendant of super.
1466 *******************************************************************************/
1468 bool class_issubclass(classinfo *sub, classinfo *super)
1477 sub = sub->super.cls;
1482 /* class_isanysubclass *********************************************************
1484 Checks a subclass relation between two classes. Implemented
1485 interfaces are interpreted as super classes.
1487 Return value: 1 ... sub is subclass of super
1490 *******************************************************************************/
1492 bool class_isanysubclass(classinfo *sub, classinfo *super)
1494 castinfo classvalues;
1498 /* This is the trivial case. */
1503 /* Primitive classes are only subclasses of themselves. */
1505 if ((sub->flags & ACC_CLASS_PRIMITIVE) ||
1506 (super->flags & ACC_CLASS_PRIMITIVE))
1509 /* Check for interfaces. */
1511 if (super->flags & ACC_INTERFACE) {
1512 result = (sub->vftbl->interfacetablelength > super->index) &&
1513 (sub->vftbl->interfacetable[-super->index] != NULL);
1516 /* java.lang.Object is the only super class of any
1519 if (sub->flags & ACC_INTERFACE)
1520 return (super == class_java_lang_Object);
1522 ASM_GETCLASSVALUES_ATOMIC(super->vftbl, sub->vftbl, &classvalues);
1524 diffval = classvalues.sub_baseval - classvalues.super_baseval;
1525 result = diffval <= (u4) classvalues.super_diffval;
1532 /* class_printflags ************************************************************
1534 Prints flags of a class.
1536 *******************************************************************************/
1538 #if !defined(NDEBUG)
1539 void class_printflags(classinfo *c)
1546 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1547 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1548 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1549 if (c->flags & ACC_STATIC) printf(" STATIC");
1550 if (c->flags & ACC_FINAL) printf(" FINAL");
1551 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1552 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1553 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1554 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1555 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1556 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1561 /* class_print *****************************************************************
1563 Prints classname plus flags.
1565 *******************************************************************************/
1567 #if !defined(NDEBUG)
1568 void class_print(classinfo *c)
1575 utf_display_printable_ascii(c->name);
1576 class_printflags(c);
1581 /* class_classref_print ********************************************************
1583 Prints classname plus referer class.
1585 *******************************************************************************/
1587 #if !defined(NDEBUG)
1588 void class_classref_print(constant_classref *cr)
1595 utf_display_printable_ascii(cr->name);
1598 class_print(cr->referer);
1606 /* class_println ***************************************************************
1608 Prints classname plus flags and new line.
1610 *******************************************************************************/
1612 #if !defined(NDEBUG)
1613 void class_println(classinfo *c)
1621 /* class_classref_println ******************************************************
1623 Prints classname plus referer class and new line.
1625 *******************************************************************************/
1627 #if !defined(NDEBUG)
1628 void class_classref_println(constant_classref *cr)
1630 class_classref_print(cr);
1636 /* class_classref_or_classinfo_print *******************************************
1638 Prints classname plus referer class.
1640 *******************************************************************************/
1642 #if !defined(NDEBUG)
1643 void class_classref_or_classinfo_print(classref_or_classinfo c)
1645 if (c.any == NULL) {
1646 printf("(classref_or_classinfo) NULL");
1650 class_classref_print(c.ref);
1657 /* class_classref_or_classinfo_println *****************************************
1659 Prints classname plus referer class and a newline.
1661 *******************************************************************************/
1663 void class_classref_or_classinfo_println(classref_or_classinfo c)
1665 class_classref_or_classinfo_println(c);
1670 /* class_showconstantpool ******************************************************
1672 Dump the constant pool of the given class to stdout.
1674 *******************************************************************************/
1676 #if !defined(NDEBUG)
1677 void class_showconstantpool (classinfo *c)
1682 printf ("---- dump of constant pool ----\n");
1684 for (i=0; i<c->cpcount; i++) {
1685 printf ("#%d: ", (int) i);
1687 e = c -> cpinfos [i];
1690 switch (c -> cptags [i]) {
1691 case CONSTANT_Class:
1692 printf ("Classreference -> ");
1693 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
1695 case CONSTANT_Fieldref:
1696 printf ("Fieldref -> ");
1697 field_fieldref_print((constant_FMIref *) e);
1699 case CONSTANT_Methodref:
1700 printf ("Methodref -> ");
1701 method_methodref_print((constant_FMIref *) e);
1703 case CONSTANT_InterfaceMethodref:
1704 printf ("InterfaceMethod -> ");
1705 method_methodref_print((constant_FMIref *) e);
1707 case CONSTANT_String:
1708 printf ("String -> ");
1709 utf_display_printable_ascii (e);
1711 case CONSTANT_Integer:
1712 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
1714 case CONSTANT_Float:
1715 printf ("Float -> %f", ((constant_float*)e) -> value);
1717 case CONSTANT_Double:
1718 printf ("Double -> %f", ((constant_double*)e) -> value);
1722 u8 v = ((constant_long*)e) -> value;
1724 printf ("Long -> %ld", (long int) v);
1726 printf ("Long -> HI: %ld, LO: %ld\n",
1727 (long int) v.high, (long int) v.low);
1731 case CONSTANT_NameAndType:
1733 constant_nameandtype *cnt = e;
1734 printf ("NameAndType: ");
1735 utf_display_printable_ascii (cnt->name);
1737 utf_display_printable_ascii (cnt->descriptor);
1741 printf ("Utf8 -> ");
1742 utf_display_printable_ascii (e);
1745 log_text("Invalid type of ConstantPool-Entry");
1753 #endif /* !defined(NDEBUG) */
1756 /* class_showmethods ***********************************************************
1758 Dump info about the fields and methods of the given class to stdout.
1760 *******************************************************************************/
1762 #if !defined(NDEBUG)
1763 void class_showmethods (classinfo *c)
1767 printf("--------- Fields and Methods ----------------\n");
1769 class_printflags(c);
1773 utf_display_printable_ascii(c->name);
1778 utf_display_printable_ascii(c->super.cls->name);
1782 printf("Index: %d\n", c->index);
1784 printf("Interfaces:\n");
1785 for (i = 0; i < c->interfacescount; i++) {
1787 utf_display_printable_ascii(c->interfaces[i].cls->name);
1788 printf (" (%d)\n", c->interfaces[i].cls->index);
1791 printf("Fields:\n");
1792 for (i = 0; i < c->fieldscount; i++)
1793 field_println(&(c->fields[i]));
1795 printf("Methods:\n");
1796 for (i = 0; i < c->methodscount; i++) {
1797 methodinfo *m = &(c->methods[i]);
1799 if (!(m->flags & ACC_STATIC))
1800 printf("vftblindex: %d ", m->vftblindex);
1805 printf ("Virtual function table:\n");
1806 for (i = 0; i < c->vftbl->vftbllength; i++)
1807 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
1809 #endif /* !defined(NDEBUG) */
1813 * These are local overrides for various environment variables in Emacs.
1814 * Please do not remove this and leave it at the end of the file, where
1815 * Emacs will automagically detect them.
1816 * ---------------------------------------------------------------------
1819 * indent-tabs-mode: t
1823 * vim:noexpandtab:sw=4:ts=4: