1 /* src/vmcore/class.c - class related functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 #include "mm/memory.h"
39 #include "native/llni.h"
41 #include "threads/lock-common.h"
43 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/resolve.h"
51 #include "vm/jit/asmpart.h"
53 #include "vmcore/class.h"
54 #include "vmcore/classcache.h"
55 #include "vmcore/linker.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/statistics.h"
63 #include "vmcore/suck.h"
64 #include "vmcore/utf8.h"
67 /* global variables ***********************************************************/
69 /* frequently used classes ****************************************************/
71 /* important system classes */
73 classinfo *class_java_lang_Object;
74 classinfo *class_java_lang_Class;
75 classinfo *class_java_lang_ClassLoader;
76 classinfo *class_java_lang_Cloneable;
77 classinfo *class_java_lang_SecurityManager;
78 classinfo *class_java_lang_String;
79 classinfo *class_java_lang_System;
80 classinfo *class_java_lang_Thread;
81 classinfo *class_java_lang_ThreadGroup;
82 classinfo *class_java_lang_Throwable;
83 classinfo *class_java_io_Serializable;
85 #if defined(WITH_CLASSPATH_GNU)
86 classinfo *class_java_lang_VMSystem;
87 classinfo *class_java_lang_VMThread;
88 classinfo *class_java_lang_VMThrowable;
91 #if defined(WITH_CLASSPATH_SUN)
92 classinfo *class_sun_reflect_MagicAccessorImpl;
95 #if defined(ENABLE_JAVASE)
96 classinfo *class_java_lang_Void;
98 classinfo *class_java_lang_Boolean;
99 classinfo *class_java_lang_Byte;
100 classinfo *class_java_lang_Character;
101 classinfo *class_java_lang_Short;
102 classinfo *class_java_lang_Integer;
103 classinfo *class_java_lang_Long;
104 classinfo *class_java_lang_Float;
105 classinfo *class_java_lang_Double;
107 /* some classes which may be used more often */
109 #if defined(ENABLE_JAVASE)
110 classinfo *class_java_lang_StackTraceElement;
111 classinfo *class_java_lang_reflect_Constructor;
112 classinfo *class_java_lang_reflect_Field;
113 classinfo *class_java_lang_reflect_Method;
114 classinfo *class_java_security_PrivilegedAction;
115 classinfo *class_java_util_Vector;
116 classinfo *class_java_util_HashMap;
118 classinfo *arrayclass_java_lang_Object;
120 # if defined(ENABLE_ANNOTATIONS)
121 classinfo *class_sun_reflect_ConstantPool;
122 # if defined(WITH_CLASSPATH_GNU)
123 classinfo *class_sun_reflect_annotation_AnnotationParser;
128 /* pseudo classes for the typechecker */
130 classinfo *pseudo_class_Arraystub;
131 classinfo *pseudo_class_Null;
132 classinfo *pseudo_class_New;
135 /* class_set_packagename *******************************************************
137 Derive the package name from the class name and store it in the
140 An internal package name consists of the package name plus the
141 trailing '/', e.g. "java/lang/".
143 For classes in the unnamed package, the package name is set to
146 *******************************************************************************/
148 void class_set_packagename(classinfo *c)
153 p = UTF_END(c->name) - 1;
154 start = c->name->text;
156 if (c->name->text[0] == '[') {
157 /* Set packagename of arrays to the element's package. */
159 for (; *start == '['; start++);
161 /* Skip the 'L' in arrays of references. */
167 /* Search for last '/'. */
169 for (; (p > start) && (*p != '/'); --p);
171 /* If we found a '/' we set the package name plus the trailing
172 '/'. Otherwise we set the packagename to NULL. */
175 c->packagename = utf_new(start, p - start + 1);
177 c->packagename = NULL;
181 /* class_create_classinfo ******************************************************
183 Create a new classinfo struct. The class name is set to the given utf *,
184 most other fields are initialized to zero.
186 Note: classname may be NULL. In this case a not-yet-named classinfo is
187 created. The name must be filled in later and class_set_packagename
188 must be called after that.
190 *******************************************************************************/
192 classinfo *class_create_classinfo(utf *classname)
196 #if defined(ENABLE_STATISTICS)
198 size_classinfo += sizeof(classinfo);
201 /* we use a safe name for temporarily unnamed classes */
203 if (classname == NULL)
204 classname = utf_not_named_yet;
208 log_message_utf("Creating class: ", classname);
211 #if !defined(ENABLE_GC_BOEHM)
212 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
213 /*c = NEW(classinfo);
214 MZERO(c, classinfo, 1);*/
216 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
217 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
222 /* Set the header.vftbl of all loaded classes to the one of
223 java.lang.Class, so Java code can use a class as object. */
225 if (class_java_lang_Class != NULL)
226 if (class_java_lang_Class->vftbl != NULL)
227 c->object.header.vftbl = class_java_lang_Class->vftbl;
229 #if defined(ENABLE_JAVASE)
230 /* check if the class is a reference class and flag it */
232 if (classname == utf_java_lang_ref_SoftReference) {
233 c->flags |= ACC_CLASS_REFERENCE_SOFT;
235 else if (classname == utf_java_lang_ref_WeakReference) {
236 c->flags |= ACC_CLASS_REFERENCE_WEAK;
238 else if (classname == utf_java_lang_ref_PhantomReference) {
239 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
243 if (classname != utf_not_named_yet)
244 class_set_packagename(c);
246 LOCK_INIT_OBJECT_LOCK(&c->object.header);
252 /* class_postset_header_vftbl **************************************************
254 Set the header.vftbl of all classes created before java.lang.Class
255 was linked. This is necessary that Java code can use a class as
258 *******************************************************************************/
260 void class_postset_header_vftbl(void)
264 classcache_name_entry *nmen;
265 classcache_class_entry *clsen;
267 assert(class_java_lang_Class);
269 for (slot = 0; slot < hashtable_classcache.size; slot++) {
270 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
272 for (; nmen; nmen = nmen->hashlink) {
273 /* iterate over all class entries */
275 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
278 /* now set the the vftbl */
280 if (c->object.header.vftbl == NULL)
281 c->object.header.vftbl = class_java_lang_Class->vftbl;
287 /* class_define ****************************************************************
289 Calls the loader and defines a class in the VM.
291 *******************************************************************************/
293 classinfo *class_define(utf *name, classloader *cl, int32_t length, uint8_t *data, java_handle_t *pd)
300 /* check if this class has already been defined */
302 c = classcache_lookup_defined_or_initiated(cl, name);
305 exceptions_throw_linkageerror("duplicate class definition: ", c);
310 /* create a new classinfo struct */
312 c = class_create_classinfo(name);
314 #if defined(ENABLE_STATISTICS)
317 if (opt_getloadingtime)
321 /* build a classbuffer with the given data */
323 cb = NEW(classbuffer);
330 /* preset the defining classloader */
334 /* load the class from this buffer */
336 r = load_class_from_classbuffer(cb);
340 FREE(cb, classbuffer);
342 #if defined(ENABLE_STATISTICS)
345 if (opt_getloadingtime)
350 /* If return value is NULL, we had a problem and the class is
351 not loaded. Now free the allocated memory, otherwise we
352 could run into a DOS. */
359 #if defined(ENABLE_JAVASE)
360 # if defined(WITH_CLASSPATH_SUN)
361 /* Store the protection domain. */
363 c->protectiondomain = pd;
367 /* Store the newly defined class in the class cache. This call
368 also checks whether a class of the same name has already been
369 defined by the same defining loader, and if so, replaces the
370 newly created class by the one defined earlier. */
372 /* Important: The classinfo given to classcache_store must be
373 fully prepared because another thread may return
374 this pointer after the lookup at to top of this
375 function directly after the class cache lock has
378 c = classcache_store(cl, c, true);
384 /* class_load_attribute_sourcefile *********************************************
386 SourceFile_attribute {
387 u2 attribute_name_index;
392 *******************************************************************************/
394 static bool class_load_attribute_sourcefile(classbuffer *cb)
405 /* check buffer size */
407 if (!suck_check_classbuffer_size(cb, 4 + 2))
410 /* check attribute length */
412 attribute_length = suck_u4(cb);
414 if (attribute_length != 2) {
415 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
419 /* there can be no more than one SourceFile attribute */
421 if (c->sourcefile != NULL) {
422 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
428 sourcefile_index = suck_u2(cb);
429 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
431 if (sourcefile == NULL)
434 /* store sourcefile */
436 c->sourcefile = sourcefile;
442 /* class_load_attribute_enclosingmethod ****************************************
444 EnclosingMethod_attribute {
445 u2 attribute_name_index;
451 *******************************************************************************/
453 #if defined(ENABLE_JAVASE)
454 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
460 classref_or_classinfo cr;
461 constant_nameandtype *cn;
467 /* check buffer size */
469 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
472 /* check attribute length */
474 attribute_length = suck_u4(cb);
476 if (attribute_length != 4) {
477 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
481 /* there can be no more than one EnclosingMethod attribute */
483 if (c->enclosingmethod != NULL) {
484 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
488 /* get class index */
490 class_index = suck_u2(cb);
491 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
493 /* get method index */
495 method_index = suck_u2(cb);
496 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
498 /* store info in classinfo */
500 c->enclosingclass.any = cr.any;
501 c->enclosingmethod = cn;
505 #endif /* defined(ENABLE_JAVASE) */
508 /* class_load_attributes *******************************************************
510 Read attributes from ClassFile.
513 u2 attribute_name_index;
515 u1 info[attribute_length];
518 InnerClasses_attribute {
519 u2 attribute_name_index;
523 *******************************************************************************/
525 bool class_load_attributes(classbuffer *cb)
528 uint16_t attributes_count;
529 uint16_t attribute_name_index;
531 innerclassinfo *info;
532 classref_or_classinfo inner;
533 classref_or_classinfo outer;
540 /* get attributes count */
542 if (!suck_check_classbuffer_size(cb, 2))
545 attributes_count = suck_u2(cb);
547 for (i = 0; i < attributes_count; i++) {
548 /* get attribute name */
550 if (!suck_check_classbuffer_size(cb, 2))
553 attribute_name_index = suck_u2(cb);
555 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
557 if (attribute_name == NULL)
560 if (attribute_name == utf_InnerClasses) {
563 if (c->innerclass != NULL) {
564 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
568 if (!suck_check_classbuffer_size(cb, 4 + 2))
571 /* skip attribute length */
574 /* number of records */
575 c->innerclasscount = suck_u2(cb);
577 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
580 /* allocate memory for innerclass structure */
581 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
583 for (j = 0; j < c->innerclasscount; j++) {
584 /* The innerclass structure contains a class with an encoded
585 name, its defining scope, its simple name and a bitmask of
588 info = c->innerclass + j;
590 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
591 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
592 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
595 /* If the current inner-class is the currently loaded
596 class check for some special flags. */
598 if (inner.ref->name == c->name) {
599 /* If an inner-class is not a member, its
600 outer-class is NULL. */
602 if (outer.ref != NULL) {
603 c->flags |= ACC_CLASS_MEMBER;
605 /* A member class doesn't have an
606 EnclosingMethod attribute, so set the
607 enclosing-class to be the same as the
610 c->declaringclass = outer;
611 c->enclosingclass = outer;
614 /* If an inner-class is anonymous, its name is
618 c->flags |= ACC_CLASS_ANONYMOUS;
621 info->inner_class = inner;
622 info->outer_class = outer;
627 else if (attribute_name == utf_SourceFile) {
630 if (!class_load_attribute_sourcefile(cb))
633 #if defined(ENABLE_JAVASE)
634 else if (attribute_name == utf_EnclosingMethod) {
635 /* EnclosingMethod */
637 if (!class_load_attribute_enclosingmethod(cb))
640 else if (attribute_name == utf_Signature) {
643 if (!loader_load_attribute_signature(cb, &(c->signature)))
648 #if defined(ENABLE_ANNOTATIONS)
649 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
650 /* RuntimeVisibleAnnotations */
651 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
654 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
655 /* RuntimeInvisibleAnnotations */
656 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
662 /* unknown attribute */
664 if (!loader_skip_attribute_body(cb))
673 /* class_freepool **************************************************************
675 Frees all resources used by this classes Constant Pool.
677 *******************************************************************************/
679 static void class_freecpool(classinfo *c)
685 if (c->cptags && c->cpinfos) {
686 for (idx = 0; idx < c->cpcount; idx++) {
687 tag = c->cptags[idx];
688 info = c->cpinfos[idx];
692 case CONSTANT_Fieldref:
693 case CONSTANT_Methodref:
694 case CONSTANT_InterfaceMethodref:
695 FREE(info, constant_FMIref);
697 case CONSTANT_Integer:
698 FREE(info, constant_integer);
701 FREE(info, constant_float);
704 FREE(info, constant_long);
706 case CONSTANT_Double:
707 FREE(info, constant_double);
709 case CONSTANT_NameAndType:
710 FREE(info, constant_nameandtype);
718 MFREE(c->cptags, u1, c->cpcount);
721 MFREE(c->cpinfos, voidptr, c->cpcount);
725 /* class_getconstant ***********************************************************
727 Retrieves the value at position 'pos' of the constantpool of a
728 class. If the type of the value is other than 'ctype', an error is
731 *******************************************************************************/
733 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
735 /* check index and type of constantpool entry */
736 /* (pos == 0 is caught by type comparison) */
738 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
739 exceptions_throw_classformaterror(c, "Illegal constant pool index");
743 return c->cpinfos[pos];
747 /* innerclass_getconstant ******************************************************
749 Like class_getconstant, but if cptags is ZERO, null is returned.
751 *******************************************************************************/
753 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
755 /* invalid position in constantpool */
757 if (pos >= c->cpcount) {
758 exceptions_throw_classformaterror(c, "Illegal constant pool index");
762 /* constantpool entry of type 0 */
764 if (c->cptags[pos] == 0)
767 /* check type of constantpool entry */
769 if (c->cptags[pos] != ctype) {
770 exceptions_throw_classformaterror(c, "Illegal constant pool index");
774 return c->cpinfos[pos];
778 /* class_free ******************************************************************
780 Frees all resources used by the class.
782 *******************************************************************************/
784 void class_free(classinfo *c)
791 if (c->interfaces != NULL)
792 MFREE(c->interfaces, classinfo*, c->interfacescount);
795 for (i = 0; i < c->fieldscount; i++)
796 field_free(&(c->fields[i]));
797 MFREE(c->fields, fieldinfo, c->fieldscount);
801 for (i = 0; i < c->methodscount; i++)
802 method_free(&(c->methods[i]));
803 MFREE(c->methods, methodinfo, c->methodscount);
806 if ((v = c->vftbl) != NULL) {
808 mem_free(v->arraydesc,sizeof(arraydescriptor));
810 for (i = 0; i < v->interfacetablelength; i++) {
811 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
813 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
815 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
816 sizeof(methodptr*) * (v->interfacetablelength -
817 (v->interfacetablelength > 0));
818 v = (vftbl_t*) (((methodptr*) v) -
819 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
824 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
826 /* if (c->classvftbl)
827 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
833 /* get_array_class *************************************************************
835 Returns the array class with the given name for the given
836 classloader, or NULL if an exception occurred.
838 Note: This function does eager loading.
840 *******************************************************************************/
842 static classinfo *get_array_class(utf *name,classloader *initloader,
843 classloader *defloader,bool link)
847 /* lookup this class in the classcache */
848 c = classcache_lookup(initloader,name);
850 c = classcache_lookup_defined(defloader,name);
853 /* we have to create it */
854 c = class_create_classinfo(name);
855 c = load_newly_created_array(c,initloader);
861 assert(c->state & CLASS_LOADED);
862 assert(c->classloader == defloader);
864 if (link && !(c->state & CLASS_LINKED))
868 assert(!link || (c->state & CLASS_LINKED));
874 /* class_array_of **************************************************************
876 Returns an array class with the given component class. The array
877 class is dynamically created if neccessary.
879 *******************************************************************************/
881 classinfo *class_array_of(classinfo *component, bool link)
890 cl = component->classloader;
894 /* Assemble the array class name */
895 namelen = component->name->blength;
897 if (component->name->text[0] == '[') {
898 /* the component is itself an array */
899 namebuf = DMNEW(char, namelen + 1);
901 MCOPY(namebuf + 1, component->name->text, char, namelen);
905 /* the component is a non-array class */
906 namebuf = DMNEW(char, namelen + 3);
909 MCOPY(namebuf + 2, component->name->text, char, namelen);
910 namebuf[2 + namelen] = ';';
914 u = utf_new(namebuf, namelen);
916 c = get_array_class(u, cl, cl, link);
924 /* class_multiarray_of *********************************************************
926 Returns an array class with the given dimension and element class.
927 The array class is dynamically created if neccessary.
929 *******************************************************************************/
931 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
941 log_text("Invalid array dimension requested");
945 /* Assemble the array class name */
946 namelen = element->name->blength;
948 if (element->name->text[0] == '[') {
949 /* the element is itself an array */
950 namebuf = DMNEW(char, namelen + dim);
951 memcpy(namebuf + dim, element->name->text, namelen);
955 /* the element is a non-array class */
956 namebuf = DMNEW(char, namelen + 2 + dim);
958 memcpy(namebuf + dim + 1, element->name->text, namelen);
959 namelen += (2 + dim);
960 namebuf[namelen - 1] = ';';
962 memset(namebuf, '[', dim);
964 c = get_array_class(utf_new(namebuf, namelen),
965 element->classloader,
966 element->classloader,
975 /* class_lookup_classref *******************************************************
977 Looks up the constant_classref for a given classname in the classref
981 cls..............the class containing the reference
982 name.............the name of the class refered to
985 a pointer to a constant_classref, or
986 NULL if the reference was not found
988 *******************************************************************************/
990 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
992 constant_classref *ref;
993 extra_classref *xref;
998 assert(!cls->classrefcount || cls->classrefs);
1000 /* first search the main classref table */
1001 count = cls->classrefcount;
1002 ref = cls->classrefs;
1003 for (; count; --count, ++ref)
1004 if (ref->name == name)
1007 /* next try the list of extra classrefs */
1008 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1009 if (xref->classref.name == name)
1010 return &(xref->classref);
1018 /* class_get_classref **********************************************************
1020 Returns the constant_classref for a given classname.
1023 cls..............the class containing the reference
1024 name.............the name of the class refered to
1027 a pointer to a constant_classref (never NULL)
1030 The given name is not checked for validity!
1032 *******************************************************************************/
1034 constant_classref *class_get_classref(classinfo *cls, utf *name)
1036 constant_classref *ref;
1037 extra_classref *xref;
1042 ref = class_lookup_classref(cls,name);
1046 xref = NEW(extra_classref);
1047 CLASSREF_INIT(xref->classref,cls,name);
1049 xref->next = cls->extclassrefs;
1050 cls->extclassrefs = xref;
1052 return &(xref->classref);
1056 /* class_get_self_classref *****************************************************
1058 Returns the constant_classref to the class itself.
1061 cls..............the class containing the reference
1064 a pointer to a constant_classref (never NULL)
1066 *******************************************************************************/
1068 constant_classref *class_get_self_classref(classinfo *cls)
1070 /* XXX this should be done in a faster way. Maybe always make */
1071 /* the classref of index 0 a self reference. */
1072 return class_get_classref(cls,cls->name);
1075 /* class_get_classref_multiarray_of ********************************************
1077 Returns an array type reference with the given dimension and element class
1081 dim..............the requested dimension
1082 dim must be in [1;255]. This is NOT checked!
1083 ref..............the component class reference
1086 a pointer to the class reference for the array type
1089 The referer of `ref` is used as the referer for the new classref.
1091 *******************************************************************************/
1093 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1097 constant_classref *cr;
1101 assert(dim >= 1 && dim <= 255);
1105 /* Assemble the array class name */
1106 namelen = ref->name->blength;
1108 if (ref->name->text[0] == '[') {
1109 /* the element is itself an array */
1110 namebuf = DMNEW(char, namelen + dim);
1111 memcpy(namebuf + dim, ref->name->text, namelen);
1115 /* the element is a non-array class */
1116 namebuf = DMNEW(char, namelen + 2 + dim);
1118 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1119 namelen += (2 + dim);
1120 namebuf[namelen - 1] = ';';
1122 memset(namebuf, '[', dim);
1124 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1132 /* class_get_classref_component_of *********************************************
1134 Returns the component classref of a given array type reference
1137 ref..............the array type reference
1140 a reference to the component class, or
1141 NULL if `ref` is not an object array type reference
1144 The referer of `ref` is used as the referer for the new classref.
1146 *******************************************************************************/
1148 constant_classref *class_get_classref_component_of(constant_classref *ref)
1155 name = ref->name->text;
1159 namelen = ref->name->blength - 1;
1164 else if (*name != '[') {
1168 return class_get_classref(ref->referer, utf_new(name, namelen));
1172 /* class_findmethod ************************************************************
1174 Searches a 'classinfo' structure for a method having the given name
1175 and descriptor. If descriptor is NULL, it is ignored.
1177 *******************************************************************************/
1179 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1184 for (i = 0; i < c->methodscount; i++) {
1185 m = &(c->methods[i]);
1187 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1195 /* class_resolvemethod *********************************************************
1197 Searches a class and it's super classes for a method.
1199 Superinterfaces are *not* searched.
1201 *******************************************************************************/
1203 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1208 m = class_findmethod(c, name, desc);
1213 /* JVM Specification bug:
1215 It is important NOT to resolve special <init> and <clinit>
1216 methods to super classes or interfaces; yet, this is not
1217 explicited in the specification. Section 5.4.3.3 should be
1218 updated appropriately. */
1220 if (name == utf_init || name == utf_clinit)
1230 /* class_resolveinterfacemethod_intern *****************************************
1232 Internally used helper function. Do not use this directly.
1234 *******************************************************************************/
1236 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1237 utf *name, utf *desc)
1242 /* try to find the method in the class */
1244 m = class_findmethod(c, name, desc);
1249 /* No method found? Try the super interfaces. */
1251 for (i = 0; i < c->interfacescount; i++) {
1252 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1258 /* no method found */
1264 /* class_resolveclassmethod ****************************************************
1266 Resolves a reference from REFERER to a method with NAME and DESC in
1269 If the method cannot be resolved the return value is NULL. If
1270 EXCEPT is true *exceptionptr is set, too.
1272 *******************************************************************************/
1274 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1275 classinfo *referer, bool throwexception)
1281 /* if (c->flags & ACC_INTERFACE) { */
1282 /* if (throwexception) */
1283 /* *exceptionptr = */
1284 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1288 /* try class c and its superclasses */
1292 m = class_resolvemethod(cls, name, desc);
1297 /* Try the super interfaces. */
1299 for (i = 0; i < c->interfacescount; i++) {
1300 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1307 exceptions_throw_nosuchmethoderror(c, name, desc);
1312 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1314 exceptions_throw_abstractmethoderror();
1319 /* XXX check access rights */
1325 /* class_resolveinterfacemethod ************************************************
1327 Resolves a reference from REFERER to a method with NAME and DESC in
1330 If the method cannot be resolved the return value is NULL. If
1331 EXCEPT is true *exceptionptr is set, too.
1333 *******************************************************************************/
1335 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1336 classinfo *referer, bool throwexception)
1340 if (!(c->flags & ACC_INTERFACE)) {
1342 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1347 mi = class_resolveinterfacemethod_intern(c, name, desc);
1352 /* try class java.lang.Object */
1354 mi = class_findmethod(class_java_lang_Object, name, desc);
1360 exceptions_throw_nosuchmethoderror(c, name, desc);
1366 /* class_findfield *************************************************************
1368 Searches for field with specified name and type in a classinfo
1369 structure. If no such field is found NULL is returned.
1371 *******************************************************************************/
1373 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1377 for (i = 0; i < c->fieldscount; i++)
1378 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1379 return &(c->fields[i]);
1381 if (c->super != NULL)
1382 return class_findfield(c->super, name, desc);
1388 /* class_findfield_approx ******************************************************
1390 Searches in 'classinfo'-structure for a field with the specified
1393 *******************************************************************************/
1395 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1399 /* get field index */
1401 i = class_findfield_index_by_name(c, name);
1403 /* field was not found, return */
1408 /* return field address */
1410 return &(c->fields[i]);
1414 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1418 for (i = 0; i < c->fieldscount; i++) {
1419 /* compare field names */
1421 if ((c->fields[i].name == name))
1425 /* field was not found, raise exception */
1427 exceptions_throw_nosuchfielderror(c, name);
1433 /****************** Function: class_resolvefield_int ***************************
1435 This is an internally used helper function. Do not use this directly.
1437 Tries to resolve a field having the given name and type.
1438 If the field cannot be resolved, NULL is returned.
1440 *******************************************************************************/
1442 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1447 /* search for field in class c */
1449 for (i = 0; i < c->fieldscount; i++) {
1450 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1451 return &(c->fields[i]);
1455 /* Try super interfaces recursively. */
1457 for (i = 0; i < c->interfacescount; i++) {
1458 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1464 /* Try super class. */
1466 if (c->super != NULL)
1467 return class_resolvefield_int(c->super, name, desc);
1475 /********************* Function: class_resolvefield ***************************
1477 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1479 If the field cannot be resolved the return value is NULL. If EXCEPT is
1480 true *exceptionptr is set, too.
1482 *******************************************************************************/
1484 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1485 classinfo *referer, bool throwexception)
1489 fi = class_resolvefield_int(c, name, desc);
1493 exceptions_throw_nosuchfielderror(c, name);
1498 /* XXX check access rights */
1504 /* class_issubclass ************************************************************
1506 Checks if sub is a descendant of super.
1508 *******************************************************************************/
1510 bool class_issubclass(classinfo *sub, classinfo *super)
1517 /* We reached java/lang/Object and did not find the requested
1523 /* We found the requested super class. */
1533 /* class_isanysubclass *********************************************************
1535 Checks a subclass relation between two classes. Implemented
1536 interfaces are interpreted as super classes.
1538 Return value: 1 ... sub is subclass of super
1541 *******************************************************************************/
1543 bool class_isanysubclass(classinfo *sub, classinfo *super)
1548 /* This is the trivial case. */
1553 /* Primitive classes are only subclasses of themselves. */
1555 if (class_is_primitive(sub) || class_is_primitive(super))
1558 /* Check for interfaces. */
1560 if (super->flags & ACC_INTERFACE) {
1561 result = (sub->vftbl->interfacetablelength > super->index) &&
1562 (sub->vftbl->interfacetable[-super->index] != NULL);
1565 /* java.lang.Object is the only super class of any
1568 if (sub->flags & ACC_INTERFACE)
1569 return (super == class_java_lang_Object);
1571 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1573 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1574 result = diffval <= (uint32_t) super->vftbl->diffval;
1576 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1583 /* class_get_componenttype *****************************************************
1585 Return the component class of the given class. If the given class
1586 is not an array, return NULL.
1588 *******************************************************************************/
1590 classinfo *class_get_componenttype(classinfo *c)
1592 classinfo *component;
1593 arraydescriptor *ad;
1595 /* XXX maybe we could find a way to do this without linking. */
1596 /* This way should be safe and easy, however. */
1598 if (!(c->state & CLASS_LINKED))
1602 ad = c->vftbl->arraydesc;
1607 if (ad->arraytype == ARRAYTYPE_OBJECT)
1608 component = ad->componentvftbl->class;
1610 component = primitive_class_get_by_type(ad->arraytype);
1616 /* class_get_declaredclasses ***************************************************
1618 Return an array of declared classes of the given class.
1620 *******************************************************************************/
1622 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1624 classref_or_classinfo inner;
1625 classref_or_classinfo outer;
1627 int declaredclasscount; /* number of declared classes */
1628 int pos; /* current declared class */
1629 java_handle_objectarray_t *oa; /* array of declared classes */
1633 declaredclasscount = 0;
1635 if (!class_is_primitive(c) && !class_is_array(c)) {
1636 /* Determine number of declared classes. */
1638 for (i = 0; i < c->innerclasscount; i++) {
1639 /* Get outer-class. If the inner-class is not a member
1640 class, the outer-class is NULL. */
1642 outer = c->innerclass[i].outer_class;
1644 if (outer.any == NULL)
1647 /* Check if outer-class is a classref or a real class and
1648 get the class name from the structure. */
1650 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1652 /* Outer class is this class. */
1654 if ((outername == c->name) &&
1655 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1656 declaredclasscount++;
1660 /* Allocate Class[] and check for OOM. */
1662 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1667 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1668 inner = c->innerclass[i].inner_class;
1669 outer = c->innerclass[i].outer_class;
1671 /* Get outer-class. If the inner-class is not a member class,
1672 the outer-class is NULL. */
1674 if (outer.any == NULL)
1677 /* Check if outer_class is a classref or a real class and get
1678 the class name from the structure. */
1680 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1682 /* Outer class is this class. */
1684 if ((outername == c->name) &&
1685 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1687 ic = resolve_classref_or_classinfo_eager(inner, false);
1692 if (!(ic->state & CLASS_LINKED))
1693 if (!link_class(ic))
1696 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1704 /* class_get_declaringclass ****************************************************
1706 If the class or interface given is a member of another class,
1707 return the declaring class. For array and primitive classes return
1710 *******************************************************************************/
1712 classinfo *class_get_declaringclass(classinfo *c)
1714 classref_or_classinfo cr;
1717 /* Get declaring class. */
1719 cr = c->declaringclass;
1724 /* Resolve the class if necessary. */
1726 if (IS_CLASSREF(cr)) {
1727 /* dc = resolve_classref_eager(cr.ref); */
1728 dc = resolve_classref_or_classinfo_eager(cr, true);
1733 /* Store the resolved class in the class structure. */
1744 /* class_get_enclosingclass ****************************************************
1746 Return the enclosing class for the given class.
1748 *******************************************************************************/
1750 classinfo *class_get_enclosingclass(classinfo *c)
1752 classref_or_classinfo cr;
1755 /* Get enclosing class. */
1757 cr = c->enclosingclass;
1762 /* Resolve the class if necessary. */
1764 if (IS_CLASSREF(cr)) {
1765 /* ec = resolve_classref_eager(cr.ref); */
1766 ec = resolve_classref_or_classinfo_eager(cr, true);
1771 /* Store the resolved class in the class structure. */
1782 /* class_get_enclosingmethod ***************************************************
1784 Return the enclosing method for the given class.
1787 c ... class to return the enclosing method for
1790 methodinfo of the enclosing method
1792 *******************************************************************************/
1794 methodinfo *class_get_enclosingmethod(classinfo *c)
1796 constant_nameandtype *cn;
1800 /* get enclosing class and method */
1802 ec = class_get_enclosingclass(c);
1803 cn = c->enclosingmethod;
1805 /* check for enclosing class and method */
1813 /* find method in enclosing class */
1815 m = class_findmethod(ec, cn->name, cn->descriptor);
1818 exceptions_throw_internalerror("Enclosing method doesn't exist");
1826 /* class_get_interfaces ********************************************************
1828 Return an array of interfaces of the given class.
1830 *******************************************************************************/
1832 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1835 java_handle_objectarray_t *oa;
1838 if (!(c->state & CLASS_LINKED))
1842 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1847 for (i = 0; i < c->interfacescount; i++) {
1848 ic = c->interfaces[i];
1850 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1857 /* class_get_annotations *******************************************************
1859 Get the unparsed declared annotations in a byte array
1863 c........the class of which the annotations should be returned
1866 The unparsed declared annotations in a byte array
1867 (or NULL if there aren't any).
1869 *******************************************************************************/
1871 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1873 #if defined(ENABLE_ANNOTATIONS)
1874 java_handle_t *annotations; /* unparsed annotations */
1876 LLNI_classinfo_field_get(c, annotations, annotations);
1878 return (java_handle_bytearray_t*)annotations;
1885 /* class_get_modifiers *********************************************************
1887 Get the modifier flags of the given class.
1890 c....the class of which the modifier flags should be returned
1891 ignoreInnerClassesAttrib
1895 *******************************************************************************/
1897 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
1899 classref_or_classinfo inner;
1900 classref_or_classinfo outer;
1904 if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
1905 /* search for passed class as inner class */
1907 for (i = 0; i < c->innerclasscount; i++) {
1908 inner = c->innerclass[i].inner_class;
1909 outer = c->innerclass[i].outer_class;
1911 /* Check if inner is a classref or a real class and get
1912 the name of the structure */
1914 innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
1916 /* innerclass is this class */
1918 if (innername == c->name) {
1919 /* has the class actually an outer class? */
1922 /* return flags got from the outer class file */
1923 return c->innerclass[i].flags & ACC_CLASS_REFLECT_MASK;
1925 return c->flags & ACC_CLASS_REFLECT_MASK;
1930 /* passed class is no inner class or it was not requested */
1932 return c->flags & ACC_CLASS_REFLECT_MASK;
1936 /* class_get_signature *********************************************************
1938 Return the signature of the given class. For array and primitive
1939 classes return NULL.
1941 *******************************************************************************/
1943 #if defined(ENABLE_JAVASE)
1944 utf *class_get_signature(classinfo *c)
1946 /* For array and primitive classes return NULL. */
1948 if (class_is_array(c) || class_is_primitive(c))
1951 return c->signature;
1956 /* class_printflags ************************************************************
1958 Prints flags of a class.
1960 *******************************************************************************/
1962 #if !defined(NDEBUG)
1963 void class_printflags(classinfo *c)
1970 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1971 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1972 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1973 if (c->flags & ACC_STATIC) printf(" STATIC");
1974 if (c->flags & ACC_FINAL) printf(" FINAL");
1975 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1976 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1977 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1978 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1979 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1980 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1985 /* class_print *****************************************************************
1987 Prints classname plus flags.
1989 *******************************************************************************/
1991 #if !defined(NDEBUG)
1992 void class_print(classinfo *c)
1999 utf_display_printable_ascii(c->name);
2000 class_printflags(c);
2005 /* class_classref_print ********************************************************
2007 Prints classname plus referer class.
2009 *******************************************************************************/
2011 #if !defined(NDEBUG)
2012 void class_classref_print(constant_classref *cr)
2019 utf_display_printable_ascii(cr->name);
2022 class_print(cr->referer);
2030 /* class_println ***************************************************************
2032 Prints classname plus flags and new line.
2034 *******************************************************************************/
2036 #if !defined(NDEBUG)
2037 void class_println(classinfo *c)
2045 /* class_classref_println ******************************************************
2047 Prints classname plus referer class and new line.
2049 *******************************************************************************/
2051 #if !defined(NDEBUG)
2052 void class_classref_println(constant_classref *cr)
2054 class_classref_print(cr);
2060 /* class_classref_or_classinfo_print *******************************************
2062 Prints classname plus referer class.
2064 *******************************************************************************/
2066 #if !defined(NDEBUG)
2067 void class_classref_or_classinfo_print(classref_or_classinfo c)
2069 if (c.any == NULL) {
2070 printf("(classref_or_classinfo) NULL");
2074 class_classref_print(c.ref);
2081 /* class_classref_or_classinfo_println *****************************************
2083 Prints classname plus referer class and a newline.
2085 *******************************************************************************/
2087 #if !defined(NDEBUG)
2088 void class_classref_or_classinfo_println(classref_or_classinfo c)
2090 class_classref_or_classinfo_print(c);
2096 /* class_showconstantpool ******************************************************
2098 Dump the constant pool of the given class to stdout.
2100 *******************************************************************************/
2102 #if !defined(NDEBUG)
2103 void class_showconstantpool (classinfo *c)
2108 printf ("---- dump of constant pool ----\n");
2110 for (i=0; i<c->cpcount; i++) {
2111 printf ("#%d: ", (int) i);
2113 e = c -> cpinfos [i];
2116 switch (c -> cptags [i]) {
2117 case CONSTANT_Class:
2118 printf ("Classreference -> ");
2119 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2121 case CONSTANT_Fieldref:
2122 printf ("Fieldref -> ");
2123 field_fieldref_print((constant_FMIref *) e);
2125 case CONSTANT_Methodref:
2126 printf ("Methodref -> ");
2127 method_methodref_print((constant_FMIref *) e);
2129 case CONSTANT_InterfaceMethodref:
2130 printf ("InterfaceMethod -> ");
2131 method_methodref_print((constant_FMIref *) e);
2133 case CONSTANT_String:
2134 printf ("String -> ");
2135 utf_display_printable_ascii (e);
2137 case CONSTANT_Integer:
2138 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2140 case CONSTANT_Float:
2141 printf ("Float -> %f", ((constant_float*)e) -> value);
2143 case CONSTANT_Double:
2144 printf ("Double -> %f", ((constant_double*)e) -> value);
2148 u8 v = ((constant_long*)e) -> value;
2150 printf ("Long -> %ld", (long int) v);
2152 printf ("Long -> HI: %ld, LO: %ld\n",
2153 (long int) v.high, (long int) v.low);
2157 case CONSTANT_NameAndType:
2159 constant_nameandtype *cnt = e;
2160 printf ("NameAndType: ");
2161 utf_display_printable_ascii (cnt->name);
2163 utf_display_printable_ascii (cnt->descriptor);
2167 printf ("Utf8 -> ");
2168 utf_display_printable_ascii (e);
2171 log_text("Invalid type of ConstantPool-Entry");
2179 #endif /* !defined(NDEBUG) */
2182 /* class_showmethods ***********************************************************
2184 Dump info about the fields and methods of the given class to stdout.
2186 *******************************************************************************/
2188 #if !defined(NDEBUG)
2189 void class_showmethods (classinfo *c)
2193 printf("--------- Fields and Methods ----------------\n");
2195 class_printflags(c);
2199 utf_display_printable_ascii(c->name);
2204 utf_display_printable_ascii(c->super->name);
2208 printf("Index: %d\n", c->index);
2210 printf("Interfaces:\n");
2211 for (i = 0; i < c->interfacescount; i++) {
2213 utf_display_printable_ascii(c->interfaces[i]->name);
2214 printf (" (%d)\n", c->interfaces[i]->index);
2217 printf("Fields:\n");
2218 for (i = 0; i < c->fieldscount; i++)
2219 field_println(&(c->fields[i]));
2221 printf("Methods:\n");
2222 for (i = 0; i < c->methodscount; i++) {
2223 methodinfo *m = &(c->methods[i]);
2225 if (!(m->flags & ACC_STATIC))
2226 printf("vftblindex: %d ", m->vftblindex);
2231 printf ("Virtual function table:\n");
2232 for (i = 0; i < c->vftbl->vftbllength; i++)
2233 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2235 #endif /* !defined(NDEBUG) */
2239 * These are local overrides for various environment variables in Emacs.
2240 * Please do not remove this and leave it at the end of the file, where
2241 * Emacs will automagically detect them.
2242 * ---------------------------------------------------------------------
2245 * indent-tabs-mode: t
2249 * vim:noexpandtab:sw=4:ts=4: