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
39 #include "mm/memory.h"
41 #include "native/llni.h"
43 #include "threads/lock-common.h"
45 #include "toolbox/logging.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/resolve.h"
53 #include "vm/jit/asmpart.h"
55 #include "vmcore/class.h"
56 #include "vmcore/classcache.h"
57 #include "vmcore/linker.h"
58 #include "vmcore/loader.h"
59 #include "vmcore/options.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
65 #include "vmcore/suck.h"
66 #include "vmcore/utf8.h"
69 /* global variables ***********************************************************/
71 /* frequently used classes ****************************************************/
73 /* important system classes */
75 classinfo *class_java_lang_Object;
76 classinfo *class_java_lang_Class;
77 classinfo *class_java_lang_ClassLoader;
78 classinfo *class_java_lang_Cloneable;
79 classinfo *class_java_lang_SecurityManager;
80 classinfo *class_java_lang_String;
81 classinfo *class_java_lang_System;
82 classinfo *class_java_lang_Thread;
83 classinfo *class_java_lang_ThreadGroup;
84 classinfo *class_java_lang_VMSystem;
85 classinfo *class_java_lang_VMThread;
86 classinfo *class_java_io_Serializable;
88 #if defined(WITH_CLASSPATH_SUN)
89 classinfo *class_sun_reflect_MagicAccessorImpl;
92 /* system exception classes required in cacao */
94 classinfo *class_java_lang_Throwable;
95 classinfo *class_java_lang_Error;
96 classinfo *class_java_lang_LinkageError;
97 classinfo *class_java_lang_NoClassDefFoundError;
98 classinfo *class_java_lang_OutOfMemoryError;
99 classinfo *class_java_lang_VirtualMachineError;
101 #if defined(WITH_CLASSPATH_GNU)
102 classinfo *class_java_lang_VMThrowable;
105 classinfo *class_java_lang_Exception;
106 classinfo *class_java_lang_ClassCastException;
107 classinfo *class_java_lang_ClassNotFoundException;
109 #if defined(ENABLE_JAVASE)
110 classinfo *class_java_lang_Void;
112 classinfo *class_java_lang_Boolean;
113 classinfo *class_java_lang_Byte;
114 classinfo *class_java_lang_Character;
115 classinfo *class_java_lang_Short;
116 classinfo *class_java_lang_Integer;
117 classinfo *class_java_lang_Long;
118 classinfo *class_java_lang_Float;
119 classinfo *class_java_lang_Double;
122 /* some runtime exception */
124 classinfo *class_java_lang_NullPointerException;
127 /* some classes which may be used more often */
129 #if defined(ENABLE_JAVASE)
130 classinfo *class_java_lang_StackTraceElement;
131 classinfo *class_java_lang_reflect_Constructor;
132 classinfo *class_java_lang_reflect_Field;
133 classinfo *class_java_lang_reflect_Method;
134 classinfo *class_java_security_PrivilegedAction;
135 classinfo *class_java_util_Vector;
137 classinfo *arrayclass_java_lang_Object;
139 #if defined(ENABLE_ANNOTATIONS)
140 classinfo *class_sun_reflect_ConstantPool;
141 #if defined(WITH_CLASSPATH_GNU)
142 classinfo *class_sun_reflect_annotation_AnnotationParser;
148 /* pseudo classes for the typechecker */
150 classinfo *pseudo_class_Arraystub;
151 classinfo *pseudo_class_Null;
152 classinfo *pseudo_class_New;
155 /* class_set_packagename *******************************************************
157 Derive the package name from the class name and store it in the
160 An internal package name consists of the package name plus the
161 trailing '/', e.g. "java/lang/".
163 For classes in the unnamed package, the package name is set to
166 *******************************************************************************/
168 void class_set_packagename(classinfo *c)
173 p = UTF_END(c->name) - 1;
174 start = c->name->text;
176 if (c->name->text[0] == '[') {
177 /* Set packagename of arrays to the element's package. */
179 for (; *start == '['; start++);
181 /* Skip the 'L' in arrays of references. */
187 /* Search for last '/'. */
189 for (; (p > start) && (*p != '/'); --p);
191 /* If we found a '/' we set the package name plus the trailing
192 '/'. Otherwise we set the packagename to NULL. */
195 c->packagename = utf_new(start, p - start + 1);
197 c->packagename = NULL;
201 /* class_create_classinfo ******************************************************
203 Create a new classinfo struct. The class name is set to the given utf *,
204 most other fields are initialized to zero.
206 Note: classname may be NULL. In this case a not-yet-named classinfo is
207 created. The name must be filled in later and class_set_packagename
208 must be called after that.
210 *******************************************************************************/
212 classinfo *class_create_classinfo(utf *classname)
216 #if defined(ENABLE_STATISTICS)
218 size_classinfo += sizeof(classinfo);
221 /* we use a safe name for temporarily unnamed classes */
223 if (classname == NULL)
224 classname = utf_not_named_yet;
228 log_message_utf("Creating class: ", classname);
231 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
233 #if defined(ENABLE_GC_CACAO)
234 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
236 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
237 /*c=NEW(classinfo);*/
241 /* Set the header.vftbl of all loaded classes to the one of
242 java.lang.Class, so Java code can use a class as object. */
244 if (class_java_lang_Class != NULL)
245 if (class_java_lang_Class->vftbl != NULL)
246 c->object.header.vftbl = class_java_lang_Class->vftbl;
248 #if defined(ENABLE_JAVASE)
249 /* check if the class is a reference class and flag it */
251 if (classname == utf_java_lang_ref_SoftReference) {
252 c->flags |= ACC_CLASS_REFERENCE_SOFT;
254 else if (classname == utf_java_lang_ref_WeakReference) {
255 c->flags |= ACC_CLASS_REFERENCE_WEAK;
257 else if (classname == utf_java_lang_ref_PhantomReference) {
258 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
262 if (classname != utf_not_named_yet)
263 class_set_packagename(c);
265 LOCK_INIT_OBJECT_LOCK(&c->object.header);
271 /* class_postset_header_vftbl **************************************************
273 Set the header.vftbl of all classes created before java.lang.Class
274 was linked. This is necessary that Java code can use a class as
277 *******************************************************************************/
279 void class_postset_header_vftbl(void)
283 classcache_name_entry *nmen;
284 classcache_class_entry *clsen;
286 assert(class_java_lang_Class);
288 for (slot = 0; slot < hashtable_classcache.size; slot++) {
289 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
291 for (; nmen; nmen = nmen->hashlink) {
292 /* iterate over all class entries */
294 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
297 /* now set the the vftbl */
299 if (c->object.header.vftbl == NULL)
300 c->object.header.vftbl = class_java_lang_Class->vftbl;
306 /* class_define ****************************************************************
308 Calls the loader and defines a class in the VM.
310 *******************************************************************************/
312 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
319 /* check if this class has already been defined */
321 c = classcache_lookup_defined_or_initiated(cl, name);
324 exceptions_throw_linkageerror("duplicate class definition: ", c);
329 /* create a new classinfo struct */
331 c = class_create_classinfo(name);
333 #if defined(ENABLE_STATISTICS)
336 if (opt_getloadingtime)
340 /* build a classbuffer with the given data */
342 cb = NEW(classbuffer);
349 /* preset the defining classloader */
353 /* load the class from this buffer */
355 r = load_class_from_classbuffer(cb);
359 FREE(cb, classbuffer);
361 #if defined(ENABLE_STATISTICS)
364 if (opt_getloadingtime)
369 /* If return value is NULL, we had a problem and the class is
370 not loaded. Now free the allocated memory, otherwise we
371 could run into a DOS. */
378 #if defined(ENABLE_JAVASE)
379 # if defined(WITH_CLASSPATH_SUN)
380 /* Store the protection domain. */
382 c->protectiondomain = pd;
386 /* Store the newly defined class in the class cache. This call
387 also checks whether a class of the same name has already been
388 defined by the same defining loader, and if so, replaces the
389 newly created class by the one defined earlier. */
391 /* Important: The classinfo given to classcache_store must be
392 fully prepared because another thread may return
393 this pointer after the lookup at to top of this
394 function directly after the class cache lock has
397 c = classcache_store(cl, c, true);
403 /* class_load_attribute_sourcefile *********************************************
405 SourceFile_attribute {
406 u2 attribute_name_index;
411 *******************************************************************************/
413 static bool class_load_attribute_sourcefile(classbuffer *cb)
424 /* check buffer size */
426 if (!suck_check_classbuffer_size(cb, 4 + 2))
429 /* check attribute length */
431 attribute_length = suck_u4(cb);
433 if (attribute_length != 2) {
434 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
438 /* there can be no more than one SourceFile attribute */
440 if (c->sourcefile != NULL) {
441 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
447 sourcefile_index = suck_u2(cb);
448 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
450 if (sourcefile == NULL)
453 /* store sourcefile */
455 c->sourcefile = sourcefile;
461 /* class_load_attribute_enclosingmethod ****************************************
463 EnclosingMethod_attribute {
464 u2 attribute_name_index;
470 *******************************************************************************/
472 #if defined(ENABLE_JAVASE)
473 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
479 classref_or_classinfo cr;
480 constant_nameandtype *cn;
486 /* check buffer size */
488 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
491 /* check attribute length */
493 attribute_length = suck_u4(cb);
495 if (attribute_length != 4) {
496 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
500 /* there can be no more than one EnclosingMethod attribute */
502 if (c->enclosingmethod != NULL) {
503 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
507 /* get class index */
509 class_index = suck_u2(cb);
510 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
512 /* get method index */
514 method_index = suck_u2(cb);
515 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
517 /* store info in classinfo */
519 c->enclosingclass.any = cr.any;
520 c->enclosingmethod = cn;
524 #endif /* defined(ENABLE_JAVASE) */
527 /* class_load_attributes *******************************************************
529 Read attributes from ClassFile.
532 u2 attribute_name_index;
534 u1 info[attribute_length];
537 InnerClasses_attribute {
538 u2 attribute_name_index;
542 *******************************************************************************/
544 bool class_load_attributes(classbuffer *cb)
547 uint16_t attributes_count;
548 uint16_t attribute_name_index;
550 innerclassinfo *info;
551 classref_or_classinfo inner;
552 classref_or_classinfo outer;
559 /* get attributes count */
561 if (!suck_check_classbuffer_size(cb, 2))
564 attributes_count = suck_u2(cb);
566 for (i = 0; i < attributes_count; i++) {
567 /* get attribute name */
569 if (!suck_check_classbuffer_size(cb, 2))
572 attribute_name_index = suck_u2(cb);
574 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
576 if (attribute_name == NULL)
579 if (attribute_name == utf_InnerClasses) {
582 if (c->innerclass != NULL) {
583 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
587 if (!suck_check_classbuffer_size(cb, 4 + 2))
590 /* skip attribute length */
593 /* number of records */
594 c->innerclasscount = suck_u2(cb);
596 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
599 /* allocate memory for innerclass structure */
600 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
602 for (j = 0; j < c->innerclasscount; j++) {
603 /* The innerclass structure contains a class with an encoded
604 name, its defining scope, its simple name and a bitmask of
607 info = c->innerclass + j;
609 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
610 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
611 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
614 /* If the current inner-class is the currently loaded
615 class check for some special flags. */
617 if (inner.ref->name == c->name) {
618 /* If an inner-class is not a member, its
619 outer-class is NULL. */
621 if (outer.ref != NULL) {
622 c->flags |= ACC_CLASS_MEMBER;
624 /* A member class doesn't have an
625 EnclosingMethod attribute, so set the
626 enclosing-class to be the same as the
629 c->declaringclass = outer;
630 c->enclosingclass = outer;
633 /* If an inner-class is anonymous, its name is
637 c->flags |= ACC_CLASS_ANONYMOUS;
640 info->inner_class = inner;
641 info->outer_class = outer;
646 else if (attribute_name == utf_SourceFile) {
649 if (!class_load_attribute_sourcefile(cb))
652 #if defined(ENABLE_JAVASE)
653 else if (attribute_name == utf_EnclosingMethod) {
654 /* EnclosingMethod */
656 if (!class_load_attribute_enclosingmethod(cb))
659 else if (attribute_name == utf_Signature) {
662 if (!loader_load_attribute_signature(cb, &(c->signature)))
667 #if defined(ENABLE_ANNOTATIONS)
668 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
669 /* RuntimeVisibleAnnotations */
670 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
673 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
674 /* RuntimeInvisibleAnnotations */
675 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
681 /* unknown attribute */
683 if (!loader_skip_attribute_body(cb))
692 /* class_freepool **************************************************************
694 Frees all resources used by this classes Constant Pool.
696 *******************************************************************************/
698 static void class_freecpool(classinfo *c)
704 if (c->cptags && c->cpinfos) {
705 for (idx = 0; idx < c->cpcount; idx++) {
706 tag = c->cptags[idx];
707 info = c->cpinfos[idx];
711 case CONSTANT_Fieldref:
712 case CONSTANT_Methodref:
713 case CONSTANT_InterfaceMethodref:
714 FREE(info, constant_FMIref);
716 case CONSTANT_Integer:
717 FREE(info, constant_integer);
720 FREE(info, constant_float);
723 FREE(info, constant_long);
725 case CONSTANT_Double:
726 FREE(info, constant_double);
728 case CONSTANT_NameAndType:
729 FREE(info, constant_nameandtype);
737 MFREE(c->cptags, u1, c->cpcount);
740 MFREE(c->cpinfos, voidptr, c->cpcount);
744 /* class_getconstant ***********************************************************
746 Retrieves the value at position 'pos' of the constantpool of a
747 class. If the type of the value is other than 'ctype', an error is
750 *******************************************************************************/
752 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
754 /* check index and type of constantpool entry */
755 /* (pos == 0 is caught by type comparison) */
757 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
758 exceptions_throw_classformaterror(c, "Illegal constant pool index");
762 return c->cpinfos[pos];
766 /* innerclass_getconstant ******************************************************
768 Like class_getconstant, but if cptags is ZERO, null is returned.
770 *******************************************************************************/
772 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
774 /* invalid position in constantpool */
776 if (pos >= c->cpcount) {
777 exceptions_throw_classformaterror(c, "Illegal constant pool index");
781 /* constantpool entry of type 0 */
783 if (c->cptags[pos] == 0)
786 /* check type of constantpool entry */
788 if (c->cptags[pos] != ctype) {
789 exceptions_throw_classformaterror(c, "Illegal constant pool index");
793 return c->cpinfos[pos];
797 /* class_free ******************************************************************
799 Frees all resources used by the class.
801 *******************************************************************************/
803 void class_free(classinfo *c)
810 if (c->interfaces != NULL)
811 MFREE(c->interfaces, classinfo*, c->interfacescount);
814 for (i = 0; i < c->fieldscount; i++)
815 field_free(&(c->fields[i]));
816 #if defined(ENABLE_CACAO_GC)
817 MFREE(c->fields, fieldinfo, c->fieldscount);
822 for (i = 0; i < c->methodscount; i++)
823 method_free(&(c->methods[i]));
824 MFREE(c->methods, methodinfo, c->methodscount);
827 if ((v = c->vftbl) != NULL) {
829 mem_free(v->arraydesc,sizeof(arraydescriptor));
831 for (i = 0; i < v->interfacetablelength; i++) {
832 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
834 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
836 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
837 sizeof(methodptr*) * (v->interfacetablelength -
838 (v->interfacetablelength > 0));
839 v = (vftbl_t*) (((methodptr*) v) -
840 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
845 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
847 /* if (c->classvftbl)
848 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
854 /* get_array_class *************************************************************
856 Returns the array class with the given name for the given
857 classloader, or NULL if an exception occurred.
859 Note: This function does eager loading.
861 *******************************************************************************/
863 static classinfo *get_array_class(utf *name,classloader *initloader,
864 classloader *defloader,bool link)
868 /* lookup this class in the classcache */
869 c = classcache_lookup(initloader,name);
871 c = classcache_lookup_defined(defloader,name);
874 /* we have to create it */
875 c = class_create_classinfo(name);
876 c = load_newly_created_array(c,initloader);
882 assert(c->state & CLASS_LOADED);
883 assert(c->classloader == defloader);
885 if (link && !(c->state & CLASS_LINKED))
889 assert(!link || (c->state & CLASS_LINKED));
895 /* class_array_of **************************************************************
897 Returns an array class with the given component class. The array
898 class is dynamically created if neccessary.
900 *******************************************************************************/
902 classinfo *class_array_of(classinfo *component, bool link)
911 cl = component->classloader;
913 dumpsize = dump_size();
915 /* Assemble the array class name */
916 namelen = component->name->blength;
918 if (component->name->text[0] == '[') {
919 /* the component is itself an array */
920 namebuf = DMNEW(char, namelen + 1);
922 MCOPY(namebuf + 1, component->name->text, char, namelen);
926 /* the component is a non-array class */
927 namebuf = DMNEW(char, namelen + 3);
930 MCOPY(namebuf + 2, component->name->text, char, namelen);
931 namebuf[2 + namelen] = ';';
935 u = utf_new(namebuf, namelen);
937 c = get_array_class(u, cl, cl, link);
939 dump_release(dumpsize);
945 /* class_multiarray_of *********************************************************
947 Returns an array class with the given dimension and element class.
948 The array class is dynamically created if neccessary.
950 *******************************************************************************/
952 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
959 dumpsize = dump_size();
962 log_text("Invalid array dimension requested");
966 /* Assemble the array class name */
967 namelen = element->name->blength;
969 if (element->name->text[0] == '[') {
970 /* the element is itself an array */
971 namebuf = DMNEW(char, namelen + dim);
972 memcpy(namebuf + dim, element->name->text, namelen);
976 /* the element is a non-array class */
977 namebuf = DMNEW(char, namelen + 2 + dim);
979 memcpy(namebuf + dim + 1, element->name->text, namelen);
980 namelen += (2 + dim);
981 namebuf[namelen - 1] = ';';
983 memset(namebuf, '[', dim);
985 c = get_array_class(utf_new(namebuf, namelen),
986 element->classloader,
987 element->classloader,
990 dump_release(dumpsize);
996 /* class_lookup_classref *******************************************************
998 Looks up the constant_classref for a given classname in the classref
1002 cls..............the class containing the reference
1003 name.............the name of the class refered to
1006 a pointer to a constant_classref, or
1007 NULL if the reference was not found
1009 *******************************************************************************/
1011 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1013 constant_classref *ref;
1014 extra_classref *xref;
1019 assert(!cls->classrefcount || cls->classrefs);
1021 /* first search the main classref table */
1022 count = cls->classrefcount;
1023 ref = cls->classrefs;
1024 for (; count; --count, ++ref)
1025 if (ref->name == name)
1028 /* next try the list of extra classrefs */
1029 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1030 if (xref->classref.name == name)
1031 return &(xref->classref);
1039 /* class_get_classref **********************************************************
1041 Returns the constant_classref for a given classname.
1044 cls..............the class containing the reference
1045 name.............the name of the class refered to
1048 a pointer to a constant_classref (never NULL)
1051 The given name is not checked for validity!
1053 *******************************************************************************/
1055 constant_classref *class_get_classref(classinfo *cls, utf *name)
1057 constant_classref *ref;
1058 extra_classref *xref;
1063 ref = class_lookup_classref(cls,name);
1067 xref = NEW(extra_classref);
1068 CLASSREF_INIT(xref->classref,cls,name);
1070 xref->next = cls->extclassrefs;
1071 cls->extclassrefs = xref;
1073 return &(xref->classref);
1077 /* class_get_self_classref *****************************************************
1079 Returns the constant_classref to the class itself.
1082 cls..............the class containing the reference
1085 a pointer to a constant_classref (never NULL)
1087 *******************************************************************************/
1089 constant_classref *class_get_self_classref(classinfo *cls)
1091 /* XXX this should be done in a faster way. Maybe always make */
1092 /* the classref of index 0 a self reference. */
1093 return class_get_classref(cls,cls->name);
1096 /* class_get_classref_multiarray_of ********************************************
1098 Returns an array type reference with the given dimension and element class
1102 dim..............the requested dimension
1103 dim must be in [1;255]. This is NOT checked!
1104 ref..............the component class reference
1107 a pointer to the class reference for the array type
1110 The referer of `ref` is used as the referer for the new classref.
1112 *******************************************************************************/
1114 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1119 constant_classref *cr;
1122 assert(dim >= 1 && dim <= 255);
1124 dumpsize = dump_size();
1126 /* Assemble the array class name */
1127 namelen = ref->name->blength;
1129 if (ref->name->text[0] == '[') {
1130 /* the element is itself an array */
1131 namebuf = DMNEW(char, namelen + dim);
1132 memcpy(namebuf + dim, ref->name->text, namelen);
1136 /* the element is a non-array class */
1137 namebuf = DMNEW(char, namelen + 2 + dim);
1139 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1140 namelen += (2 + dim);
1141 namebuf[namelen - 1] = ';';
1143 memset(namebuf, '[', dim);
1145 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1147 dump_release(dumpsize);
1153 /* class_get_classref_component_of *********************************************
1155 Returns the component classref of a given array type reference
1158 ref..............the array type reference
1161 a reference to the component class, or
1162 NULL if `ref` is not an object array type reference
1165 The referer of `ref` is used as the referer for the new classref.
1167 *******************************************************************************/
1169 constant_classref *class_get_classref_component_of(constant_classref *ref)
1176 name = ref->name->text;
1180 namelen = ref->name->blength - 1;
1185 else if (*name != '[') {
1189 return class_get_classref(ref->referer, utf_new(name, namelen));
1193 /* class_findmethod ************************************************************
1195 Searches a 'classinfo' structure for a method having the given name
1196 and descriptor. If descriptor is NULL, it is ignored.
1198 *******************************************************************************/
1200 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1205 for (i = 0; i < c->methodscount; i++) {
1206 m = &(c->methods[i]);
1208 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1216 /* class_resolvemethod *********************************************************
1218 Searches a class and it's super classes for a method.
1220 Superinterfaces are *not* searched.
1222 *******************************************************************************/
1224 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1229 m = class_findmethod(c, name, desc);
1234 /* JVM Specification bug:
1236 It is important NOT to resolve special <init> and <clinit>
1237 methods to super classes or interfaces; yet, this is not
1238 explicited in the specification. Section 5.4.3.3 should be
1239 updated appropriately. */
1241 if (name == utf_init || name == utf_clinit)
1251 /* class_resolveinterfacemethod_intern *****************************************
1253 Internally used helper function. Do not use this directly.
1255 *******************************************************************************/
1257 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1258 utf *name, utf *desc)
1263 /* try to find the method in the class */
1265 m = class_findmethod(c, name, desc);
1270 /* No method found? Try the super interfaces. */
1272 for (i = 0; i < c->interfacescount; i++) {
1273 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1279 /* no method found */
1285 /* class_resolveclassmethod ****************************************************
1287 Resolves a reference from REFERER to a method with NAME and DESC in
1290 If the method cannot be resolved the return value is NULL. If
1291 EXCEPT is true *exceptionptr is set, too.
1293 *******************************************************************************/
1295 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1296 classinfo *referer, bool throwexception)
1302 /* if (c->flags & ACC_INTERFACE) { */
1303 /* if (throwexception) */
1304 /* *exceptionptr = */
1305 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1309 /* try class c and its superclasses */
1313 m = class_resolvemethod(cls, name, desc);
1318 /* Try the super interfaces. */
1320 for (i = 0; i < c->interfacescount; i++) {
1321 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1328 exceptions_throw_nosuchmethoderror(c, name, desc);
1333 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1335 exceptions_throw_abstractmethoderror();
1340 /* XXX check access rights */
1346 /* class_resolveinterfacemethod ************************************************
1348 Resolves a reference from REFERER to a method with NAME and DESC in
1351 If the method cannot be resolved the return value is NULL. If
1352 EXCEPT is true *exceptionptr is set, too.
1354 *******************************************************************************/
1356 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1357 classinfo *referer, bool throwexception)
1361 if (!(c->flags & ACC_INTERFACE)) {
1363 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1368 mi = class_resolveinterfacemethod_intern(c, name, desc);
1373 /* try class java.lang.Object */
1375 mi = class_findmethod(class_java_lang_Object, name, desc);
1381 exceptions_throw_nosuchmethoderror(c, name, desc);
1387 /* class_findfield *************************************************************
1389 Searches for field with specified name and type in a classinfo
1390 structure. If no such field is found NULL is returned.
1392 *******************************************************************************/
1394 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1398 for (i = 0; i < c->fieldscount; i++)
1399 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1400 return &(c->fields[i]);
1402 if (c->super != NULL)
1403 return class_findfield(c->super, name, desc);
1409 /* class_findfield_approx ******************************************************
1411 Searches in 'classinfo'-structure for a field with the specified
1414 *******************************************************************************/
1416 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1420 /* get field index */
1422 i = class_findfield_index_by_name(c, name);
1424 /* field was not found, return */
1429 /* return field address */
1431 return &(c->fields[i]);
1435 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1439 for (i = 0; i < c->fieldscount; i++) {
1440 /* compare field names */
1442 if ((c->fields[i].name == name))
1446 /* field was not found, raise exception */
1448 exceptions_throw_nosuchfielderror(c, name);
1454 /****************** Function: class_resolvefield_int ***************************
1456 This is an internally used helper function. Do not use this directly.
1458 Tries to resolve a field having the given name and type.
1459 If the field cannot be resolved, NULL is returned.
1461 *******************************************************************************/
1463 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1468 /* search for field in class c */
1470 for (i = 0; i < c->fieldscount; i++) {
1471 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1472 return &(c->fields[i]);
1476 /* Try super interfaces recursively. */
1478 for (i = 0; i < c->interfacescount; i++) {
1479 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1485 /* Try super class. */
1487 if (c->super != NULL)
1488 return class_resolvefield_int(c->super, name, desc);
1496 /********************* Function: class_resolvefield ***************************
1498 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1500 If the field cannot be resolved the return value is NULL. If EXCEPT is
1501 true *exceptionptr is set, too.
1503 *******************************************************************************/
1505 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1506 classinfo *referer, bool throwexception)
1510 fi = class_resolvefield_int(c, name, desc);
1514 exceptions_throw_nosuchfielderror(c, name);
1519 /* XXX check access rights */
1525 /* class_issubclass ************************************************************
1527 Checks if sub is a descendant of super.
1529 *******************************************************************************/
1531 bool class_issubclass(classinfo *sub, classinfo *super)
1538 /* We reached java/lang/Object and did not find the requested
1544 /* We found the requested super class. */
1554 /* class_isanysubclass *********************************************************
1556 Checks a subclass relation between two classes. Implemented
1557 interfaces are interpreted as super classes.
1559 Return value: 1 ... sub is subclass of super
1562 *******************************************************************************/
1564 bool class_isanysubclass(classinfo *sub, classinfo *super)
1569 /* This is the trivial case. */
1574 /* Primitive classes are only subclasses of themselves. */
1576 if (class_is_primitive(sub) || class_is_primitive(super))
1579 /* Check for interfaces. */
1581 if (super->flags & ACC_INTERFACE) {
1582 result = (sub->vftbl->interfacetablelength > super->index) &&
1583 (sub->vftbl->interfacetable[-super->index] != NULL);
1586 /* java.lang.Object is the only super class of any
1589 if (sub->flags & ACC_INTERFACE)
1590 return (super == class_java_lang_Object);
1592 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1594 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1595 result = diffval <= (uint32_t) super->vftbl->diffval;
1597 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1604 /* class_is_primitive **********************************************************
1606 Checks if the given class is a primitive class.
1608 *******************************************************************************/
1610 bool class_is_primitive(classinfo *c)
1612 if (c->flags & ACC_CLASS_PRIMITIVE)
1619 /* class_is_anonymousclass *****************************************************
1621 Checks if the given class is an anonymous class.
1623 *******************************************************************************/
1625 bool class_is_anonymousclass(classinfo *c)
1627 if (c->flags & ACC_CLASS_ANONYMOUS)
1634 /* class_is_array **************************************************************
1636 Checks if the given class is an array class.
1638 *******************************************************************************/
1640 bool class_is_array(classinfo *c)
1642 if (!(c->state & CLASS_LINKED))
1646 return (c->vftbl->arraydesc != NULL);
1650 /* class_is_interface **********************************************************
1652 Checks if the given class is an interface.
1654 *******************************************************************************/
1656 bool class_is_interface(classinfo *c)
1658 if (c->flags & ACC_INTERFACE)
1665 /* class_is_localclass *********************************************************
1667 Checks if the given class is a local class.
1669 *******************************************************************************/
1671 bool class_is_localclass(classinfo *c)
1673 if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1680 /* class_is_memberclass ********************************************************
1682 Checks if the given class is a member class.
1684 *******************************************************************************/
1686 bool class_is_memberclass(classinfo *c)
1688 if (c->flags & ACC_CLASS_MEMBER)
1695 /* class_get_classloader *******************************************************
1697 Return the classloader of the given class.
1699 *******************************************************************************/
1701 classloader *class_get_classloader(classinfo *c)
1705 cl = c->classloader;
1714 /* class_get_superclass ********************************************************
1716 Return the super class of the given class.
1718 *******************************************************************************/
1720 classinfo *class_get_superclass(classinfo *c)
1722 /* For interfaces we return NULL. */
1724 if (c->flags & ACC_INTERFACE)
1727 /* For java/lang/Object, primitive-type and Void classes c->super
1728 is NULL and we return NULL. */
1734 /* class_get_componenttype *****************************************************
1736 Return the component class of the given class. If the given class
1737 is not an array, return NULL.
1739 *******************************************************************************/
1741 classinfo *class_get_componenttype(classinfo *c)
1743 classinfo *component;
1744 arraydescriptor *ad;
1746 /* XXX maybe we could find a way to do this without linking. */
1747 /* This way should be safe and easy, however. */
1749 if (!(c->state & CLASS_LINKED))
1753 ad = c->vftbl->arraydesc;
1758 if (ad->arraytype == ARRAYTYPE_OBJECT)
1759 component = ad->componentvftbl->class;
1761 component = primitive_class_get_by_type(ad->arraytype);
1767 /* class_get_declaredclasses ***************************************************
1769 Return an array of declared classes of the given class.
1771 *******************************************************************************/
1773 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1775 classref_or_classinfo inner;
1776 classref_or_classinfo outer;
1778 int declaredclasscount; /* number of declared classes */
1779 int pos; /* current declared class */
1780 java_handle_objectarray_t *oa; /* array of declared classes */
1784 declaredclasscount = 0;
1786 if (!class_is_primitive(c) && !class_is_array(c)) {
1787 /* Determine number of declared classes. */
1789 for (i = 0; i < c->innerclasscount; i++) {
1790 /* Get outer-class. If the inner-class is not a member
1791 class, the outer-class is NULL. */
1793 outer = c->innerclass[i].outer_class;
1795 if (outer.any == NULL)
1798 /* Check if outer-class is a classref or a real class and
1799 get the class name from the structure. */
1801 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1803 /* Outer class is this class. */
1805 if ((outername == c->name) &&
1806 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1807 declaredclasscount++;
1811 /* Allocate Class[] and check for OOM. */
1813 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1818 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1819 inner = c->innerclass[i].inner_class;
1820 outer = c->innerclass[i].outer_class;
1822 /* Get outer-class. If the inner-class is not a member class,
1823 the outer-class is NULL. */
1825 if (outer.any == NULL)
1828 /* Check if outer_class is a classref or a real class and get
1829 the class name from the structure. */
1831 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1833 /* Outer class is this class. */
1835 if ((outername == c->name) &&
1836 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1838 ic = resolve_classref_or_classinfo_eager(inner, false);
1843 if (!(ic->state & CLASS_LINKED))
1844 if (!link_class(ic))
1847 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1855 /* class_get_declaringclass ****************************************************
1857 If the class or interface given is a member of another class,
1858 return the declaring class. For array and primitive classes return
1861 *******************************************************************************/
1863 classinfo *class_get_declaringclass(classinfo *c)
1865 classref_or_classinfo cr;
1868 /* Get declaring class. */
1870 cr = c->declaringclass;
1875 /* Resolve the class if necessary. */
1877 if (IS_CLASSREF(cr)) {
1878 /* dc = resolve_classref_eager(cr.ref); */
1879 dc = resolve_classref_or_classinfo_eager(cr, true);
1884 /* Store the resolved class in the class structure. */
1895 /* class_get_enclosingclass ****************************************************
1897 Return the enclosing class for the given class.
1899 *******************************************************************************/
1901 classinfo *class_get_enclosingclass(classinfo *c)
1903 classref_or_classinfo cr;
1906 /* Get enclosing class. */
1908 cr = c->enclosingclass;
1913 /* Resolve the class if necessary. */
1915 if (IS_CLASSREF(cr)) {
1916 /* ec = resolve_classref_eager(cr.ref); */
1917 ec = resolve_classref_or_classinfo_eager(cr, true);
1922 /* Store the resolved class in the class structure. */
1933 /* class_get_interfaces ********************************************************
1935 Return an array of interfaces of the given class.
1937 *******************************************************************************/
1939 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1942 java_handle_objectarray_t *oa;
1945 if (!(c->state & CLASS_LINKED))
1949 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1954 for (i = 0; i < c->interfacescount; i++) {
1955 ic = c->interfaces[i];
1957 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1964 /* class_get_annotations *******************************************************
1966 Get the unparsed declared annotations in a byte array
1970 c........the class of which the annotations should be returned
1973 The unparsed declared annotations in a byte array
1974 (or NULL if there aren't any).
1976 *******************************************************************************/
1978 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1980 #if defined(ENABLE_ANNOTATIONS)
1981 java_handle_t *annotations; /* unparsed annotations */
1983 LLNI_classinfo_field_get(c, annotations, annotations);
1985 return (java_handle_bytearray_t*)annotations;
1992 /* class_get_signature *********************************************************
1994 Return the signature of the given class. For array and primitive
1995 classes return NULL.
1997 *******************************************************************************/
1999 #if defined(ENABLE_JAVASE)
2000 utf *class_get_signature(classinfo *c)
2002 /* For array and primitive classes return NULL. */
2004 if (class_is_array(c) || class_is_primitive(c))
2007 return c->signature;
2012 /* class_printflags ************************************************************
2014 Prints flags of a class.
2016 *******************************************************************************/
2018 #if !defined(NDEBUG)
2019 void class_printflags(classinfo *c)
2026 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
2027 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
2028 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
2029 if (c->flags & ACC_STATIC) printf(" STATIC");
2030 if (c->flags & ACC_FINAL) printf(" FINAL");
2031 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2032 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
2033 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
2034 if (c->flags & ACC_NATIVE) printf(" NATIVE");
2035 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
2036 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
2041 /* class_print *****************************************************************
2043 Prints classname plus flags.
2045 *******************************************************************************/
2047 #if !defined(NDEBUG)
2048 void class_print(classinfo *c)
2055 utf_display_printable_ascii(c->name);
2056 class_printflags(c);
2061 /* class_classref_print ********************************************************
2063 Prints classname plus referer class.
2065 *******************************************************************************/
2067 #if !defined(NDEBUG)
2068 void class_classref_print(constant_classref *cr)
2075 utf_display_printable_ascii(cr->name);
2078 class_print(cr->referer);
2086 /* class_println ***************************************************************
2088 Prints classname plus flags and new line.
2090 *******************************************************************************/
2092 #if !defined(NDEBUG)
2093 void class_println(classinfo *c)
2101 /* class_classref_println ******************************************************
2103 Prints classname plus referer class and new line.
2105 *******************************************************************************/
2107 #if !defined(NDEBUG)
2108 void class_classref_println(constant_classref *cr)
2110 class_classref_print(cr);
2116 /* class_classref_or_classinfo_print *******************************************
2118 Prints classname plus referer class.
2120 *******************************************************************************/
2122 #if !defined(NDEBUG)
2123 void class_classref_or_classinfo_print(classref_or_classinfo c)
2125 if (c.any == NULL) {
2126 printf("(classref_or_classinfo) NULL");
2130 class_classref_print(c.ref);
2137 /* class_classref_or_classinfo_println *****************************************
2139 Prints classname plus referer class and a newline.
2141 *******************************************************************************/
2143 void class_classref_or_classinfo_println(classref_or_classinfo c)
2145 class_classref_or_classinfo_print(c);
2150 /* class_showconstantpool ******************************************************
2152 Dump the constant pool of the given class to stdout.
2154 *******************************************************************************/
2156 #if !defined(NDEBUG)
2157 void class_showconstantpool (classinfo *c)
2162 printf ("---- dump of constant pool ----\n");
2164 for (i=0; i<c->cpcount; i++) {
2165 printf ("#%d: ", (int) i);
2167 e = c -> cpinfos [i];
2170 switch (c -> cptags [i]) {
2171 case CONSTANT_Class:
2172 printf ("Classreference -> ");
2173 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2175 case CONSTANT_Fieldref:
2176 printf ("Fieldref -> ");
2177 field_fieldref_print((constant_FMIref *) e);
2179 case CONSTANT_Methodref:
2180 printf ("Methodref -> ");
2181 method_methodref_print((constant_FMIref *) e);
2183 case CONSTANT_InterfaceMethodref:
2184 printf ("InterfaceMethod -> ");
2185 method_methodref_print((constant_FMIref *) e);
2187 case CONSTANT_String:
2188 printf ("String -> ");
2189 utf_display_printable_ascii (e);
2191 case CONSTANT_Integer:
2192 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2194 case CONSTANT_Float:
2195 printf ("Float -> %f", ((constant_float*)e) -> value);
2197 case CONSTANT_Double:
2198 printf ("Double -> %f", ((constant_double*)e) -> value);
2202 u8 v = ((constant_long*)e) -> value;
2204 printf ("Long -> %ld", (long int) v);
2206 printf ("Long -> HI: %ld, LO: %ld\n",
2207 (long int) v.high, (long int) v.low);
2211 case CONSTANT_NameAndType:
2213 constant_nameandtype *cnt = e;
2214 printf ("NameAndType: ");
2215 utf_display_printable_ascii (cnt->name);
2217 utf_display_printable_ascii (cnt->descriptor);
2221 printf ("Utf8 -> ");
2222 utf_display_printable_ascii (e);
2225 log_text("Invalid type of ConstantPool-Entry");
2233 #endif /* !defined(NDEBUG) */
2236 /* class_showmethods ***********************************************************
2238 Dump info about the fields and methods of the given class to stdout.
2240 *******************************************************************************/
2242 #if !defined(NDEBUG)
2243 void class_showmethods (classinfo *c)
2247 printf("--------- Fields and Methods ----------------\n");
2249 class_printflags(c);
2253 utf_display_printable_ascii(c->name);
2258 utf_display_printable_ascii(c->super->name);
2262 printf("Index: %d\n", c->index);
2264 printf("Interfaces:\n");
2265 for (i = 0; i < c->interfacescount; i++) {
2267 utf_display_printable_ascii(c->interfaces[i]->name);
2268 printf (" (%d)\n", c->interfaces[i]->index);
2271 printf("Fields:\n");
2272 for (i = 0; i < c->fieldscount; i++)
2273 field_println(&(c->fields[i]));
2275 printf("Methods:\n");
2276 for (i = 0; i < c->methodscount; i++) {
2277 methodinfo *m = &(c->methods[i]);
2279 if (!(m->flags & ACC_STATIC))
2280 printf("vftblindex: %d ", m->vftblindex);
2285 printf ("Virtual function table:\n");
2286 for (i = 0; i < c->vftbl->vftbllength; i++)
2287 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2289 #endif /* !defined(NDEBUG) */
2293 * These are local overrides for various environment variables in Emacs.
2294 * Please do not remove this and leave it at the end of the file, where
2295 * Emacs will automagically detect them.
2296 * ---------------------------------------------------------------------
2299 * indent-tabs-mode: t
2303 * vim:noexpandtab:sw=4:ts=4: