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_Throwable;
85 classinfo *class_java_io_Serializable;
87 #if defined(WITH_CLASSPATH_GNU)
88 classinfo *class_java_lang_VMSystem;
89 classinfo *class_java_lang_VMThread;
90 classinfo *class_java_lang_VMThrowable;
93 #if defined(WITH_CLASSPATH_SUN)
94 classinfo *class_sun_reflect_MagicAccessorImpl;
97 #if defined(ENABLE_JAVASE)
98 classinfo *class_java_lang_Void;
100 classinfo *class_java_lang_Boolean;
101 classinfo *class_java_lang_Byte;
102 classinfo *class_java_lang_Character;
103 classinfo *class_java_lang_Short;
104 classinfo *class_java_lang_Integer;
105 classinfo *class_java_lang_Long;
106 classinfo *class_java_lang_Float;
107 classinfo *class_java_lang_Double;
109 /* some classes which may be used more often */
111 #if defined(ENABLE_JAVASE)
112 classinfo *class_java_lang_StackTraceElement;
113 classinfo *class_java_lang_reflect_Constructor;
114 classinfo *class_java_lang_reflect_Field;
115 classinfo *class_java_lang_reflect_Method;
116 classinfo *class_java_security_PrivilegedAction;
117 classinfo *class_java_util_Vector;
119 classinfo *arrayclass_java_lang_Object;
121 # if defined(ENABLE_ANNOTATIONS)
122 classinfo *class_sun_reflect_ConstantPool;
123 # if defined(WITH_CLASSPATH_GNU)
124 classinfo *class_sun_reflect_annotation_AnnotationParser;
129 /* pseudo classes for the typechecker */
131 classinfo *pseudo_class_Arraystub;
132 classinfo *pseudo_class_Null;
133 classinfo *pseudo_class_New;
136 /* class_set_packagename *******************************************************
138 Derive the package name from the class name and store it in the
141 An internal package name consists of the package name plus the
142 trailing '/', e.g. "java/lang/".
144 For classes in the unnamed package, the package name is set to
147 *******************************************************************************/
149 void class_set_packagename(classinfo *c)
154 p = UTF_END(c->name) - 1;
155 start = c->name->text;
157 if (c->name->text[0] == '[') {
158 /* Set packagename of arrays to the element's package. */
160 for (; *start == '['; start++);
162 /* Skip the 'L' in arrays of references. */
168 /* Search for last '/'. */
170 for (; (p > start) && (*p != '/'); --p);
172 /* If we found a '/' we set the package name plus the trailing
173 '/'. Otherwise we set the packagename to NULL. */
176 c->packagename = utf_new(start, p - start + 1);
178 c->packagename = NULL;
182 /* class_create_classinfo ******************************************************
184 Create a new classinfo struct. The class name is set to the given utf *,
185 most other fields are initialized to zero.
187 Note: classname may be NULL. In this case a not-yet-named classinfo is
188 created. The name must be filled in later and class_set_packagename
189 must be called after that.
191 *******************************************************************************/
193 classinfo *class_create_classinfo(utf *classname)
197 #if defined(ENABLE_STATISTICS)
199 size_classinfo += sizeof(classinfo);
202 /* we use a safe name for temporarily unnamed classes */
204 if (classname == NULL)
205 classname = utf_not_named_yet;
209 log_message_utf("Creating class: ", classname);
212 #if !defined(ENABLE_GC_BOEHM)
213 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
214 /*c = NEW(classinfo);
215 MZERO(c, classinfo, 1);*/
217 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
218 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
223 /* Set the header.vftbl of all loaded classes to the one of
224 java.lang.Class, so Java code can use a class as object. */
226 if (class_java_lang_Class != NULL)
227 if (class_java_lang_Class->vftbl != NULL)
228 c->object.header.vftbl = class_java_lang_Class->vftbl;
230 #if defined(ENABLE_JAVASE)
231 /* check if the class is a reference class and flag it */
233 if (classname == utf_java_lang_ref_SoftReference) {
234 c->flags |= ACC_CLASS_REFERENCE_SOFT;
236 else if (classname == utf_java_lang_ref_WeakReference) {
237 c->flags |= ACC_CLASS_REFERENCE_WEAK;
239 else if (classname == utf_java_lang_ref_PhantomReference) {
240 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
244 if (classname != utf_not_named_yet)
245 class_set_packagename(c);
247 LOCK_INIT_OBJECT_LOCK(&c->object.header);
253 /* class_postset_header_vftbl **************************************************
255 Set the header.vftbl of all classes created before java.lang.Class
256 was linked. This is necessary that Java code can use a class as
259 *******************************************************************************/
261 void class_postset_header_vftbl(void)
265 classcache_name_entry *nmen;
266 classcache_class_entry *clsen;
268 assert(class_java_lang_Class);
270 for (slot = 0; slot < hashtable_classcache.size; slot++) {
271 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
273 for (; nmen; nmen = nmen->hashlink) {
274 /* iterate over all class entries */
276 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
279 /* now set the the vftbl */
281 if (c->object.header.vftbl == NULL)
282 c->object.header.vftbl = class_java_lang_Class->vftbl;
288 /* class_define ****************************************************************
290 Calls the loader and defines a class in the VM.
292 *******************************************************************************/
294 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
301 /* check if this class has already been defined */
303 c = classcache_lookup_defined_or_initiated(cl, name);
306 exceptions_throw_linkageerror("duplicate class definition: ", c);
311 /* create a new classinfo struct */
313 c = class_create_classinfo(name);
315 #if defined(ENABLE_STATISTICS)
318 if (opt_getloadingtime)
322 /* build a classbuffer with the given data */
324 cb = NEW(classbuffer);
331 /* preset the defining classloader */
335 /* load the class from this buffer */
337 r = load_class_from_classbuffer(cb);
341 FREE(cb, classbuffer);
343 #if defined(ENABLE_STATISTICS)
346 if (opt_getloadingtime)
351 /* If return value is NULL, we had a problem and the class is
352 not loaded. Now free the allocated memory, otherwise we
353 could run into a DOS. */
360 #if defined(ENABLE_JAVASE)
361 # if defined(WITH_CLASSPATH_SUN)
362 /* Store the protection domain. */
364 c->protectiondomain = pd;
368 /* Store the newly defined class in the class cache. This call
369 also checks whether a class of the same name has already been
370 defined by the same defining loader, and if so, replaces the
371 newly created class by the one defined earlier. */
373 /* Important: The classinfo given to classcache_store must be
374 fully prepared because another thread may return
375 this pointer after the lookup at to top of this
376 function directly after the class cache lock has
379 c = classcache_store(cl, c, true);
385 /* class_load_attribute_sourcefile *********************************************
387 SourceFile_attribute {
388 u2 attribute_name_index;
393 *******************************************************************************/
395 static bool class_load_attribute_sourcefile(classbuffer *cb)
406 /* check buffer size */
408 if (!suck_check_classbuffer_size(cb, 4 + 2))
411 /* check attribute length */
413 attribute_length = suck_u4(cb);
415 if (attribute_length != 2) {
416 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
420 /* there can be no more than one SourceFile attribute */
422 if (c->sourcefile != NULL) {
423 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
429 sourcefile_index = suck_u2(cb);
430 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
432 if (sourcefile == NULL)
435 /* store sourcefile */
437 c->sourcefile = sourcefile;
443 /* class_load_attribute_enclosingmethod ****************************************
445 EnclosingMethod_attribute {
446 u2 attribute_name_index;
452 *******************************************************************************/
454 #if defined(ENABLE_JAVASE)
455 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
461 classref_or_classinfo cr;
462 constant_nameandtype *cn;
468 /* check buffer size */
470 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
473 /* check attribute length */
475 attribute_length = suck_u4(cb);
477 if (attribute_length != 4) {
478 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
482 /* there can be no more than one EnclosingMethod attribute */
484 if (c->enclosingmethod != NULL) {
485 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
489 /* get class index */
491 class_index = suck_u2(cb);
492 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
494 /* get method index */
496 method_index = suck_u2(cb);
497 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
499 /* store info in classinfo */
501 c->enclosingclass.any = cr.any;
502 c->enclosingmethod = cn;
506 #endif /* defined(ENABLE_JAVASE) */
509 /* class_load_attributes *******************************************************
511 Read attributes from ClassFile.
514 u2 attribute_name_index;
516 u1 info[attribute_length];
519 InnerClasses_attribute {
520 u2 attribute_name_index;
524 *******************************************************************************/
526 bool class_load_attributes(classbuffer *cb)
529 uint16_t attributes_count;
530 uint16_t attribute_name_index;
532 innerclassinfo *info;
533 classref_or_classinfo inner;
534 classref_or_classinfo outer;
541 /* get attributes count */
543 if (!suck_check_classbuffer_size(cb, 2))
546 attributes_count = suck_u2(cb);
548 for (i = 0; i < attributes_count; i++) {
549 /* get attribute name */
551 if (!suck_check_classbuffer_size(cb, 2))
554 attribute_name_index = suck_u2(cb);
556 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
558 if (attribute_name == NULL)
561 if (attribute_name == utf_InnerClasses) {
564 if (c->innerclass != NULL) {
565 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
569 if (!suck_check_classbuffer_size(cb, 4 + 2))
572 /* skip attribute length */
575 /* number of records */
576 c->innerclasscount = suck_u2(cb);
578 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
581 /* allocate memory for innerclass structure */
582 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
584 for (j = 0; j < c->innerclasscount; j++) {
585 /* The innerclass structure contains a class with an encoded
586 name, its defining scope, its simple name and a bitmask of
589 info = c->innerclass + j;
591 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
592 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
593 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
596 /* If the current inner-class is the currently loaded
597 class check for some special flags. */
599 if (inner.ref->name == c->name) {
600 /* If an inner-class is not a member, its
601 outer-class is NULL. */
603 if (outer.ref != NULL) {
604 c->flags |= ACC_CLASS_MEMBER;
606 /* A member class doesn't have an
607 EnclosingMethod attribute, so set the
608 enclosing-class to be the same as the
611 c->declaringclass = outer;
612 c->enclosingclass = outer;
615 /* If an inner-class is anonymous, its name is
619 c->flags |= ACC_CLASS_ANONYMOUS;
622 info->inner_class = inner;
623 info->outer_class = outer;
628 else if (attribute_name == utf_SourceFile) {
631 if (!class_load_attribute_sourcefile(cb))
634 #if defined(ENABLE_JAVASE)
635 else if (attribute_name == utf_EnclosingMethod) {
636 /* EnclosingMethod */
638 if (!class_load_attribute_enclosingmethod(cb))
641 else if (attribute_name == utf_Signature) {
644 if (!loader_load_attribute_signature(cb, &(c->signature)))
649 #if defined(ENABLE_ANNOTATIONS)
650 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
651 /* RuntimeVisibleAnnotations */
652 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
655 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
656 /* RuntimeInvisibleAnnotations */
657 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
663 /* unknown attribute */
665 if (!loader_skip_attribute_body(cb))
674 /* class_freepool **************************************************************
676 Frees all resources used by this classes Constant Pool.
678 *******************************************************************************/
680 static void class_freecpool(classinfo *c)
686 if (c->cptags && c->cpinfos) {
687 for (idx = 0; idx < c->cpcount; idx++) {
688 tag = c->cptags[idx];
689 info = c->cpinfos[idx];
693 case CONSTANT_Fieldref:
694 case CONSTANT_Methodref:
695 case CONSTANT_InterfaceMethodref:
696 FREE(info, constant_FMIref);
698 case CONSTANT_Integer:
699 FREE(info, constant_integer);
702 FREE(info, constant_float);
705 FREE(info, constant_long);
707 case CONSTANT_Double:
708 FREE(info, constant_double);
710 case CONSTANT_NameAndType:
711 FREE(info, constant_nameandtype);
719 MFREE(c->cptags, u1, c->cpcount);
722 MFREE(c->cpinfos, voidptr, c->cpcount);
726 /* class_getconstant ***********************************************************
728 Retrieves the value at position 'pos' of the constantpool of a
729 class. If the type of the value is other than 'ctype', an error is
732 *******************************************************************************/
734 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
736 /* check index and type of constantpool entry */
737 /* (pos == 0 is caught by type comparison) */
739 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
740 exceptions_throw_classformaterror(c, "Illegal constant pool index");
744 return c->cpinfos[pos];
748 /* innerclass_getconstant ******************************************************
750 Like class_getconstant, but if cptags is ZERO, null is returned.
752 *******************************************************************************/
754 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
756 /* invalid position in constantpool */
758 if (pos >= c->cpcount) {
759 exceptions_throw_classformaterror(c, "Illegal constant pool index");
763 /* constantpool entry of type 0 */
765 if (c->cptags[pos] == 0)
768 /* check type of constantpool entry */
770 if (c->cptags[pos] != ctype) {
771 exceptions_throw_classformaterror(c, "Illegal constant pool index");
775 return c->cpinfos[pos];
779 /* class_free ******************************************************************
781 Frees all resources used by the class.
783 *******************************************************************************/
785 void class_free(classinfo *c)
792 if (c->interfaces != NULL)
793 MFREE(c->interfaces, classinfo*, c->interfacescount);
796 for (i = 0; i < c->fieldscount; i++)
797 field_free(&(c->fields[i]));
798 MFREE(c->fields, fieldinfo, c->fieldscount);
802 for (i = 0; i < c->methodscount; i++)
803 method_free(&(c->methods[i]));
804 MFREE(c->methods, methodinfo, c->methodscount);
807 if ((v = c->vftbl) != NULL) {
809 mem_free(v->arraydesc,sizeof(arraydescriptor));
811 for (i = 0; i < v->interfacetablelength; i++) {
812 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
814 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
816 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
817 sizeof(methodptr*) * (v->interfacetablelength -
818 (v->interfacetablelength > 0));
819 v = (vftbl_t*) (((methodptr*) v) -
820 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
825 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
827 /* if (c->classvftbl)
828 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
834 /* get_array_class *************************************************************
836 Returns the array class with the given name for the given
837 classloader, or NULL if an exception occurred.
839 Note: This function does eager loading.
841 *******************************************************************************/
843 static classinfo *get_array_class(utf *name,classloader *initloader,
844 classloader *defloader,bool link)
848 /* lookup this class in the classcache */
849 c = classcache_lookup(initloader,name);
851 c = classcache_lookup_defined(defloader,name);
854 /* we have to create it */
855 c = class_create_classinfo(name);
856 c = load_newly_created_array(c,initloader);
862 assert(c->state & CLASS_LOADED);
863 assert(c->classloader == defloader);
865 if (link && !(c->state & CLASS_LINKED))
869 assert(!link || (c->state & CLASS_LINKED));
875 /* class_array_of **************************************************************
877 Returns an array class with the given component class. The array
878 class is dynamically created if neccessary.
880 *******************************************************************************/
882 classinfo *class_array_of(classinfo *component, bool link)
891 cl = component->classloader;
893 dumpsize = dump_size();
895 /* Assemble the array class name */
896 namelen = component->name->blength;
898 if (component->name->text[0] == '[') {
899 /* the component is itself an array */
900 namebuf = DMNEW(char, namelen + 1);
902 MCOPY(namebuf + 1, component->name->text, char, namelen);
906 /* the component is a non-array class */
907 namebuf = DMNEW(char, namelen + 3);
910 MCOPY(namebuf + 2, component->name->text, char, namelen);
911 namebuf[2 + namelen] = ';';
915 u = utf_new(namebuf, namelen);
917 c = get_array_class(u, cl, cl, link);
919 dump_release(dumpsize);
925 /* class_multiarray_of *********************************************************
927 Returns an array class with the given dimension and element class.
928 The array class is dynamically created if neccessary.
930 *******************************************************************************/
932 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
939 dumpsize = dump_size();
942 log_text("Invalid array dimension requested");
946 /* Assemble the array class name */
947 namelen = element->name->blength;
949 if (element->name->text[0] == '[') {
950 /* the element is itself an array */
951 namebuf = DMNEW(char, namelen + dim);
952 memcpy(namebuf + dim, element->name->text, namelen);
956 /* the element is a non-array class */
957 namebuf = DMNEW(char, namelen + 2 + dim);
959 memcpy(namebuf + dim + 1, element->name->text, namelen);
960 namelen += (2 + dim);
961 namebuf[namelen - 1] = ';';
963 memset(namebuf, '[', dim);
965 c = get_array_class(utf_new(namebuf, namelen),
966 element->classloader,
967 element->classloader,
970 dump_release(dumpsize);
976 /* class_lookup_classref *******************************************************
978 Looks up the constant_classref for a given classname in the classref
982 cls..............the class containing the reference
983 name.............the name of the class refered to
986 a pointer to a constant_classref, or
987 NULL if the reference was not found
989 *******************************************************************************/
991 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
993 constant_classref *ref;
994 extra_classref *xref;
999 assert(!cls->classrefcount || cls->classrefs);
1001 /* first search the main classref table */
1002 count = cls->classrefcount;
1003 ref = cls->classrefs;
1004 for (; count; --count, ++ref)
1005 if (ref->name == name)
1008 /* next try the list of extra classrefs */
1009 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1010 if (xref->classref.name == name)
1011 return &(xref->classref);
1019 /* class_get_classref **********************************************************
1021 Returns the constant_classref for a given classname.
1024 cls..............the class containing the reference
1025 name.............the name of the class refered to
1028 a pointer to a constant_classref (never NULL)
1031 The given name is not checked for validity!
1033 *******************************************************************************/
1035 constant_classref *class_get_classref(classinfo *cls, utf *name)
1037 constant_classref *ref;
1038 extra_classref *xref;
1043 ref = class_lookup_classref(cls,name);
1047 xref = NEW(extra_classref);
1048 CLASSREF_INIT(xref->classref,cls,name);
1050 xref->next = cls->extclassrefs;
1051 cls->extclassrefs = xref;
1053 return &(xref->classref);
1057 /* class_get_self_classref *****************************************************
1059 Returns the constant_classref to the class itself.
1062 cls..............the class containing the reference
1065 a pointer to a constant_classref (never NULL)
1067 *******************************************************************************/
1069 constant_classref *class_get_self_classref(classinfo *cls)
1071 /* XXX this should be done in a faster way. Maybe always make */
1072 /* the classref of index 0 a self reference. */
1073 return class_get_classref(cls,cls->name);
1076 /* class_get_classref_multiarray_of ********************************************
1078 Returns an array type reference with the given dimension and element class
1082 dim..............the requested dimension
1083 dim must be in [1;255]. This is NOT checked!
1084 ref..............the component class reference
1087 a pointer to the class reference for the array type
1090 The referer of `ref` is used as the referer for the new classref.
1092 *******************************************************************************/
1094 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1099 constant_classref *cr;
1102 assert(dim >= 1 && dim <= 255);
1104 dumpsize = dump_size();
1106 /* Assemble the array class name */
1107 namelen = ref->name->blength;
1109 if (ref->name->text[0] == '[') {
1110 /* the element is itself an array */
1111 namebuf = DMNEW(char, namelen + dim);
1112 memcpy(namebuf + dim, ref->name->text, namelen);
1116 /* the element is a non-array class */
1117 namebuf = DMNEW(char, namelen + 2 + dim);
1119 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1120 namelen += (2 + dim);
1121 namebuf[namelen - 1] = ';';
1123 memset(namebuf, '[', dim);
1125 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1127 dump_release(dumpsize);
1133 /* class_get_classref_component_of *********************************************
1135 Returns the component classref of a given array type reference
1138 ref..............the array type reference
1141 a reference to the component class, or
1142 NULL if `ref` is not an object array type reference
1145 The referer of `ref` is used as the referer for the new classref.
1147 *******************************************************************************/
1149 constant_classref *class_get_classref_component_of(constant_classref *ref)
1156 name = ref->name->text;
1160 namelen = ref->name->blength - 1;
1165 else if (*name != '[') {
1169 return class_get_classref(ref->referer, utf_new(name, namelen));
1173 /* class_findmethod ************************************************************
1175 Searches a 'classinfo' structure for a method having the given name
1176 and descriptor. If descriptor is NULL, it is ignored.
1178 *******************************************************************************/
1180 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1185 for (i = 0; i < c->methodscount; i++) {
1186 m = &(c->methods[i]);
1188 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1196 /* class_resolvemethod *********************************************************
1198 Searches a class and it's super classes for a method.
1200 Superinterfaces are *not* searched.
1202 *******************************************************************************/
1204 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1209 m = class_findmethod(c, name, desc);
1214 /* JVM Specification bug:
1216 It is important NOT to resolve special <init> and <clinit>
1217 methods to super classes or interfaces; yet, this is not
1218 explicited in the specification. Section 5.4.3.3 should be
1219 updated appropriately. */
1221 if (name == utf_init || name == utf_clinit)
1231 /* class_resolveinterfacemethod_intern *****************************************
1233 Internally used helper function. Do not use this directly.
1235 *******************************************************************************/
1237 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1238 utf *name, utf *desc)
1243 /* try to find the method in the class */
1245 m = class_findmethod(c, name, desc);
1250 /* No method found? Try the super interfaces. */
1252 for (i = 0; i < c->interfacescount; i++) {
1253 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1259 /* no method found */
1265 /* class_resolveclassmethod ****************************************************
1267 Resolves a reference from REFERER to a method with NAME and DESC in
1270 If the method cannot be resolved the return value is NULL. If
1271 EXCEPT is true *exceptionptr is set, too.
1273 *******************************************************************************/
1275 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1276 classinfo *referer, bool throwexception)
1282 /* if (c->flags & ACC_INTERFACE) { */
1283 /* if (throwexception) */
1284 /* *exceptionptr = */
1285 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1289 /* try class c and its superclasses */
1293 m = class_resolvemethod(cls, name, desc);
1298 /* Try the super interfaces. */
1300 for (i = 0; i < c->interfacescount; i++) {
1301 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1308 exceptions_throw_nosuchmethoderror(c, name, desc);
1313 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1315 exceptions_throw_abstractmethoderror();
1320 /* XXX check access rights */
1326 /* class_resolveinterfacemethod ************************************************
1328 Resolves a reference from REFERER to a method with NAME and DESC in
1331 If the method cannot be resolved the return value is NULL. If
1332 EXCEPT is true *exceptionptr is set, too.
1334 *******************************************************************************/
1336 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1337 classinfo *referer, bool throwexception)
1341 if (!(c->flags & ACC_INTERFACE)) {
1343 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1348 mi = class_resolveinterfacemethod_intern(c, name, desc);
1353 /* try class java.lang.Object */
1355 mi = class_findmethod(class_java_lang_Object, name, desc);
1361 exceptions_throw_nosuchmethoderror(c, name, desc);
1367 /* class_findfield *************************************************************
1369 Searches for field with specified name and type in a classinfo
1370 structure. If no such field is found NULL is returned.
1372 *******************************************************************************/
1374 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1378 for (i = 0; i < c->fieldscount; i++)
1379 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1380 return &(c->fields[i]);
1382 if (c->super != NULL)
1383 return class_findfield(c->super, name, desc);
1389 /* class_findfield_approx ******************************************************
1391 Searches in 'classinfo'-structure for a field with the specified
1394 *******************************************************************************/
1396 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1400 /* get field index */
1402 i = class_findfield_index_by_name(c, name);
1404 /* field was not found, return */
1409 /* return field address */
1411 return &(c->fields[i]);
1415 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1419 for (i = 0; i < c->fieldscount; i++) {
1420 /* compare field names */
1422 if ((c->fields[i].name == name))
1426 /* field was not found, raise exception */
1428 exceptions_throw_nosuchfielderror(c, name);
1434 /****************** Function: class_resolvefield_int ***************************
1436 This is an internally used helper function. Do not use this directly.
1438 Tries to resolve a field having the given name and type.
1439 If the field cannot be resolved, NULL is returned.
1441 *******************************************************************************/
1443 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1448 /* search for field in class c */
1450 for (i = 0; i < c->fieldscount; i++) {
1451 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1452 return &(c->fields[i]);
1456 /* Try super interfaces recursively. */
1458 for (i = 0; i < c->interfacescount; i++) {
1459 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1465 /* Try super class. */
1467 if (c->super != NULL)
1468 return class_resolvefield_int(c->super, name, desc);
1476 /********************* Function: class_resolvefield ***************************
1478 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1480 If the field cannot be resolved the return value is NULL. If EXCEPT is
1481 true *exceptionptr is set, too.
1483 *******************************************************************************/
1485 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1486 classinfo *referer, bool throwexception)
1490 fi = class_resolvefield_int(c, name, desc);
1494 exceptions_throw_nosuchfielderror(c, name);
1499 /* XXX check access rights */
1505 /* class_issubclass ************************************************************
1507 Checks if sub is a descendant of super.
1509 *******************************************************************************/
1511 bool class_issubclass(classinfo *sub, classinfo *super)
1518 /* We reached java/lang/Object and did not find the requested
1524 /* We found the requested super class. */
1534 /* class_isanysubclass *********************************************************
1536 Checks a subclass relation between two classes. Implemented
1537 interfaces are interpreted as super classes.
1539 Return value: 1 ... sub is subclass of super
1542 *******************************************************************************/
1544 bool class_isanysubclass(classinfo *sub, classinfo *super)
1549 /* This is the trivial case. */
1554 /* Primitive classes are only subclasses of themselves. */
1556 if (class_is_primitive(sub) || class_is_primitive(super))
1559 /* Check for interfaces. */
1561 if (super->flags & ACC_INTERFACE) {
1562 result = (sub->vftbl->interfacetablelength > super->index) &&
1563 (sub->vftbl->interfacetable[-super->index] != NULL);
1566 /* java.lang.Object is the only super class of any
1569 if (sub->flags & ACC_INTERFACE)
1570 return (super == class_java_lang_Object);
1572 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1574 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1575 result = diffval <= (uint32_t) super->vftbl->diffval;
1577 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1584 /* class_get_componenttype *****************************************************
1586 Return the component class of the given class. If the given class
1587 is not an array, return NULL.
1589 *******************************************************************************/
1591 classinfo *class_get_componenttype(classinfo *c)
1593 classinfo *component;
1594 arraydescriptor *ad;
1596 /* XXX maybe we could find a way to do this without linking. */
1597 /* This way should be safe and easy, however. */
1599 if (!(c->state & CLASS_LINKED))
1603 ad = c->vftbl->arraydesc;
1608 if (ad->arraytype == ARRAYTYPE_OBJECT)
1609 component = ad->componentvftbl->class;
1611 component = primitive_class_get_by_type(ad->arraytype);
1617 /* class_get_declaredclasses ***************************************************
1619 Return an array of declared classes of the given class.
1621 *******************************************************************************/
1623 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1625 classref_or_classinfo inner;
1626 classref_or_classinfo outer;
1628 int declaredclasscount; /* number of declared classes */
1629 int pos; /* current declared class */
1630 java_handle_objectarray_t *oa; /* array of declared classes */
1634 declaredclasscount = 0;
1636 if (!class_is_primitive(c) && !class_is_array(c)) {
1637 /* Determine number of declared classes. */
1639 for (i = 0; i < c->innerclasscount; i++) {
1640 /* Get outer-class. If the inner-class is not a member
1641 class, the outer-class is NULL. */
1643 outer = c->innerclass[i].outer_class;
1645 if (outer.any == NULL)
1648 /* Check if outer-class is a classref or a real class and
1649 get the class name from the structure. */
1651 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1653 /* Outer class is this class. */
1655 if ((outername == c->name) &&
1656 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1657 declaredclasscount++;
1661 /* Allocate Class[] and check for OOM. */
1663 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1668 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1669 inner = c->innerclass[i].inner_class;
1670 outer = c->innerclass[i].outer_class;
1672 /* Get outer-class. If the inner-class is not a member class,
1673 the outer-class is NULL. */
1675 if (outer.any == NULL)
1678 /* Check if outer_class is a classref or a real class and get
1679 the class name from the structure. */
1681 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1683 /* Outer class is this class. */
1685 if ((outername == c->name) &&
1686 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1688 ic = resolve_classref_or_classinfo_eager(inner, false);
1693 if (!(ic->state & CLASS_LINKED))
1694 if (!link_class(ic))
1697 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1705 /* class_get_declaringclass ****************************************************
1707 If the class or interface given is a member of another class,
1708 return the declaring class. For array and primitive classes return
1711 *******************************************************************************/
1713 classinfo *class_get_declaringclass(classinfo *c)
1715 classref_or_classinfo cr;
1718 /* Get declaring class. */
1720 cr = c->declaringclass;
1725 /* Resolve the class if necessary. */
1727 if (IS_CLASSREF(cr)) {
1728 /* dc = resolve_classref_eager(cr.ref); */
1729 dc = resolve_classref_or_classinfo_eager(cr, true);
1734 /* Store the resolved class in the class structure. */
1745 /* class_get_enclosingclass ****************************************************
1747 Return the enclosing class for the given class.
1749 *******************************************************************************/
1751 classinfo *class_get_enclosingclass(classinfo *c)
1753 classref_or_classinfo cr;
1756 /* Get enclosing class. */
1758 cr = c->enclosingclass;
1763 /* Resolve the class if necessary. */
1765 if (IS_CLASSREF(cr)) {
1766 /* ec = resolve_classref_eager(cr.ref); */
1767 ec = resolve_classref_or_classinfo_eager(cr, true);
1772 /* Store the resolved class in the class structure. */
1783 /* class_get_interfaces ********************************************************
1785 Return an array of interfaces of the given class.
1787 *******************************************************************************/
1789 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1792 java_handle_objectarray_t *oa;
1795 if (!(c->state & CLASS_LINKED))
1799 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1804 for (i = 0; i < c->interfacescount; i++) {
1805 ic = c->interfaces[i];
1807 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1814 /* class_get_annotations *******************************************************
1816 Get the unparsed declared annotations in a byte array
1820 c........the class of which the annotations should be returned
1823 The unparsed declared annotations in a byte array
1824 (or NULL if there aren't any).
1826 *******************************************************************************/
1828 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1830 #if defined(ENABLE_ANNOTATIONS)
1831 java_handle_t *annotations; /* unparsed annotations */
1833 LLNI_classinfo_field_get(c, annotations, annotations);
1835 return (java_handle_bytearray_t*)annotations;
1842 /* class_get_modifiers *********************************************************
1844 Get the modifier flags of the given class.
1847 c....the class of which the modifier flags should be returned
1848 ignoreInnerClassesAttrib
1852 *******************************************************************************/
1854 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
1856 classref_or_classinfo inner;
1857 classref_or_classinfo outer;
1861 if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
1862 /* search for passed class as inner class */
1864 for (i = 0; i < c->innerclasscount; i++) {
1865 inner = c->innerclass[i].inner_class;
1866 outer = c->innerclass[i].outer_class;
1868 /* Check if inner is a classref or a real class and get
1869 the name of the structure */
1871 innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
1873 /* innerclass is this class */
1875 if (innername == c->name) {
1876 /* has the class actually an outer class? */
1879 /* return flags got from the outer class file */
1880 return c->innerclass[i].flags & ACC_CLASS_REFLECT_MASK;
1882 return c->flags & ACC_CLASS_REFLECT_MASK;
1887 /* passed class is no inner class or it was not requested */
1889 return c->flags & ACC_CLASS_REFLECT_MASK;
1893 /* class_get_signature *********************************************************
1895 Return the signature of the given class. For array and primitive
1896 classes return NULL.
1898 *******************************************************************************/
1900 #if defined(ENABLE_JAVASE)
1901 utf *class_get_signature(classinfo *c)
1903 /* For array and primitive classes return NULL. */
1905 if (class_is_array(c) || class_is_primitive(c))
1908 return c->signature;
1913 /* class_printflags ************************************************************
1915 Prints flags of a class.
1917 *******************************************************************************/
1919 #if !defined(NDEBUG)
1920 void class_printflags(classinfo *c)
1927 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1928 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1929 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1930 if (c->flags & ACC_STATIC) printf(" STATIC");
1931 if (c->flags & ACC_FINAL) printf(" FINAL");
1932 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1933 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1934 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1935 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1936 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1937 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1942 /* class_print *****************************************************************
1944 Prints classname plus flags.
1946 *******************************************************************************/
1948 #if !defined(NDEBUG)
1949 void class_print(classinfo *c)
1956 utf_display_printable_ascii(c->name);
1957 class_printflags(c);
1962 /* class_classref_print ********************************************************
1964 Prints classname plus referer class.
1966 *******************************************************************************/
1968 #if !defined(NDEBUG)
1969 void class_classref_print(constant_classref *cr)
1976 utf_display_printable_ascii(cr->name);
1979 class_print(cr->referer);
1987 /* class_println ***************************************************************
1989 Prints classname plus flags and new line.
1991 *******************************************************************************/
1993 #if !defined(NDEBUG)
1994 void class_println(classinfo *c)
2002 /* class_classref_println ******************************************************
2004 Prints classname plus referer class and new line.
2006 *******************************************************************************/
2008 #if !defined(NDEBUG)
2009 void class_classref_println(constant_classref *cr)
2011 class_classref_print(cr);
2017 /* class_classref_or_classinfo_print *******************************************
2019 Prints classname plus referer class.
2021 *******************************************************************************/
2023 #if !defined(NDEBUG)
2024 void class_classref_or_classinfo_print(classref_or_classinfo c)
2026 if (c.any == NULL) {
2027 printf("(classref_or_classinfo) NULL");
2031 class_classref_print(c.ref);
2038 /* class_classref_or_classinfo_println *****************************************
2040 Prints classname plus referer class and a newline.
2042 *******************************************************************************/
2044 #if !defined(NDEBUG)
2045 void class_classref_or_classinfo_println(classref_or_classinfo c)
2047 class_classref_or_classinfo_print(c);
2053 /* class_showconstantpool ******************************************************
2055 Dump the constant pool of the given class to stdout.
2057 *******************************************************************************/
2059 #if !defined(NDEBUG)
2060 void class_showconstantpool (classinfo *c)
2065 printf ("---- dump of constant pool ----\n");
2067 for (i=0; i<c->cpcount; i++) {
2068 printf ("#%d: ", (int) i);
2070 e = c -> cpinfos [i];
2073 switch (c -> cptags [i]) {
2074 case CONSTANT_Class:
2075 printf ("Classreference -> ");
2076 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2078 case CONSTANT_Fieldref:
2079 printf ("Fieldref -> ");
2080 field_fieldref_print((constant_FMIref *) e);
2082 case CONSTANT_Methodref:
2083 printf ("Methodref -> ");
2084 method_methodref_print((constant_FMIref *) e);
2086 case CONSTANT_InterfaceMethodref:
2087 printf ("InterfaceMethod -> ");
2088 method_methodref_print((constant_FMIref *) e);
2090 case CONSTANT_String:
2091 printf ("String -> ");
2092 utf_display_printable_ascii (e);
2094 case CONSTANT_Integer:
2095 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2097 case CONSTANT_Float:
2098 printf ("Float -> %f", ((constant_float*)e) -> value);
2100 case CONSTANT_Double:
2101 printf ("Double -> %f", ((constant_double*)e) -> value);
2105 u8 v = ((constant_long*)e) -> value;
2107 printf ("Long -> %ld", (long int) v);
2109 printf ("Long -> HI: %ld, LO: %ld\n",
2110 (long int) v.high, (long int) v.low);
2114 case CONSTANT_NameAndType:
2116 constant_nameandtype *cnt = e;
2117 printf ("NameAndType: ");
2118 utf_display_printable_ascii (cnt->name);
2120 utf_display_printable_ascii (cnt->descriptor);
2124 printf ("Utf8 -> ");
2125 utf_display_printable_ascii (e);
2128 log_text("Invalid type of ConstantPool-Entry");
2136 #endif /* !defined(NDEBUG) */
2139 /* class_showmethods ***********************************************************
2141 Dump info about the fields and methods of the given class to stdout.
2143 *******************************************************************************/
2145 #if !defined(NDEBUG)
2146 void class_showmethods (classinfo *c)
2150 printf("--------- Fields and Methods ----------------\n");
2152 class_printflags(c);
2156 utf_display_printable_ascii(c->name);
2161 utf_display_printable_ascii(c->super->name);
2165 printf("Index: %d\n", c->index);
2167 printf("Interfaces:\n");
2168 for (i = 0; i < c->interfacescount; i++) {
2170 utf_display_printable_ascii(c->interfaces[i]->name);
2171 printf (" (%d)\n", c->interfaces[i]->index);
2174 printf("Fields:\n");
2175 for (i = 0; i < c->fieldscount; i++)
2176 field_println(&(c->fields[i]));
2178 printf("Methods:\n");
2179 for (i = 0; i < c->methodscount; i++) {
2180 methodinfo *m = &(c->methods[i]);
2182 if (!(m->flags & ACC_STATIC))
2183 printf("vftblindex: %d ", m->vftblindex);
2188 printf ("Virtual function table:\n");
2189 for (i = 0; i < c->vftbl->vftbllength; i++)
2190 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2192 #endif /* !defined(NDEBUG) */
2196 * These are local overrides for various environment variables in Emacs.
2197 * Please do not remove this and leave it at the end of the file, where
2198 * Emacs will automagically detect them.
2199 * ---------------------------------------------------------------------
2202 * indent-tabs-mode: t
2206 * vim:noexpandtab:sw=4:ts=4: