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 8299 2007-08-13 08:41:18Z 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"
49 #include "vm/resolve.h"
51 #include "vm/jit/asmpart.h"
53 #include "vmcore/class.h"
54 #include "vmcore/classcache.h"
55 #include "vmcore/linker.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/statistics.h"
63 #include "vmcore/suck.h"
64 #include "vmcore/utf8.h"
67 /* global variables ***********************************************************/
69 /* frequently used classes ****************************************************/
71 /* important system classes */
73 classinfo *class_java_lang_Object;
74 classinfo *class_java_lang_Class;
75 classinfo *class_java_lang_ClassLoader;
76 classinfo *class_java_lang_Cloneable;
77 classinfo *class_java_lang_SecurityManager;
78 classinfo *class_java_lang_String;
79 classinfo *class_java_lang_System;
80 classinfo *class_java_lang_Thread;
81 classinfo *class_java_lang_ThreadGroup;
82 classinfo *class_java_lang_VMSystem;
83 classinfo *class_java_lang_VMThread;
84 classinfo *class_java_io_Serializable;
86 #if defined(WITH_CLASSPATH_SUN)
87 classinfo *class_sun_reflect_MagicAccessorImpl;
90 /* system exception classes required in cacao */
92 classinfo *class_java_lang_Throwable;
93 classinfo *class_java_lang_Error;
94 classinfo *class_java_lang_LinkageError;
95 classinfo *class_java_lang_NoClassDefFoundError;
96 classinfo *class_java_lang_OutOfMemoryError;
97 classinfo *class_java_lang_VirtualMachineError;
99 #if defined(WITH_CLASSPATH_GNU)
100 classinfo *class_java_lang_VMThrowable;
103 classinfo *class_java_lang_Exception;
104 classinfo *class_java_lang_ClassCastException;
105 classinfo *class_java_lang_ClassNotFoundException;
107 #if defined(ENABLE_JAVASE)
108 classinfo *class_java_lang_Void;
110 classinfo *class_java_lang_Boolean;
111 classinfo *class_java_lang_Byte;
112 classinfo *class_java_lang_Character;
113 classinfo *class_java_lang_Short;
114 classinfo *class_java_lang_Integer;
115 classinfo *class_java_lang_Long;
116 classinfo *class_java_lang_Float;
117 classinfo *class_java_lang_Double;
120 /* some runtime exception */
122 classinfo *class_java_lang_NullPointerException;
125 /* some classes which may be used more often */
127 #if defined(ENABLE_JAVASE)
128 classinfo *class_java_lang_StackTraceElement;
129 classinfo *class_java_lang_reflect_Constructor;
130 classinfo *class_java_lang_reflect_Field;
131 classinfo *class_java_lang_reflect_Method;
132 classinfo *class_java_security_PrivilegedAction;
133 classinfo *class_java_util_Vector;
135 classinfo *arrayclass_java_lang_Object;
137 #if defined(ENABLE_ANNOTATIONS)
138 classinfo *class_sun_reflect_ConstantPool;
139 classinfo *class_sun_reflect_annotation_AnnotationParser;
144 /* pseudo classes for the typechecker */
146 classinfo *pseudo_class_Arraystub;
147 classinfo *pseudo_class_Null;
148 classinfo *pseudo_class_New;
151 /* class_set_packagename *******************************************************
153 Derive the package name from the class name and store it in the struct.
155 *******************************************************************************/
157 void class_set_packagename(classinfo *c)
159 char *p = UTF_END(c->name) - 1;
160 char *start = c->name->text;
162 /* set the package name */
163 /* classes in the unnamed package keep packagename == NULL */
165 if (c->name->text[0] == '[') {
166 /* set packagename of arrays to the element's package */
168 for (; *start == '['; start++);
170 /* skip the 'L' in arrays of references */
174 for (; (p > start) && (*p != '/'); --p);
176 c->packagename = utf_new(start, p - start);
179 for (; (p > start) && (*p != '/'); --p);
181 c->packagename = utf_new(start, p - start);
186 /* class_create_classinfo ******************************************************
188 Create a new classinfo struct. The class name is set to the given utf *,
189 most other fields are initialized to zero.
191 Note: classname may be NULL. In this case a not-yet-named classinfo is
192 created. The name must be filled in later and class_set_packagename
193 must be called after that.
195 *******************************************************************************/
197 classinfo *class_create_classinfo(utf *classname)
201 #if defined(ENABLE_STATISTICS)
203 size_classinfo += sizeof(classinfo);
206 /* we use a safe name for temporarily unnamed classes */
208 if (classname == NULL)
209 classname = utf_not_named_yet;
213 log_message_utf("Creating class: ", classname);
216 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
218 #if defined(ENABLE_GC_CACAO)
219 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
221 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
222 /*c=NEW(classinfo);*/
226 /* Set the header.vftbl of all loaded classes to the one of
227 java.lang.Class, so Java code can use a class as object. */
229 if (class_java_lang_Class != NULL)
230 if (class_java_lang_Class->vftbl != NULL)
231 c->object.header.vftbl = class_java_lang_Class->vftbl;
233 #if defined(ENABLE_JAVASE)
234 /* check if the class is a reference class and flag it */
236 if (classname == utf_java_lang_ref_SoftReference) {
237 c->flags |= ACC_CLASS_REFERENCE_SOFT;
239 else if (classname == utf_java_lang_ref_WeakReference) {
240 c->flags |= ACC_CLASS_REFERENCE_WEAK;
242 else if (classname == utf_java_lang_ref_PhantomReference) {
243 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
247 if (classname != utf_not_named_yet)
248 class_set_packagename(c);
250 LOCK_INIT_OBJECT_LOCK(&c->object.header);
256 /* class_postset_header_vftbl **************************************************
258 Set the header.vftbl of all classes created before java.lang.Class
259 was linked. This is necessary that Java code can use a class as
262 *******************************************************************************/
264 void class_postset_header_vftbl(void)
268 classcache_name_entry *nmen;
269 classcache_class_entry *clsen;
271 assert(class_java_lang_Class);
273 for (slot = 0; slot < hashtable_classcache.size; slot++) {
274 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
276 for (; nmen; nmen = nmen->hashlink) {
277 /* iterate over all class entries */
279 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
282 /* now set the the vftbl */
284 if (c->object.header.vftbl == NULL)
285 c->object.header.vftbl = class_java_lang_Class->vftbl;
291 /* class_define ****************************************************************
293 Calls the loader and defines a class in the VM.
295 *******************************************************************************/
297 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
304 /* check if this class has already been defined */
306 c = classcache_lookup_defined_or_initiated(cl, name);
309 exceptions_throw_linkageerror("duplicate class definition: ", c);
314 /* create a new classinfo struct */
316 c = class_create_classinfo(name);
318 #if defined(ENABLE_STATISTICS)
321 if (opt_getloadingtime)
325 /* build a classbuffer with the given data */
327 cb = NEW(classbuffer);
334 /* preset the defining classloader */
338 /* load the class from this buffer */
340 r = load_class_from_classbuffer(cb);
344 FREE(cb, classbuffer);
346 #if defined(ENABLE_STATISTICS)
349 if (opt_getloadingtime)
354 /* If return value is NULL, we had a problem and the class is
355 not loaded. Now free the allocated memory, otherwise we
356 could run into a DOS. */
363 /* Store the newly defined class in the class cache. This call
364 also checks whether a class of the same name has already been
365 defined by the same defining loader, and if so, replaces the
366 newly created class by the one defined earlier. */
368 /* Important: The classinfo given to classcache_store must be
369 fully prepared because another thread may return
370 this pointer after the lookup at to top of this
371 function directly after the class cache lock has
374 c = classcache_store(cl, c, true);
380 /* class_load_attribute_sourcefile *********************************************
382 SourceFile_attribute {
383 u2 attribute_name_index;
388 *******************************************************************************/
390 static bool class_load_attribute_sourcefile(classbuffer *cb)
401 /* check buffer size */
403 if (!suck_check_classbuffer_size(cb, 4 + 2))
406 /* check attribute length */
408 attribute_length = suck_u4(cb);
410 if (attribute_length != 2) {
411 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
415 /* there can be no more than one SourceFile attribute */
417 if (c->sourcefile != NULL) {
418 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
424 sourcefile_index = suck_u2(cb);
425 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
427 if (sourcefile == NULL)
430 /* store sourcefile */
432 c->sourcefile = sourcefile;
438 /* class_load_attribute_enclosingmethod ****************************************
440 EnclosingMethod_attribute {
441 u2 attribute_name_index;
447 *******************************************************************************/
449 #if defined(ENABLE_JAVASE)
450 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
456 classref_or_classinfo cr;
457 constant_nameandtype *cn;
463 /* check buffer size */
465 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
468 /* check attribute length */
470 attribute_length = suck_u4(cb);
472 if (attribute_length != 4) {
473 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
477 /* there can be no more than one EnclosingMethod attribute */
479 if (c->enclosingmethod != NULL) {
480 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
484 /* get class index */
486 class_index = suck_u2(cb);
487 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
489 /* get method index */
491 method_index = suck_u2(cb);
492 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
494 /* store info in classinfo */
496 c->enclosingclass.any = cr.any;
497 c->enclosingmethod = cn;
501 #endif /* defined(ENABLE_JAVASE) */
504 /* class_load_attributes *******************************************************
506 Read attributes from ClassFile.
509 u2 attribute_name_index;
511 u1 info[attribute_length];
514 InnerClasses_attribute {
515 u2 attribute_name_index;
519 *******************************************************************************/
521 bool class_load_attributes(classbuffer *cb)
526 u2 attribute_name_index;
531 /* get attributes count */
533 if (!suck_check_classbuffer_size(cb, 2))
536 attributes_count = suck_u2(cb);
538 for (i = 0; i < attributes_count; i++) {
539 /* get attribute name */
541 if (!suck_check_classbuffer_size(cb, 2))
544 attribute_name_index = suck_u2(cb);
546 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
548 if (attribute_name == NULL)
551 if (attribute_name == utf_InnerClasses) {
554 if (c->innerclass != NULL) {
555 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
559 if (!suck_check_classbuffer_size(cb, 4 + 2))
562 /* skip attribute length */
565 /* number of records */
566 c->innerclasscount = suck_u2(cb);
568 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
571 /* allocate memory for innerclass structure */
572 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
574 for (j = 0; j < c->innerclasscount; j++) {
575 /* The innerclass structure contains a class with an encoded
576 name, its defining scope, its simple name and a bitmask of
577 the access flags. If an inner class is not a member, its
578 outer_class is NULL, if a class is anonymous, its name is
581 innerclassinfo *info = c->innerclass + j;
583 info->inner_class.ref =
584 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
585 info->outer_class.ref =
586 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
588 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
589 info->flags = suck_u2(cb);
592 else if (attribute_name == utf_SourceFile) {
595 if (!class_load_attribute_sourcefile(cb))
598 #if defined(ENABLE_JAVASE)
599 else if (attribute_name == utf_EnclosingMethod) {
600 /* EnclosingMethod */
602 if (!class_load_attribute_enclosingmethod(cb))
605 else if (attribute_name == utf_Signature) {
608 if (!loader_load_attribute_signature(cb, &(c->signature)))
613 #if defined(ENABLE_ANNOTATIONS)
614 /* XXX We can't do a release with that enabled */
616 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
617 /* RuntimeVisibleAnnotations */
618 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
621 /* XXX RuntimeInvisibleAnnotations should only be loaded
622 * (or returned to Java) if some commandline options says so.
623 * Currently there is no such option available in cacao,
624 * therefore I load them allways (for testing purpose).
625 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
626 * generated if you tell javac to do so. So in most cases
627 * there won't be any.
629 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
630 /* RuntimeInvisibleAnnotations */
631 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
637 /* unknown attribute */
639 if (!loader_skip_attribute_body(cb))
648 /* class_freepool **************************************************************
650 Frees all resources used by this classes Constant Pool.
652 *******************************************************************************/
654 static void class_freecpool(classinfo *c)
660 if (c->cptags && c->cpinfos) {
661 for (idx = 0; idx < c->cpcount; idx++) {
662 tag = c->cptags[idx];
663 info = c->cpinfos[idx];
667 case CONSTANT_Fieldref:
668 case CONSTANT_Methodref:
669 case CONSTANT_InterfaceMethodref:
670 FREE(info, constant_FMIref);
672 case CONSTANT_Integer:
673 FREE(info, constant_integer);
676 FREE(info, constant_float);
679 FREE(info, constant_long);
681 case CONSTANT_Double:
682 FREE(info, constant_double);
684 case CONSTANT_NameAndType:
685 FREE(info, constant_nameandtype);
693 MFREE(c->cptags, u1, c->cpcount);
696 MFREE(c->cpinfos, voidptr, c->cpcount);
700 /* class_getconstant ***********************************************************
702 Retrieves the value at position 'pos' of the constantpool of a
703 class. If the type of the value is other than 'ctype', an error is
706 *******************************************************************************/
708 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
710 /* check index and type of constantpool entry */
711 /* (pos == 0 is caught by type comparison) */
713 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
714 exceptions_throw_classformaterror(c, "Illegal constant pool index");
718 return c->cpinfos[pos];
722 /* innerclass_getconstant ******************************************************
724 Like class_getconstant, but if cptags is ZERO, null is returned.
726 *******************************************************************************/
728 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
730 /* invalid position in constantpool */
732 if (pos >= c->cpcount) {
733 exceptions_throw_classformaterror(c, "Illegal constant pool index");
737 /* constantpool entry of type 0 */
739 if (c->cptags[pos] == 0)
742 /* check type of constantpool entry */
744 if (c->cptags[pos] != ctype) {
745 exceptions_throw_classformaterror(c, "Illegal constant pool index");
749 return c->cpinfos[pos];
753 /* class_free ******************************************************************
755 Frees all resources used by the class.
757 *******************************************************************************/
759 void class_free(classinfo *c)
767 MFREE(c->interfaces, classinfo*, c->interfacescount);
770 for (i = 0; i < c->fieldscount; i++)
771 field_free(&(c->fields[i]));
772 #if defined(ENABLE_CACAO_GC)
773 MFREE(c->fields, fieldinfo, c->fieldscount);
778 for (i = 0; i < c->methodscount; i++)
779 method_free(&(c->methods[i]));
780 MFREE(c->methods, methodinfo, c->methodscount);
783 if ((v = c->vftbl) != NULL) {
785 mem_free(v->arraydesc,sizeof(arraydescriptor));
787 for (i = 0; i < v->interfacetablelength; i++) {
788 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
790 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
792 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
793 sizeof(methodptr*) * (v->interfacetablelength -
794 (v->interfacetablelength > 0));
795 v = (vftbl_t*) (((methodptr*) v) -
796 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
801 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
803 /* if (c->classvftbl)
804 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
808 #if defined(ENABLE_ANNOTATIONS)
809 annotation_bytearray_free(c->annotations);
811 annotation_bytearrays_free(c->method_annotations);
812 annotation_bytearrays_free(c->method_parameterannotations);
813 annotation_bytearrays_free(c->method_annotationdefaults);
815 annotation_bytearrays_free(c->field_annotations);
820 /* get_array_class *************************************************************
822 Returns the array class with the given name for the given
823 classloader, or NULL if an exception occurred.
825 Note: This function does eager loading.
827 *******************************************************************************/
829 static classinfo *get_array_class(utf *name,classloader *initloader,
830 classloader *defloader,bool link)
834 /* lookup this class in the classcache */
835 c = classcache_lookup(initloader,name);
837 c = classcache_lookup_defined(defloader,name);
840 /* we have to create it */
841 c = class_create_classinfo(name);
842 c = load_newly_created_array(c,initloader);
848 assert(c->state & CLASS_LOADED);
849 assert(c->classloader == defloader);
851 if (link && !(c->state & CLASS_LINKED))
855 assert(!link || (c->state & CLASS_LINKED));
861 /* class_array_of **************************************************************
863 Returns an array class with the given component class. The array
864 class is dynamically created if neccessary.
866 *******************************************************************************/
868 classinfo *class_array_of(classinfo *component, bool link)
877 cl = component->classloader;
879 dumpsize = dump_size();
881 /* Assemble the array class name */
882 namelen = component->name->blength;
884 if (component->name->text[0] == '[') {
885 /* the component is itself an array */
886 namebuf = DMNEW(char, namelen + 1);
888 MCOPY(namebuf + 1, component->name->text, char, namelen);
892 /* the component is a non-array class */
893 namebuf = DMNEW(char, namelen + 3);
896 MCOPY(namebuf + 2, component->name->text, char, namelen);
897 namebuf[2 + namelen] = ';';
901 u = utf_new(namebuf, namelen);
903 c = get_array_class(u, cl, cl, link);
905 dump_release(dumpsize);
911 /* class_multiarray_of *********************************************************
913 Returns an array class with the given dimension and element class.
914 The array class is dynamically created if neccessary.
916 *******************************************************************************/
918 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
925 dumpsize = dump_size();
928 log_text("Invalid array dimension requested");
932 /* Assemble the array class name */
933 namelen = element->name->blength;
935 if (element->name->text[0] == '[') {
936 /* the element is itself an array */
937 namebuf = DMNEW(char, namelen + dim);
938 memcpy(namebuf + dim, element->name->text, namelen);
942 /* the element is a non-array class */
943 namebuf = DMNEW(char, namelen + 2 + dim);
945 memcpy(namebuf + dim + 1, element->name->text, namelen);
946 namelen += (2 + dim);
947 namebuf[namelen - 1] = ';';
949 memset(namebuf, '[', dim);
951 c = get_array_class(utf_new(namebuf, namelen),
952 element->classloader,
953 element->classloader,
956 dump_release(dumpsize);
962 /* class_lookup_classref *******************************************************
964 Looks up the constant_classref for a given classname in the classref
968 cls..............the class containing the reference
969 name.............the name of the class refered to
972 a pointer to a constant_classref, or
973 NULL if the reference was not found
975 *******************************************************************************/
977 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
979 constant_classref *ref;
980 extra_classref *xref;
985 assert(!cls->classrefcount || cls->classrefs);
987 /* first search the main classref table */
988 count = cls->classrefcount;
989 ref = cls->classrefs;
990 for (; count; --count, ++ref)
991 if (ref->name == name)
994 /* next try the list of extra classrefs */
995 for (xref = cls->extclassrefs; xref; xref = xref->next) {
996 if (xref->classref.name == name)
997 return &(xref->classref);
1005 /* class_get_classref **********************************************************
1007 Returns the constant_classref for a given classname.
1010 cls..............the class containing the reference
1011 name.............the name of the class refered to
1014 a pointer to a constant_classref (never NULL)
1017 The given name is not checked for validity!
1019 *******************************************************************************/
1021 constant_classref *class_get_classref(classinfo *cls, utf *name)
1023 constant_classref *ref;
1024 extra_classref *xref;
1029 ref = class_lookup_classref(cls,name);
1033 xref = NEW(extra_classref);
1034 CLASSREF_INIT(xref->classref,cls,name);
1036 xref->next = cls->extclassrefs;
1037 cls->extclassrefs = xref;
1039 return &(xref->classref);
1043 /* class_get_self_classref *****************************************************
1045 Returns the constant_classref to the class itself.
1048 cls..............the class containing the reference
1051 a pointer to a constant_classref (never NULL)
1053 *******************************************************************************/
1055 constant_classref *class_get_self_classref(classinfo *cls)
1057 /* XXX this should be done in a faster way. Maybe always make */
1058 /* the classref of index 0 a self reference. */
1059 return class_get_classref(cls,cls->name);
1062 /* class_get_classref_multiarray_of ********************************************
1064 Returns an array type reference with the given dimension and element class
1068 dim..............the requested dimension
1069 dim must be in [1;255]. This is NOT checked!
1070 ref..............the component class reference
1073 a pointer to the class reference for the array type
1076 The referer of `ref` is used as the referer for the new classref.
1078 *******************************************************************************/
1080 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1085 constant_classref *cr;
1088 assert(dim >= 1 && dim <= 255);
1090 dumpsize = dump_size();
1092 /* Assemble the array class name */
1093 namelen = ref->name->blength;
1095 if (ref->name->text[0] == '[') {
1096 /* the element is itself an array */
1097 namebuf = DMNEW(char, namelen + dim);
1098 memcpy(namebuf + dim, ref->name->text, namelen);
1102 /* the element is a non-array class */
1103 namebuf = DMNEW(char, namelen + 2 + dim);
1105 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1106 namelen += (2 + dim);
1107 namebuf[namelen - 1] = ';';
1109 memset(namebuf, '[', dim);
1111 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1113 dump_release(dumpsize);
1119 /* class_get_classref_component_of *********************************************
1121 Returns the component classref of a given array type reference
1124 ref..............the array type reference
1127 a reference to the component class, or
1128 NULL if `ref` is not an object array type reference
1131 The referer of `ref` is used as the referer for the new classref.
1133 *******************************************************************************/
1135 constant_classref *class_get_classref_component_of(constant_classref *ref)
1142 name = ref->name->text;
1146 namelen = ref->name->blength - 1;
1151 else if (*name != '[') {
1155 return class_get_classref(ref->referer, utf_new(name, namelen));
1159 /* class_findmethod ************************************************************
1161 Searches a 'classinfo' structure for a method having the given name
1162 and descriptor. If descriptor is NULL, it is ignored.
1164 *******************************************************************************/
1166 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1171 for (i = 0; i < c->methodscount; i++) {
1172 m = &(c->methods[i]);
1174 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1182 /* class_resolvemethod *********************************************************
1184 Searches a class and it's super classes for a method.
1186 Superinterfaces are *not* searched.
1188 *******************************************************************************/
1190 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1195 m = class_findmethod(c, name, desc);
1200 /* JVM Specification bug:
1202 It is important NOT to resolve special <init> and <clinit>
1203 methods to super classes or interfaces; yet, this is not
1204 explicited in the specification. Section 5.4.3.3 should be
1205 updated appropriately. */
1207 if (name == utf_init || name == utf_clinit)
1217 /* class_resolveinterfacemethod_intern *****************************************
1219 Internally used helper function. Do not use this directly.
1221 *******************************************************************************/
1223 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1224 utf *name, utf *desc)
1229 /* try to find the method in the class */
1231 m = class_findmethod(c, name, desc);
1236 /* no method found? try the superinterfaces */
1238 for (i = 0; i < c->interfacescount; i++) {
1239 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1246 /* no method found */
1252 /* class_resolveclassmethod ****************************************************
1254 Resolves a reference from REFERER to a method with NAME and DESC in
1257 If the method cannot be resolved the return value is NULL. If
1258 EXCEPT is true *exceptionptr is set, too.
1260 *******************************************************************************/
1262 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1263 classinfo *referer, bool throwexception)
1269 /* if (c->flags & ACC_INTERFACE) { */
1270 /* if (throwexception) */
1271 /* *exceptionptr = */
1272 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1276 /* try class c and its superclasses */
1280 m = class_resolvemethod(cls, name, desc);
1285 /* try the superinterfaces */
1287 for (i = 0; i < c->interfacescount; i++) {
1288 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1296 exceptions_throw_nosuchmethoderror(c, name, desc);
1301 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1303 exceptions_throw_abstractmethoderror();
1308 /* XXX check access rights */
1314 /* class_resolveinterfacemethod ************************************************
1316 Resolves a reference from REFERER to a method with NAME and DESC in
1319 If the method cannot be resolved the return value is NULL. If
1320 EXCEPT is true *exceptionptr is set, too.
1322 *******************************************************************************/
1324 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1325 classinfo *referer, bool throwexception)
1329 if (!(c->flags & ACC_INTERFACE)) {
1331 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1336 mi = class_resolveinterfacemethod_intern(c, name, desc);
1341 /* try class java.lang.Object */
1343 mi = class_findmethod(class_java_lang_Object, name, desc);
1349 exceptions_throw_nosuchmethoderror(c, name, desc);
1355 /* class_findfield *************************************************************
1357 Searches for field with specified name and type in a classinfo
1358 structure. If no such field is found NULL is returned.
1360 *******************************************************************************/
1362 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1366 for (i = 0; i < c->fieldscount; i++)
1367 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1368 return &(c->fields[i]);
1371 return class_findfield(c->super.cls, name, desc);
1377 /* class_findfield_approx ******************************************************
1379 Searches in 'classinfo'-structure for a field with the specified
1382 *******************************************************************************/
1384 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1388 /* get field index */
1390 i = class_findfield_index_by_name(c, name);
1392 /* field was not found, return */
1397 /* return field address */
1399 return &(c->fields[i]);
1403 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1407 for (i = 0; i < c->fieldscount; i++) {
1408 /* compare field names */
1410 if ((c->fields[i].name == name))
1414 /* field was not found, raise exception */
1416 exceptions_throw_nosuchfielderror(c, name);
1422 /****************** Function: class_resolvefield_int ***************************
1424 This is an internally used helper function. Do not use this directly.
1426 Tries to resolve a field having the given name and type.
1427 If the field cannot be resolved, NULL is returned.
1429 *******************************************************************************/
1431 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1436 /* search for field in class c */
1438 for (i = 0; i < c->fieldscount; i++) {
1439 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1440 return &(c->fields[i]);
1444 /* try superinterfaces recursively */
1446 for (i = 0; i < c->interfacescount; i++) {
1447 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1452 /* try superclass */
1455 return class_resolvefield_int(c->super.cls, name, desc);
1463 /********************* Function: class_resolvefield ***************************
1465 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1467 If the field cannot be resolved the return value is NULL. If EXCEPT is
1468 true *exceptionptr is set, too.
1470 *******************************************************************************/
1472 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1473 classinfo *referer, bool throwexception)
1477 fi = class_resolvefield_int(c, name, desc);
1481 exceptions_throw_nosuchfielderror(c, name);
1486 /* XXX check access rights */
1492 /* class_resolve_superclass ****************************************************
1494 Resolves the super class reference of the given class if necessary.
1496 *******************************************************************************/
1498 static classinfo *class_resolve_superclass(classinfo *c)
1502 if (c->super.any == NULL)
1505 /* Do we have a super class reference or is it already
1508 if (IS_CLASSREF(c->super)) {
1509 super = resolve_classref_or_classinfo_eager(c->super, true);
1514 /* Store the resolved super class in the class structure. */
1516 c->super.cls = super;
1519 return c->super.cls;
1523 /* class_issubclass ************************************************************
1525 Checks if sub is a descendant of super.
1527 *******************************************************************************/
1529 bool class_issubclass(classinfo *sub, classinfo *super)
1538 sub = class_resolve_superclass(sub);
1543 /* class_isanysubclass *********************************************************
1545 Checks a subclass relation between two classes. Implemented
1546 interfaces are interpreted as super classes.
1548 Return value: 1 ... sub is subclass of super
1551 *******************************************************************************/
1553 bool class_isanysubclass(classinfo *sub, classinfo *super)
1558 /* This is the trivial case. */
1563 /* Primitive classes are only subclasses of themselves. */
1565 if (class_is_primitive(sub) || class_is_primitive(super))
1568 /* Check for interfaces. */
1570 if (super->flags & ACC_INTERFACE) {
1571 result = (sub->vftbl->interfacetablelength > super->index) &&
1572 (sub->vftbl->interfacetable[-super->index] != NULL);
1575 /* java.lang.Object is the only super class of any
1578 if (sub->flags & ACC_INTERFACE)
1579 return (super == class_java_lang_Object);
1581 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1583 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1584 result = diffval <= (uint32_t) super->vftbl->diffval;
1586 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1593 /* class_is_primitive **********************************************************
1595 Checks if the given class is a primitive class.
1597 *******************************************************************************/
1599 bool class_is_primitive(classinfo *c)
1601 if (c->flags & ACC_CLASS_PRIMITIVE)
1608 /* class_is_array **************************************************************
1610 Checks if the given class is an array class.
1612 *******************************************************************************/
1614 bool class_is_array(classinfo *c)
1616 if (!(c->state & CLASS_LINKED))
1620 return (c->vftbl->arraydesc != NULL);
1624 /* class_is_interface **********************************************************
1626 Checks if the given class is an interface.
1628 *******************************************************************************/
1630 bool class_is_interface(classinfo *c)
1632 if (c->flags & ACC_INTERFACE)
1639 /* class_get_superclass ********************************************************
1641 Return the super class of the given class. If the super-field is a
1642 class-reference, resolve it and store it in the classinfo.
1644 *******************************************************************************/
1646 classinfo *class_get_superclass(classinfo *c)
1650 /* For java.lang.Object, primitive and Void classes we return
1653 if (c->super.any == NULL)
1656 /* For interfaces we also return NULL. */
1658 if (c->flags & ACC_INTERFACE)
1661 /* We may have to resolve the super class reference. */
1663 super = class_resolve_superclass(c);
1669 /* class_get_declaringclass ****************************************************
1671 If the class or interface given is a member of another class,
1672 return the declaring class. For array and primitive classes return
1675 *******************************************************************************/
1677 classinfo *class_get_declaringclass(classinfo *c)
1679 classref_or_classinfo innercr;
1681 classref_or_classinfo outercr;
1685 /* return NULL for arrayclasses and primitive classes */
1687 if (class_is_primitive(c) || (c->name->text[0] == '['))
1690 /* no innerclasses exist */
1692 if (c->innerclasscount == 0)
1695 for (i = 0; i < c->innerclasscount; i++) {
1696 /* Check if inner_class is a classref or a real class and get
1697 the class name from the structure. */
1699 innercr = c->innerclass[i].inner_class;
1701 innername = IS_CLASSREF(innercr) ?
1702 innercr.ref->name : innercr.cls->name;
1704 /* Is the current innerclass this class? */
1706 if (innername == c->name) {
1707 /* Maybe the outer class is not loaded yet. */
1709 outercr = c->innerclass[i].outer_class;
1711 outer = resolve_classref_or_classinfo_eager(outercr, false);
1716 if (!(outer->state & CLASS_LINKED))
1717 if (!link_class(outer))
1728 /* class_get_signature *********************************************************
1730 Return the signature of the given class. For array and primitive
1731 classes return NULL.
1733 *******************************************************************************/
1735 #if defined(ENABLE_JAVASE)
1736 utf *class_get_signature(classinfo *c)
1738 /* For array and primitive classes return NULL. */
1740 if (class_is_array(c) || class_is_primitive(c))
1743 return c->signature;
1748 /* class_printflags ************************************************************
1750 Prints flags of a class.
1752 *******************************************************************************/
1754 #if !defined(NDEBUG)
1755 void class_printflags(classinfo *c)
1762 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1763 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1764 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1765 if (c->flags & ACC_STATIC) printf(" STATIC");
1766 if (c->flags & ACC_FINAL) printf(" FINAL");
1767 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1768 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1769 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1770 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1771 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1772 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1777 /* class_print *****************************************************************
1779 Prints classname plus flags.
1781 *******************************************************************************/
1783 #if !defined(NDEBUG)
1784 void class_print(classinfo *c)
1791 utf_display_printable_ascii(c->name);
1792 class_printflags(c);
1797 /* class_classref_print ********************************************************
1799 Prints classname plus referer class.
1801 *******************************************************************************/
1803 #if !defined(NDEBUG)
1804 void class_classref_print(constant_classref *cr)
1811 utf_display_printable_ascii(cr->name);
1814 class_print(cr->referer);
1822 /* class_println ***************************************************************
1824 Prints classname plus flags and new line.
1826 *******************************************************************************/
1828 #if !defined(NDEBUG)
1829 void class_println(classinfo *c)
1837 /* class_classref_println ******************************************************
1839 Prints classname plus referer class and new line.
1841 *******************************************************************************/
1843 #if !defined(NDEBUG)
1844 void class_classref_println(constant_classref *cr)
1846 class_classref_print(cr);
1852 /* class_classref_or_classinfo_print *******************************************
1854 Prints classname plus referer class.
1856 *******************************************************************************/
1858 #if !defined(NDEBUG)
1859 void class_classref_or_classinfo_print(classref_or_classinfo c)
1861 if (c.any == NULL) {
1862 printf("(classref_or_classinfo) NULL");
1866 class_classref_print(c.ref);
1873 /* class_classref_or_classinfo_println *****************************************
1875 Prints classname plus referer class and a newline.
1877 *******************************************************************************/
1879 void class_classref_or_classinfo_println(classref_or_classinfo c)
1881 class_classref_or_classinfo_println(c);
1886 /* class_showconstantpool ******************************************************
1888 Dump the constant pool of the given class to stdout.
1890 *******************************************************************************/
1892 #if !defined(NDEBUG)
1893 void class_showconstantpool (classinfo *c)
1898 printf ("---- dump of constant pool ----\n");
1900 for (i=0; i<c->cpcount; i++) {
1901 printf ("#%d: ", (int) i);
1903 e = c -> cpinfos [i];
1906 switch (c -> cptags [i]) {
1907 case CONSTANT_Class:
1908 printf ("Classreference -> ");
1909 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
1911 case CONSTANT_Fieldref:
1912 printf ("Fieldref -> ");
1913 field_fieldref_print((constant_FMIref *) e);
1915 case CONSTANT_Methodref:
1916 printf ("Methodref -> ");
1917 method_methodref_print((constant_FMIref *) e);
1919 case CONSTANT_InterfaceMethodref:
1920 printf ("InterfaceMethod -> ");
1921 method_methodref_print((constant_FMIref *) e);
1923 case CONSTANT_String:
1924 printf ("String -> ");
1925 utf_display_printable_ascii (e);
1927 case CONSTANT_Integer:
1928 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
1930 case CONSTANT_Float:
1931 printf ("Float -> %f", ((constant_float*)e) -> value);
1933 case CONSTANT_Double:
1934 printf ("Double -> %f", ((constant_double*)e) -> value);
1938 u8 v = ((constant_long*)e) -> value;
1940 printf ("Long -> %ld", (long int) v);
1942 printf ("Long -> HI: %ld, LO: %ld\n",
1943 (long int) v.high, (long int) v.low);
1947 case CONSTANT_NameAndType:
1949 constant_nameandtype *cnt = e;
1950 printf ("NameAndType: ");
1951 utf_display_printable_ascii (cnt->name);
1953 utf_display_printable_ascii (cnt->descriptor);
1957 printf ("Utf8 -> ");
1958 utf_display_printable_ascii (e);
1961 log_text("Invalid type of ConstantPool-Entry");
1969 #endif /* !defined(NDEBUG) */
1972 /* class_showmethods ***********************************************************
1974 Dump info about the fields and methods of the given class to stdout.
1976 *******************************************************************************/
1978 #if !defined(NDEBUG)
1979 void class_showmethods (classinfo *c)
1983 printf("--------- Fields and Methods ----------------\n");
1985 class_printflags(c);
1989 utf_display_printable_ascii(c->name);
1994 utf_display_printable_ascii(c->super.cls->name);
1998 printf("Index: %d\n", c->index);
2000 printf("Interfaces:\n");
2001 for (i = 0; i < c->interfacescount; i++) {
2003 utf_display_printable_ascii(c->interfaces[i].cls->name);
2004 printf (" (%d)\n", c->interfaces[i].cls->index);
2007 printf("Fields:\n");
2008 for (i = 0; i < c->fieldscount; i++)
2009 field_println(&(c->fields[i]));
2011 printf("Methods:\n");
2012 for (i = 0; i < c->methodscount; i++) {
2013 methodinfo *m = &(c->methods[i]);
2015 if (!(m->flags & ACC_STATIC))
2016 printf("vftblindex: %d ", m->vftblindex);
2021 printf ("Virtual function table:\n");
2022 for (i = 0; i < c->vftbl->vftbllength; i++)
2023 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2025 #endif /* !defined(NDEBUG) */
2029 * These are local overrides for various environment variables in Emacs.
2030 * Please do not remove this and leave it at the end of the file, where
2031 * Emacs will automagically detect them.
2032 * ---------------------------------------------------------------------
2035 * indent-tabs-mode: t
2039 * vim:noexpandtab:sw=4:ts=4: