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 classinfo *class_sun_reflect_annotation_AnnotationParser;
146 /* pseudo classes for the typechecker */
148 classinfo *pseudo_class_Arraystub;
149 classinfo *pseudo_class_Null;
150 classinfo *pseudo_class_New;
153 /* class_set_packagename *******************************************************
155 Derive the package name from the class name and store it in the struct.
157 *******************************************************************************/
159 void class_set_packagename(classinfo *c)
161 char *p = UTF_END(c->name) - 1;
162 char *start = c->name->text;
164 /* set the package name */
165 /* classes in the unnamed package keep packagename == NULL */
167 if (c->name->text[0] == '[') {
168 /* set packagename of arrays to the element's package */
170 for (; *start == '['; start++);
172 /* skip the 'L' in arrays of references */
176 for (; (p > start) && (*p != '/'); --p);
178 c->packagename = utf_new(start, p - start);
181 for (; (p > start) && (*p != '/'); --p);
183 c->packagename = utf_new(start, p - start);
188 /* class_create_classinfo ******************************************************
190 Create a new classinfo struct. The class name is set to the given utf *,
191 most other fields are initialized to zero.
193 Note: classname may be NULL. In this case a not-yet-named classinfo is
194 created. The name must be filled in later and class_set_packagename
195 must be called after that.
197 *******************************************************************************/
199 classinfo *class_create_classinfo(utf *classname)
203 #if defined(ENABLE_STATISTICS)
205 size_classinfo += sizeof(classinfo);
208 /* we use a safe name for temporarily unnamed classes */
210 if (classname == NULL)
211 classname = utf_not_named_yet;
215 log_message_utf("Creating class: ", classname);
218 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
220 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
221 /*c=NEW(classinfo);*/
224 /* Set the header.vftbl of all loaded classes to the one of
225 java.lang.Class, so Java code can use a class as object. */
227 if (class_java_lang_Class != NULL)
228 if (class_java_lang_Class->vftbl != NULL)
229 c->object.header.vftbl = class_java_lang_Class->vftbl;
231 #if defined(ENABLE_JAVASE)
232 /* check if the class is a reference class and flag it */
234 if (classname == utf_java_lang_ref_SoftReference) {
235 c->flags |= ACC_CLASS_REFERENCE_SOFT;
237 else if (classname == utf_java_lang_ref_WeakReference) {
238 c->flags |= ACC_CLASS_REFERENCE_WEAK;
240 else if (classname == utf_java_lang_ref_PhantomReference) {
241 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
245 if (classname != utf_not_named_yet)
246 class_set_packagename(c);
248 LOCK_INIT_OBJECT_LOCK(&c->object.header);
254 /* class_postset_header_vftbl **************************************************
256 Set the header.vftbl of all classes created before java.lang.Class
257 was linked. This is necessary that Java code can use a class as
260 *******************************************************************************/
262 void class_postset_header_vftbl(void)
266 classcache_name_entry *nmen;
267 classcache_class_entry *clsen;
269 assert(class_java_lang_Class);
271 for (slot = 0; slot < hashtable_classcache.size; slot++) {
272 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
274 for (; nmen; nmen = nmen->hashlink) {
275 /* iterate over all class entries */
277 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
280 /* now set the the vftbl */
282 if (c->object.header.vftbl == NULL)
283 c->object.header.vftbl = class_java_lang_Class->vftbl;
289 /* class_define ****************************************************************
291 Calls the loader and defines a class in the VM.
293 *******************************************************************************/
295 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
302 /* check if this class has already been defined */
304 c = classcache_lookup_defined_or_initiated(cl, name);
307 exceptions_throw_linkageerror("duplicate class definition: ", c);
312 /* create a new classinfo struct */
314 c = class_create_classinfo(name);
316 #if defined(ENABLE_STATISTICS)
319 if (opt_getloadingtime)
323 /* build a classbuffer with the given data */
325 cb = NEW(classbuffer);
332 /* preset the defining classloader */
336 /* load the class from this buffer */
338 r = load_class_from_classbuffer(cb);
342 FREE(cb, classbuffer);
344 #if defined(ENABLE_STATISTICS)
347 if (opt_getloadingtime)
352 /* If return value is NULL, we had a problem and the class is
353 not loaded. Now free the allocated memory, otherwise we
354 could run into a DOS. */
361 #if defined(ENABLE_JAVASE)
362 # if defined(WITH_CLASSPATH_SUN)
363 /* Store the protection domain. */
365 c->protectiondomain = pd;
369 /* Store the newly defined class in the class cache. This call
370 also checks whether a class of the same name has already been
371 defined by the same defining loader, and if so, replaces the
372 newly created class by the one defined earlier. */
374 /* Important: The classinfo given to classcache_store must be
375 fully prepared because another thread may return
376 this pointer after the lookup at to top of this
377 function directly after the class cache lock has
380 c = classcache_store(cl, c, true);
386 /* class_load_attribute_sourcefile *********************************************
388 SourceFile_attribute {
389 u2 attribute_name_index;
394 *******************************************************************************/
396 static bool class_load_attribute_sourcefile(classbuffer *cb)
407 /* check buffer size */
409 if (!suck_check_classbuffer_size(cb, 4 + 2))
412 /* check attribute length */
414 attribute_length = suck_u4(cb);
416 if (attribute_length != 2) {
417 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
421 /* there can be no more than one SourceFile attribute */
423 if (c->sourcefile != NULL) {
424 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
430 sourcefile_index = suck_u2(cb);
431 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
433 if (sourcefile == NULL)
436 /* store sourcefile */
438 c->sourcefile = sourcefile;
444 /* class_load_attribute_enclosingmethod ****************************************
446 EnclosingMethod_attribute {
447 u2 attribute_name_index;
453 *******************************************************************************/
455 #if defined(ENABLE_JAVASE)
456 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
462 classref_or_classinfo cr;
463 constant_nameandtype *cn;
469 /* check buffer size */
471 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
474 /* check attribute length */
476 attribute_length = suck_u4(cb);
478 if (attribute_length != 4) {
479 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
483 /* there can be no more than one EnclosingMethod attribute */
485 if (c->enclosingmethod != NULL) {
486 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
490 /* get class index */
492 class_index = suck_u2(cb);
493 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
495 /* get method index */
497 method_index = suck_u2(cb);
498 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
500 /* store info in classinfo */
502 c->enclosingclass.any = cr.any;
503 c->enclosingmethod = cn;
507 #endif /* defined(ENABLE_JAVASE) */
510 /* class_load_attributes *******************************************************
512 Read attributes from ClassFile.
515 u2 attribute_name_index;
517 u1 info[attribute_length];
520 InnerClasses_attribute {
521 u2 attribute_name_index;
525 *******************************************************************************/
527 bool class_load_attributes(classbuffer *cb)
530 uint16_t attributes_count;
531 uint16_t attribute_name_index;
533 innerclassinfo *info;
534 classref_or_classinfo inner;
535 classref_or_classinfo outer;
542 /* get attributes count */
544 if (!suck_check_classbuffer_size(cb, 2))
547 attributes_count = suck_u2(cb);
549 for (i = 0; i < attributes_count; i++) {
550 /* get attribute name */
552 if (!suck_check_classbuffer_size(cb, 2))
555 attribute_name_index = suck_u2(cb);
557 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
559 if (attribute_name == NULL)
562 if (attribute_name == utf_InnerClasses) {
565 if (c->innerclass != NULL) {
566 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
570 if (!suck_check_classbuffer_size(cb, 4 + 2))
573 /* skip attribute length */
576 /* number of records */
577 c->innerclasscount = suck_u2(cb);
579 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
582 /* allocate memory for innerclass structure */
583 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
585 for (j = 0; j < c->innerclasscount; j++) {
586 /* The innerclass structure contains a class with an encoded
587 name, its defining scope, its simple name and a bitmask of
590 info = c->innerclass + j;
592 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
593 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
594 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
597 /* If the current inner-class is the currently loaded
598 class check for some special flags. */
600 if (inner.ref->name == c->name) {
601 /* If an inner-class is not a member, its
602 outer-class is NULL. */
604 if (outer.ref != NULL) {
605 c->flags |= ACC_CLASS_MEMBER;
607 /* A member class doesn't have an
608 EnclosingMethod attribute, so set the
609 enclosing-class to be the same as the
612 c->declaringclass = outer;
613 c->enclosingclass = outer;
616 /* If an inner-class is anonymous, its name is
620 c->flags |= ACC_CLASS_ANONYMOUS;
623 info->inner_class = inner;
624 info->outer_class = outer;
629 else if (attribute_name == utf_SourceFile) {
632 if (!class_load_attribute_sourcefile(cb))
635 #if defined(ENABLE_JAVASE)
636 else if (attribute_name == utf_EnclosingMethod) {
637 /* EnclosingMethod */
639 if (!class_load_attribute_enclosingmethod(cb))
642 else if (attribute_name == utf_Signature) {
645 if (!loader_load_attribute_signature(cb, &(c->signature)))
650 #if defined(ENABLE_ANNOTATIONS)
651 /* XXX We can't do a release with that enabled */
653 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
654 /* RuntimeVisibleAnnotations */
655 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
658 /* XXX RuntimeInvisibleAnnotations should only be loaded
659 * (or returned to Java) if some commandline options says so.
660 * Currently there is no such option available in cacao,
661 * therefore I load them allways (for testing purpose).
662 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
663 * generated if you tell javac to do so. So in most cases
664 * there won't be any.
666 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
667 /* RuntimeInvisibleAnnotations */
668 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
674 /* unknown attribute */
676 if (!loader_skip_attribute_body(cb))
685 /* class_freepool **************************************************************
687 Frees all resources used by this classes Constant Pool.
689 *******************************************************************************/
691 static void class_freecpool(classinfo *c)
697 if (c->cptags && c->cpinfos) {
698 for (idx = 0; idx < c->cpcount; idx++) {
699 tag = c->cptags[idx];
700 info = c->cpinfos[idx];
704 case CONSTANT_Fieldref:
705 case CONSTANT_Methodref:
706 case CONSTANT_InterfaceMethodref:
707 FREE(info, constant_FMIref);
709 case CONSTANT_Integer:
710 FREE(info, constant_integer);
713 FREE(info, constant_float);
716 FREE(info, constant_long);
718 case CONSTANT_Double:
719 FREE(info, constant_double);
721 case CONSTANT_NameAndType:
722 FREE(info, constant_nameandtype);
730 MFREE(c->cptags, u1, c->cpcount);
733 MFREE(c->cpinfos, voidptr, c->cpcount);
737 /* class_getconstant ***********************************************************
739 Retrieves the value at position 'pos' of the constantpool of a
740 class. If the type of the value is other than 'ctype', an error is
743 *******************************************************************************/
745 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
747 /* check index and type of constantpool entry */
748 /* (pos == 0 is caught by type comparison) */
750 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
751 exceptions_throw_classformaterror(c, "Illegal constant pool index");
755 return c->cpinfos[pos];
759 /* innerclass_getconstant ******************************************************
761 Like class_getconstant, but if cptags is ZERO, null is returned.
763 *******************************************************************************/
765 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
767 /* invalid position in constantpool */
769 if (pos >= c->cpcount) {
770 exceptions_throw_classformaterror(c, "Illegal constant pool index");
774 /* constantpool entry of type 0 */
776 if (c->cptags[pos] == 0)
779 /* check type of constantpool entry */
781 if (c->cptags[pos] != ctype) {
782 exceptions_throw_classformaterror(c, "Illegal constant pool index");
786 return c->cpinfos[pos];
790 /* class_free ******************************************************************
792 Frees all resources used by the class.
794 *******************************************************************************/
796 void class_free(classinfo *c)
804 MFREE(c->interfaces, classinfo*, c->interfacescount);
807 for (i = 0; i < c->fieldscount; i++)
808 field_free(&(c->fields[i]));
809 #if defined(ENABLE_CACAO_GC)
810 MFREE(c->fields, fieldinfo, c->fieldscount);
815 for (i = 0; i < c->methodscount; i++)
816 method_free(&(c->methods[i]));
817 MFREE(c->methods, methodinfo, c->methodscount);
820 if ((v = c->vftbl) != NULL) {
822 mem_free(v->arraydesc,sizeof(arraydescriptor));
824 for (i = 0; i < v->interfacetablelength; i++) {
825 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
827 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
829 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
830 sizeof(methodptr*) * (v->interfacetablelength -
831 (v->interfacetablelength > 0));
832 v = (vftbl_t*) (((methodptr*) v) -
833 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
838 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
840 /* if (c->classvftbl)
841 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
845 #if defined(ENABLE_ANNOTATIONS)
846 annotation_bytearray_free(c->annotations);
848 annotation_bytearrays_free(c->method_annotations);
849 annotation_bytearrays_free(c->method_parameterannotations);
850 annotation_bytearrays_free(c->method_annotationdefaults);
852 annotation_bytearrays_free(c->field_annotations);
857 /* get_array_class *************************************************************
859 Returns the array class with the given name for the given
860 classloader, or NULL if an exception occurred.
862 Note: This function does eager loading.
864 *******************************************************************************/
866 static classinfo *get_array_class(utf *name,classloader *initloader,
867 classloader *defloader,bool link)
871 /* lookup this class in the classcache */
872 c = classcache_lookup(initloader,name);
874 c = classcache_lookup_defined(defloader,name);
877 /* we have to create it */
878 c = class_create_classinfo(name);
879 c = load_newly_created_array(c,initloader);
885 assert(c->state & CLASS_LOADED);
886 assert(c->classloader == defloader);
888 if (link && !(c->state & CLASS_LINKED))
892 assert(!link || (c->state & CLASS_LINKED));
898 /* class_array_of **************************************************************
900 Returns an array class with the given component class. The array
901 class is dynamically created if neccessary.
903 *******************************************************************************/
905 classinfo *class_array_of(classinfo *component, bool link)
914 cl = component->classloader;
916 dumpsize = dump_size();
918 /* Assemble the array class name */
919 namelen = component->name->blength;
921 if (component->name->text[0] == '[') {
922 /* the component is itself an array */
923 namebuf = DMNEW(char, namelen + 1);
925 MCOPY(namebuf + 1, component->name->text, char, namelen);
929 /* the component is a non-array class */
930 namebuf = DMNEW(char, namelen + 3);
933 MCOPY(namebuf + 2, component->name->text, char, namelen);
934 namebuf[2 + namelen] = ';';
938 u = utf_new(namebuf, namelen);
940 c = get_array_class(u, cl, cl, link);
942 dump_release(dumpsize);
948 /* class_multiarray_of *********************************************************
950 Returns an array class with the given dimension and element class.
951 The array class is dynamically created if neccessary.
953 *******************************************************************************/
955 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
962 dumpsize = dump_size();
965 log_text("Invalid array dimension requested");
969 /* Assemble the array class name */
970 namelen = element->name->blength;
972 if (element->name->text[0] == '[') {
973 /* the element is itself an array */
974 namebuf = DMNEW(char, namelen + dim);
975 memcpy(namebuf + dim, element->name->text, namelen);
979 /* the element is a non-array class */
980 namebuf = DMNEW(char, namelen + 2 + dim);
982 memcpy(namebuf + dim + 1, element->name->text, namelen);
983 namelen += (2 + dim);
984 namebuf[namelen - 1] = ';';
986 memset(namebuf, '[', dim);
988 c = get_array_class(utf_new(namebuf, namelen),
989 element->classloader,
990 element->classloader,
993 dump_release(dumpsize);
999 /* class_lookup_classref *******************************************************
1001 Looks up the constant_classref for a given classname in the classref
1005 cls..............the class containing the reference
1006 name.............the name of the class refered to
1009 a pointer to a constant_classref, or
1010 NULL if the reference was not found
1012 *******************************************************************************/
1014 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1016 constant_classref *ref;
1017 extra_classref *xref;
1022 assert(!cls->classrefcount || cls->classrefs);
1024 /* first search the main classref table */
1025 count = cls->classrefcount;
1026 ref = cls->classrefs;
1027 for (; count; --count, ++ref)
1028 if (ref->name == name)
1031 /* next try the list of extra classrefs */
1032 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1033 if (xref->classref.name == name)
1034 return &(xref->classref);
1042 /* class_get_classref **********************************************************
1044 Returns the constant_classref for a given classname.
1047 cls..............the class containing the reference
1048 name.............the name of the class refered to
1051 a pointer to a constant_classref (never NULL)
1054 The given name is not checked for validity!
1056 *******************************************************************************/
1058 constant_classref *class_get_classref(classinfo *cls, utf *name)
1060 constant_classref *ref;
1061 extra_classref *xref;
1066 ref = class_lookup_classref(cls,name);
1070 xref = NEW(extra_classref);
1071 CLASSREF_INIT(xref->classref,cls,name);
1073 xref->next = cls->extclassrefs;
1074 cls->extclassrefs = xref;
1076 return &(xref->classref);
1080 /* class_get_self_classref *****************************************************
1082 Returns the constant_classref to the class itself.
1085 cls..............the class containing the reference
1088 a pointer to a constant_classref (never NULL)
1090 *******************************************************************************/
1092 constant_classref *class_get_self_classref(classinfo *cls)
1094 /* XXX this should be done in a faster way. Maybe always make */
1095 /* the classref of index 0 a self reference. */
1096 return class_get_classref(cls,cls->name);
1099 /* class_get_classref_multiarray_of ********************************************
1101 Returns an array type reference with the given dimension and element class
1105 dim..............the requested dimension
1106 dim must be in [1;255]. This is NOT checked!
1107 ref..............the component class reference
1110 a pointer to the class reference for the array type
1113 The referer of `ref` is used as the referer for the new classref.
1115 *******************************************************************************/
1117 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1122 constant_classref *cr;
1125 assert(dim >= 1 && dim <= 255);
1127 dumpsize = dump_size();
1129 /* Assemble the array class name */
1130 namelen = ref->name->blength;
1132 if (ref->name->text[0] == '[') {
1133 /* the element is itself an array */
1134 namebuf = DMNEW(char, namelen + dim);
1135 memcpy(namebuf + dim, ref->name->text, namelen);
1139 /* the element is a non-array class */
1140 namebuf = DMNEW(char, namelen + 2 + dim);
1142 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1143 namelen += (2 + dim);
1144 namebuf[namelen - 1] = ';';
1146 memset(namebuf, '[', dim);
1148 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1150 dump_release(dumpsize);
1156 /* class_get_classref_component_of *********************************************
1158 Returns the component classref of a given array type reference
1161 ref..............the array type reference
1164 a reference to the component class, or
1165 NULL if `ref` is not an object array type reference
1168 The referer of `ref` is used as the referer for the new classref.
1170 *******************************************************************************/
1172 constant_classref *class_get_classref_component_of(constant_classref *ref)
1179 name = ref->name->text;
1183 namelen = ref->name->blength - 1;
1188 else if (*name != '[') {
1192 return class_get_classref(ref->referer, utf_new(name, namelen));
1196 /* class_findmethod ************************************************************
1198 Searches a 'classinfo' structure for a method having the given name
1199 and descriptor. If descriptor is NULL, it is ignored.
1201 *******************************************************************************/
1203 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1208 for (i = 0; i < c->methodscount; i++) {
1209 m = &(c->methods[i]);
1211 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1219 /* class_resolvemethod *********************************************************
1221 Searches a class and it's super classes for a method.
1223 Superinterfaces are *not* searched.
1225 *******************************************************************************/
1227 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1232 m = class_findmethod(c, name, desc);
1237 /* JVM Specification bug:
1239 It is important NOT to resolve special <init> and <clinit>
1240 methods to super classes or interfaces; yet, this is not
1241 explicited in the specification. Section 5.4.3.3 should be
1242 updated appropriately. */
1244 if (name == utf_init || name == utf_clinit)
1254 /* class_resolveinterfacemethod_intern *****************************************
1256 Internally used helper function. Do not use this directly.
1258 *******************************************************************************/
1260 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1261 utf *name, utf *desc)
1266 /* try to find the method in the class */
1268 m = class_findmethod(c, name, desc);
1273 /* no method found? try the superinterfaces */
1275 for (i = 0; i < c->interfacescount; i++) {
1276 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1283 /* no method found */
1289 /* class_resolveclassmethod ****************************************************
1291 Resolves a reference from REFERER to a method with NAME and DESC in
1294 If the method cannot be resolved the return value is NULL. If
1295 EXCEPT is true *exceptionptr is set, too.
1297 *******************************************************************************/
1299 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1300 classinfo *referer, bool throwexception)
1306 /* if (c->flags & ACC_INTERFACE) { */
1307 /* if (throwexception) */
1308 /* *exceptionptr = */
1309 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1313 /* try class c and its superclasses */
1317 m = class_resolvemethod(cls, name, desc);
1322 /* try the superinterfaces */
1324 for (i = 0; i < c->interfacescount; i++) {
1325 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1333 exceptions_throw_nosuchmethoderror(c, name, desc);
1338 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1340 exceptions_throw_abstractmethoderror();
1345 /* XXX check access rights */
1351 /* class_resolveinterfacemethod ************************************************
1353 Resolves a reference from REFERER to a method with NAME and DESC in
1356 If the method cannot be resolved the return value is NULL. If
1357 EXCEPT is true *exceptionptr is set, too.
1359 *******************************************************************************/
1361 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1362 classinfo *referer, bool throwexception)
1366 if (!(c->flags & ACC_INTERFACE)) {
1368 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1373 mi = class_resolveinterfacemethod_intern(c, name, desc);
1378 /* try class java.lang.Object */
1380 mi = class_findmethod(class_java_lang_Object, name, desc);
1386 exceptions_throw_nosuchmethoderror(c, name, desc);
1392 /* class_findfield *************************************************************
1394 Searches for field with specified name and type in a classinfo
1395 structure. If no such field is found NULL is returned.
1397 *******************************************************************************/
1399 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1403 for (i = 0; i < c->fieldscount; i++)
1404 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1405 return &(c->fields[i]);
1408 return class_findfield(c->super.cls, name, desc);
1414 /* class_findfield_approx ******************************************************
1416 Searches in 'classinfo'-structure for a field with the specified
1419 *******************************************************************************/
1421 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1425 /* get field index */
1427 i = class_findfield_index_by_name(c, name);
1429 /* field was not found, return */
1434 /* return field address */
1436 return &(c->fields[i]);
1440 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1444 for (i = 0; i < c->fieldscount; i++) {
1445 /* compare field names */
1447 if ((c->fields[i].name == name))
1451 /* field was not found, raise exception */
1453 exceptions_throw_nosuchfielderror(c, name);
1459 /****************** Function: class_resolvefield_int ***************************
1461 This is an internally used helper function. Do not use this directly.
1463 Tries to resolve a field having the given name and type.
1464 If the field cannot be resolved, NULL is returned.
1466 *******************************************************************************/
1468 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1473 /* search for field in class c */
1475 for (i = 0; i < c->fieldscount; i++) {
1476 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1477 return &(c->fields[i]);
1481 /* try superinterfaces recursively */
1483 for (i = 0; i < c->interfacescount; i++) {
1484 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1489 /* try superclass */
1492 return class_resolvefield_int(c->super.cls, name, desc);
1500 /********************* Function: class_resolvefield ***************************
1502 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1504 If the field cannot be resolved the return value is NULL. If EXCEPT is
1505 true *exceptionptr is set, too.
1507 *******************************************************************************/
1509 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1510 classinfo *referer, bool throwexception)
1514 fi = class_resolvefield_int(c, name, desc);
1518 exceptions_throw_nosuchfielderror(c, name);
1523 /* XXX check access rights */
1529 /* class_resolve_superclass ****************************************************
1531 Resolves the super class reference of the given class if necessary.
1533 *******************************************************************************/
1535 static classinfo *class_resolve_superclass(classinfo *c)
1539 if (c->super.any == NULL)
1542 /* Check if the super class is a reference. */
1544 if (IS_CLASSREF(c->super)) {
1545 /* XXX I'm very sure this is not correct. */
1546 super = resolve_classref_or_classinfo_eager(c->super, true);
1547 /* super = resolve_classref_or_classinfo_eager(c->super, false); */
1552 /* Store the resolved super class in the class structure. */
1554 c->super.cls = super;
1557 return c->super.cls;
1561 /* class_issubclass ************************************************************
1563 Checks if sub is a descendant of super.
1565 *******************************************************************************/
1567 bool class_issubclass(classinfo *sub, classinfo *super)
1576 /* sub = class_resolve_superclass(sub); */
1577 if (sub->super.any == NULL)
1580 assert(IS_CLASSREF(sub->super) == 0);
1582 sub = sub->super.cls;
1587 /* class_isanysubclass *********************************************************
1589 Checks a subclass relation between two classes. Implemented
1590 interfaces are interpreted as super classes.
1592 Return value: 1 ... sub is subclass of super
1595 *******************************************************************************/
1597 bool class_isanysubclass(classinfo *sub, classinfo *super)
1602 /* This is the trivial case. */
1607 /* Primitive classes are only subclasses of themselves. */
1609 if (class_is_primitive(sub) || class_is_primitive(super))
1612 /* Check for interfaces. */
1614 if (super->flags & ACC_INTERFACE) {
1615 result = (sub->vftbl->interfacetablelength > super->index) &&
1616 (sub->vftbl->interfacetable[-super->index] != NULL);
1619 /* java.lang.Object is the only super class of any
1622 if (sub->flags & ACC_INTERFACE)
1623 return (super == class_java_lang_Object);
1625 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1627 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1628 result = diffval <= (uint32_t) super->vftbl->diffval;
1630 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1637 /* class_is_primitive **********************************************************
1639 Checks if the given class is a primitive class.
1641 *******************************************************************************/
1643 bool class_is_primitive(classinfo *c)
1645 if (c->flags & ACC_CLASS_PRIMITIVE)
1652 /* class_is_anonymousclass *****************************************************
1654 Checks if the given class is an anonymous class.
1656 *******************************************************************************/
1658 bool class_is_anonymousclass(classinfo *c)
1660 if (c->flags & ACC_CLASS_ANONYMOUS)
1667 /* class_is_array **************************************************************
1669 Checks if the given class is an array class.
1671 *******************************************************************************/
1673 bool class_is_array(classinfo *c)
1675 if (!(c->state & CLASS_LINKED))
1679 return (c->vftbl->arraydesc != NULL);
1683 /* class_is_interface **********************************************************
1685 Checks if the given class is an interface.
1687 *******************************************************************************/
1689 bool class_is_interface(classinfo *c)
1691 if (c->flags & ACC_INTERFACE)
1698 /* class_is_localclass *********************************************************
1700 Checks if the given class is a local class.
1702 *******************************************************************************/
1704 bool class_is_localclass(classinfo *c)
1706 if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1713 /* class_is_memberclass ********************************************************
1715 Checks if the given class is a member class.
1717 *******************************************************************************/
1719 bool class_is_memberclass(classinfo *c)
1721 if (c->flags & ACC_CLASS_MEMBER)
1728 /* class_get_superclass ********************************************************
1730 Return the super class of the given class. If the super-field is a
1731 class-reference, resolve it and store it in the classinfo.
1733 *******************************************************************************/
1735 classinfo *class_get_superclass(classinfo *c)
1739 /* For java.lang.Object, primitive and Void classes we return
1742 if (c->super.any == NULL)
1745 /* For interfaces we also return NULL. */
1747 if (c->flags & ACC_INTERFACE)
1750 /* We may have to resolve the super class reference. */
1752 super = class_resolve_superclass(c);
1758 /* class_get_componenttype *****************************************************
1760 Return the component class of the given class. If the given class
1761 is not an array, return NULL.
1763 *******************************************************************************/
1765 classinfo *class_get_componenttype(classinfo *c)
1767 classinfo *component;
1768 arraydescriptor *ad;
1770 /* XXX maybe we could find a way to do this without linking. */
1771 /* This way should be safe and easy, however. */
1773 if (!(c->state & CLASS_LINKED))
1777 ad = c->vftbl->arraydesc;
1782 if (ad->arraytype == ARRAYTYPE_OBJECT)
1783 component = ad->componentvftbl->class;
1785 component = primitive_class_get_by_type(ad->arraytype);
1791 /* class_get_declaredclasses ***************************************************
1793 Return an array of declared classes of the given class.
1795 *******************************************************************************/
1797 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1799 classref_or_classinfo inner;
1800 classref_or_classinfo outer;
1802 int declaredclasscount; /* number of declared classes */
1803 int pos; /* current declared class */
1804 java_handle_objectarray_t *oa; /* array of declared classes */
1808 declaredclasscount = 0;
1810 if (!class_is_primitive(c) && !class_is_array(c)) {
1811 /* Determine number of declared classes. */
1813 for (i = 0; i < c->innerclasscount; i++) {
1814 /* Get outer-class. If the inner-class is not a member
1815 class, the outer-class is NULL. */
1817 outer = c->innerclass[i].outer_class;
1819 if (outer.any == NULL)
1822 /* Check if outer-class is a classref or a real class and
1823 get the class name from the structure. */
1825 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1827 /* Outer class is this class. */
1829 if ((outername == c->name) &&
1830 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1831 declaredclasscount++;
1835 /* Allocate Class[] and check for OOM. */
1837 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1842 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1843 inner = c->innerclass[i].inner_class;
1844 outer = c->innerclass[i].outer_class;
1846 /* Get outer-class. If the inner-class is not a member class,
1847 the outer-class is NULL. */
1849 if (outer.any == NULL)
1852 /* Check if outer_class is a classref or a real class and get
1853 the class name from the structure. */
1855 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1857 /* Outer class is this class. */
1859 if ((outername == c->name) &&
1860 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1862 ic = resolve_classref_or_classinfo_eager(inner, false);
1867 if (!(ic->state & CLASS_LINKED))
1868 if (!link_class(ic))
1871 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1879 /* class_get_declaringclass ****************************************************
1881 If the class or interface given is a member of another class,
1882 return the declaring class. For array and primitive classes return
1885 *******************************************************************************/
1887 classinfo *class_get_declaringclass(classinfo *c)
1889 classref_or_classinfo cr;
1892 /* Get declaring class. */
1894 cr = c->declaringclass;
1899 /* Resolve the class if necessary. */
1901 if (IS_CLASSREF(cr)) {
1902 /* dc = resolve_classref_eager(cr.ref); */
1903 dc = resolve_classref_or_classinfo_eager(cr, true);
1908 /* Store the resolved class in the class structure. */
1919 /* class_get_enclosingclass ****************************************************
1921 Return the enclosing class for the given class.
1923 *******************************************************************************/
1925 classinfo *class_get_enclosingclass(classinfo *c)
1927 classref_or_classinfo cr;
1930 /* Get enclosing class. */
1932 cr = c->enclosingclass;
1937 /* Resolve the class if necessary. */
1939 if (IS_CLASSREF(cr)) {
1940 /* ec = resolve_classref_eager(cr.ref); */
1941 ec = resolve_classref_or_classinfo_eager(cr, true);
1946 /* Store the resolved class in the class structure. */
1957 /* class_get_interfaces ********************************************************
1959 Return an array of interfaces of the given class.
1961 *******************************************************************************/
1963 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1966 java_handle_objectarray_t *oa;
1969 if (!(c->state & CLASS_LINKED))
1973 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1978 for (i = 0; i < c->interfacescount; i++) {
1979 ic = c->interfaces[i].cls;
1981 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1988 /* class_get_annotations *******************************************************
1990 Return the unparsed declared annotations in an byte array
1991 of the given class (or NULL if there aren't any).
1993 *******************************************************************************/
1995 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1997 #if defined(ENABLE_ANNOTATIONS)
1998 java_handle_bytearray_t *annotations = NULL;
2001 /* Return null for arrays and primitives: */
2002 if (class_is_primitive(c) || class_is_array(c)) {
2006 /* copy the annotations into a java byte array: */
2007 if (c->annotations != NULL) {
2008 size = c->annotations->size;
2009 annotations = builtin_newarray_byte(size);
2011 if(annotations != NULL) {
2012 MCOPY(annotations->data, c->annotations->data, uint8_t, size);
2023 /* class_get_signature *********************************************************
2025 Return the signature of the given class. For array and primitive
2026 classes return NULL.
2028 *******************************************************************************/
2030 #if defined(ENABLE_JAVASE)
2031 utf *class_get_signature(classinfo *c)
2033 /* For array and primitive classes return NULL. */
2035 if (class_is_array(c) || class_is_primitive(c))
2038 return c->signature;
2043 /* class_printflags ************************************************************
2045 Prints flags of a class.
2047 *******************************************************************************/
2049 #if !defined(NDEBUG)
2050 void class_printflags(classinfo *c)
2057 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
2058 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
2059 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
2060 if (c->flags & ACC_STATIC) printf(" STATIC");
2061 if (c->flags & ACC_FINAL) printf(" FINAL");
2062 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2063 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
2064 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
2065 if (c->flags & ACC_NATIVE) printf(" NATIVE");
2066 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
2067 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
2072 /* class_print *****************************************************************
2074 Prints classname plus flags.
2076 *******************************************************************************/
2078 #if !defined(NDEBUG)
2079 void class_print(classinfo *c)
2086 utf_display_printable_ascii(c->name);
2087 class_printflags(c);
2092 /* class_classref_print ********************************************************
2094 Prints classname plus referer class.
2096 *******************************************************************************/
2098 #if !defined(NDEBUG)
2099 void class_classref_print(constant_classref *cr)
2106 utf_display_printable_ascii(cr->name);
2109 class_print(cr->referer);
2117 /* class_println ***************************************************************
2119 Prints classname plus flags and new line.
2121 *******************************************************************************/
2123 #if !defined(NDEBUG)
2124 void class_println(classinfo *c)
2132 /* class_classref_println ******************************************************
2134 Prints classname plus referer class and new line.
2136 *******************************************************************************/
2138 #if !defined(NDEBUG)
2139 void class_classref_println(constant_classref *cr)
2141 class_classref_print(cr);
2147 /* class_classref_or_classinfo_print *******************************************
2149 Prints classname plus referer class.
2151 *******************************************************************************/
2153 #if !defined(NDEBUG)
2154 void class_classref_or_classinfo_print(classref_or_classinfo c)
2156 if (c.any == NULL) {
2157 printf("(classref_or_classinfo) NULL");
2161 class_classref_print(c.ref);
2168 /* class_classref_or_classinfo_println *****************************************
2170 Prints classname plus referer class and a newline.
2172 *******************************************************************************/
2174 void class_classref_or_classinfo_println(classref_or_classinfo c)
2176 class_classref_or_classinfo_println(c);
2181 /* class_showconstantpool ******************************************************
2183 Dump the constant pool of the given class to stdout.
2185 *******************************************************************************/
2187 #if !defined(NDEBUG)
2188 void class_showconstantpool (classinfo *c)
2193 printf ("---- dump of constant pool ----\n");
2195 for (i=0; i<c->cpcount; i++) {
2196 printf ("#%d: ", (int) i);
2198 e = c -> cpinfos [i];
2201 switch (c -> cptags [i]) {
2202 case CONSTANT_Class:
2203 printf ("Classreference -> ");
2204 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2206 case CONSTANT_Fieldref:
2207 printf ("Fieldref -> ");
2208 field_fieldref_print((constant_FMIref *) e);
2210 case CONSTANT_Methodref:
2211 printf ("Methodref -> ");
2212 method_methodref_print((constant_FMIref *) e);
2214 case CONSTANT_InterfaceMethodref:
2215 printf ("InterfaceMethod -> ");
2216 method_methodref_print((constant_FMIref *) e);
2218 case CONSTANT_String:
2219 printf ("String -> ");
2220 utf_display_printable_ascii (e);
2222 case CONSTANT_Integer:
2223 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2225 case CONSTANT_Float:
2226 printf ("Float -> %f", ((constant_float*)e) -> value);
2228 case CONSTANT_Double:
2229 printf ("Double -> %f", ((constant_double*)e) -> value);
2233 u8 v = ((constant_long*)e) -> value;
2235 printf ("Long -> %ld", (long int) v);
2237 printf ("Long -> HI: %ld, LO: %ld\n",
2238 (long int) v.high, (long int) v.low);
2242 case CONSTANT_NameAndType:
2244 constant_nameandtype *cnt = e;
2245 printf ("NameAndType: ");
2246 utf_display_printable_ascii (cnt->name);
2248 utf_display_printable_ascii (cnt->descriptor);
2252 printf ("Utf8 -> ");
2253 utf_display_printable_ascii (e);
2256 log_text("Invalid type of ConstantPool-Entry");
2264 #endif /* !defined(NDEBUG) */
2267 /* class_showmethods ***********************************************************
2269 Dump info about the fields and methods of the given class to stdout.
2271 *******************************************************************************/
2273 #if !defined(NDEBUG)
2274 void class_showmethods (classinfo *c)
2278 printf("--------- Fields and Methods ----------------\n");
2280 class_printflags(c);
2284 utf_display_printable_ascii(c->name);
2289 utf_display_printable_ascii(c->super.cls->name);
2293 printf("Index: %d\n", c->index);
2295 printf("Interfaces:\n");
2296 for (i = 0; i < c->interfacescount; i++) {
2298 utf_display_printable_ascii(c->interfaces[i].cls->name);
2299 printf (" (%d)\n", c->interfaces[i].cls->index);
2302 printf("Fields:\n");
2303 for (i = 0; i < c->fieldscount; i++)
2304 field_println(&(c->fields[i]));
2306 printf("Methods:\n");
2307 for (i = 0; i < c->methodscount; i++) {
2308 methodinfo *m = &(c->methods[i]);
2310 if (!(m->flags & ACC_STATIC))
2311 printf("vftblindex: %d ", m->vftblindex);
2316 printf ("Virtual function table:\n");
2317 for (i = 0; i < c->vftbl->vftbllength; i++)
2318 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2320 #endif /* !defined(NDEBUG) */
2324 * These are local overrides for various environment variables in Emacs.
2325 * Please do not remove this and leave it at the end of the file, where
2326 * Emacs will automagically detect them.
2327 * ---------------------------------------------------------------------
2330 * indent-tabs-mode: t
2334 * vim:noexpandtab:sw=4:ts=4: