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 struct.
159 *******************************************************************************/
161 void class_set_packagename(classinfo *c)
163 char *p = UTF_END(c->name) - 1;
164 char *start = c->name->text;
166 /* set the package name */
167 /* classes in the unnamed package keep packagename == NULL */
169 if (c->name->text[0] == '[') {
170 /* set packagename of arrays to the element's package */
172 for (; *start == '['; start++);
174 /* skip the 'L' in arrays of references */
178 for (; (p > start) && (*p != '/'); --p);
180 c->packagename = utf_new(start, p - start);
183 for (; (p > start) && (*p != '/'); --p);
185 c->packagename = utf_new(start, p - start);
190 /* class_create_classinfo ******************************************************
192 Create a new classinfo struct. The class name is set to the given utf *,
193 most other fields are initialized to zero.
195 Note: classname may be NULL. In this case a not-yet-named classinfo is
196 created. The name must be filled in later and class_set_packagename
197 must be called after that.
199 *******************************************************************************/
201 classinfo *class_create_classinfo(utf *classname)
205 #if defined(ENABLE_STATISTICS)
207 size_classinfo += sizeof(classinfo);
210 /* we use a safe name for temporarily unnamed classes */
212 if (classname == NULL)
213 classname = utf_not_named_yet;
217 log_message_utf("Creating class: ", classname);
220 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
222 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
223 /*c=NEW(classinfo);*/
226 /* Set the header.vftbl of all loaded classes to the one of
227 java.lang.Class, so Java code can use a class as object. */
229 if (class_java_lang_Class != NULL)
230 if (class_java_lang_Class->vftbl != NULL)
231 c->object.header.vftbl = class_java_lang_Class->vftbl;
233 #if defined(ENABLE_JAVASE)
234 /* check if the class is a reference class and flag it */
236 if (classname == utf_java_lang_ref_SoftReference) {
237 c->flags |= ACC_CLASS_REFERENCE_SOFT;
239 else if (classname == utf_java_lang_ref_WeakReference) {
240 c->flags |= ACC_CLASS_REFERENCE_WEAK;
242 else if (classname == utf_java_lang_ref_PhantomReference) {
243 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
247 if (classname != utf_not_named_yet)
248 class_set_packagename(c);
250 LOCK_INIT_OBJECT_LOCK(&c->object.header);
256 /* class_postset_header_vftbl **************************************************
258 Set the header.vftbl of all classes created before java.lang.Class
259 was linked. This is necessary that Java code can use a class as
262 *******************************************************************************/
264 void class_postset_header_vftbl(void)
268 classcache_name_entry *nmen;
269 classcache_class_entry *clsen;
271 assert(class_java_lang_Class);
273 for (slot = 0; slot < hashtable_classcache.size; slot++) {
274 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
276 for (; nmen; nmen = nmen->hashlink) {
277 /* iterate over all class entries */
279 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
282 /* now set the the vftbl */
284 if (c->object.header.vftbl == NULL)
285 c->object.header.vftbl = class_java_lang_Class->vftbl;
291 /* class_define ****************************************************************
293 Calls the loader and defines a class in the VM.
295 *******************************************************************************/
297 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
304 /* check if this class has already been defined */
306 c = classcache_lookup_defined_or_initiated(cl, name);
309 exceptions_throw_linkageerror("duplicate class definition: ", c);
314 /* create a new classinfo struct */
316 c = class_create_classinfo(name);
318 #if defined(ENABLE_STATISTICS)
321 if (opt_getloadingtime)
325 /* build a classbuffer with the given data */
327 cb = NEW(classbuffer);
334 /* preset the defining classloader */
338 /* load the class from this buffer */
340 r = load_class_from_classbuffer(cb);
344 FREE(cb, classbuffer);
346 #if defined(ENABLE_STATISTICS)
349 if (opt_getloadingtime)
354 /* If return value is NULL, we had a problem and the class is
355 not loaded. Now free the allocated memory, otherwise we
356 could run into a DOS. */
363 #if defined(ENABLE_JAVASE)
364 # if defined(WITH_CLASSPATH_SUN)
365 /* Store the protection domain. */
367 c->protectiondomain = pd;
371 /* Store the newly defined class in the class cache. This call
372 also checks whether a class of the same name has already been
373 defined by the same defining loader, and if so, replaces the
374 newly created class by the one defined earlier. */
376 /* Important: The classinfo given to classcache_store must be
377 fully prepared because another thread may return
378 this pointer after the lookup at to top of this
379 function directly after the class cache lock has
382 c = classcache_store(cl, c, true);
388 /* class_load_attribute_sourcefile *********************************************
390 SourceFile_attribute {
391 u2 attribute_name_index;
396 *******************************************************************************/
398 static bool class_load_attribute_sourcefile(classbuffer *cb)
409 /* check buffer size */
411 if (!suck_check_classbuffer_size(cb, 4 + 2))
414 /* check attribute length */
416 attribute_length = suck_u4(cb);
418 if (attribute_length != 2) {
419 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
423 /* there can be no more than one SourceFile attribute */
425 if (c->sourcefile != NULL) {
426 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
432 sourcefile_index = suck_u2(cb);
433 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
435 if (sourcefile == NULL)
438 /* store sourcefile */
440 c->sourcefile = sourcefile;
446 /* class_load_attribute_enclosingmethod ****************************************
448 EnclosingMethod_attribute {
449 u2 attribute_name_index;
455 *******************************************************************************/
457 #if defined(ENABLE_JAVASE)
458 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
464 classref_or_classinfo cr;
465 constant_nameandtype *cn;
471 /* check buffer size */
473 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
476 /* check attribute length */
478 attribute_length = suck_u4(cb);
480 if (attribute_length != 4) {
481 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
485 /* there can be no more than one EnclosingMethod attribute */
487 if (c->enclosingmethod != NULL) {
488 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
492 /* get class index */
494 class_index = suck_u2(cb);
495 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
497 /* get method index */
499 method_index = suck_u2(cb);
500 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
502 /* store info in classinfo */
504 c->enclosingclass.any = cr.any;
505 c->enclosingmethod = cn;
509 #endif /* defined(ENABLE_JAVASE) */
512 /* class_load_attributes *******************************************************
514 Read attributes from ClassFile.
517 u2 attribute_name_index;
519 u1 info[attribute_length];
522 InnerClasses_attribute {
523 u2 attribute_name_index;
527 *******************************************************************************/
529 bool class_load_attributes(classbuffer *cb)
532 uint16_t attributes_count;
533 uint16_t attribute_name_index;
535 innerclassinfo *info;
536 classref_or_classinfo inner;
537 classref_or_classinfo outer;
544 /* get attributes count */
546 if (!suck_check_classbuffer_size(cb, 2))
549 attributes_count = suck_u2(cb);
551 for (i = 0; i < attributes_count; i++) {
552 /* get attribute name */
554 if (!suck_check_classbuffer_size(cb, 2))
557 attribute_name_index = suck_u2(cb);
559 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
561 if (attribute_name == NULL)
564 if (attribute_name == utf_InnerClasses) {
567 if (c->innerclass != NULL) {
568 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
572 if (!suck_check_classbuffer_size(cb, 4 + 2))
575 /* skip attribute length */
578 /* number of records */
579 c->innerclasscount = suck_u2(cb);
581 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
584 /* allocate memory for innerclass structure */
585 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
587 for (j = 0; j < c->innerclasscount; j++) {
588 /* The innerclass structure contains a class with an encoded
589 name, its defining scope, its simple name and a bitmask of
592 info = c->innerclass + j;
594 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
595 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
596 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
599 /* If the current inner-class is the currently loaded
600 class check for some special flags. */
602 if (inner.ref->name == c->name) {
603 /* If an inner-class is not a member, its
604 outer-class is NULL. */
606 if (outer.ref != NULL) {
607 c->flags |= ACC_CLASS_MEMBER;
609 /* A member class doesn't have an
610 EnclosingMethod attribute, so set the
611 enclosing-class to be the same as the
614 c->declaringclass = outer;
615 c->enclosingclass = outer;
618 /* If an inner-class is anonymous, its name is
622 c->flags |= ACC_CLASS_ANONYMOUS;
625 info->inner_class = inner;
626 info->outer_class = outer;
631 else if (attribute_name == utf_SourceFile) {
634 if (!class_load_attribute_sourcefile(cb))
637 #if defined(ENABLE_JAVASE)
638 else if (attribute_name == utf_EnclosingMethod) {
639 /* EnclosingMethod */
641 if (!class_load_attribute_enclosingmethod(cb))
644 else if (attribute_name == utf_Signature) {
647 if (!loader_load_attribute_signature(cb, &(c->signature)))
652 #if defined(ENABLE_ANNOTATIONS)
653 /* XXX We can't do a release with that enabled */
655 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
656 /* RuntimeVisibleAnnotations */
657 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
660 /* XXX RuntimeInvisibleAnnotations should only be loaded
661 * (or returned to Java) if some commandline options says so.
662 * Currently there is no such option available in cacao,
663 * therefore I load them allways (for testing purpose).
664 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
665 * generated if you tell javac to do so. So in most cases
666 * there won't be any.
668 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
669 /* RuntimeInvisibleAnnotations */
670 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
676 /* unknown attribute */
678 if (!loader_skip_attribute_body(cb))
687 /* class_freepool **************************************************************
689 Frees all resources used by this classes Constant Pool.
691 *******************************************************************************/
693 static void class_freecpool(classinfo *c)
699 if (c->cptags && c->cpinfos) {
700 for (idx = 0; idx < c->cpcount; idx++) {
701 tag = c->cptags[idx];
702 info = c->cpinfos[idx];
706 case CONSTANT_Fieldref:
707 case CONSTANT_Methodref:
708 case CONSTANT_InterfaceMethodref:
709 FREE(info, constant_FMIref);
711 case CONSTANT_Integer:
712 FREE(info, constant_integer);
715 FREE(info, constant_float);
718 FREE(info, constant_long);
720 case CONSTANT_Double:
721 FREE(info, constant_double);
723 case CONSTANT_NameAndType:
724 FREE(info, constant_nameandtype);
732 MFREE(c->cptags, u1, c->cpcount);
735 MFREE(c->cpinfos, voidptr, c->cpcount);
739 /* class_getconstant ***********************************************************
741 Retrieves the value at position 'pos' of the constantpool of a
742 class. If the type of the value is other than 'ctype', an error is
745 *******************************************************************************/
747 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
749 /* check index and type of constantpool entry */
750 /* (pos == 0 is caught by type comparison) */
752 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
753 exceptions_throw_classformaterror(c, "Illegal constant pool index");
757 return c->cpinfos[pos];
761 /* innerclass_getconstant ******************************************************
763 Like class_getconstant, but if cptags is ZERO, null is returned.
765 *******************************************************************************/
767 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
769 /* invalid position in constantpool */
771 if (pos >= c->cpcount) {
772 exceptions_throw_classformaterror(c, "Illegal constant pool index");
776 /* constantpool entry of type 0 */
778 if (c->cptags[pos] == 0)
781 /* check type of constantpool entry */
783 if (c->cptags[pos] != ctype) {
784 exceptions_throw_classformaterror(c, "Illegal constant pool index");
788 return c->cpinfos[pos];
792 /* class_free ******************************************************************
794 Frees all resources used by the class.
796 *******************************************************************************/
798 void class_free(classinfo *c)
806 MFREE(c->interfaces, classinfo*, c->interfacescount);
809 for (i = 0; i < c->fieldscount; i++)
810 field_free(&(c->fields[i]));
811 #if defined(ENABLE_CACAO_GC)
812 MFREE(c->fields, fieldinfo, c->fieldscount);
817 for (i = 0; i < c->methodscount; i++)
818 method_free(&(c->methods[i]));
819 MFREE(c->methods, methodinfo, c->methodscount);
822 if ((v = c->vftbl) != NULL) {
824 mem_free(v->arraydesc,sizeof(arraydescriptor));
826 for (i = 0; i < v->interfacetablelength; i++) {
827 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
829 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
831 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
832 sizeof(methodptr*) * (v->interfacetablelength -
833 (v->interfacetablelength > 0));
834 v = (vftbl_t*) (((methodptr*) v) -
835 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
840 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
842 /* if (c->classvftbl)
843 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
849 /* get_array_class *************************************************************
851 Returns the array class with the given name for the given
852 classloader, or NULL if an exception occurred.
854 Note: This function does eager loading.
856 *******************************************************************************/
858 static classinfo *get_array_class(utf *name,classloader *initloader,
859 classloader *defloader,bool link)
863 /* lookup this class in the classcache */
864 c = classcache_lookup(initloader,name);
866 c = classcache_lookup_defined(defloader,name);
869 /* we have to create it */
870 c = class_create_classinfo(name);
871 c = load_newly_created_array(c,initloader);
877 assert(c->state & CLASS_LOADED);
878 assert(c->classloader == defloader);
880 if (link && !(c->state & CLASS_LINKED))
884 assert(!link || (c->state & CLASS_LINKED));
890 /* class_array_of **************************************************************
892 Returns an array class with the given component class. The array
893 class is dynamically created if neccessary.
895 *******************************************************************************/
897 classinfo *class_array_of(classinfo *component, bool link)
906 cl = component->classloader;
908 dumpsize = dump_size();
910 /* Assemble the array class name */
911 namelen = component->name->blength;
913 if (component->name->text[0] == '[') {
914 /* the component is itself an array */
915 namebuf = DMNEW(char, namelen + 1);
917 MCOPY(namebuf + 1, component->name->text, char, namelen);
921 /* the component is a non-array class */
922 namebuf = DMNEW(char, namelen + 3);
925 MCOPY(namebuf + 2, component->name->text, char, namelen);
926 namebuf[2 + namelen] = ';';
930 u = utf_new(namebuf, namelen);
932 c = get_array_class(u, cl, cl, link);
934 dump_release(dumpsize);
940 /* class_multiarray_of *********************************************************
942 Returns an array class with the given dimension and element class.
943 The array class is dynamically created if neccessary.
945 *******************************************************************************/
947 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
954 dumpsize = dump_size();
957 log_text("Invalid array dimension requested");
961 /* Assemble the array class name */
962 namelen = element->name->blength;
964 if (element->name->text[0] == '[') {
965 /* the element is itself an array */
966 namebuf = DMNEW(char, namelen + dim);
967 memcpy(namebuf + dim, element->name->text, namelen);
971 /* the element is a non-array class */
972 namebuf = DMNEW(char, namelen + 2 + dim);
974 memcpy(namebuf + dim + 1, element->name->text, namelen);
975 namelen += (2 + dim);
976 namebuf[namelen - 1] = ';';
978 memset(namebuf, '[', dim);
980 c = get_array_class(utf_new(namebuf, namelen),
981 element->classloader,
982 element->classloader,
985 dump_release(dumpsize);
991 /* class_lookup_classref *******************************************************
993 Looks up the constant_classref for a given classname in the classref
997 cls..............the class containing the reference
998 name.............the name of the class refered to
1001 a pointer to a constant_classref, or
1002 NULL if the reference was not found
1004 *******************************************************************************/
1006 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1008 constant_classref *ref;
1009 extra_classref *xref;
1014 assert(!cls->classrefcount || cls->classrefs);
1016 /* first search the main classref table */
1017 count = cls->classrefcount;
1018 ref = cls->classrefs;
1019 for (; count; --count, ++ref)
1020 if (ref->name == name)
1023 /* next try the list of extra classrefs */
1024 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1025 if (xref->classref.name == name)
1026 return &(xref->classref);
1034 /* class_get_classref **********************************************************
1036 Returns the constant_classref for a given classname.
1039 cls..............the class containing the reference
1040 name.............the name of the class refered to
1043 a pointer to a constant_classref (never NULL)
1046 The given name is not checked for validity!
1048 *******************************************************************************/
1050 constant_classref *class_get_classref(classinfo *cls, utf *name)
1052 constant_classref *ref;
1053 extra_classref *xref;
1058 ref = class_lookup_classref(cls,name);
1062 xref = NEW(extra_classref);
1063 CLASSREF_INIT(xref->classref,cls,name);
1065 xref->next = cls->extclassrefs;
1066 cls->extclassrefs = xref;
1068 return &(xref->classref);
1072 /* class_get_self_classref *****************************************************
1074 Returns the constant_classref to the class itself.
1077 cls..............the class containing the reference
1080 a pointer to a constant_classref (never NULL)
1082 *******************************************************************************/
1084 constant_classref *class_get_self_classref(classinfo *cls)
1086 /* XXX this should be done in a faster way. Maybe always make */
1087 /* the classref of index 0 a self reference. */
1088 return class_get_classref(cls,cls->name);
1091 /* class_get_classref_multiarray_of ********************************************
1093 Returns an array type reference with the given dimension and element class
1097 dim..............the requested dimension
1098 dim must be in [1;255]. This is NOT checked!
1099 ref..............the component class reference
1102 a pointer to the class reference for the array type
1105 The referer of `ref` is used as the referer for the new classref.
1107 *******************************************************************************/
1109 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1114 constant_classref *cr;
1117 assert(dim >= 1 && dim <= 255);
1119 dumpsize = dump_size();
1121 /* Assemble the array class name */
1122 namelen = ref->name->blength;
1124 if (ref->name->text[0] == '[') {
1125 /* the element is itself an array */
1126 namebuf = DMNEW(char, namelen + dim);
1127 memcpy(namebuf + dim, ref->name->text, namelen);
1131 /* the element is a non-array class */
1132 namebuf = DMNEW(char, namelen + 2 + dim);
1134 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1135 namelen += (2 + dim);
1136 namebuf[namelen - 1] = ';';
1138 memset(namebuf, '[', dim);
1140 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1142 dump_release(dumpsize);
1148 /* class_get_classref_component_of *********************************************
1150 Returns the component classref of a given array type reference
1153 ref..............the array type reference
1156 a reference to the component class, or
1157 NULL if `ref` is not an object array type reference
1160 The referer of `ref` is used as the referer for the new classref.
1162 *******************************************************************************/
1164 constant_classref *class_get_classref_component_of(constant_classref *ref)
1171 name = ref->name->text;
1175 namelen = ref->name->blength - 1;
1180 else if (*name != '[') {
1184 return class_get_classref(ref->referer, utf_new(name, namelen));
1188 /* class_findmethod ************************************************************
1190 Searches a 'classinfo' structure for a method having the given name
1191 and descriptor. If descriptor is NULL, it is ignored.
1193 *******************************************************************************/
1195 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1200 for (i = 0; i < c->methodscount; i++) {
1201 m = &(c->methods[i]);
1203 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1211 /* class_resolvemethod *********************************************************
1213 Searches a class and it's super classes for a method.
1215 Superinterfaces are *not* searched.
1217 *******************************************************************************/
1219 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1224 m = class_findmethod(c, name, desc);
1229 /* JVM Specification bug:
1231 It is important NOT to resolve special <init> and <clinit>
1232 methods to super classes or interfaces; yet, this is not
1233 explicited in the specification. Section 5.4.3.3 should be
1234 updated appropriately. */
1236 if (name == utf_init || name == utf_clinit)
1246 /* class_resolveinterfacemethod_intern *****************************************
1248 Internally used helper function. Do not use this directly.
1250 *******************************************************************************/
1252 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1253 utf *name, utf *desc)
1258 /* try to find the method in the class */
1260 m = class_findmethod(c, name, desc);
1265 /* no method found? try the superinterfaces */
1267 for (i = 0; i < c->interfacescount; i++) {
1268 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1275 /* no method found */
1281 /* class_resolveclassmethod ****************************************************
1283 Resolves a reference from REFERER to a method with NAME and DESC in
1286 If the method cannot be resolved the return value is NULL. If
1287 EXCEPT is true *exceptionptr is set, too.
1289 *******************************************************************************/
1291 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1292 classinfo *referer, bool throwexception)
1298 /* if (c->flags & ACC_INTERFACE) { */
1299 /* if (throwexception) */
1300 /* *exceptionptr = */
1301 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1305 /* try class c and its superclasses */
1309 m = class_resolvemethod(cls, name, desc);
1314 /* try the superinterfaces */
1316 for (i = 0; i < c->interfacescount; i++) {
1317 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1325 exceptions_throw_nosuchmethoderror(c, name, desc);
1330 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1332 exceptions_throw_abstractmethoderror();
1337 /* XXX check access rights */
1343 /* class_resolveinterfacemethod ************************************************
1345 Resolves a reference from REFERER to a method with NAME and DESC in
1348 If the method cannot be resolved the return value is NULL. If
1349 EXCEPT is true *exceptionptr is set, too.
1351 *******************************************************************************/
1353 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1354 classinfo *referer, bool throwexception)
1358 if (!(c->flags & ACC_INTERFACE)) {
1360 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1365 mi = class_resolveinterfacemethod_intern(c, name, desc);
1370 /* try class java.lang.Object */
1372 mi = class_findmethod(class_java_lang_Object, name, desc);
1378 exceptions_throw_nosuchmethoderror(c, name, desc);
1384 /* class_findfield *************************************************************
1386 Searches for field with specified name and type in a classinfo
1387 structure. If no such field is found NULL is returned.
1389 *******************************************************************************/
1391 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1395 for (i = 0; i < c->fieldscount; i++)
1396 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1397 return &(c->fields[i]);
1400 return class_findfield(c->super.cls, name, desc);
1406 /* class_findfield_approx ******************************************************
1408 Searches in 'classinfo'-structure for a field with the specified
1411 *******************************************************************************/
1413 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1417 /* get field index */
1419 i = class_findfield_index_by_name(c, name);
1421 /* field was not found, return */
1426 /* return field address */
1428 return &(c->fields[i]);
1432 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1436 for (i = 0; i < c->fieldscount; i++) {
1437 /* compare field names */
1439 if ((c->fields[i].name == name))
1443 /* field was not found, raise exception */
1445 exceptions_throw_nosuchfielderror(c, name);
1451 /****************** Function: class_resolvefield_int ***************************
1453 This is an internally used helper function. Do not use this directly.
1455 Tries to resolve a field having the given name and type.
1456 If the field cannot be resolved, NULL is returned.
1458 *******************************************************************************/
1460 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1465 /* search for field in class c */
1467 for (i = 0; i < c->fieldscount; i++) {
1468 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1469 return &(c->fields[i]);
1473 /* try superinterfaces recursively */
1475 for (i = 0; i < c->interfacescount; i++) {
1476 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1481 /* try superclass */
1484 return class_resolvefield_int(c->super.cls, name, desc);
1492 /********************* Function: class_resolvefield ***************************
1494 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1496 If the field cannot be resolved the return value is NULL. If EXCEPT is
1497 true *exceptionptr is set, too.
1499 *******************************************************************************/
1501 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1502 classinfo *referer, bool throwexception)
1506 fi = class_resolvefield_int(c, name, desc);
1510 exceptions_throw_nosuchfielderror(c, name);
1515 /* XXX check access rights */
1521 /* class_resolve_superclass ****************************************************
1523 Resolves the super class reference of the given class if necessary.
1525 *******************************************************************************/
1527 static classinfo *class_resolve_superclass(classinfo *c)
1531 if (c->super.any == NULL)
1534 /* Check if the super class is a reference. */
1536 if (IS_CLASSREF(c->super)) {
1537 /* XXX I'm very sure this is not correct. */
1538 super = resolve_classref_or_classinfo_eager(c->super, true);
1539 /* super = resolve_classref_or_classinfo_eager(c->super, false); */
1544 /* Store the resolved super class in the class structure. */
1546 c->super.cls = super;
1549 return c->super.cls;
1553 /* class_issubclass ************************************************************
1555 Checks if sub is a descendant of super.
1557 *******************************************************************************/
1559 bool class_issubclass(classinfo *sub, classinfo *super)
1568 /* sub = class_resolve_superclass(sub); */
1569 if (sub->super.any == NULL)
1572 assert(IS_CLASSREF(sub->super) == 0);
1574 sub = sub->super.cls;
1579 /* class_isanysubclass *********************************************************
1581 Checks a subclass relation between two classes. Implemented
1582 interfaces are interpreted as super classes.
1584 Return value: 1 ... sub is subclass of super
1587 *******************************************************************************/
1589 bool class_isanysubclass(classinfo *sub, classinfo *super)
1594 /* This is the trivial case. */
1599 /* Primitive classes are only subclasses of themselves. */
1601 if (class_is_primitive(sub) || class_is_primitive(super))
1604 /* Check for interfaces. */
1606 if (super->flags & ACC_INTERFACE) {
1607 result = (sub->vftbl->interfacetablelength > super->index) &&
1608 (sub->vftbl->interfacetable[-super->index] != NULL);
1611 /* java.lang.Object is the only super class of any
1614 if (sub->flags & ACC_INTERFACE)
1615 return (super == class_java_lang_Object);
1617 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1619 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1620 result = diffval <= (uint32_t) super->vftbl->diffval;
1622 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1629 /* class_is_primitive **********************************************************
1631 Checks if the given class is a primitive class.
1633 *******************************************************************************/
1635 bool class_is_primitive(classinfo *c)
1637 if (c->flags & ACC_CLASS_PRIMITIVE)
1644 /* class_is_anonymousclass *****************************************************
1646 Checks if the given class is an anonymous class.
1648 *******************************************************************************/
1650 bool class_is_anonymousclass(classinfo *c)
1652 if (c->flags & ACC_CLASS_ANONYMOUS)
1659 /* class_is_array **************************************************************
1661 Checks if the given class is an array class.
1663 *******************************************************************************/
1665 bool class_is_array(classinfo *c)
1667 if (!(c->state & CLASS_LINKED))
1671 return (c->vftbl->arraydesc != NULL);
1675 /* class_is_interface **********************************************************
1677 Checks if the given class is an interface.
1679 *******************************************************************************/
1681 bool class_is_interface(classinfo *c)
1683 if (c->flags & ACC_INTERFACE)
1690 /* class_is_localclass *********************************************************
1692 Checks if the given class is a local class.
1694 *******************************************************************************/
1696 bool class_is_localclass(classinfo *c)
1698 if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1705 /* class_is_memberclass ********************************************************
1707 Checks if the given class is a member class.
1709 *******************************************************************************/
1711 bool class_is_memberclass(classinfo *c)
1713 if (c->flags & ACC_CLASS_MEMBER)
1720 /* class_get_superclass ********************************************************
1722 Return the super class of the given class. If the super-field is a
1723 class-reference, resolve it and store it in the classinfo.
1725 *******************************************************************************/
1727 classinfo *class_get_superclass(classinfo *c)
1731 /* For java.lang.Object, primitive and Void classes we return
1734 if (c->super.any == NULL)
1737 /* For interfaces we also return NULL. */
1739 if (c->flags & ACC_INTERFACE)
1742 /* We may have to resolve the super class reference. */
1744 super = class_resolve_superclass(c);
1750 /* class_get_componenttype *****************************************************
1752 Return the component class of the given class. If the given class
1753 is not an array, return NULL.
1755 *******************************************************************************/
1757 classinfo *class_get_componenttype(classinfo *c)
1759 classinfo *component;
1760 arraydescriptor *ad;
1762 /* XXX maybe we could find a way to do this without linking. */
1763 /* This way should be safe and easy, however. */
1765 if (!(c->state & CLASS_LINKED))
1769 ad = c->vftbl->arraydesc;
1774 if (ad->arraytype == ARRAYTYPE_OBJECT)
1775 component = ad->componentvftbl->class;
1777 component = primitive_class_get_by_type(ad->arraytype);
1783 /* class_get_declaredclasses ***************************************************
1785 Return an array of declared classes of the given class.
1787 *******************************************************************************/
1789 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1791 classref_or_classinfo inner;
1792 classref_or_classinfo outer;
1794 int declaredclasscount; /* number of declared classes */
1795 int pos; /* current declared class */
1796 java_handle_objectarray_t *oa; /* array of declared classes */
1800 declaredclasscount = 0;
1802 if (!class_is_primitive(c) && !class_is_array(c)) {
1803 /* Determine number of declared classes. */
1805 for (i = 0; i < c->innerclasscount; i++) {
1806 /* Get outer-class. If the inner-class is not a member
1807 class, the outer-class is NULL. */
1809 outer = c->innerclass[i].outer_class;
1811 if (outer.any == NULL)
1814 /* Check if outer-class is a classref or a real class and
1815 get the class name from the structure. */
1817 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1819 /* Outer class is this class. */
1821 if ((outername == c->name) &&
1822 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1823 declaredclasscount++;
1827 /* Allocate Class[] and check for OOM. */
1829 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1834 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1835 inner = c->innerclass[i].inner_class;
1836 outer = c->innerclass[i].outer_class;
1838 /* Get outer-class. If the inner-class is not a member class,
1839 the outer-class is NULL. */
1841 if (outer.any == NULL)
1844 /* Check if outer_class is a classref or a real class and get
1845 the class name from the structure. */
1847 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1849 /* Outer class is this class. */
1851 if ((outername == c->name) &&
1852 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1854 ic = resolve_classref_or_classinfo_eager(inner, false);
1859 if (!(ic->state & CLASS_LINKED))
1860 if (!link_class(ic))
1863 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1871 /* class_get_declaringclass ****************************************************
1873 If the class or interface given is a member of another class,
1874 return the declaring class. For array and primitive classes return
1877 *******************************************************************************/
1879 classinfo *class_get_declaringclass(classinfo *c)
1881 classref_or_classinfo cr;
1884 /* Get declaring class. */
1886 cr = c->declaringclass;
1891 /* Resolve the class if necessary. */
1893 if (IS_CLASSREF(cr)) {
1894 /* dc = resolve_classref_eager(cr.ref); */
1895 dc = resolve_classref_or_classinfo_eager(cr, true);
1900 /* Store the resolved class in the class structure. */
1911 /* class_get_enclosingclass ****************************************************
1913 Return the enclosing class for the given class.
1915 *******************************************************************************/
1917 classinfo *class_get_enclosingclass(classinfo *c)
1919 classref_or_classinfo cr;
1922 /* Get enclosing class. */
1924 cr = c->enclosingclass;
1929 /* Resolve the class if necessary. */
1931 if (IS_CLASSREF(cr)) {
1932 /* ec = resolve_classref_eager(cr.ref); */
1933 ec = resolve_classref_or_classinfo_eager(cr, true);
1938 /* Store the resolved class in the class structure. */
1949 /* class_get_interfaces ********************************************************
1951 Return an array of interfaces of the given class.
1953 *******************************************************************************/
1955 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1958 java_handle_objectarray_t *oa;
1961 if (!(c->state & CLASS_LINKED))
1965 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1970 for (i = 0; i < c->interfacescount; i++) {
1971 ic = c->interfaces[i].cls;
1973 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1980 /* class_get_annotations *******************************************************
1982 Return the unparsed declared annotations in an byte array
1983 of the given class (or NULL if there aren't any).
1985 *******************************************************************************/
1987 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1989 #if defined(ENABLE_ANNOTATIONS)
1990 return c->annotations;
1997 /* class_get_signature *********************************************************
1999 Return the signature of the given class. For array and primitive
2000 classes return NULL.
2002 *******************************************************************************/
2004 #if defined(ENABLE_JAVASE)
2005 utf *class_get_signature(classinfo *c)
2007 /* For array and primitive classes return NULL. */
2009 if (class_is_array(c) || class_is_primitive(c))
2012 return c->signature;
2017 /* class_printflags ************************************************************
2019 Prints flags of a class.
2021 *******************************************************************************/
2023 #if !defined(NDEBUG)
2024 void class_printflags(classinfo *c)
2031 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
2032 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
2033 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
2034 if (c->flags & ACC_STATIC) printf(" STATIC");
2035 if (c->flags & ACC_FINAL) printf(" FINAL");
2036 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2037 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
2038 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
2039 if (c->flags & ACC_NATIVE) printf(" NATIVE");
2040 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
2041 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
2046 /* class_print *****************************************************************
2048 Prints classname plus flags.
2050 *******************************************************************************/
2052 #if !defined(NDEBUG)
2053 void class_print(classinfo *c)
2060 utf_display_printable_ascii(c->name);
2061 class_printflags(c);
2066 /* class_classref_print ********************************************************
2068 Prints classname plus referer class.
2070 *******************************************************************************/
2072 #if !defined(NDEBUG)
2073 void class_classref_print(constant_classref *cr)
2080 utf_display_printable_ascii(cr->name);
2083 class_print(cr->referer);
2091 /* class_println ***************************************************************
2093 Prints classname plus flags and new line.
2095 *******************************************************************************/
2097 #if !defined(NDEBUG)
2098 void class_println(classinfo *c)
2106 /* class_classref_println ******************************************************
2108 Prints classname plus referer class and new line.
2110 *******************************************************************************/
2112 #if !defined(NDEBUG)
2113 void class_classref_println(constant_classref *cr)
2115 class_classref_print(cr);
2121 /* class_classref_or_classinfo_print *******************************************
2123 Prints classname plus referer class.
2125 *******************************************************************************/
2127 #if !defined(NDEBUG)
2128 void class_classref_or_classinfo_print(classref_or_classinfo c)
2130 if (c.any == NULL) {
2131 printf("(classref_or_classinfo) NULL");
2135 class_classref_print(c.ref);
2142 /* class_classref_or_classinfo_println *****************************************
2144 Prints classname plus referer class and a newline.
2146 *******************************************************************************/
2148 void class_classref_or_classinfo_println(classref_or_classinfo c)
2150 class_classref_or_classinfo_println(c);
2155 /* class_showconstantpool ******************************************************
2157 Dump the constant pool of the given class to stdout.
2159 *******************************************************************************/
2161 #if !defined(NDEBUG)
2162 void class_showconstantpool (classinfo *c)
2167 printf ("---- dump of constant pool ----\n");
2169 for (i=0; i<c->cpcount; i++) {
2170 printf ("#%d: ", (int) i);
2172 e = c -> cpinfos [i];
2175 switch (c -> cptags [i]) {
2176 case CONSTANT_Class:
2177 printf ("Classreference -> ");
2178 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2180 case CONSTANT_Fieldref:
2181 printf ("Fieldref -> ");
2182 field_fieldref_print((constant_FMIref *) e);
2184 case CONSTANT_Methodref:
2185 printf ("Methodref -> ");
2186 method_methodref_print((constant_FMIref *) e);
2188 case CONSTANT_InterfaceMethodref:
2189 printf ("InterfaceMethod -> ");
2190 method_methodref_print((constant_FMIref *) e);
2192 case CONSTANT_String:
2193 printf ("String -> ");
2194 utf_display_printable_ascii (e);
2196 case CONSTANT_Integer:
2197 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2199 case CONSTANT_Float:
2200 printf ("Float -> %f", ((constant_float*)e) -> value);
2202 case CONSTANT_Double:
2203 printf ("Double -> %f", ((constant_double*)e) -> value);
2207 u8 v = ((constant_long*)e) -> value;
2209 printf ("Long -> %ld", (long int) v);
2211 printf ("Long -> HI: %ld, LO: %ld\n",
2212 (long int) v.high, (long int) v.low);
2216 case CONSTANT_NameAndType:
2218 constant_nameandtype *cnt = e;
2219 printf ("NameAndType: ");
2220 utf_display_printable_ascii (cnt->name);
2222 utf_display_printable_ascii (cnt->descriptor);
2226 printf ("Utf8 -> ");
2227 utf_display_printable_ascii (e);
2230 log_text("Invalid type of ConstantPool-Entry");
2238 #endif /* !defined(NDEBUG) */
2241 /* class_showmethods ***********************************************************
2243 Dump info about the fields and methods of the given class to stdout.
2245 *******************************************************************************/
2247 #if !defined(NDEBUG)
2248 void class_showmethods (classinfo *c)
2252 printf("--------- Fields and Methods ----------------\n");
2254 class_printflags(c);
2258 utf_display_printable_ascii(c->name);
2263 utf_display_printable_ascii(c->super.cls->name);
2267 printf("Index: %d\n", c->index);
2269 printf("Interfaces:\n");
2270 for (i = 0; i < c->interfacescount; i++) {
2272 utf_display_printable_ascii(c->interfaces[i].cls->name);
2273 printf (" (%d)\n", c->interfaces[i].cls->index);
2276 printf("Fields:\n");
2277 for (i = 0; i < c->fieldscount; i++)
2278 field_println(&(c->fields[i]));
2280 printf("Methods:\n");
2281 for (i = 0; i < c->methodscount; i++) {
2282 methodinfo *m = &(c->methods[i]);
2284 if (!(m->flags & ACC_STATIC))
2285 printf("vftblindex: %d ", m->vftblindex);
2290 printf ("Virtual function table:\n");
2291 for (i = 0; i < c->vftbl->vftbllength; i++)
2292 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2294 #endif /* !defined(NDEBUG) */
2298 * These are local overrides for various environment variables in Emacs.
2299 * Please do not remove this and leave it at the end of the file, where
2300 * Emacs will automagically detect them.
2301 * ---------------------------------------------------------------------
2304 * indent-tabs-mode: t
2308 * vim:noexpandtab:sw=4:ts=4: