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 8318 2007-08-16 10:05:34Z michi $
41 #include "mm/memory.h"
43 #include "native/llni.h"
45 #include "threads/lock-common.h"
47 #include "toolbox/logging.h"
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/global.h"
52 #include "vm/resolve.h"
54 #include "vm/jit/asmpart.h"
56 #include "vmcore/class.h"
57 #include "vmcore/classcache.h"
58 #include "vmcore/linker.h"
59 #include "vmcore/loader.h"
60 #include "vmcore/options.h"
62 #if defined(ENABLE_STATISTICS)
63 # include "vmcore/statistics.h"
66 #include "vmcore/suck.h"
67 #include "vmcore/utf8.h"
70 /* global variables ***********************************************************/
72 /* frequently used classes ****************************************************/
74 /* important system classes */
76 classinfo *class_java_lang_Object;
77 classinfo *class_java_lang_Class;
78 classinfo *class_java_lang_ClassLoader;
79 classinfo *class_java_lang_Cloneable;
80 classinfo *class_java_lang_SecurityManager;
81 classinfo *class_java_lang_String;
82 classinfo *class_java_lang_System;
83 classinfo *class_java_lang_Thread;
84 classinfo *class_java_lang_ThreadGroup;
85 classinfo *class_java_lang_VMSystem;
86 classinfo *class_java_lang_VMThread;
87 classinfo *class_java_io_Serializable;
89 #if defined(WITH_CLASSPATH_SUN)
90 classinfo *class_sun_reflect_MagicAccessorImpl;
93 /* system exception classes required in cacao */
95 classinfo *class_java_lang_Throwable;
96 classinfo *class_java_lang_Error;
97 classinfo *class_java_lang_LinkageError;
98 classinfo *class_java_lang_NoClassDefFoundError;
99 classinfo *class_java_lang_OutOfMemoryError;
100 classinfo *class_java_lang_VirtualMachineError;
102 #if defined(WITH_CLASSPATH_GNU)
103 classinfo *class_java_lang_VMThrowable;
106 classinfo *class_java_lang_Exception;
107 classinfo *class_java_lang_ClassCastException;
108 classinfo *class_java_lang_ClassNotFoundException;
110 #if defined(ENABLE_JAVASE)
111 classinfo *class_java_lang_Void;
113 classinfo *class_java_lang_Boolean;
114 classinfo *class_java_lang_Byte;
115 classinfo *class_java_lang_Character;
116 classinfo *class_java_lang_Short;
117 classinfo *class_java_lang_Integer;
118 classinfo *class_java_lang_Long;
119 classinfo *class_java_lang_Float;
120 classinfo *class_java_lang_Double;
123 /* some runtime exception */
125 classinfo *class_java_lang_NullPointerException;
128 /* some classes which may be used more often */
130 #if defined(ENABLE_JAVASE)
131 classinfo *class_java_lang_StackTraceElement;
132 classinfo *class_java_lang_reflect_Constructor;
133 classinfo *class_java_lang_reflect_Field;
134 classinfo *class_java_lang_reflect_Method;
135 classinfo *class_java_security_PrivilegedAction;
136 classinfo *class_java_util_Vector;
138 classinfo *arrayclass_java_lang_Object;
140 #if defined(ENABLE_ANNOTATIONS)
141 classinfo *class_sun_reflect_ConstantPool;
142 classinfo *class_sun_reflect_annotation_AnnotationParser;
147 /* pseudo classes for the typechecker */
149 classinfo *pseudo_class_Arraystub;
150 classinfo *pseudo_class_Null;
151 classinfo *pseudo_class_New;
154 /* class_set_packagename *******************************************************
156 Derive the package name from the class name and store it in the struct.
158 *******************************************************************************/
160 void class_set_packagename(classinfo *c)
162 char *p = UTF_END(c->name) - 1;
163 char *start = c->name->text;
165 /* set the package name */
166 /* classes in the unnamed package keep packagename == NULL */
168 if (c->name->text[0] == '[') {
169 /* set packagename of arrays to the element's package */
171 for (; *start == '['; start++);
173 /* skip the 'L' in arrays of references */
177 for (; (p > start) && (*p != '/'); --p);
179 c->packagename = utf_new(start, p - start);
182 for (; (p > start) && (*p != '/'); --p);
184 c->packagename = utf_new(start, p - start);
189 /* class_create_classinfo ******************************************************
191 Create a new classinfo struct. The class name is set to the given utf *,
192 most other fields are initialized to zero.
194 Note: classname may be NULL. In this case a not-yet-named classinfo is
195 created. The name must be filled in later and class_set_packagename
196 must be called after that.
198 *******************************************************************************/
200 classinfo *class_create_classinfo(utf *classname)
204 #if defined(ENABLE_STATISTICS)
206 size_classinfo += sizeof(classinfo);
209 /* we use a safe name for temporarily unnamed classes */
211 if (classname == NULL)
212 classname = utf_not_named_yet;
216 log_message_utf("Creating class: ", classname);
219 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
221 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
222 /*c=NEW(classinfo);*/
225 /* Set the header.vftbl of all loaded classes to the one of
226 java.lang.Class, so Java code can use a class as object. */
228 if (class_java_lang_Class != NULL)
229 if (class_java_lang_Class->vftbl != NULL)
230 c->object.header.vftbl = class_java_lang_Class->vftbl;
232 #if defined(ENABLE_JAVASE)
233 /* check if the class is a reference class and flag it */
235 if (classname == utf_java_lang_ref_SoftReference) {
236 c->flags |= ACC_CLASS_REFERENCE_SOFT;
238 else if (classname == utf_java_lang_ref_WeakReference) {
239 c->flags |= ACC_CLASS_REFERENCE_WEAK;
241 else if (classname == utf_java_lang_ref_PhantomReference) {
242 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
246 if (classname != utf_not_named_yet)
247 class_set_packagename(c);
249 LOCK_INIT_OBJECT_LOCK(&c->object.header);
255 /* class_postset_header_vftbl **************************************************
257 Set the header.vftbl of all classes created before java.lang.Class
258 was linked. This is necessary that Java code can use a class as
261 *******************************************************************************/
263 void class_postset_header_vftbl(void)
267 classcache_name_entry *nmen;
268 classcache_class_entry *clsen;
270 assert(class_java_lang_Class);
272 for (slot = 0; slot < hashtable_classcache.size; slot++) {
273 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
275 for (; nmen; nmen = nmen->hashlink) {
276 /* iterate over all class entries */
278 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
281 /* now set the the vftbl */
283 if (c->object.header.vftbl == NULL)
284 c->object.header.vftbl = class_java_lang_Class->vftbl;
290 /* class_define ****************************************************************
292 Calls the loader and defines a class in the VM.
294 *******************************************************************************/
296 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
303 /* check if this class has already been defined */
305 c = classcache_lookup_defined_or_initiated(cl, name);
308 exceptions_throw_linkageerror("duplicate class definition: ", c);
313 /* create a new classinfo struct */
315 c = class_create_classinfo(name);
317 #if defined(ENABLE_STATISTICS)
320 if (opt_getloadingtime)
324 /* build a classbuffer with the given data */
326 cb = NEW(classbuffer);
333 /* preset the defining classloader */
337 /* load the class from this buffer */
339 r = load_class_from_classbuffer(cb);
343 FREE(cb, classbuffer);
345 #if defined(ENABLE_STATISTICS)
348 if (opt_getloadingtime)
353 /* If return value is NULL, we had a problem and the class is
354 not loaded. Now free the allocated memory, otherwise we
355 could run into a DOS. */
362 /* Store the newly defined class in the class cache. This call
363 also checks whether a class of the same name has already been
364 defined by the same defining loader, and if so, replaces the
365 newly created class by the one defined earlier. */
367 /* Important: The classinfo given to classcache_store must be
368 fully prepared because another thread may return
369 this pointer after the lookup at to top of this
370 function directly after the class cache lock has
373 c = classcache_store(cl, c, true);
379 /* class_load_attribute_sourcefile *********************************************
381 SourceFile_attribute {
382 u2 attribute_name_index;
387 *******************************************************************************/
389 static bool class_load_attribute_sourcefile(classbuffer *cb)
400 /* check buffer size */
402 if (!suck_check_classbuffer_size(cb, 4 + 2))
405 /* check attribute length */
407 attribute_length = suck_u4(cb);
409 if (attribute_length != 2) {
410 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
414 /* there can be no more than one SourceFile attribute */
416 if (c->sourcefile != NULL) {
417 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
423 sourcefile_index = suck_u2(cb);
424 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
426 if (sourcefile == NULL)
429 /* store sourcefile */
431 c->sourcefile = sourcefile;
437 /* class_load_attribute_enclosingmethod ****************************************
439 EnclosingMethod_attribute {
440 u2 attribute_name_index;
446 *******************************************************************************/
448 #if defined(ENABLE_JAVASE)
449 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
455 classref_or_classinfo cr;
456 constant_nameandtype *cn;
462 /* check buffer size */
464 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
467 /* check attribute length */
469 attribute_length = suck_u4(cb);
471 if (attribute_length != 4) {
472 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
476 /* there can be no more than one EnclosingMethod attribute */
478 if (c->enclosingmethod != NULL) {
479 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
483 /* get class index */
485 class_index = suck_u2(cb);
486 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
488 /* get method index */
490 method_index = suck_u2(cb);
491 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
493 /* store info in classinfo */
495 c->enclosingclass.any = cr.any;
496 c->enclosingmethod = cn;
500 #endif /* defined(ENABLE_JAVASE) */
503 /* class_load_attributes *******************************************************
505 Read attributes from ClassFile.
508 u2 attribute_name_index;
510 u1 info[attribute_length];
513 InnerClasses_attribute {
514 u2 attribute_name_index;
518 *******************************************************************************/
520 bool class_load_attributes(classbuffer *cb)
525 u2 attribute_name_index;
530 /* get attributes count */
532 if (!suck_check_classbuffer_size(cb, 2))
535 attributes_count = suck_u2(cb);
537 for (i = 0; i < attributes_count; i++) {
538 /* get attribute name */
540 if (!suck_check_classbuffer_size(cb, 2))
543 attribute_name_index = suck_u2(cb);
545 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
547 if (attribute_name == NULL)
550 if (attribute_name == utf_InnerClasses) {
553 if (c->innerclass != NULL) {
554 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
558 if (!suck_check_classbuffer_size(cb, 4 + 2))
561 /* skip attribute length */
564 /* number of records */
565 c->innerclasscount = suck_u2(cb);
567 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
570 /* allocate memory for innerclass structure */
571 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
573 for (j = 0; j < c->innerclasscount; j++) {
574 /* The innerclass structure contains a class with an encoded
575 name, its defining scope, its simple name and a bitmask of
576 the access flags. If an inner class is not a member, its
577 outer_class is NULL, if a class is anonymous, its name is
580 innerclassinfo *info = c->innerclass + j;
582 info->inner_class.ref =
583 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
584 info->outer_class.ref =
585 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
587 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
588 info->flags = suck_u2(cb);
591 else if (attribute_name == utf_SourceFile) {
594 if (!class_load_attribute_sourcefile(cb))
597 #if defined(ENABLE_JAVASE)
598 else if (attribute_name == utf_EnclosingMethod) {
599 /* EnclosingMethod */
601 if (!class_load_attribute_enclosingmethod(cb))
604 else if (attribute_name == utf_Signature) {
607 if (!loader_load_attribute_signature(cb, &(c->signature)))
612 #if defined(ENABLE_ANNOTATIONS)
613 /* XXX We can't do a release with that enabled */
615 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
616 /* RuntimeVisibleAnnotations */
617 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
620 /* XXX RuntimeInvisibleAnnotations should only be loaded
621 * (or returned to Java) if some commandline options says so.
622 * Currently there is no such option available in cacao,
623 * therefore I load them allways (for testing purpose).
624 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
625 * generated if you tell javac to do so. So in most cases
626 * there won't be any.
628 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
629 /* RuntimeInvisibleAnnotations */
630 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
636 /* unknown attribute */
638 if (!loader_skip_attribute_body(cb))
647 /* class_freepool **************************************************************
649 Frees all resources used by this classes Constant Pool.
651 *******************************************************************************/
653 static void class_freecpool(classinfo *c)
659 if (c->cptags && c->cpinfos) {
660 for (idx = 0; idx < c->cpcount; idx++) {
661 tag = c->cptags[idx];
662 info = c->cpinfos[idx];
666 case CONSTANT_Fieldref:
667 case CONSTANT_Methodref:
668 case CONSTANT_InterfaceMethodref:
669 FREE(info, constant_FMIref);
671 case CONSTANT_Integer:
672 FREE(info, constant_integer);
675 FREE(info, constant_float);
678 FREE(info, constant_long);
680 case CONSTANT_Double:
681 FREE(info, constant_double);
683 case CONSTANT_NameAndType:
684 FREE(info, constant_nameandtype);
692 MFREE(c->cptags, u1, c->cpcount);
695 MFREE(c->cpinfos, voidptr, c->cpcount);
699 /* class_getconstant ***********************************************************
701 Retrieves the value at position 'pos' of the constantpool of a
702 class. If the type of the value is other than 'ctype', an error is
705 *******************************************************************************/
707 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
709 /* check index and type of constantpool entry */
710 /* (pos == 0 is caught by type comparison) */
712 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
713 exceptions_throw_classformaterror(c, "Illegal constant pool index");
717 return c->cpinfos[pos];
721 /* innerclass_getconstant ******************************************************
723 Like class_getconstant, but if cptags is ZERO, null is returned.
725 *******************************************************************************/
727 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
729 /* invalid position in constantpool */
731 if (pos >= c->cpcount) {
732 exceptions_throw_classformaterror(c, "Illegal constant pool index");
736 /* constantpool entry of type 0 */
738 if (c->cptags[pos] == 0)
741 /* check type of constantpool entry */
743 if (c->cptags[pos] != ctype) {
744 exceptions_throw_classformaterror(c, "Illegal constant pool index");
748 return c->cpinfos[pos];
752 /* class_free ******************************************************************
754 Frees all resources used by the class.
756 *******************************************************************************/
758 void class_free(classinfo *c)
766 MFREE(c->interfaces, classinfo*, c->interfacescount);
769 for (i = 0; i < c->fieldscount; i++)
770 field_free(&(c->fields[i]));
771 #if defined(ENABLE_CACAO_GC)
772 MFREE(c->fields, fieldinfo, c->fieldscount);
777 for (i = 0; i < c->methodscount; i++)
778 method_free(&(c->methods[i]));
779 MFREE(c->methods, methodinfo, c->methodscount);
782 if ((v = c->vftbl) != NULL) {
784 mem_free(v->arraydesc,sizeof(arraydescriptor));
786 for (i = 0; i < v->interfacetablelength; i++) {
787 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
789 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
791 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
792 sizeof(methodptr*) * (v->interfacetablelength -
793 (v->interfacetablelength > 0));
794 v = (vftbl_t*) (((methodptr*) v) -
795 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
800 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
802 /* if (c->classvftbl)
803 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
807 #if defined(ENABLE_ANNOTATIONS)
808 annotation_bytearray_free(c->annotations);
810 annotation_bytearrays_free(c->method_annotations);
811 annotation_bytearrays_free(c->method_parameterannotations);
812 annotation_bytearrays_free(c->method_annotationdefaults);
814 annotation_bytearrays_free(c->field_annotations);
819 /* get_array_class *************************************************************
821 Returns the array class with the given name for the given
822 classloader, or NULL if an exception occurred.
824 Note: This function does eager loading.
826 *******************************************************************************/
828 static classinfo *get_array_class(utf *name,classloader *initloader,
829 classloader *defloader,bool link)
833 /* lookup this class in the classcache */
834 c = classcache_lookup(initloader,name);
836 c = classcache_lookup_defined(defloader,name);
839 /* we have to create it */
840 c = class_create_classinfo(name);
841 c = load_newly_created_array(c,initloader);
847 assert(c->state & CLASS_LOADED);
848 assert(c->classloader == defloader);
850 if (link && !(c->state & CLASS_LINKED))
854 assert(!link || (c->state & CLASS_LINKED));
860 /* class_array_of **************************************************************
862 Returns an array class with the given component class. The array
863 class is dynamically created if neccessary.
865 *******************************************************************************/
867 classinfo *class_array_of(classinfo *component, bool link)
876 cl = component->classloader;
878 dumpsize = dump_size();
880 /* Assemble the array class name */
881 namelen = component->name->blength;
883 if (component->name->text[0] == '[') {
884 /* the component is itself an array */
885 namebuf = DMNEW(char, namelen + 1);
887 MCOPY(namebuf + 1, component->name->text, char, namelen);
891 /* the component is a non-array class */
892 namebuf = DMNEW(char, namelen + 3);
895 MCOPY(namebuf + 2, component->name->text, char, namelen);
896 namebuf[2 + namelen] = ';';
900 u = utf_new(namebuf, namelen);
902 c = get_array_class(u, cl, cl, link);
904 dump_release(dumpsize);
910 /* class_multiarray_of *********************************************************
912 Returns an array class with the given dimension and element class.
913 The array class is dynamically created if neccessary.
915 *******************************************************************************/
917 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
924 dumpsize = dump_size();
927 log_text("Invalid array dimension requested");
931 /* Assemble the array class name */
932 namelen = element->name->blength;
934 if (element->name->text[0] == '[') {
935 /* the element is itself an array */
936 namebuf = DMNEW(char, namelen + dim);
937 memcpy(namebuf + dim, element->name->text, namelen);
941 /* the element is a non-array class */
942 namebuf = DMNEW(char, namelen + 2 + dim);
944 memcpy(namebuf + dim + 1, element->name->text, namelen);
945 namelen += (2 + dim);
946 namebuf[namelen - 1] = ';';
948 memset(namebuf, '[', dim);
950 c = get_array_class(utf_new(namebuf, namelen),
951 element->classloader,
952 element->classloader,
955 dump_release(dumpsize);
961 /* class_lookup_classref *******************************************************
963 Looks up the constant_classref for a given classname in the classref
967 cls..............the class containing the reference
968 name.............the name of the class refered to
971 a pointer to a constant_classref, or
972 NULL if the reference was not found
974 *******************************************************************************/
976 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
978 constant_classref *ref;
979 extra_classref *xref;
984 assert(!cls->classrefcount || cls->classrefs);
986 /* first search the main classref table */
987 count = cls->classrefcount;
988 ref = cls->classrefs;
989 for (; count; --count, ++ref)
990 if (ref->name == name)
993 /* next try the list of extra classrefs */
994 for (xref = cls->extclassrefs; xref; xref = xref->next) {
995 if (xref->classref.name == name)
996 return &(xref->classref);
1004 /* class_get_classref **********************************************************
1006 Returns the constant_classref for a given classname.
1009 cls..............the class containing the reference
1010 name.............the name of the class refered to
1013 a pointer to a constant_classref (never NULL)
1016 The given name is not checked for validity!
1018 *******************************************************************************/
1020 constant_classref *class_get_classref(classinfo *cls, utf *name)
1022 constant_classref *ref;
1023 extra_classref *xref;
1028 ref = class_lookup_classref(cls,name);
1032 xref = NEW(extra_classref);
1033 CLASSREF_INIT(xref->classref,cls,name);
1035 xref->next = cls->extclassrefs;
1036 cls->extclassrefs = xref;
1038 return &(xref->classref);
1042 /* class_get_self_classref *****************************************************
1044 Returns the constant_classref to the class itself.
1047 cls..............the class containing the reference
1050 a pointer to a constant_classref (never NULL)
1052 *******************************************************************************/
1054 constant_classref *class_get_self_classref(classinfo *cls)
1056 /* XXX this should be done in a faster way. Maybe always make */
1057 /* the classref of index 0 a self reference. */
1058 return class_get_classref(cls,cls->name);
1061 /* class_get_classref_multiarray_of ********************************************
1063 Returns an array type reference with the given dimension and element class
1067 dim..............the requested dimension
1068 dim must be in [1;255]. This is NOT checked!
1069 ref..............the component class reference
1072 a pointer to the class reference for the array type
1075 The referer of `ref` is used as the referer for the new classref.
1077 *******************************************************************************/
1079 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1084 constant_classref *cr;
1087 assert(dim >= 1 && dim <= 255);
1089 dumpsize = dump_size();
1091 /* Assemble the array class name */
1092 namelen = ref->name->blength;
1094 if (ref->name->text[0] == '[') {
1095 /* the element is itself an array */
1096 namebuf = DMNEW(char, namelen + dim);
1097 memcpy(namebuf + dim, ref->name->text, namelen);
1101 /* the element is a non-array class */
1102 namebuf = DMNEW(char, namelen + 2 + dim);
1104 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1105 namelen += (2 + dim);
1106 namebuf[namelen - 1] = ';';
1108 memset(namebuf, '[', dim);
1110 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1112 dump_release(dumpsize);
1118 /* class_get_classref_component_of *********************************************
1120 Returns the component classref of a given array type reference
1123 ref..............the array type reference
1126 a reference to the component class, or
1127 NULL if `ref` is not an object array type reference
1130 The referer of `ref` is used as the referer for the new classref.
1132 *******************************************************************************/
1134 constant_classref *class_get_classref_component_of(constant_classref *ref)
1141 name = ref->name->text;
1145 namelen = ref->name->blength - 1;
1150 else if (*name != '[') {
1154 return class_get_classref(ref->referer, utf_new(name, namelen));
1158 /* class_findmethod ************************************************************
1160 Searches a 'classinfo' structure for a method having the given name
1161 and descriptor. If descriptor is NULL, it is ignored.
1163 *******************************************************************************/
1165 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1170 for (i = 0; i < c->methodscount; i++) {
1171 m = &(c->methods[i]);
1173 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1181 /* class_resolvemethod *********************************************************
1183 Searches a class and it's super classes for a method.
1185 Superinterfaces are *not* searched.
1187 *******************************************************************************/
1189 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1194 m = class_findmethod(c, name, desc);
1199 /* JVM Specification bug:
1201 It is important NOT to resolve special <init> and <clinit>
1202 methods to super classes or interfaces; yet, this is not
1203 explicited in the specification. Section 5.4.3.3 should be
1204 updated appropriately. */
1206 if (name == utf_init || name == utf_clinit)
1216 /* class_resolveinterfacemethod_intern *****************************************
1218 Internally used helper function. Do not use this directly.
1220 *******************************************************************************/
1222 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1223 utf *name, utf *desc)
1228 /* try to find the method in the class */
1230 m = class_findmethod(c, name, desc);
1235 /* no method found? try the superinterfaces */
1237 for (i = 0; i < c->interfacescount; i++) {
1238 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1245 /* no method found */
1251 /* class_resolveclassmethod ****************************************************
1253 Resolves a reference from REFERER to a method with NAME and DESC in
1256 If the method cannot be resolved the return value is NULL. If
1257 EXCEPT is true *exceptionptr is set, too.
1259 *******************************************************************************/
1261 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1262 classinfo *referer, bool throwexception)
1268 /* if (c->flags & ACC_INTERFACE) { */
1269 /* if (throwexception) */
1270 /* *exceptionptr = */
1271 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1275 /* try class c and its superclasses */
1279 m = class_resolvemethod(cls, name, desc);
1284 /* try the superinterfaces */
1286 for (i = 0; i < c->interfacescount; i++) {
1287 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1295 exceptions_throw_nosuchmethoderror(c, name, desc);
1300 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1302 exceptions_throw_abstractmethoderror();
1307 /* XXX check access rights */
1313 /* class_resolveinterfacemethod ************************************************
1315 Resolves a reference from REFERER to a method with NAME and DESC in
1318 If the method cannot be resolved the return value is NULL. If
1319 EXCEPT is true *exceptionptr is set, too.
1321 *******************************************************************************/
1323 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1324 classinfo *referer, bool throwexception)
1328 if (!(c->flags & ACC_INTERFACE)) {
1330 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1335 mi = class_resolveinterfacemethod_intern(c, name, desc);
1340 /* try class java.lang.Object */
1342 mi = class_findmethod(class_java_lang_Object, name, desc);
1348 exceptions_throw_nosuchmethoderror(c, name, desc);
1354 /* class_findfield *************************************************************
1356 Searches for field with specified name and type in a classinfo
1357 structure. If no such field is found NULL is returned.
1359 *******************************************************************************/
1361 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1365 for (i = 0; i < c->fieldscount; i++)
1366 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1367 return &(c->fields[i]);
1370 return class_findfield(c->super.cls, name, desc);
1376 /* class_findfield_approx ******************************************************
1378 Searches in 'classinfo'-structure for a field with the specified
1381 *******************************************************************************/
1383 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1387 /* get field index */
1389 i = class_findfield_index_by_name(c, name);
1391 /* field was not found, return */
1396 /* return field address */
1398 return &(c->fields[i]);
1402 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1406 for (i = 0; i < c->fieldscount; i++) {
1407 /* compare field names */
1409 if ((c->fields[i].name == name))
1413 /* field was not found, raise exception */
1415 exceptions_throw_nosuchfielderror(c, name);
1421 /****************** Function: class_resolvefield_int ***************************
1423 This is an internally used helper function. Do not use this directly.
1425 Tries to resolve a field having the given name and type.
1426 If the field cannot be resolved, NULL is returned.
1428 *******************************************************************************/
1430 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1435 /* search for field in class c */
1437 for (i = 0; i < c->fieldscount; i++) {
1438 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1439 return &(c->fields[i]);
1443 /* try superinterfaces recursively */
1445 for (i = 0; i < c->interfacescount; i++) {
1446 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1451 /* try superclass */
1454 return class_resolvefield_int(c->super.cls, name, desc);
1462 /********************* Function: class_resolvefield ***************************
1464 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1466 If the field cannot be resolved the return value is NULL. If EXCEPT is
1467 true *exceptionptr is set, too.
1469 *******************************************************************************/
1471 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1472 classinfo *referer, bool throwexception)
1476 fi = class_resolvefield_int(c, name, desc);
1480 exceptions_throw_nosuchfielderror(c, name);
1485 /* XXX check access rights */
1491 /* class_resolve_superclass ****************************************************
1493 Resolves the super class reference of the given class if necessary.
1495 *******************************************************************************/
1497 static classinfo *class_resolve_superclass(classinfo *c)
1501 if (c->super.any == NULL)
1504 /* Do we have a super class reference or is it already
1507 if (IS_CLASSREF(c->super)) {
1508 super = resolve_classref_or_classinfo_eager(c->super, true);
1513 /* Store the resolved super class in the class structure. */
1515 c->super.cls = super;
1518 return c->super.cls;
1522 /* class_issubclass ************************************************************
1524 Checks if sub is a descendant of super.
1526 *******************************************************************************/
1528 bool class_issubclass(classinfo *sub, classinfo *super)
1537 sub = class_resolve_superclass(sub);
1542 /* class_isanysubclass *********************************************************
1544 Checks a subclass relation between two classes. Implemented
1545 interfaces are interpreted as super classes.
1547 Return value: 1 ... sub is subclass of super
1550 *******************************************************************************/
1552 bool class_isanysubclass(classinfo *sub, classinfo *super)
1557 /* This is the trivial case. */
1562 /* Primitive classes are only subclasses of themselves. */
1564 if (class_is_primitive(sub) || class_is_primitive(super))
1567 /* Check for interfaces. */
1569 if (super->flags & ACC_INTERFACE) {
1570 result = (sub->vftbl->interfacetablelength > super->index) &&
1571 (sub->vftbl->interfacetable[-super->index] != NULL);
1574 /* java.lang.Object is the only super class of any
1577 if (sub->flags & ACC_INTERFACE)
1578 return (super == class_java_lang_Object);
1580 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1582 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1583 result = diffval <= (uint32_t) super->vftbl->diffval;
1585 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1592 /* class_is_primitive **********************************************************
1594 Checks if the given class is a primitive class.
1596 *******************************************************************************/
1598 bool class_is_primitive(classinfo *c)
1600 if (c->flags & ACC_CLASS_PRIMITIVE)
1607 /* class_is_array **************************************************************
1609 Checks if the given class is an array class.
1611 *******************************************************************************/
1613 bool class_is_array(classinfo *c)
1615 if (!(c->state & CLASS_LINKED))
1619 return (c->vftbl->arraydesc != NULL);
1623 /* class_is_interface **********************************************************
1625 Checks if the given class is an interface.
1627 *******************************************************************************/
1629 bool class_is_interface(classinfo *c)
1631 if (c->flags & ACC_INTERFACE)
1638 /* class_get_superclass ********************************************************
1640 Return the super class of the given class. If the super-field is a
1641 class-reference, resolve it and store it in the classinfo.
1643 *******************************************************************************/
1645 classinfo *class_get_superclass(classinfo *c)
1649 /* For java.lang.Object, primitive and Void classes we return
1652 if (c->super.any == NULL)
1655 /* For interfaces we also return NULL. */
1657 if (c->flags & ACC_INTERFACE)
1660 /* We may have to resolve the super class reference. */
1662 super = class_resolve_superclass(c);
1668 /* class_get_declaredclasses ***************************************************
1670 Return an array of declared classes of the given class.
1672 *******************************************************************************/
1674 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1676 classref_or_classinfo inner;
1677 classref_or_classinfo outer;
1679 int declaredclasscount; /* number of declared classes */
1680 int pos; /* current declared class */
1681 java_handle_objectarray_t *oa; /* array of declared classes */
1685 declaredclasscount = 0;
1687 if (!class_is_primitive(c) && !class_is_array(c)) {
1688 /* Determine number of declared classes. */
1690 for (i = 0; i < c->innerclasscount; i++) {
1691 outer = c->innerclass[i].outer_class;
1693 /* Check if outer_class is a classref or a real class and
1694 get the class name from the structure. */
1696 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1698 /* Outer class is this class. */
1700 if ((outername == c->name) &&
1701 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1702 declaredclasscount++;
1706 /* Allocate Class[] and check for OOM. */
1708 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1713 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1714 inner = c->innerclass[i].inner_class;
1715 outer = c->innerclass[i].outer_class;
1717 /* Check if outer_class is a classref or a real class and get
1718 the class name from the structure. */
1720 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1722 /* Outer class is this class. */
1724 if ((outername == c->name) &&
1725 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1727 ic = resolve_classref_or_classinfo_eager(inner, false);
1732 if (!(ic->state & CLASS_LINKED))
1733 if (!link_class(ic))
1736 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1744 /* class_get_declaringclass ****************************************************
1746 If the class or interface given is a member of another class,
1747 return the declaring class. For array and primitive classes return
1750 *******************************************************************************/
1752 classinfo *class_get_declaringclass(classinfo *c)
1754 classref_or_classinfo innercr;
1756 classref_or_classinfo outercr;
1760 /* return NULL for arrayclasses and primitive classes */
1762 if (class_is_primitive(c) || (c->name->text[0] == '['))
1765 /* no innerclasses exist */
1767 if (c->innerclasscount == 0)
1770 for (i = 0; i < c->innerclasscount; i++) {
1771 /* Check if inner_class is a classref or a real class and get
1772 the class name from the structure. */
1774 innercr = c->innerclass[i].inner_class;
1776 innername = IS_CLASSREF(innercr) ?
1777 innercr.ref->name : innercr.cls->name;
1779 /* Is the current innerclass this class? */
1781 if (innername == c->name) {
1782 /* Maybe the outer class is not loaded yet. */
1784 outercr = c->innerclass[i].outer_class;
1786 outer = resolve_classref_or_classinfo_eager(outercr, false);
1791 if (!(outer->state & CLASS_LINKED))
1792 if (!link_class(outer))
1803 /* class_get_interfaces ********************************************************
1805 Return an array of interfaces of the given class.
1807 *******************************************************************************/
1809 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1812 java_handle_objectarray_t *oa;
1815 if (!(c->state & CLASS_LINKED))
1819 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1824 for (i = 0; i < c->interfacescount; i++) {
1825 ic = c->interfaces[i].cls;
1827 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1834 /* class_get_signature *********************************************************
1836 Return the signature of the given class. For array and primitive
1837 classes return NULL.
1839 *******************************************************************************/
1841 #if defined(ENABLE_JAVASE)
1842 utf *class_get_signature(classinfo *c)
1844 /* For array and primitive classes return NULL. */
1846 if (class_is_array(c) || class_is_primitive(c))
1849 return c->signature;
1854 /* class_printflags ************************************************************
1856 Prints flags of a class.
1858 *******************************************************************************/
1860 #if !defined(NDEBUG)
1861 void class_printflags(classinfo *c)
1868 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1869 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1870 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1871 if (c->flags & ACC_STATIC) printf(" STATIC");
1872 if (c->flags & ACC_FINAL) printf(" FINAL");
1873 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1874 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1875 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1876 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1877 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1878 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1883 /* class_print *****************************************************************
1885 Prints classname plus flags.
1887 *******************************************************************************/
1889 #if !defined(NDEBUG)
1890 void class_print(classinfo *c)
1897 utf_display_printable_ascii(c->name);
1898 class_printflags(c);
1903 /* class_classref_print ********************************************************
1905 Prints classname plus referer class.
1907 *******************************************************************************/
1909 #if !defined(NDEBUG)
1910 void class_classref_print(constant_classref *cr)
1917 utf_display_printable_ascii(cr->name);
1920 class_print(cr->referer);
1928 /* class_println ***************************************************************
1930 Prints classname plus flags and new line.
1932 *******************************************************************************/
1934 #if !defined(NDEBUG)
1935 void class_println(classinfo *c)
1943 /* class_classref_println ******************************************************
1945 Prints classname plus referer class and new line.
1947 *******************************************************************************/
1949 #if !defined(NDEBUG)
1950 void class_classref_println(constant_classref *cr)
1952 class_classref_print(cr);
1958 /* class_classref_or_classinfo_print *******************************************
1960 Prints classname plus referer class.
1962 *******************************************************************************/
1964 #if !defined(NDEBUG)
1965 void class_classref_or_classinfo_print(classref_or_classinfo c)
1967 if (c.any == NULL) {
1968 printf("(classref_or_classinfo) NULL");
1972 class_classref_print(c.ref);
1979 /* class_classref_or_classinfo_println *****************************************
1981 Prints classname plus referer class and a newline.
1983 *******************************************************************************/
1985 void class_classref_or_classinfo_println(classref_or_classinfo c)
1987 class_classref_or_classinfo_println(c);
1992 /* class_showconstantpool ******************************************************
1994 Dump the constant pool of the given class to stdout.
1996 *******************************************************************************/
1998 #if !defined(NDEBUG)
1999 void class_showconstantpool (classinfo *c)
2004 printf ("---- dump of constant pool ----\n");
2006 for (i=0; i<c->cpcount; i++) {
2007 printf ("#%d: ", (int) i);
2009 e = c -> cpinfos [i];
2012 switch (c -> cptags [i]) {
2013 case CONSTANT_Class:
2014 printf ("Classreference -> ");
2015 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2017 case CONSTANT_Fieldref:
2018 printf ("Fieldref -> ");
2019 field_fieldref_print((constant_FMIref *) e);
2021 case CONSTANT_Methodref:
2022 printf ("Methodref -> ");
2023 method_methodref_print((constant_FMIref *) e);
2025 case CONSTANT_InterfaceMethodref:
2026 printf ("InterfaceMethod -> ");
2027 method_methodref_print((constant_FMIref *) e);
2029 case CONSTANT_String:
2030 printf ("String -> ");
2031 utf_display_printable_ascii (e);
2033 case CONSTANT_Integer:
2034 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2036 case CONSTANT_Float:
2037 printf ("Float -> %f", ((constant_float*)e) -> value);
2039 case CONSTANT_Double:
2040 printf ("Double -> %f", ((constant_double*)e) -> value);
2044 u8 v = ((constant_long*)e) -> value;
2046 printf ("Long -> %ld", (long int) v);
2048 printf ("Long -> HI: %ld, LO: %ld\n",
2049 (long int) v.high, (long int) v.low);
2053 case CONSTANT_NameAndType:
2055 constant_nameandtype *cnt = e;
2056 printf ("NameAndType: ");
2057 utf_display_printable_ascii (cnt->name);
2059 utf_display_printable_ascii (cnt->descriptor);
2063 printf ("Utf8 -> ");
2064 utf_display_printable_ascii (e);
2067 log_text("Invalid type of ConstantPool-Entry");
2075 #endif /* !defined(NDEBUG) */
2078 /* class_showmethods ***********************************************************
2080 Dump info about the fields and methods of the given class to stdout.
2082 *******************************************************************************/
2084 #if !defined(NDEBUG)
2085 void class_showmethods (classinfo *c)
2089 printf("--------- Fields and Methods ----------------\n");
2091 class_printflags(c);
2095 utf_display_printable_ascii(c->name);
2100 utf_display_printable_ascii(c->super.cls->name);
2104 printf("Index: %d\n", c->index);
2106 printf("Interfaces:\n");
2107 for (i = 0; i < c->interfacescount; i++) {
2109 utf_display_printable_ascii(c->interfaces[i].cls->name);
2110 printf (" (%d)\n", c->interfaces[i].cls->index);
2113 printf("Fields:\n");
2114 for (i = 0; i < c->fieldscount; i++)
2115 field_println(&(c->fields[i]));
2117 printf("Methods:\n");
2118 for (i = 0; i < c->methodscount; i++) {
2119 methodinfo *m = &(c->methods[i]);
2121 if (!(m->flags & ACC_STATIC))
2122 printf("vftblindex: %d ", m->vftblindex);
2127 printf ("Virtual function table:\n");
2128 for (i = 0; i < c->vftbl->vftbllength; i++)
2129 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2131 #endif /* !defined(NDEBUG) */
2135 * These are local overrides for various environment variables in Emacs.
2136 * Please do not remove this and leave it at the end of the file, where
2137 * Emacs will automagically detect them.
2138 * ---------------------------------------------------------------------
2141 * indent-tabs-mode: t
2145 * vim:noexpandtab:sw=4:ts=4: