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;
118 classinfo *class_java_util_HashMap;
120 classinfo *arrayclass_java_lang_Object;
122 # if defined(ENABLE_ANNOTATIONS)
123 classinfo *class_sun_reflect_ConstantPool;
124 # if defined(WITH_CLASSPATH_GNU)
125 classinfo *class_sun_reflect_annotation_AnnotationParser;
130 /* pseudo classes for the typechecker */
132 classinfo *pseudo_class_Arraystub;
133 classinfo *pseudo_class_Null;
134 classinfo *pseudo_class_New;
137 /* class_set_packagename *******************************************************
139 Derive the package name from the class name and store it in the
142 An internal package name consists of the package name plus the
143 trailing '/', e.g. "java/lang/".
145 For classes in the unnamed package, the package name is set to
148 *******************************************************************************/
150 void class_set_packagename(classinfo *c)
155 p = UTF_END(c->name) - 1;
156 start = c->name->text;
158 if (c->name->text[0] == '[') {
159 /* Set packagename of arrays to the element's package. */
161 for (; *start == '['; start++);
163 /* Skip the 'L' in arrays of references. */
169 /* Search for last '/'. */
171 for (; (p > start) && (*p != '/'); --p);
173 /* If we found a '/' we set the package name plus the trailing
174 '/'. Otherwise we set the packagename to NULL. */
177 c->packagename = utf_new(start, p - start + 1);
179 c->packagename = NULL;
183 /* class_create_classinfo ******************************************************
185 Create a new classinfo struct. The class name is set to the given utf *,
186 most other fields are initialized to zero.
188 Note: classname may be NULL. In this case a not-yet-named classinfo is
189 created. The name must be filled in later and class_set_packagename
190 must be called after that.
192 *******************************************************************************/
194 classinfo *class_create_classinfo(utf *classname)
198 #if defined(ENABLE_STATISTICS)
200 size_classinfo += sizeof(classinfo);
203 /* we use a safe name for temporarily unnamed classes */
205 if (classname == NULL)
206 classname = utf_not_named_yet;
210 log_message_utf("Creating class: ", classname);
213 #if !defined(ENABLE_GC_BOEHM)
214 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
215 /*c = NEW(classinfo);
216 MZERO(c, classinfo, 1);*/
218 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
219 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
224 /* Set the header.vftbl of all loaded classes to the one of
225 java.lang.Class, so Java code can use a class as object. */
227 if (class_java_lang_Class != NULL)
228 if (class_java_lang_Class->vftbl != NULL)
229 c->object.header.vftbl = class_java_lang_Class->vftbl;
231 #if defined(ENABLE_JAVASE)
232 /* check if the class is a reference class and flag it */
234 if (classname == utf_java_lang_ref_SoftReference) {
235 c->flags |= ACC_CLASS_REFERENCE_SOFT;
237 else if (classname == utf_java_lang_ref_WeakReference) {
238 c->flags |= ACC_CLASS_REFERENCE_WEAK;
240 else if (classname == utf_java_lang_ref_PhantomReference) {
241 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
245 if (classname != utf_not_named_yet)
246 class_set_packagename(c);
248 LOCK_INIT_OBJECT_LOCK(&c->object.header);
254 /* class_postset_header_vftbl **************************************************
256 Set the header.vftbl of all classes created before java.lang.Class
257 was linked. This is necessary that Java code can use a class as
260 *******************************************************************************/
262 void class_postset_header_vftbl(void)
266 classcache_name_entry *nmen;
267 classcache_class_entry *clsen;
269 assert(class_java_lang_Class);
271 for (slot = 0; slot < hashtable_classcache.size; slot++) {
272 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
274 for (; nmen; nmen = nmen->hashlink) {
275 /* iterate over all class entries */
277 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
280 /* now set the the vftbl */
282 if (c->object.header.vftbl == NULL)
283 c->object.header.vftbl = class_java_lang_Class->vftbl;
289 /* class_define ****************************************************************
291 Calls the loader and defines a class in the VM.
293 *******************************************************************************/
295 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
302 /* check if this class has already been defined */
304 c = classcache_lookup_defined_or_initiated(cl, name);
307 exceptions_throw_linkageerror("duplicate class definition: ", c);
312 /* create a new classinfo struct */
314 c = class_create_classinfo(name);
316 #if defined(ENABLE_STATISTICS)
319 if (opt_getloadingtime)
323 /* build a classbuffer with the given data */
325 cb = NEW(classbuffer);
332 /* preset the defining classloader */
336 /* load the class from this buffer */
338 r = load_class_from_classbuffer(cb);
342 FREE(cb, classbuffer);
344 #if defined(ENABLE_STATISTICS)
347 if (opt_getloadingtime)
352 /* If return value is NULL, we had a problem and the class is
353 not loaded. Now free the allocated memory, otherwise we
354 could run into a DOS. */
361 #if defined(ENABLE_JAVASE)
362 # if defined(WITH_CLASSPATH_SUN)
363 /* Store the protection domain. */
365 c->protectiondomain = pd;
369 /* Store the newly defined class in the class cache. This call
370 also checks whether a class of the same name has already been
371 defined by the same defining loader, and if so, replaces the
372 newly created class by the one defined earlier. */
374 /* Important: The classinfo given to classcache_store must be
375 fully prepared because another thread may return
376 this pointer after the lookup at to top of this
377 function directly after the class cache lock has
380 c = classcache_store(cl, c, true);
386 /* class_load_attribute_sourcefile *********************************************
388 SourceFile_attribute {
389 u2 attribute_name_index;
394 *******************************************************************************/
396 static bool class_load_attribute_sourcefile(classbuffer *cb)
407 /* check buffer size */
409 if (!suck_check_classbuffer_size(cb, 4 + 2))
412 /* check attribute length */
414 attribute_length = suck_u4(cb);
416 if (attribute_length != 2) {
417 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
421 /* there can be no more than one SourceFile attribute */
423 if (c->sourcefile != NULL) {
424 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
430 sourcefile_index = suck_u2(cb);
431 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
433 if (sourcefile == NULL)
436 /* store sourcefile */
438 c->sourcefile = sourcefile;
444 /* class_load_attribute_enclosingmethod ****************************************
446 EnclosingMethod_attribute {
447 u2 attribute_name_index;
453 *******************************************************************************/
455 #if defined(ENABLE_JAVASE)
456 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
462 classref_or_classinfo cr;
463 constant_nameandtype *cn;
469 /* check buffer size */
471 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
474 /* check attribute length */
476 attribute_length = suck_u4(cb);
478 if (attribute_length != 4) {
479 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
483 /* there can be no more than one EnclosingMethod attribute */
485 if (c->enclosingmethod != NULL) {
486 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
490 /* get class index */
492 class_index = suck_u2(cb);
493 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
495 /* get method index */
497 method_index = suck_u2(cb);
498 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
500 /* store info in classinfo */
502 c->enclosingclass.any = cr.any;
503 c->enclosingmethod = cn;
507 #endif /* defined(ENABLE_JAVASE) */
510 /* class_load_attributes *******************************************************
512 Read attributes from ClassFile.
515 u2 attribute_name_index;
517 u1 info[attribute_length];
520 InnerClasses_attribute {
521 u2 attribute_name_index;
525 *******************************************************************************/
527 bool class_load_attributes(classbuffer *cb)
530 uint16_t attributes_count;
531 uint16_t attribute_name_index;
533 innerclassinfo *info;
534 classref_or_classinfo inner;
535 classref_or_classinfo outer;
542 /* get attributes count */
544 if (!suck_check_classbuffer_size(cb, 2))
547 attributes_count = suck_u2(cb);
549 for (i = 0; i < attributes_count; i++) {
550 /* get attribute name */
552 if (!suck_check_classbuffer_size(cb, 2))
555 attribute_name_index = suck_u2(cb);
557 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
559 if (attribute_name == NULL)
562 if (attribute_name == utf_InnerClasses) {
565 if (c->innerclass != NULL) {
566 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
570 if (!suck_check_classbuffer_size(cb, 4 + 2))
573 /* skip attribute length */
576 /* number of records */
577 c->innerclasscount = suck_u2(cb);
579 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
582 /* allocate memory for innerclass structure */
583 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
585 for (j = 0; j < c->innerclasscount; j++) {
586 /* The innerclass structure contains a class with an encoded
587 name, its defining scope, its simple name and a bitmask of
590 info = c->innerclass + j;
592 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
593 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
594 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
597 /* If the current inner-class is the currently loaded
598 class check for some special flags. */
600 if (inner.ref->name == c->name) {
601 /* If an inner-class is not a member, its
602 outer-class is NULL. */
604 if (outer.ref != NULL) {
605 c->flags |= ACC_CLASS_MEMBER;
607 /* A member class doesn't have an
608 EnclosingMethod attribute, so set the
609 enclosing-class to be the same as the
612 c->declaringclass = outer;
613 c->enclosingclass = outer;
616 /* If an inner-class is anonymous, its name is
620 c->flags |= ACC_CLASS_ANONYMOUS;
623 info->inner_class = inner;
624 info->outer_class = outer;
629 else if (attribute_name == utf_SourceFile) {
632 if (!class_load_attribute_sourcefile(cb))
635 #if defined(ENABLE_JAVASE)
636 else if (attribute_name == utf_EnclosingMethod) {
637 /* EnclosingMethod */
639 if (!class_load_attribute_enclosingmethod(cb))
642 else if (attribute_name == utf_Signature) {
645 if (!loader_load_attribute_signature(cb, &(c->signature)))
650 #if defined(ENABLE_ANNOTATIONS)
651 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
652 /* RuntimeVisibleAnnotations */
653 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
656 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
657 /* RuntimeInvisibleAnnotations */
658 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
664 /* unknown attribute */
666 if (!loader_skip_attribute_body(cb))
675 /* class_freepool **************************************************************
677 Frees all resources used by this classes Constant Pool.
679 *******************************************************************************/
681 static void class_freecpool(classinfo *c)
687 if (c->cptags && c->cpinfos) {
688 for (idx = 0; idx < c->cpcount; idx++) {
689 tag = c->cptags[idx];
690 info = c->cpinfos[idx];
694 case CONSTANT_Fieldref:
695 case CONSTANT_Methodref:
696 case CONSTANT_InterfaceMethodref:
697 FREE(info, constant_FMIref);
699 case CONSTANT_Integer:
700 FREE(info, constant_integer);
703 FREE(info, constant_float);
706 FREE(info, constant_long);
708 case CONSTANT_Double:
709 FREE(info, constant_double);
711 case CONSTANT_NameAndType:
712 FREE(info, constant_nameandtype);
720 MFREE(c->cptags, u1, c->cpcount);
723 MFREE(c->cpinfos, voidptr, c->cpcount);
727 /* class_getconstant ***********************************************************
729 Retrieves the value at position 'pos' of the constantpool of a
730 class. If the type of the value is other than 'ctype', an error is
733 *******************************************************************************/
735 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
737 /* check index and type of constantpool entry */
738 /* (pos == 0 is caught by type comparison) */
740 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
741 exceptions_throw_classformaterror(c, "Illegal constant pool index");
745 return c->cpinfos[pos];
749 /* innerclass_getconstant ******************************************************
751 Like class_getconstant, but if cptags is ZERO, null is returned.
753 *******************************************************************************/
755 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
757 /* invalid position in constantpool */
759 if (pos >= c->cpcount) {
760 exceptions_throw_classformaterror(c, "Illegal constant pool index");
764 /* constantpool entry of type 0 */
766 if (c->cptags[pos] == 0)
769 /* check type of constantpool entry */
771 if (c->cptags[pos] != ctype) {
772 exceptions_throw_classformaterror(c, "Illegal constant pool index");
776 return c->cpinfos[pos];
780 /* class_free ******************************************************************
782 Frees all resources used by the class.
784 *******************************************************************************/
786 void class_free(classinfo *c)
793 if (c->interfaces != NULL)
794 MFREE(c->interfaces, classinfo*, c->interfacescount);
797 for (i = 0; i < c->fieldscount; i++)
798 field_free(&(c->fields[i]));
799 MFREE(c->fields, fieldinfo, c->fieldscount);
803 for (i = 0; i < c->methodscount; i++)
804 method_free(&(c->methods[i]));
805 MFREE(c->methods, methodinfo, c->methodscount);
808 if ((v = c->vftbl) != NULL) {
810 mem_free(v->arraydesc,sizeof(arraydescriptor));
812 for (i = 0; i < v->interfacetablelength; i++) {
813 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
815 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
817 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
818 sizeof(methodptr*) * (v->interfacetablelength -
819 (v->interfacetablelength > 0));
820 v = (vftbl_t*) (((methodptr*) v) -
821 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
826 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
828 /* if (c->classvftbl)
829 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
835 /* get_array_class *************************************************************
837 Returns the array class with the given name for the given
838 classloader, or NULL if an exception occurred.
840 Note: This function does eager loading.
842 *******************************************************************************/
844 static classinfo *get_array_class(utf *name,classloader *initloader,
845 classloader *defloader,bool link)
849 /* lookup this class in the classcache */
850 c = classcache_lookup(initloader,name);
852 c = classcache_lookup_defined(defloader,name);
855 /* we have to create it */
856 c = class_create_classinfo(name);
857 c = load_newly_created_array(c,initloader);
863 assert(c->state & CLASS_LOADED);
864 assert(c->classloader == defloader);
866 if (link && !(c->state & CLASS_LINKED))
870 assert(!link || (c->state & CLASS_LINKED));
876 /* class_array_of **************************************************************
878 Returns an array class with the given component class. The array
879 class is dynamically created if neccessary.
881 *******************************************************************************/
883 classinfo *class_array_of(classinfo *component, bool link)
892 cl = component->classloader;
894 dumpsize = dump_size();
896 /* Assemble the array class name */
897 namelen = component->name->blength;
899 if (component->name->text[0] == '[') {
900 /* the component is itself an array */
901 namebuf = DMNEW(char, namelen + 1);
903 MCOPY(namebuf + 1, component->name->text, char, namelen);
907 /* the component is a non-array class */
908 namebuf = DMNEW(char, namelen + 3);
911 MCOPY(namebuf + 2, component->name->text, char, namelen);
912 namebuf[2 + namelen] = ';';
916 u = utf_new(namebuf, namelen);
918 c = get_array_class(u, cl, cl, link);
920 dump_release(dumpsize);
926 /* class_multiarray_of *********************************************************
928 Returns an array class with the given dimension and element class.
929 The array class is dynamically created if neccessary.
931 *******************************************************************************/
933 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
940 dumpsize = dump_size();
943 log_text("Invalid array dimension requested");
947 /* Assemble the array class name */
948 namelen = element->name->blength;
950 if (element->name->text[0] == '[') {
951 /* the element is itself an array */
952 namebuf = DMNEW(char, namelen + dim);
953 memcpy(namebuf + dim, element->name->text, namelen);
957 /* the element is a non-array class */
958 namebuf = DMNEW(char, namelen + 2 + dim);
960 memcpy(namebuf + dim + 1, element->name->text, namelen);
961 namelen += (2 + dim);
962 namebuf[namelen - 1] = ';';
964 memset(namebuf, '[', dim);
966 c = get_array_class(utf_new(namebuf, namelen),
967 element->classloader,
968 element->classloader,
971 dump_release(dumpsize);
977 /* class_lookup_classref *******************************************************
979 Looks up the constant_classref for a given classname in the classref
983 cls..............the class containing the reference
984 name.............the name of the class refered to
987 a pointer to a constant_classref, or
988 NULL if the reference was not found
990 *******************************************************************************/
992 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
994 constant_classref *ref;
995 extra_classref *xref;
1000 assert(!cls->classrefcount || cls->classrefs);
1002 /* first search the main classref table */
1003 count = cls->classrefcount;
1004 ref = cls->classrefs;
1005 for (; count; --count, ++ref)
1006 if (ref->name == name)
1009 /* next try the list of extra classrefs */
1010 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1011 if (xref->classref.name == name)
1012 return &(xref->classref);
1020 /* class_get_classref **********************************************************
1022 Returns the constant_classref for a given classname.
1025 cls..............the class containing the reference
1026 name.............the name of the class refered to
1029 a pointer to a constant_classref (never NULL)
1032 The given name is not checked for validity!
1034 *******************************************************************************/
1036 constant_classref *class_get_classref(classinfo *cls, utf *name)
1038 constant_classref *ref;
1039 extra_classref *xref;
1044 ref = class_lookup_classref(cls,name);
1048 xref = NEW(extra_classref);
1049 CLASSREF_INIT(xref->classref,cls,name);
1051 xref->next = cls->extclassrefs;
1052 cls->extclassrefs = xref;
1054 return &(xref->classref);
1058 /* class_get_self_classref *****************************************************
1060 Returns the constant_classref to the class itself.
1063 cls..............the class containing the reference
1066 a pointer to a constant_classref (never NULL)
1068 *******************************************************************************/
1070 constant_classref *class_get_self_classref(classinfo *cls)
1072 /* XXX this should be done in a faster way. Maybe always make */
1073 /* the classref of index 0 a self reference. */
1074 return class_get_classref(cls,cls->name);
1077 /* class_get_classref_multiarray_of ********************************************
1079 Returns an array type reference with the given dimension and element class
1083 dim..............the requested dimension
1084 dim must be in [1;255]. This is NOT checked!
1085 ref..............the component class reference
1088 a pointer to the class reference for the array type
1091 The referer of `ref` is used as the referer for the new classref.
1093 *******************************************************************************/
1095 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1100 constant_classref *cr;
1103 assert(dim >= 1 && dim <= 255);
1105 dumpsize = dump_size();
1107 /* Assemble the array class name */
1108 namelen = ref->name->blength;
1110 if (ref->name->text[0] == '[') {
1111 /* the element is itself an array */
1112 namebuf = DMNEW(char, namelen + dim);
1113 memcpy(namebuf + dim, ref->name->text, namelen);
1117 /* the element is a non-array class */
1118 namebuf = DMNEW(char, namelen + 2 + dim);
1120 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1121 namelen += (2 + dim);
1122 namebuf[namelen - 1] = ';';
1124 memset(namebuf, '[', dim);
1126 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1128 dump_release(dumpsize);
1134 /* class_get_classref_component_of *********************************************
1136 Returns the component classref of a given array type reference
1139 ref..............the array type reference
1142 a reference to the component class, or
1143 NULL if `ref` is not an object array type reference
1146 The referer of `ref` is used as the referer for the new classref.
1148 *******************************************************************************/
1150 constant_classref *class_get_classref_component_of(constant_classref *ref)
1157 name = ref->name->text;
1161 namelen = ref->name->blength - 1;
1166 else if (*name != '[') {
1170 return class_get_classref(ref->referer, utf_new(name, namelen));
1174 /* class_findmethod ************************************************************
1176 Searches a 'classinfo' structure for a method having the given name
1177 and descriptor. If descriptor is NULL, it is ignored.
1179 *******************************************************************************/
1181 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1186 for (i = 0; i < c->methodscount; i++) {
1187 m = &(c->methods[i]);
1189 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1197 /* class_resolvemethod *********************************************************
1199 Searches a class and it's super classes for a method.
1201 Superinterfaces are *not* searched.
1203 *******************************************************************************/
1205 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1210 m = class_findmethod(c, name, desc);
1215 /* JVM Specification bug:
1217 It is important NOT to resolve special <init> and <clinit>
1218 methods to super classes or interfaces; yet, this is not
1219 explicited in the specification. Section 5.4.3.3 should be
1220 updated appropriately. */
1222 if (name == utf_init || name == utf_clinit)
1232 /* class_resolveinterfacemethod_intern *****************************************
1234 Internally used helper function. Do not use this directly.
1236 *******************************************************************************/
1238 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1239 utf *name, utf *desc)
1244 /* try to find the method in the class */
1246 m = class_findmethod(c, name, desc);
1251 /* No method found? Try the super interfaces. */
1253 for (i = 0; i < c->interfacescount; i++) {
1254 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1260 /* no method found */
1266 /* class_resolveclassmethod ****************************************************
1268 Resolves a reference from REFERER to a method with NAME and DESC in
1271 If the method cannot be resolved the return value is NULL. If
1272 EXCEPT is true *exceptionptr is set, too.
1274 *******************************************************************************/
1276 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1277 classinfo *referer, bool throwexception)
1283 /* if (c->flags & ACC_INTERFACE) { */
1284 /* if (throwexception) */
1285 /* *exceptionptr = */
1286 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1290 /* try class c and its superclasses */
1294 m = class_resolvemethod(cls, name, desc);
1299 /* Try the super interfaces. */
1301 for (i = 0; i < c->interfacescount; i++) {
1302 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1309 exceptions_throw_nosuchmethoderror(c, name, desc);
1314 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1316 exceptions_throw_abstractmethoderror();
1321 /* XXX check access rights */
1327 /* class_resolveinterfacemethod ************************************************
1329 Resolves a reference from REFERER to a method with NAME and DESC in
1332 If the method cannot be resolved the return value is NULL. If
1333 EXCEPT is true *exceptionptr is set, too.
1335 *******************************************************************************/
1337 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1338 classinfo *referer, bool throwexception)
1342 if (!(c->flags & ACC_INTERFACE)) {
1344 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1349 mi = class_resolveinterfacemethod_intern(c, name, desc);
1354 /* try class java.lang.Object */
1356 mi = class_findmethod(class_java_lang_Object, name, desc);
1362 exceptions_throw_nosuchmethoderror(c, name, desc);
1368 /* class_findfield *************************************************************
1370 Searches for field with specified name and type in a classinfo
1371 structure. If no such field is found NULL is returned.
1373 *******************************************************************************/
1375 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1379 for (i = 0; i < c->fieldscount; i++)
1380 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1381 return &(c->fields[i]);
1383 if (c->super != NULL)
1384 return class_findfield(c->super, name, desc);
1390 /* class_findfield_approx ******************************************************
1392 Searches in 'classinfo'-structure for a field with the specified
1395 *******************************************************************************/
1397 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1401 /* get field index */
1403 i = class_findfield_index_by_name(c, name);
1405 /* field was not found, return */
1410 /* return field address */
1412 return &(c->fields[i]);
1416 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1420 for (i = 0; i < c->fieldscount; i++) {
1421 /* compare field names */
1423 if ((c->fields[i].name == name))
1427 /* field was not found, raise exception */
1429 exceptions_throw_nosuchfielderror(c, name);
1435 /****************** Function: class_resolvefield_int ***************************
1437 This is an internally used helper function. Do not use this directly.
1439 Tries to resolve a field having the given name and type.
1440 If the field cannot be resolved, NULL is returned.
1442 *******************************************************************************/
1444 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1449 /* search for field in class c */
1451 for (i = 0; i < c->fieldscount; i++) {
1452 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1453 return &(c->fields[i]);
1457 /* Try super interfaces recursively. */
1459 for (i = 0; i < c->interfacescount; i++) {
1460 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1466 /* Try super class. */
1468 if (c->super != NULL)
1469 return class_resolvefield_int(c->super, name, desc);
1477 /********************* Function: class_resolvefield ***************************
1479 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1481 If the field cannot be resolved the return value is NULL. If EXCEPT is
1482 true *exceptionptr is set, too.
1484 *******************************************************************************/
1486 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1487 classinfo *referer, bool throwexception)
1491 fi = class_resolvefield_int(c, name, desc);
1495 exceptions_throw_nosuchfielderror(c, name);
1500 /* XXX check access rights */
1506 /* class_issubclass ************************************************************
1508 Checks if sub is a descendant of super.
1510 *******************************************************************************/
1512 bool class_issubclass(classinfo *sub, classinfo *super)
1519 /* We reached java/lang/Object and did not find the requested
1525 /* We found the requested super class. */
1535 /* class_isanysubclass *********************************************************
1537 Checks a subclass relation between two classes. Implemented
1538 interfaces are interpreted as super classes.
1540 Return value: 1 ... sub is subclass of super
1543 *******************************************************************************/
1545 bool class_isanysubclass(classinfo *sub, classinfo *super)
1550 /* This is the trivial case. */
1555 /* Primitive classes are only subclasses of themselves. */
1557 if (class_is_primitive(sub) || class_is_primitive(super))
1560 /* Check for interfaces. */
1562 if (super->flags & ACC_INTERFACE) {
1563 result = (sub->vftbl->interfacetablelength > super->index) &&
1564 (sub->vftbl->interfacetable[-super->index] != NULL);
1567 /* java.lang.Object is the only super class of any
1570 if (sub->flags & ACC_INTERFACE)
1571 return (super == class_java_lang_Object);
1573 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1575 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1576 result = diffval <= (uint32_t) super->vftbl->diffval;
1578 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1585 /* class_get_componenttype *****************************************************
1587 Return the component class of the given class. If the given class
1588 is not an array, return NULL.
1590 *******************************************************************************/
1592 classinfo *class_get_componenttype(classinfo *c)
1594 classinfo *component;
1595 arraydescriptor *ad;
1597 /* XXX maybe we could find a way to do this without linking. */
1598 /* This way should be safe and easy, however. */
1600 if (!(c->state & CLASS_LINKED))
1604 ad = c->vftbl->arraydesc;
1609 if (ad->arraytype == ARRAYTYPE_OBJECT)
1610 component = ad->componentvftbl->class;
1612 component = primitive_class_get_by_type(ad->arraytype);
1618 /* class_get_declaredclasses ***************************************************
1620 Return an array of declared classes of the given class.
1622 *******************************************************************************/
1624 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1626 classref_or_classinfo inner;
1627 classref_or_classinfo outer;
1629 int declaredclasscount; /* number of declared classes */
1630 int pos; /* current declared class */
1631 java_handle_objectarray_t *oa; /* array of declared classes */
1635 declaredclasscount = 0;
1637 if (!class_is_primitive(c) && !class_is_array(c)) {
1638 /* Determine number of declared classes. */
1640 for (i = 0; i < c->innerclasscount; i++) {
1641 /* Get outer-class. If the inner-class is not a member
1642 class, the outer-class is NULL. */
1644 outer = c->innerclass[i].outer_class;
1646 if (outer.any == NULL)
1649 /* Check if outer-class is a classref or a real class and
1650 get the class name from the structure. */
1652 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1654 /* Outer class is this class. */
1656 if ((outername == c->name) &&
1657 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1658 declaredclasscount++;
1662 /* Allocate Class[] and check for OOM. */
1664 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1669 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1670 inner = c->innerclass[i].inner_class;
1671 outer = c->innerclass[i].outer_class;
1673 /* Get outer-class. If the inner-class is not a member class,
1674 the outer-class is NULL. */
1676 if (outer.any == NULL)
1679 /* Check if outer_class is a classref or a real class and get
1680 the class name from the structure. */
1682 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1684 /* Outer class is this class. */
1686 if ((outername == c->name) &&
1687 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1689 ic = resolve_classref_or_classinfo_eager(inner, false);
1694 if (!(ic->state & CLASS_LINKED))
1695 if (!link_class(ic))
1698 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1706 /* class_get_declaringclass ****************************************************
1708 If the class or interface given is a member of another class,
1709 return the declaring class. For array and primitive classes return
1712 *******************************************************************************/
1714 classinfo *class_get_declaringclass(classinfo *c)
1716 classref_or_classinfo cr;
1719 /* Get declaring class. */
1721 cr = c->declaringclass;
1726 /* Resolve the class if necessary. */
1728 if (IS_CLASSREF(cr)) {
1729 /* dc = resolve_classref_eager(cr.ref); */
1730 dc = resolve_classref_or_classinfo_eager(cr, true);
1735 /* Store the resolved class in the class structure. */
1746 /* class_get_enclosingclass ****************************************************
1748 Return the enclosing class for the given class.
1750 *******************************************************************************/
1752 classinfo *class_get_enclosingclass(classinfo *c)
1754 classref_or_classinfo cr;
1757 /* Get enclosing class. */
1759 cr = c->enclosingclass;
1764 /* Resolve the class if necessary. */
1766 if (IS_CLASSREF(cr)) {
1767 /* ec = resolve_classref_eager(cr.ref); */
1768 ec = resolve_classref_or_classinfo_eager(cr, true);
1773 /* Store the resolved class in the class structure. */
1784 /* class_get_interfaces ********************************************************
1786 Return an array of interfaces of the given class.
1788 *******************************************************************************/
1790 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1793 java_handle_objectarray_t *oa;
1796 if (!(c->state & CLASS_LINKED))
1800 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1805 for (i = 0; i < c->interfacescount; i++) {
1806 ic = c->interfaces[i];
1808 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1815 /* class_get_annotations *******************************************************
1817 Get the unparsed declared annotations in a byte array
1821 c........the class of which the annotations should be returned
1824 The unparsed declared annotations in a byte array
1825 (or NULL if there aren't any).
1827 *******************************************************************************/
1829 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1831 #if defined(ENABLE_ANNOTATIONS)
1832 java_handle_t *annotations; /* unparsed annotations */
1834 LLNI_classinfo_field_get(c, annotations, annotations);
1836 return (java_handle_bytearray_t*)annotations;
1843 /* class_get_modifiers *********************************************************
1845 Get the modifier flags of the given class.
1848 c....the class of which the modifier flags should be returned
1849 ignoreInnerClassesAttrib
1853 *******************************************************************************/
1855 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
1857 classref_or_classinfo inner;
1858 classref_or_classinfo outer;
1862 if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
1863 /* search for passed class as inner class */
1865 for (i = 0; i < c->innerclasscount; i++) {
1866 inner = c->innerclass[i].inner_class;
1867 outer = c->innerclass[i].outer_class;
1869 /* Check if inner is a classref or a real class and get
1870 the name of the structure */
1872 innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
1874 /* innerclass is this class */
1876 if (innername == c->name) {
1877 /* has the class actually an outer class? */
1880 /* return flags got from the outer class file */
1881 return c->innerclass[i].flags & ACC_CLASS_REFLECT_MASK;
1883 return c->flags & ACC_CLASS_REFLECT_MASK;
1888 /* passed class is no inner class or it was not requested */
1890 return c->flags & ACC_CLASS_REFLECT_MASK;
1894 /* class_get_signature *********************************************************
1896 Return the signature of the given class. For array and primitive
1897 classes return NULL.
1899 *******************************************************************************/
1901 #if defined(ENABLE_JAVASE)
1902 utf *class_get_signature(classinfo *c)
1904 /* For array and primitive classes return NULL. */
1906 if (class_is_array(c) || class_is_primitive(c))
1909 return c->signature;
1914 /* class_printflags ************************************************************
1916 Prints flags of a class.
1918 *******************************************************************************/
1920 #if !defined(NDEBUG)
1921 void class_printflags(classinfo *c)
1928 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1929 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1930 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1931 if (c->flags & ACC_STATIC) printf(" STATIC");
1932 if (c->flags & ACC_FINAL) printf(" FINAL");
1933 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1934 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1935 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1936 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1937 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1938 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1943 /* class_print *****************************************************************
1945 Prints classname plus flags.
1947 *******************************************************************************/
1949 #if !defined(NDEBUG)
1950 void class_print(classinfo *c)
1957 utf_display_printable_ascii(c->name);
1958 class_printflags(c);
1963 /* class_classref_print ********************************************************
1965 Prints classname plus referer class.
1967 *******************************************************************************/
1969 #if !defined(NDEBUG)
1970 void class_classref_print(constant_classref *cr)
1977 utf_display_printable_ascii(cr->name);
1980 class_print(cr->referer);
1988 /* class_println ***************************************************************
1990 Prints classname plus flags and new line.
1992 *******************************************************************************/
1994 #if !defined(NDEBUG)
1995 void class_println(classinfo *c)
2003 /* class_classref_println ******************************************************
2005 Prints classname plus referer class and new line.
2007 *******************************************************************************/
2009 #if !defined(NDEBUG)
2010 void class_classref_println(constant_classref *cr)
2012 class_classref_print(cr);
2018 /* class_classref_or_classinfo_print *******************************************
2020 Prints classname plus referer class.
2022 *******************************************************************************/
2024 #if !defined(NDEBUG)
2025 void class_classref_or_classinfo_print(classref_or_classinfo c)
2027 if (c.any == NULL) {
2028 printf("(classref_or_classinfo) NULL");
2032 class_classref_print(c.ref);
2039 /* class_classref_or_classinfo_println *****************************************
2041 Prints classname plus referer class and a newline.
2043 *******************************************************************************/
2045 #if !defined(NDEBUG)
2046 void class_classref_or_classinfo_println(classref_or_classinfo c)
2048 class_classref_or_classinfo_print(c);
2054 /* class_showconstantpool ******************************************************
2056 Dump the constant pool of the given class to stdout.
2058 *******************************************************************************/
2060 #if !defined(NDEBUG)
2061 void class_showconstantpool (classinfo *c)
2066 printf ("---- dump of constant pool ----\n");
2068 for (i=0; i<c->cpcount; i++) {
2069 printf ("#%d: ", (int) i);
2071 e = c -> cpinfos [i];
2074 switch (c -> cptags [i]) {
2075 case CONSTANT_Class:
2076 printf ("Classreference -> ");
2077 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2079 case CONSTANT_Fieldref:
2080 printf ("Fieldref -> ");
2081 field_fieldref_print((constant_FMIref *) e);
2083 case CONSTANT_Methodref:
2084 printf ("Methodref -> ");
2085 method_methodref_print((constant_FMIref *) e);
2087 case CONSTANT_InterfaceMethodref:
2088 printf ("InterfaceMethod -> ");
2089 method_methodref_print((constant_FMIref *) e);
2091 case CONSTANT_String:
2092 printf ("String -> ");
2093 utf_display_printable_ascii (e);
2095 case CONSTANT_Integer:
2096 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2098 case CONSTANT_Float:
2099 printf ("Float -> %f", ((constant_float*)e) -> value);
2101 case CONSTANT_Double:
2102 printf ("Double -> %f", ((constant_double*)e) -> value);
2106 u8 v = ((constant_long*)e) -> value;
2108 printf ("Long -> %ld", (long int) v);
2110 printf ("Long -> HI: %ld, LO: %ld\n",
2111 (long int) v.high, (long int) v.low);
2115 case CONSTANT_NameAndType:
2117 constant_nameandtype *cnt = e;
2118 printf ("NameAndType: ");
2119 utf_display_printable_ascii (cnt->name);
2121 utf_display_printable_ascii (cnt->descriptor);
2125 printf ("Utf8 -> ");
2126 utf_display_printable_ascii (e);
2129 log_text("Invalid type of ConstantPool-Entry");
2137 #endif /* !defined(NDEBUG) */
2140 /* class_showmethods ***********************************************************
2142 Dump info about the fields and methods of the given class to stdout.
2144 *******************************************************************************/
2146 #if !defined(NDEBUG)
2147 void class_showmethods (classinfo *c)
2151 printf("--------- Fields and Methods ----------------\n");
2153 class_printflags(c);
2157 utf_display_printable_ascii(c->name);
2162 utf_display_printable_ascii(c->super->name);
2166 printf("Index: %d\n", c->index);
2168 printf("Interfaces:\n");
2169 for (i = 0; i < c->interfacescount; i++) {
2171 utf_display_printable_ascii(c->interfaces[i]->name);
2172 printf (" (%d)\n", c->interfaces[i]->index);
2175 printf("Fields:\n");
2176 for (i = 0; i < c->fieldscount; i++)
2177 field_println(&(c->fields[i]));
2179 printf("Methods:\n");
2180 for (i = 0; i < c->methodscount; i++) {
2181 methodinfo *m = &(c->methods[i]);
2183 if (!(m->flags & ACC_STATIC))
2184 printf("vftblindex: %d ", m->vftblindex);
2189 printf ("Virtual function table:\n");
2190 for (i = 0; i < c->vftbl->vftbllength; i++)
2191 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2193 #endif /* !defined(NDEBUG) */
2197 * These are local overrides for various environment variables in Emacs.
2198 * Please do not remove this and leave it at the end of the file, where
2199 * Emacs will automagically detect them.
2200 * ---------------------------------------------------------------------
2203 * indent-tabs-mode: t
2207 * vim:noexpandtab:sw=4:ts=4: