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 7246 2007-01-29 18:49:05Z twisti $
39 #include "mm/memory.h"
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
45 #include "toolbox/logging.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
50 #include "vmcore/class.h"
51 #include "vmcore/classcache.h"
52 #include "vmcore/loader.h"
53 #include "vmcore/options.h"
54 #include "vmcore/resolve.h"
56 #if defined(ENABLE_STATISTICS)
57 # include "vmcore/statistics.h"
60 #include "vmcore/suck.h"
61 #include "vmcore/utf8.h"
64 /* global variables ***********************************************************/
66 list unlinkedclasses; /* this is only used for eager class */
70 /* frequently used classes ****************************************************/
72 /* important system classes */
74 classinfo *class_java_lang_Object;
75 classinfo *class_java_lang_Class;
76 classinfo *class_java_lang_ClassLoader;
77 classinfo *class_java_lang_Cloneable;
78 classinfo *class_java_lang_SecurityManager;
79 classinfo *class_java_lang_String;
80 classinfo *class_java_lang_System;
81 classinfo *class_java_lang_Thread;
82 classinfo *class_java_lang_ThreadGroup;
83 classinfo *class_java_lang_VMSystem;
84 classinfo *class_java_lang_VMThread;
85 classinfo *class_java_io_Serializable;
88 /* system exception classes required in cacao */
90 classinfo *class_java_lang_Throwable;
91 classinfo *class_java_lang_Error;
92 classinfo *class_java_lang_LinkageError;
93 classinfo *class_java_lang_NoClassDefFoundError;
94 classinfo *class_java_lang_OutOfMemoryError;
95 classinfo *class_java_lang_VirtualMachineError;
97 #if defined(WITH_CLASSPATH_GNU)
98 classinfo *class_java_lang_VMThrowable;
101 classinfo *class_java_lang_Exception;
102 classinfo *class_java_lang_ClassCastException;
103 classinfo *class_java_lang_ClassNotFoundException;
105 #if defined(ENABLE_JAVASE)
106 classinfo *class_java_lang_Void;
108 classinfo *class_java_lang_Boolean;
109 classinfo *class_java_lang_Byte;
110 classinfo *class_java_lang_Character;
111 classinfo *class_java_lang_Short;
112 classinfo *class_java_lang_Integer;
113 classinfo *class_java_lang_Long;
114 classinfo *class_java_lang_Float;
115 classinfo *class_java_lang_Double;
118 /* some runtime exception */
120 classinfo *class_java_lang_NullPointerException;
123 /* some classes which may be used more often */
125 #if defined(ENABLE_JAVASE)
126 classinfo *class_java_lang_StackTraceElement;
127 classinfo *class_java_lang_reflect_Constructor;
128 classinfo *class_java_lang_reflect_Field;
129 classinfo *class_java_lang_reflect_Method;
130 classinfo *class_java_security_PrivilegedAction;
131 classinfo *class_java_util_Vector;
133 classinfo *arrayclass_java_lang_Object;
137 /* pseudo classes for the typechecker */
139 classinfo *pseudo_class_Arraystub;
140 classinfo *pseudo_class_Null;
141 classinfo *pseudo_class_New;
144 /* class_set_packagename *******************************************************
146 Derive the package name from the class name and store it in the struct.
148 *******************************************************************************/
150 void class_set_packagename(classinfo *c)
152 char *p = UTF_END(c->name) - 1;
153 char *start = c->name->text;
155 /* set the package name */
156 /* classes in the unnamed package keep packagename == NULL */
158 if (c->name->text[0] == '[') {
159 /* set packagename of arrays to the element's package */
161 for (; *start == '['; start++);
163 /* skip the 'L' in arrays of references */
167 for (; (p > start) && (*p != '/'); --p);
169 c->packagename = utf_new(start, p - start);
172 for (; (p > start) && (*p != '/'); --p);
174 c->packagename = utf_new(start, p - start);
179 /* class_create_classinfo ******************************************************
181 Create a new classinfo struct. The class name is set to the given utf *,
182 most other fields are initialized to zero.
184 Note: classname may be NULL. In this case a not-yet-named classinfo is
185 created. The name must be filled in later and class_set_packagename
186 must be called after that.
188 *******************************************************************************/
190 classinfo *class_create_classinfo(utf *classname)
194 #if defined(ENABLE_STATISTICS)
196 size_classinfo += sizeof(classinfo);
199 /* we use a safe name for temporarily unnamed classes */
201 classname = utf_not_named_yet;
205 log_message_utf("Creating class: ", classname);
208 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
210 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
211 /*c=NEW(classinfo);*/
214 /* Set the header.vftbl of all loaded classes to the one of
215 java.lang.Class, so Java code can use a class as object. */
217 if (class_java_lang_Class)
218 if (class_java_lang_Class->vftbl)
219 c->object.header.vftbl = class_java_lang_Class->vftbl;
221 if (classname != utf_not_named_yet)
222 class_set_packagename(c);
224 #if defined(ENABLE_THREADS)
225 lock_init_object_lock(&c->object.header);
232 /* class_postset_header_vftbl **************************************************
234 Set the header.vftbl of all classes created before java.lang.Class
235 was linked. This is necessary that Java code can use a class as
238 *******************************************************************************/
240 void class_postset_header_vftbl(void)
244 classcache_name_entry *nmen;
245 classcache_class_entry *clsen;
247 assert(class_java_lang_Class);
249 for (slot = 0; slot < hashtable_classcache.size; slot++) {
250 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
252 for (; nmen; nmen = nmen->hashlink) {
253 /* iterate over all class entries */
255 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
258 /* now set the the vftbl */
260 if (c->object.header.vftbl == NULL)
261 c->object.header.vftbl = class_java_lang_Class->vftbl;
268 /* class_load_attribute_sourcefile *********************************************
270 SourceFile_attribute {
271 u2 attribute_name_index;
276 *******************************************************************************/
278 static bool class_load_attribute_sourcefile(classbuffer *cb)
289 /* check buffer size */
291 if (!suck_check_classbuffer_size(cb, 4 + 2))
294 /* check attribute length */
296 attribute_length = suck_u4(cb);
298 if (attribute_length != 2) {
299 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
303 /* there can be no more than one SourceFile attribute */
305 if (c->sourcefile != NULL) {
306 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
312 sourcefile_index = suck_u2(cb);
313 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
315 if (sourcefile == NULL)
318 /* store sourcefile */
320 c->sourcefile = sourcefile;
326 /* class_load_attribute_enclosingmethod ****************************************
328 EnclosingMethod_attribute {
329 u2 attribute_name_index;
335 *******************************************************************************/
337 #if defined(ENABLE_JAVASE)
338 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
344 classref_or_classinfo cr;
345 constant_nameandtype *cn;
351 /* check buffer size */
353 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
356 /* check attribute length */
358 attribute_length = suck_u4(cb);
360 if (attribute_length != 4) {
361 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
365 /* there can be no more than one EnclosingMethod attribute */
367 if (c->enclosingmethod != NULL) {
368 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
372 /* get class index */
374 class_index = suck_u2(cb);
375 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
377 /* get method index */
379 method_index = suck_u2(cb);
380 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
382 /* store info in classinfo */
384 c->enclosingclass.any = cr.any;
385 c->enclosingmethod = cn;
389 #endif /* defined(ENABLE_JAVASE) */
392 /* class_load_attributes *******************************************************
394 Read attributes from ClassFile.
397 u2 attribute_name_index;
399 u1 info[attribute_length];
402 InnerClasses_attribute {
403 u2 attribute_name_index;
407 *******************************************************************************/
409 bool class_load_attributes(classbuffer *cb)
414 u2 attribute_name_index;
419 /* get attributes count */
421 if (!suck_check_classbuffer_size(cb, 2))
424 attributes_count = suck_u2(cb);
426 for (i = 0; i < attributes_count; i++) {
427 /* get attribute name */
429 if (!suck_check_classbuffer_size(cb, 2))
432 attribute_name_index = suck_u2(cb);
434 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
436 if (attribute_name == NULL)
439 if (attribute_name == utf_InnerClasses) {
442 if (c->innerclass != NULL) {
443 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
447 if (!suck_check_classbuffer_size(cb, 4 + 2))
450 /* skip attribute length */
453 /* number of records */
454 c->innerclasscount = suck_u2(cb);
456 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
459 /* allocate memory for innerclass structure */
460 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
462 for (j = 0; j < c->innerclasscount; j++) {
463 /* The innerclass structure contains a class with an encoded
464 name, its defining scope, its simple name and a bitmask of
465 the access flags. If an inner class is not a member, its
466 outer_class is NULL, if a class is anonymous, its name is
469 innerclassinfo *info = c->innerclass + j;
471 info->inner_class.ref =
472 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
473 info->outer_class.ref =
474 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
476 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
477 info->flags = suck_u2(cb);
480 else if (attribute_name == utf_SourceFile) {
483 if (!class_load_attribute_sourcefile(cb))
486 #if defined(ENABLE_JAVASE)
487 else if (attribute_name == utf_EnclosingMethod) {
488 /* EnclosingMethod */
490 if (!class_load_attribute_enclosingmethod(cb))
493 else if (attribute_name == utf_Signature) {
496 if (!loader_load_attribute_signature(cb, &(c->signature)))
499 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
500 /* RuntimeVisibleAnnotations */
502 if (!annotation_load_attribute_runtimevisibleannotations(cb))
507 /* unknown attribute */
509 if (!loader_skip_attribute_body(cb))
518 /* class_freepool **************************************************************
520 Frees all resources used by this classes Constant Pool.
522 *******************************************************************************/
524 static void class_freecpool(classinfo *c)
530 if (c->cptags && c->cpinfos) {
531 for (idx = 0; idx < c->cpcount; idx++) {
532 tag = c->cptags[idx];
533 info = c->cpinfos[idx];
537 case CONSTANT_Fieldref:
538 case CONSTANT_Methodref:
539 case CONSTANT_InterfaceMethodref:
540 FREE(info, constant_FMIref);
542 case CONSTANT_Integer:
543 FREE(info, constant_integer);
546 FREE(info, constant_float);
549 FREE(info, constant_long);
551 case CONSTANT_Double:
552 FREE(info, constant_double);
554 case CONSTANT_NameAndType:
555 FREE(info, constant_nameandtype);
563 MFREE(c->cptags, u1, c->cpcount);
566 MFREE(c->cpinfos, voidptr, c->cpcount);
570 /* class_getconstant ***********************************************************
572 Retrieves the value at position 'pos' of the constantpool of a
573 class. If the type of the value is other than 'ctype', an error is
576 *******************************************************************************/
578 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
580 /* check index and type of constantpool entry */
581 /* (pos == 0 is caught by type comparison) */
583 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
584 exceptions_throw_classformaterror(c, "Illegal constant pool index");
588 return c->cpinfos[pos];
592 /* innerclass_getconstant ******************************************************
594 Like class_getconstant, but if cptags is ZERO, null is returned.
596 *******************************************************************************/
598 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
600 /* invalid position in constantpool */
602 if (pos >= c->cpcount) {
603 exceptions_throw_classformaterror(c, "Illegal constant pool index");
607 /* constantpool entry of type 0 */
609 if (c->cptags[pos] == 0)
612 /* check type of constantpool entry */
614 if (c->cptags[pos] != ctype) {
615 exceptions_throw_classformaterror(c, "Illegal constant pool index");
619 return c->cpinfos[pos];
623 /* class_free ******************************************************************
625 Frees all resources used by the class.
627 *******************************************************************************/
629 void class_free(classinfo *c)
637 MFREE(c->interfaces, classinfo*, c->interfacescount);
640 for (i = 0; i < c->fieldscount; i++)
641 field_free(&(c->fields[i]));
642 #if defined(ENABLE_CACAO_GC)
643 MFREE(c->fields, fieldinfo, c->fieldscount);
648 for (i = 0; i < c->methodscount; i++)
649 method_free(&(c->methods[i]));
650 MFREE(c->methods, methodinfo, c->methodscount);
653 if ((v = c->vftbl) != NULL) {
655 mem_free(v->arraydesc,sizeof(arraydescriptor));
657 for (i = 0; i < v->interfacetablelength; i++) {
658 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
660 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
662 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
663 sizeof(methodptr*) * (v->interfacetablelength -
664 (v->interfacetablelength > 0));
665 v = (vftbl_t*) (((methodptr*) v) -
666 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
671 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
673 /* if (c->classvftbl)
674 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
680 /* get_array_class *************************************************************
682 Returns the array class with the given name for the given
683 classloader, or NULL if an exception occurred.
685 Note: This function does eager loading.
687 *******************************************************************************/
689 static classinfo *get_array_class(utf *name,java_objectheader *initloader,
690 java_objectheader *defloader,bool link)
694 /* lookup this class in the classcache */
695 c = classcache_lookup(initloader,name);
697 c = classcache_lookup_defined(defloader,name);
700 /* we have to create it */
701 c = class_create_classinfo(name);
702 c = load_newly_created_array(c,initloader);
708 assert(c->state & CLASS_LOADED);
709 assert(c->classloader == defloader);
711 if (link && !(c->state & CLASS_LINKED))
715 assert(!link || (c->state & CLASS_LINKED));
721 /* class_array_of **************************************************************
723 Returns an array class with the given component class. The array
724 class is dynamically created if neccessary.
726 *******************************************************************************/
728 classinfo *class_array_of(classinfo *component, bool link)
735 dumpsize = dump_size();
737 /* Assemble the array class name */
738 namelen = component->name->blength;
740 if (component->name->text[0] == '[') {
741 /* the component is itself an array */
742 namebuf = DMNEW(char, namelen + 1);
744 MCOPY(namebuf + 1, component->name->text, char, namelen);
748 /* the component is a non-array class */
749 namebuf = DMNEW(char, namelen + 3);
752 MCOPY(namebuf + 2, component->name->text, char, namelen);
753 namebuf[2 + namelen] = ';';
757 c = get_array_class(utf_new(namebuf, namelen),
758 component->classloader,
759 component->classloader,
762 dump_release(dumpsize);
768 /* class_multiarray_of *********************************************************
770 Returns an array class with the given dimension and element class.
771 The array class is dynamically created if neccessary.
773 *******************************************************************************/
775 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
782 dumpsize = dump_size();
785 log_text("Invalid array dimension requested");
789 /* Assemble the array class name */
790 namelen = element->name->blength;
792 if (element->name->text[0] == '[') {
793 /* the element is itself an array */
794 namebuf = DMNEW(char, namelen + dim);
795 memcpy(namebuf + dim, element->name->text, namelen);
799 /* the element is a non-array class */
800 namebuf = DMNEW(char, namelen + 2 + dim);
802 memcpy(namebuf + dim + 1, element->name->text, namelen);
803 namelen += (2 + dim);
804 namebuf[namelen - 1] = ';';
806 memset(namebuf, '[', dim);
808 c = get_array_class(utf_new(namebuf, namelen),
809 element->classloader,
810 element->classloader,
813 dump_release(dumpsize);
819 /* class_lookup_classref *******************************************************
821 Looks up the constant_classref for a given classname in the classref
825 cls..............the class containing the reference
826 name.............the name of the class refered to
829 a pointer to a constant_classref, or
830 NULL if the reference was not found
832 *******************************************************************************/
834 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
836 constant_classref *ref;
837 extra_classref *xref;
842 assert(!cls->classrefcount || cls->classrefs);
844 /* first search the main classref table */
845 count = cls->classrefcount;
846 ref = cls->classrefs;
847 for (; count; --count, ++ref)
848 if (ref->name == name)
851 /* next try the list of extra classrefs */
852 for (xref = cls->extclassrefs; xref; xref = xref->next) {
853 if (xref->classref.name == name)
854 return &(xref->classref);
862 /* class_get_classref **********************************************************
864 Returns the constant_classref for a given classname.
867 cls..............the class containing the reference
868 name.............the name of the class refered to
871 a pointer to a constant_classref (never NULL)
874 The given name is not checked for validity!
876 *******************************************************************************/
878 constant_classref *class_get_classref(classinfo *cls, utf *name)
880 constant_classref *ref;
881 extra_classref *xref;
886 ref = class_lookup_classref(cls,name);
890 xref = NEW(extra_classref);
891 CLASSREF_INIT(xref->classref,cls,name);
893 xref->next = cls->extclassrefs;
894 cls->extclassrefs = xref;
896 return &(xref->classref);
900 /* class_get_self_classref *****************************************************
902 Returns the constant_classref to the class itself.
905 cls..............the class containing the reference
908 a pointer to a constant_classref (never NULL)
910 *******************************************************************************/
912 constant_classref *class_get_self_classref(classinfo *cls)
914 /* XXX this should be done in a faster way. Maybe always make */
915 /* the classref of index 0 a self reference. */
916 return class_get_classref(cls,cls->name);
919 /* class_get_classref_multiarray_of ********************************************
921 Returns an array type reference with the given dimension and element class
925 dim..............the requested dimension
926 dim must be in [1;255]. This is NOT checked!
927 ref..............the component class reference
930 a pointer to the class reference for the array type
933 The referer of `ref` is used as the referer for the new classref.
935 *******************************************************************************/
937 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
942 constant_classref *cr;
945 assert(dim >= 1 && dim <= 255);
947 dumpsize = dump_size();
949 /* Assemble the array class name */
950 namelen = ref->name->blength;
952 if (ref->name->text[0] == '[') {
953 /* the element is itself an array */
954 namebuf = DMNEW(char, namelen + dim);
955 memcpy(namebuf + dim, ref->name->text, namelen);
959 /* the element is a non-array class */
960 namebuf = DMNEW(char, namelen + 2 + dim);
962 memcpy(namebuf + dim + 1, ref->name->text, namelen);
963 namelen += (2 + dim);
964 namebuf[namelen - 1] = ';';
966 memset(namebuf, '[', dim);
968 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
970 dump_release(dumpsize);
976 /* class_get_classref_component_of *********************************************
978 Returns the component classref of a given array type reference
981 ref..............the array type reference
984 a reference to the component class, or
985 NULL if `ref` is not an object array type reference
988 The referer of `ref` is used as the referer for the new classref.
990 *******************************************************************************/
992 constant_classref *class_get_classref_component_of(constant_classref *ref)
999 name = ref->name->text;
1003 namelen = ref->name->blength - 1;
1008 else if (*name != '[') {
1012 return class_get_classref(ref->referer, utf_new(name, namelen));
1016 /* class_findmethod ************************************************************
1018 Searches a 'classinfo' structure for a method having the given name
1019 and descriptor. If descriptor is NULL, it is ignored.
1021 *******************************************************************************/
1023 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1028 for (i = 0; i < c->methodscount; i++) {
1029 m = &(c->methods[i]);
1031 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1039 /* class_resolvemethod *********************************************************
1041 Searches a class and it's super classes for a method.
1043 Superinterfaces are *not* searched.
1045 *******************************************************************************/
1047 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1052 m = class_findmethod(c, name, desc);
1057 /* JVM Specification bug:
1059 It is important NOT to resolve special <init> and <clinit>
1060 methods to super classes or interfaces; yet, this is not
1061 explicited in the specification. Section 5.4.3.3 should be
1062 updated appropriately. */
1064 if (name == utf_init || name == utf_clinit)
1074 /* class_resolveinterfacemethod_intern *****************************************
1076 Internally used helper function. Do not use this directly.
1078 *******************************************************************************/
1080 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1081 utf *name, utf *desc)
1086 /* try to find the method in the class */
1088 m = class_findmethod(c, name, desc);
1093 /* no method found? try the superinterfaces */
1095 for (i = 0; i < c->interfacescount; i++) {
1096 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1103 /* no method found */
1109 /* class_resolveclassmethod ****************************************************
1111 Resolves a reference from REFERER to a method with NAME and DESC in
1114 If the method cannot be resolved the return value is NULL. If
1115 EXCEPT is true *exceptionptr is set, too.
1117 *******************************************************************************/
1119 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1120 classinfo *referer, bool throwexception)
1126 /* if (c->flags & ACC_INTERFACE) { */
1127 /* if (throwexception) */
1128 /* *exceptionptr = */
1129 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1133 /* try class c and its superclasses */
1137 m = class_resolvemethod(cls, name, desc);
1142 /* try the superinterfaces */
1144 for (i = 0; i < c->interfacescount; i++) {
1145 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1152 if (throwexception) {
1153 #if defined(ENABLE_JAVASE)
1154 exceptions_throw_nosuchmethoderror(c, name, desc);
1156 exceptions_throw_virtualmachineerror();
1163 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1164 if (throwexception) {
1165 #if defined(ENABLE_JAVASE)
1166 exceptions_throw_abstractmethoderror();
1168 exceptions_throw_virtualmachineerror();
1175 /* XXX check access rights */
1181 /* class_resolveinterfacemethod ************************************************
1183 Resolves a reference from REFERER to a method with NAME and DESC in
1186 If the method cannot be resolved the return value is NULL. If
1187 EXCEPT is true *exceptionptr is set, too.
1189 *******************************************************************************/
1191 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1192 classinfo *referer, bool throwexception)
1196 if (!(c->flags & ACC_INTERFACE)) {
1198 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1203 mi = class_resolveinterfacemethod_intern(c, name, desc);
1208 /* try class java.lang.Object */
1210 mi = class_findmethod(class_java_lang_Object, name, desc);
1215 if (throwexception) {
1216 #if defined(ENABLE_JAVASE)
1217 exceptions_throw_nosuchmethoderror(c, name, desc);
1219 exceptions_throw_virtualmachineerror();
1227 /* class_findfield *************************************************************
1229 Searches for field with specified name and type in a classinfo
1230 structure. If no such field is found NULL is returned.
1232 *******************************************************************************/
1234 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1238 for (i = 0; i < c->fieldscount; i++)
1239 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1240 return &(c->fields[i]);
1243 return class_findfield(c->super.cls, name, desc);
1249 /* class_findfield_approx ******************************************************
1251 Searches in 'classinfo'-structure for a field with the specified
1254 *******************************************************************************/
1256 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1260 /* get field index */
1262 i = class_findfield_index_by_name(c, name);
1264 /* field was not found, return */
1269 /* return field address */
1271 return &(c->fields[i]);
1275 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1279 for (i = 0; i < c->fieldscount; i++) {
1280 /* compare field names */
1282 if ((c->fields[i].name == name))
1286 /* field was not found, raise exception */
1288 exceptions_throw_nosuchfielderror(c, name);
1294 /****************** Function: class_resolvefield_int ***************************
1296 This is an internally used helper function. Do not use this directly.
1298 Tries to resolve a field having the given name and type.
1299 If the field cannot be resolved, NULL is returned.
1301 *******************************************************************************/
1303 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1308 /* search for field in class c */
1310 for (i = 0; i < c->fieldscount; i++) {
1311 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1312 return &(c->fields[i]);
1316 /* try superinterfaces recursively */
1318 for (i = 0; i < c->interfacescount; i++) {
1319 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1324 /* try superclass */
1327 return class_resolvefield_int(c->super.cls, name, desc);
1335 /********************* Function: class_resolvefield ***************************
1337 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1339 If the field cannot be resolved the return value is NULL. If EXCEPT is
1340 true *exceptionptr is set, too.
1342 *******************************************************************************/
1344 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1345 classinfo *referer, bool throwexception)
1349 fi = class_resolvefield_int(c, name, desc);
1353 exceptions_throw_nosuchfielderror(c, name);
1358 /* XXX check access rights */
1364 /* class_issubclass ************************************************************
1366 Checks if sub is a descendant of super.
1368 *******************************************************************************/
1370 bool class_issubclass(classinfo *sub, classinfo *super)
1379 sub = sub->super.cls;
1384 /* class_printflags ************************************************************
1386 Prints flags of a class.
1388 *******************************************************************************/
1390 #if !defined(NDEBUG)
1391 void class_printflags(classinfo *c)
1398 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1399 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1400 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1401 if (c->flags & ACC_STATIC) printf(" STATIC");
1402 if (c->flags & ACC_FINAL) printf(" FINAL");
1403 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1404 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1405 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1406 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1407 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1408 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1413 /* class_print *****************************************************************
1415 Prints classname plus flags.
1417 *******************************************************************************/
1419 #if !defined(NDEBUG)
1420 void class_print(classinfo *c)
1427 utf_display_printable_ascii(c->name);
1428 class_printflags(c);
1433 /* class_classref_print ********************************************************
1435 Prints classname plus referer class.
1437 *******************************************************************************/
1439 #if !defined(NDEBUG)
1440 void class_classref_print(constant_classref *cr)
1447 utf_display_printable_ascii(cr->name);
1450 class_print(cr->referer);
1458 /* class_println ***************************************************************
1460 Prints classname plus flags and new line.
1462 *******************************************************************************/
1464 #if !defined(NDEBUG)
1465 void class_println(classinfo *c)
1473 /* class_classref_println ******************************************************
1475 Prints classname plus referer class and new line.
1477 *******************************************************************************/
1479 #if !defined(NDEBUG)
1480 void class_classref_println(constant_classref *cr)
1482 class_classref_print(cr);
1488 /* class_classref_or_classinfo_print *******************************************
1490 Prints classname plus referer class.
1492 *******************************************************************************/
1494 #if !defined(NDEBUG)
1495 void class_classref_or_classinfo_print(classref_or_classinfo c)
1497 if (c.any == NULL) {
1498 printf("(classref_or_classinfo) NULL");
1502 class_classref_print(c.ref);
1509 /* class_classref_or_classinfo_println *****************************************
1511 Prints classname plus referer class and a newline.
1513 *******************************************************************************/
1515 void class_classref_or_classinfo_println(classref_or_classinfo c)
1517 class_classref_or_classinfo_println(c);
1522 /* class_showconstantpool ******************************************************
1524 Dump the constant pool of the given class to stdout.
1526 *******************************************************************************/
1528 #if !defined(NDEBUG)
1529 void class_showconstantpool (classinfo *c)
1534 printf ("---- dump of constant pool ----\n");
1536 for (i=0; i<c->cpcount; i++) {
1537 printf ("#%d: ", (int) i);
1539 e = c -> cpinfos [i];
1542 switch (c -> cptags [i]) {
1543 case CONSTANT_Class:
1544 printf ("Classreference -> ");
1545 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
1547 case CONSTANT_Fieldref:
1548 printf ("Fieldref -> ");
1549 field_fieldref_print((constant_FMIref *) e);
1551 case CONSTANT_Methodref:
1552 printf ("Methodref -> ");
1553 method_methodref_print((constant_FMIref *) e);
1555 case CONSTANT_InterfaceMethodref:
1556 printf ("InterfaceMethod -> ");
1557 method_methodref_print((constant_FMIref *) e);
1559 case CONSTANT_String:
1560 printf ("String -> ");
1561 utf_display_printable_ascii (e);
1563 case CONSTANT_Integer:
1564 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
1566 case CONSTANT_Float:
1567 printf ("Float -> %f", ((constant_float*)e) -> value);
1569 case CONSTANT_Double:
1570 printf ("Double -> %f", ((constant_double*)e) -> value);
1574 u8 v = ((constant_long*)e) -> value;
1576 printf ("Long -> %ld", (long int) v);
1578 printf ("Long -> HI: %ld, LO: %ld\n",
1579 (long int) v.high, (long int) v.low);
1583 case CONSTANT_NameAndType:
1585 constant_nameandtype *cnt = e;
1586 printf ("NameAndType: ");
1587 utf_display_printable_ascii (cnt->name);
1589 utf_display_printable_ascii (cnt->descriptor);
1593 printf ("Utf8 -> ");
1594 utf_display_printable_ascii (e);
1597 log_text("Invalid type of ConstantPool-Entry");
1605 #endif /* !defined(NDEBUG) */
1608 /* class_showmethods ***********************************************************
1610 Dump info about the fields and methods of the given class to stdout.
1612 *******************************************************************************/
1614 #if !defined(NDEBUG)
1615 void class_showmethods (classinfo *c)
1619 printf("--------- Fields and Methods ----------------\n");
1621 class_printflags(c);
1625 utf_display_printable_ascii(c->name);
1630 utf_display_printable_ascii(c->super.cls->name);
1634 printf("Index: %d\n", c->index);
1636 printf("Interfaces:\n");
1637 for (i = 0; i < c->interfacescount; i++) {
1639 utf_display_printable_ascii(c->interfaces[i].cls->name);
1640 printf (" (%d)\n", c->interfaces[i].cls->index);
1643 printf("Fields:\n");
1644 for (i = 0; i < c->fieldscount; i++)
1645 field_println(&(c->fields[i]));
1647 printf("Methods:\n");
1648 for (i = 0; i < c->methodscount; i++) {
1649 methodinfo *m = &(c->methods[i]);
1651 if (!(m->flags & ACC_STATIC))
1652 printf("vftblindex: %d ", m->vftblindex);
1657 printf ("Virtual function table:\n");
1658 for (i = 0; i < c->vftbl->vftbllength; i++)
1659 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
1661 #endif /* !defined(NDEBUG) */
1665 * These are local overrides for various environment variables in Emacs.
1666 * Please do not remove this and leave it at the end of the file, where
1667 * Emacs will automagically detect them.
1668 * ---------------------------------------------------------------------
1671 * indent-tabs-mode: t
1675 * vim:noexpandtab:sw=4:ts=4: