1 /* src/vmcore/class.c - class related functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 #include "mm/memory.h"
39 #include "native/llni.h"
41 #include "threads/lock-common.h"
43 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/resolve.h"
51 #include "vm/jit/asmpart.h"
53 #include "vmcore/class.h"
54 #include "vmcore/classcache.h"
55 #include "vmcore/linker.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/statistics.h"
63 #include "vmcore/suck.h"
64 #include "vmcore/utf8.h"
67 /* global variables ***********************************************************/
69 /* frequently used classes ****************************************************/
71 /* Important system classes. */
73 classinfo *class_java_lang_Object;
74 classinfo *class_java_lang_Class;
75 classinfo *class_java_lang_ClassLoader;
76 classinfo *class_java_lang_Cloneable;
77 classinfo *class_java_lang_SecurityManager;
78 classinfo *class_java_lang_String;
79 classinfo *class_java_lang_System;
80 classinfo *class_java_lang_Thread;
81 classinfo *class_java_lang_ThreadGroup;
82 classinfo *class_java_lang_Throwable;
83 classinfo *class_java_io_Serializable;
85 #if defined(WITH_CLASSPATH_GNU)
86 classinfo *class_java_lang_VMSystem;
87 classinfo *class_java_lang_VMThread;
88 classinfo *class_java_lang_VMThrowable;
91 /* Important system exceptions. */
93 classinfo *class_java_lang_Exception;
94 classinfo *class_java_lang_ClassNotFoundException;
95 classinfo *class_java_lang_RuntimeException;
97 #if defined(WITH_CLASSPATH_SUN)
98 classinfo *class_sun_reflect_MagicAccessorImpl;
101 #if defined(ENABLE_JAVASE)
102 classinfo *class_java_lang_Void;
104 classinfo *class_java_lang_Boolean;
105 classinfo *class_java_lang_Byte;
106 classinfo *class_java_lang_Character;
107 classinfo *class_java_lang_Short;
108 classinfo *class_java_lang_Integer;
109 classinfo *class_java_lang_Long;
110 classinfo *class_java_lang_Float;
111 classinfo *class_java_lang_Double;
113 /* some classes which may be used more often */
115 #if defined(ENABLE_JAVASE)
116 classinfo *class_java_lang_StackTraceElement;
117 classinfo *class_java_lang_reflect_Constructor;
118 classinfo *class_java_lang_reflect_Field;
119 classinfo *class_java_lang_reflect_Method;
120 classinfo *class_java_security_PrivilegedAction;
121 classinfo *class_java_util_Vector;
122 classinfo *class_java_util_HashMap;
124 # if defined(WITH_CLASSPATH_GNU)
125 classinfo *class_java_lang_reflect_VMConstructor;
126 classinfo *class_java_lang_reflect_VMField;
127 classinfo *class_java_lang_reflect_VMMethod;
130 classinfo *arrayclass_java_lang_Object;
132 # if defined(ENABLE_ANNOTATIONS)
133 classinfo *class_sun_reflect_ConstantPool;
134 # if defined(WITH_CLASSPATH_GNU)
135 classinfo *class_sun_reflect_annotation_AnnotationParser;
140 /* pseudo classes for the typechecker */
142 classinfo *pseudo_class_Arraystub;
143 classinfo *pseudo_class_Null;
144 classinfo *pseudo_class_New;
147 /* class_set_packagename *******************************************************
149 Derive the package name from the class name and store it in the
152 An internal package name consists of the package name plus the
153 trailing '/', e.g. "java/lang/".
155 For classes in the unnamed package, the package name is set to
158 *******************************************************************************/
160 void class_set_packagename(classinfo *c)
165 p = UTF_END(c->name) - 1;
166 start = c->name->text;
168 if (c->name->text[0] == '[') {
169 /* Set packagename of arrays to the element's package. */
171 for (; *start == '['; start++);
173 /* Skip the 'L' in arrays of references. */
179 /* Search for last '/'. */
181 for (; (p > start) && (*p != '/'); --p);
183 /* If we found a '/' we set the package name plus the trailing
184 '/'. Otherwise we set the packagename to NULL. */
187 c->packagename = utf_new(start, p - start + 1);
189 c->packagename = NULL;
193 /* class_create_classinfo ******************************************************
195 Create a new classinfo struct. The class name is set to the given utf *,
196 most other fields are initialized to zero.
198 Note: classname may be NULL. In this case a not-yet-named classinfo is
199 created. The name must be filled in later and class_set_packagename
200 must be called after that.
202 *******************************************************************************/
204 classinfo *class_create_classinfo(utf *classname)
208 #if defined(ENABLE_STATISTICS)
210 size_classinfo += sizeof(classinfo);
213 /* we use a safe name for temporarily unnamed classes */
215 if (classname == NULL)
216 classname = utf_not_named_yet;
220 log_message_utf("Creating class: ", classname);
223 #if !defined(ENABLE_GC_BOEHM)
224 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
225 /*c = NEW(classinfo);
226 MZERO(c, classinfo, 1);*/
228 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
229 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
234 /* Set the header.vftbl of all loaded classes to the one of
235 java.lang.Class, so Java code can use a class as object. */
237 if (class_java_lang_Class != NULL)
238 if (class_java_lang_Class->vftbl != NULL)
239 c->object.header.vftbl = class_java_lang_Class->vftbl;
241 #if defined(ENABLE_JAVASE)
242 /* check if the class is a reference class and flag it */
244 if (classname == utf_java_lang_ref_SoftReference) {
245 c->flags |= ACC_CLASS_REFERENCE_SOFT;
247 else if (classname == utf_java_lang_ref_WeakReference) {
248 c->flags |= ACC_CLASS_REFERENCE_WEAK;
250 else if (classname == utf_java_lang_ref_PhantomReference) {
251 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
255 if (classname != utf_not_named_yet)
256 class_set_packagename(c);
258 LOCK_INIT_OBJECT_LOCK(&c->object.header);
264 /* class_postset_header_vftbl **************************************************
266 Set the header.vftbl of all classes created before java.lang.Class
267 was linked. This is necessary that Java code can use a class as
270 *******************************************************************************/
272 void class_postset_header_vftbl(void)
276 classcache_name_entry *nmen;
277 classcache_class_entry *clsen;
279 assert(class_java_lang_Class);
281 for (slot = 0; slot < hashtable_classcache.size; slot++) {
282 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
284 for (; nmen; nmen = nmen->hashlink) {
285 /* iterate over all class entries */
287 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
290 /* now set the the vftbl */
292 if (c->object.header.vftbl == NULL)
293 c->object.header.vftbl = class_java_lang_Class->vftbl;
299 /* class_define ****************************************************************
301 Calls the loader and defines a class in the VM.
303 *******************************************************************************/
305 classinfo *class_define(utf *name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd)
312 /* check if this class has already been defined */
314 c = classcache_lookup_defined_or_initiated(cl, name);
317 exceptions_throw_linkageerror("duplicate class definition: ", c);
322 /* create a new classinfo struct */
324 c = class_create_classinfo(name);
326 #if defined(ENABLE_STATISTICS)
329 if (opt_getloadingtime)
333 /* build a classbuffer with the given data */
335 cb = NEW(classbuffer);
342 /* preset the defining classloader */
346 /* load the class from this buffer */
348 r = load_class_from_classbuffer(cb);
352 FREE(cb, classbuffer);
354 #if defined(ENABLE_STATISTICS)
357 if (opt_getloadingtime)
362 /* If return value is NULL, we had a problem and the class is
363 not loaded. Now free the allocated memory, otherwise we
364 could run into a DOS. */
371 #if defined(ENABLE_JAVASE)
372 # if defined(WITH_CLASSPATH_SUN)
373 /* Store the protection domain. */
375 c->protectiondomain = pd;
379 /* Store the newly defined class in the class cache. This call
380 also checks whether a class of the same name has already been
381 defined by the same defining loader, and if so, replaces the
382 newly created class by the one defined earlier. */
384 /* Important: The classinfo given to classcache_store must be
385 fully prepared because another thread may return
386 this pointer after the lookup at to top of this
387 function directly after the class cache lock has
390 c = classcache_store(cl, c, true);
396 /* class_load_attribute_sourcefile *********************************************
398 SourceFile_attribute {
399 u2 attribute_name_index;
404 *******************************************************************************/
406 static bool class_load_attribute_sourcefile(classbuffer *cb)
417 /* check buffer size */
419 if (!suck_check_classbuffer_size(cb, 4 + 2))
422 /* check attribute length */
424 attribute_length = suck_u4(cb);
426 if (attribute_length != 2) {
427 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
431 /* there can be no more than one SourceFile attribute */
433 if (c->sourcefile != NULL) {
434 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
440 sourcefile_index = suck_u2(cb);
441 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
443 if (sourcefile == NULL)
446 /* store sourcefile */
448 c->sourcefile = sourcefile;
454 /* class_load_attribute_enclosingmethod ****************************************
456 EnclosingMethod_attribute {
457 u2 attribute_name_index;
463 *******************************************************************************/
465 #if defined(ENABLE_JAVASE)
466 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
472 classref_or_classinfo cr;
473 constant_nameandtype *cn;
479 /* check buffer size */
481 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
484 /* check attribute length */
486 attribute_length = suck_u4(cb);
488 if (attribute_length != 4) {
489 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
493 /* there can be no more than one EnclosingMethod attribute */
495 if (c->enclosingmethod != NULL) {
496 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
500 /* get class index */
502 class_index = suck_u2(cb);
503 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
505 /* get method index */
507 method_index = suck_u2(cb);
508 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
510 /* store info in classinfo */
512 c->enclosingclass.any = cr.any;
513 c->enclosingmethod = cn;
517 #endif /* defined(ENABLE_JAVASE) */
520 /* class_load_attributes *******************************************************
522 Read attributes from ClassFile.
525 u2 attribute_name_index;
527 u1 info[attribute_length];
530 InnerClasses_attribute {
531 u2 attribute_name_index;
535 *******************************************************************************/
537 bool class_load_attributes(classbuffer *cb)
540 uint16_t attributes_count;
541 uint16_t attribute_name_index;
543 innerclassinfo *info;
544 classref_or_classinfo inner;
545 classref_or_classinfo outer;
552 /* get attributes count */
554 if (!suck_check_classbuffer_size(cb, 2))
557 attributes_count = suck_u2(cb);
559 for (i = 0; i < attributes_count; i++) {
560 /* get attribute name */
562 if (!suck_check_classbuffer_size(cb, 2))
565 attribute_name_index = suck_u2(cb);
567 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
569 if (attribute_name == NULL)
572 if (attribute_name == utf_InnerClasses) {
575 if (c->innerclass != NULL) {
576 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
580 if (!suck_check_classbuffer_size(cb, 4 + 2))
583 /* skip attribute length */
586 /* number of records */
587 c->innerclasscount = suck_u2(cb);
589 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
592 /* allocate memory for innerclass structure */
593 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
595 for (j = 0; j < c->innerclasscount; j++) {
596 /* The innerclass structure contains a class with an encoded
597 name, its defining scope, its simple name and a bitmask of
600 info = c->innerclass + j;
602 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
603 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
604 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
607 /* If the current inner-class is the currently loaded
608 class check for some special flags. */
610 if (inner.ref->name == c->name) {
611 /* If an inner-class is not a member, its
612 outer-class is NULL. */
614 if (outer.ref != NULL) {
615 c->flags |= ACC_CLASS_MEMBER;
617 /* A member class doesn't have an
618 EnclosingMethod attribute, so set the
619 enclosing-class to be the same as the
622 c->declaringclass = outer;
623 c->enclosingclass = outer;
626 /* If an inner-class is anonymous, its name is
630 c->flags |= ACC_CLASS_ANONYMOUS;
633 info->inner_class = inner;
634 info->outer_class = outer;
639 else if (attribute_name == utf_SourceFile) {
642 if (!class_load_attribute_sourcefile(cb))
645 #if defined(ENABLE_JAVASE)
646 else if (attribute_name == utf_EnclosingMethod) {
647 /* EnclosingMethod */
649 if (!class_load_attribute_enclosingmethod(cb))
652 else if (attribute_name == utf_Signature) {
655 if (!loader_load_attribute_signature(cb, &(c->signature)))
660 #if defined(ENABLE_ANNOTATIONS)
661 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
662 /* RuntimeVisibleAnnotations */
663 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
666 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
667 /* RuntimeInvisibleAnnotations */
668 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
674 /* unknown attribute */
676 if (!loader_skip_attribute_body(cb))
685 /* class_freepool **************************************************************
687 Frees all resources used by this classes Constant Pool.
689 *******************************************************************************/
691 static void class_freecpool(classinfo *c)
697 if (c->cptags && c->cpinfos) {
698 for (idx = 0; idx < c->cpcount; idx++) {
699 tag = c->cptags[idx];
700 info = c->cpinfos[idx];
704 case CONSTANT_Fieldref:
705 case CONSTANT_Methodref:
706 case CONSTANT_InterfaceMethodref:
707 FREE(info, constant_FMIref);
709 case CONSTANT_Integer:
710 FREE(info, constant_integer);
713 FREE(info, constant_float);
716 FREE(info, constant_long);
718 case CONSTANT_Double:
719 FREE(info, constant_double);
721 case CONSTANT_NameAndType:
722 FREE(info, constant_nameandtype);
730 MFREE(c->cptags, u1, c->cpcount);
733 MFREE(c->cpinfos, voidptr, c->cpcount);
737 /* class_getconstant ***********************************************************
739 Retrieves the value at position 'pos' of the constantpool of a
740 class. If the type of the value is other than 'ctype', an error is
743 *******************************************************************************/
745 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
747 /* check index and type of constantpool entry */
748 /* (pos == 0 is caught by type comparison) */
750 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
751 exceptions_throw_classformaterror(c, "Illegal constant pool index");
755 return c->cpinfos[pos];
759 /* innerclass_getconstant ******************************************************
761 Like class_getconstant, but if cptags is ZERO, null is returned.
763 *******************************************************************************/
765 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
767 /* invalid position in constantpool */
769 if (pos >= c->cpcount) {
770 exceptions_throw_classformaterror(c, "Illegal constant pool index");
774 /* constantpool entry of type 0 */
776 if (c->cptags[pos] == 0)
779 /* check type of constantpool entry */
781 if (c->cptags[pos] != ctype) {
782 exceptions_throw_classformaterror(c, "Illegal constant pool index");
786 return c->cpinfos[pos];
790 /* class_free ******************************************************************
792 Frees all resources used by the class.
794 *******************************************************************************/
796 void class_free(classinfo *c)
803 if (c->interfaces != NULL)
804 MFREE(c->interfaces, classinfo*, c->interfacescount);
807 for (i = 0; i < c->fieldscount; i++)
808 field_free(&(c->fields[i]));
809 MFREE(c->fields, fieldinfo, c->fieldscount);
813 for (i = 0; i < c->methodscount; i++)
814 method_free(&(c->methods[i]));
815 MFREE(c->methods, methodinfo, c->methodscount);
818 if ((v = c->vftbl) != NULL) {
820 mem_free(v->arraydesc,sizeof(arraydescriptor));
822 for (i = 0; i < v->interfacetablelength; i++) {
823 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
825 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
827 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
828 sizeof(methodptr*) * (v->interfacetablelength -
829 (v->interfacetablelength > 0));
830 v = (vftbl_t*) (((methodptr*) v) -
831 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
836 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
838 /* if (c->classvftbl)
839 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
845 /* get_array_class *************************************************************
847 Returns the array class with the given name for the given
848 classloader, or NULL if an exception occurred.
850 Note: This function does eager loading.
852 *******************************************************************************/
854 static classinfo *get_array_class(utf *name,classloader_t *initloader,
855 classloader_t *defloader,bool link)
859 /* lookup this class in the classcache */
860 c = classcache_lookup(initloader,name);
862 c = classcache_lookup_defined(defloader,name);
865 /* we have to create it */
866 c = class_create_classinfo(name);
867 c = load_newly_created_array(c,initloader);
873 assert(c->state & CLASS_LOADED);
874 assert(c->classloader == defloader);
876 if (link && !(c->state & CLASS_LINKED))
880 assert(!link || (c->state & CLASS_LINKED));
886 /* class_array_of **************************************************************
888 Returns an array class with the given component class. The array
889 class is dynamically created if neccessary.
891 *******************************************************************************/
893 classinfo *class_array_of(classinfo *component, bool link)
902 cl = component->classloader;
906 /* Assemble the array class name */
907 namelen = component->name->blength;
909 if (component->name->text[0] == '[') {
910 /* the component is itself an array */
911 namebuf = DMNEW(char, namelen + 1);
913 MCOPY(namebuf + 1, component->name->text, char, namelen);
917 /* the component is a non-array class */
918 namebuf = DMNEW(char, namelen + 3);
921 MCOPY(namebuf + 2, component->name->text, char, namelen);
922 namebuf[2 + namelen] = ';';
926 u = utf_new(namebuf, namelen);
928 c = get_array_class(u, cl, cl, link);
936 /* class_multiarray_of *********************************************************
938 Returns an array class with the given dimension and element class.
939 The array class is dynamically created if neccessary.
941 *******************************************************************************/
943 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
953 log_text("Invalid array dimension requested");
957 /* Assemble the array class name */
958 namelen = element->name->blength;
960 if (element->name->text[0] == '[') {
961 /* the element is itself an array */
962 namebuf = DMNEW(char, namelen + dim);
963 memcpy(namebuf + dim, element->name->text, namelen);
967 /* the element is a non-array class */
968 namebuf = DMNEW(char, namelen + 2 + dim);
970 memcpy(namebuf + dim + 1, element->name->text, namelen);
971 namelen += (2 + dim);
972 namebuf[namelen - 1] = ';';
974 memset(namebuf, '[', dim);
976 c = get_array_class(utf_new(namebuf, namelen),
977 element->classloader,
978 element->classloader,
987 /* class_lookup_classref *******************************************************
989 Looks up the constant_classref for a given classname in the classref
993 cls..............the class containing the reference
994 name.............the name of the class refered to
997 a pointer to a constant_classref, or
998 NULL if the reference was not found
1000 *******************************************************************************/
1002 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1004 constant_classref *ref;
1005 extra_classref *xref;
1010 assert(!cls->classrefcount || cls->classrefs);
1012 /* first search the main classref table */
1013 count = cls->classrefcount;
1014 ref = cls->classrefs;
1015 for (; count; --count, ++ref)
1016 if (ref->name == name)
1019 /* next try the list of extra classrefs */
1020 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1021 if (xref->classref.name == name)
1022 return &(xref->classref);
1030 /* class_get_classref **********************************************************
1032 Returns the constant_classref for a given classname.
1035 cls..............the class containing the reference
1036 name.............the name of the class refered to
1039 a pointer to a constant_classref (never NULL)
1042 The given name is not checked for validity!
1044 *******************************************************************************/
1046 constant_classref *class_get_classref(classinfo *cls, utf *name)
1048 constant_classref *ref;
1049 extra_classref *xref;
1054 ref = class_lookup_classref(cls,name);
1058 xref = NEW(extra_classref);
1059 CLASSREF_INIT(xref->classref,cls,name);
1061 xref->next = cls->extclassrefs;
1062 cls->extclassrefs = xref;
1064 return &(xref->classref);
1068 /* class_get_self_classref *****************************************************
1070 Returns the constant_classref to the class itself.
1073 cls..............the class containing the reference
1076 a pointer to a constant_classref (never NULL)
1078 *******************************************************************************/
1080 constant_classref *class_get_self_classref(classinfo *cls)
1082 /* XXX this should be done in a faster way. Maybe always make */
1083 /* the classref of index 0 a self reference. */
1084 return class_get_classref(cls,cls->name);
1087 /* class_get_classref_multiarray_of ********************************************
1089 Returns an array type reference with the given dimension and element class
1093 dim..............the requested dimension
1094 dim must be in [1;255]. This is NOT checked!
1095 ref..............the component class reference
1098 a pointer to the class reference for the array type
1101 The referer of `ref` is used as the referer for the new classref.
1103 *******************************************************************************/
1105 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1109 constant_classref *cr;
1113 assert(dim >= 1 && dim <= 255);
1117 /* Assemble the array class name */
1118 namelen = ref->name->blength;
1120 if (ref->name->text[0] == '[') {
1121 /* the element is itself an array */
1122 namebuf = DMNEW(char, namelen + dim);
1123 memcpy(namebuf + dim, ref->name->text, namelen);
1127 /* the element is a non-array class */
1128 namebuf = DMNEW(char, namelen + 2 + dim);
1130 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1131 namelen += (2 + dim);
1132 namebuf[namelen - 1] = ';';
1134 memset(namebuf, '[', dim);
1136 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1144 /* class_get_classref_component_of *********************************************
1146 Returns the component classref of a given array type reference
1149 ref..............the array type reference
1152 a reference to the component class, or
1153 NULL if `ref` is not an object array type reference
1156 The referer of `ref` is used as the referer for the new classref.
1158 *******************************************************************************/
1160 constant_classref *class_get_classref_component_of(constant_classref *ref)
1167 name = ref->name->text;
1171 namelen = ref->name->blength - 1;
1176 else if (*name != '[') {
1180 return class_get_classref(ref->referer, utf_new(name, namelen));
1184 /* class_findmethod ************************************************************
1186 Searches a 'classinfo' structure for a method having the given name
1187 and descriptor. If descriptor is NULL, it is ignored.
1189 *******************************************************************************/
1191 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1196 for (i = 0; i < c->methodscount; i++) {
1197 m = &(c->methods[i]);
1199 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1207 /* class_resolvemethod *********************************************************
1209 Searches a class and it's super classes for a method.
1211 Superinterfaces are *not* searched.
1213 *******************************************************************************/
1215 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1220 m = class_findmethod(c, name, desc);
1225 /* JVM Specification bug:
1227 It is important NOT to resolve special <init> and <clinit>
1228 methods to super classes or interfaces; yet, this is not
1229 explicited in the specification. Section 5.4.3.3 should be
1230 updated appropriately. */
1232 if (name == utf_init || name == utf_clinit)
1242 /* class_resolveinterfacemethod_intern *****************************************
1244 Internally used helper function. Do not use this directly.
1246 *******************************************************************************/
1248 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1249 utf *name, utf *desc)
1254 /* try to find the method in the class */
1256 m = class_findmethod(c, name, desc);
1261 /* No method found? Try the super interfaces. */
1263 for (i = 0; i < c->interfacescount; i++) {
1264 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1270 /* no method found */
1276 /* class_resolveclassmethod ****************************************************
1278 Resolves a reference from REFERER to a method with NAME and DESC in
1281 If the method cannot be resolved the return value is NULL. If
1282 EXCEPT is true *exceptionptr is set, too.
1284 *******************************************************************************/
1286 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1287 classinfo *referer, bool throwexception)
1293 /* if (c->flags & ACC_INTERFACE) { */
1294 /* if (throwexception) */
1295 /* *exceptionptr = */
1296 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1300 /* try class c and its superclasses */
1304 m = class_resolvemethod(cls, name, desc);
1309 /* Try the super interfaces. */
1311 for (i = 0; i < c->interfacescount; i++) {
1312 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1319 exceptions_throw_nosuchmethoderror(c, name, desc);
1324 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1326 exceptions_throw_abstractmethoderror();
1331 /* XXX check access rights */
1337 /* class_resolveinterfacemethod ************************************************
1339 Resolves a reference from REFERER to a method with NAME and DESC in
1342 If the method cannot be resolved the return value is NULL. If
1343 EXCEPT is true *exceptionptr is set, too.
1345 *******************************************************************************/
1347 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1348 classinfo *referer, bool throwexception)
1352 if (!(c->flags & ACC_INTERFACE)) {
1354 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1359 mi = class_resolveinterfacemethod_intern(c, name, desc);
1364 /* try class java.lang.Object */
1366 mi = class_findmethod(class_java_lang_Object, name, desc);
1372 exceptions_throw_nosuchmethoderror(c, name, desc);
1378 /* class_findfield *************************************************************
1380 Searches for field with specified name and type in a classinfo
1381 structure. If no such field is found NULL is returned.
1383 *******************************************************************************/
1385 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1389 for (i = 0; i < c->fieldscount; i++)
1390 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1391 return &(c->fields[i]);
1393 if (c->super != NULL)
1394 return class_findfield(c->super, name, desc);
1400 /* class_findfield_approx ******************************************************
1402 Searches in 'classinfo'-structure for a field with the specified
1405 *******************************************************************************/
1407 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1411 /* get field index */
1413 i = class_findfield_index_by_name(c, name);
1415 /* field was not found, return */
1420 /* return field address */
1422 return &(c->fields[i]);
1426 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1430 for (i = 0; i < c->fieldscount; i++) {
1431 /* compare field names */
1433 if ((c->fields[i].name == name))
1437 /* field was not found, raise exception */
1439 exceptions_throw_nosuchfielderror(c, name);
1445 /****************** Function: class_resolvefield_int ***************************
1447 This is an internally used helper function. Do not use this directly.
1449 Tries to resolve a field having the given name and type.
1450 If the field cannot be resolved, NULL is returned.
1452 *******************************************************************************/
1454 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1459 /* search for field in class c */
1461 for (i = 0; i < c->fieldscount; i++) {
1462 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1463 return &(c->fields[i]);
1467 /* Try super interfaces recursively. */
1469 for (i = 0; i < c->interfacescount; i++) {
1470 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1476 /* Try super class. */
1478 if (c->super != NULL)
1479 return class_resolvefield_int(c->super, name, desc);
1487 /********************* Function: class_resolvefield ***************************
1489 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1491 If the field cannot be resolved the return value is NULL. If EXCEPT is
1492 true *exceptionptr is set, too.
1494 *******************************************************************************/
1496 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1497 classinfo *referer, bool throwexception)
1501 fi = class_resolvefield_int(c, name, desc);
1505 exceptions_throw_nosuchfielderror(c, name);
1510 /* XXX check access rights */
1516 /* class_issubclass ************************************************************
1518 Checks if sub is a descendant of super.
1520 *******************************************************************************/
1522 bool class_issubclass(classinfo *sub, classinfo *super)
1529 /* We reached java/lang/Object and did not find the requested
1535 /* We found the requested super class. */
1545 /* class_isanysubclass *********************************************************
1547 Checks a subclass relation between two classes. Implemented
1548 interfaces are interpreted as super classes.
1550 Return value: 1 ... sub is subclass of super
1553 *******************************************************************************/
1555 bool class_isanysubclass(classinfo *sub, classinfo *super)
1560 /* This is the trivial case. */
1565 /* Primitive classes are only subclasses of themselves. */
1567 if (class_is_primitive(sub) || class_is_primitive(super))
1570 /* Check for interfaces. */
1572 if (super->flags & ACC_INTERFACE) {
1573 result = (sub->vftbl->interfacetablelength > super->index) &&
1574 (sub->vftbl->interfacetable[-super->index] != NULL);
1577 /* java.lang.Object is the only super class of any
1580 if (sub->flags & ACC_INTERFACE)
1581 return (super == class_java_lang_Object);
1583 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1585 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1586 result = diffval <= (uint32_t) super->vftbl->diffval;
1588 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1595 /* class_get_componenttype *****************************************************
1597 Return the component class of the given class. If the given class
1598 is not an array, return NULL.
1600 *******************************************************************************/
1602 classinfo *class_get_componenttype(classinfo *c)
1604 classinfo *component;
1605 arraydescriptor *ad;
1607 /* XXX maybe we could find a way to do this without linking. */
1608 /* This way should be safe and easy, however. */
1610 if (!(c->state & CLASS_LINKED))
1614 ad = c->vftbl->arraydesc;
1619 if (ad->arraytype == ARRAYTYPE_OBJECT)
1620 component = ad->componentvftbl->clazz;
1622 component = primitive_class_get_by_type(ad->arraytype);
1628 /* class_get_declaredclasses ***************************************************
1630 Return an array of declared classes of the given class.
1632 *******************************************************************************/
1634 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1636 classref_or_classinfo inner;
1637 classref_or_classinfo outer;
1639 int declaredclasscount; /* number of declared classes */
1640 int pos; /* current declared class */
1641 java_handle_objectarray_t *oa; /* array of declared classes */
1645 declaredclasscount = 0;
1647 if (!class_is_primitive(c) && !class_is_array(c)) {
1648 /* Determine number of declared classes. */
1650 for (i = 0; i < c->innerclasscount; i++) {
1651 /* Get outer-class. If the inner-class is not a member
1652 class, the outer-class is NULL. */
1654 outer = c->innerclass[i].outer_class;
1656 if (outer.any == NULL)
1659 /* Check if outer-class is a classref or a real class and
1660 get the class name from the structure. */
1662 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1664 /* Outer class is this class. */
1666 if ((outername == c->name) &&
1667 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1668 declaredclasscount++;
1672 /* Allocate Class[] and check for OOM. */
1674 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1679 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1680 inner = c->innerclass[i].inner_class;
1681 outer = c->innerclass[i].outer_class;
1683 /* Get outer-class. If the inner-class is not a member class,
1684 the outer-class is NULL. */
1686 if (outer.any == NULL)
1689 /* Check if outer_class is a classref or a real class and get
1690 the class name from the structure. */
1692 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1694 /* Outer class is this class. */
1696 if ((outername == c->name) &&
1697 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1699 ic = resolve_classref_or_classinfo_eager(inner, false);
1704 if (!(ic->state & CLASS_LINKED))
1705 if (!link_class(ic))
1708 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1716 /* class_get_declaringclass ****************************************************
1718 If the class or interface given is a member of another class,
1719 return the declaring class. For array and primitive classes return
1722 *******************************************************************************/
1724 classinfo *class_get_declaringclass(classinfo *c)
1726 classref_or_classinfo cr;
1729 /* Get declaring class. */
1731 cr = c->declaringclass;
1736 /* Resolve the class if necessary. */
1738 if (IS_CLASSREF(cr)) {
1739 /* dc = resolve_classref_eager(cr.ref); */
1740 dc = resolve_classref_or_classinfo_eager(cr, true);
1745 /* Store the resolved class in the class structure. */
1756 /* class_get_enclosingclass ****************************************************
1758 Return the enclosing class for the given class.
1760 *******************************************************************************/
1762 classinfo *class_get_enclosingclass(classinfo *c)
1764 classref_or_classinfo cr;
1767 /* Get enclosing class. */
1769 cr = c->enclosingclass;
1774 /* Resolve the class if necessary. */
1776 if (IS_CLASSREF(cr)) {
1777 /* ec = resolve_classref_eager(cr.ref); */
1778 ec = resolve_classref_or_classinfo_eager(cr, true);
1783 /* Store the resolved class in the class structure. */
1794 /* class_get_enclosingmethod ***************************************************
1796 Return the enclosing method for the given class.
1799 c ... class to return the enclosing method for
1802 methodinfo of the enclosing method
1804 *******************************************************************************/
1806 methodinfo *class_get_enclosingmethod(classinfo *c)
1808 constant_nameandtype *cn;
1812 /* get enclosing class and method */
1814 ec = class_get_enclosingclass(c);
1815 cn = c->enclosingmethod;
1817 /* check for enclosing class and method */
1825 /* find method in enclosing class */
1827 m = class_findmethod(ec, cn->name, cn->descriptor);
1830 exceptions_throw_internalerror("Enclosing method doesn't exist");
1838 /* class_get_interfaces ********************************************************
1840 Return an array of interfaces of the given class.
1842 *******************************************************************************/
1844 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1847 java_handle_objectarray_t *oa;
1850 if (!(c->state & CLASS_LINKED))
1854 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1859 for (i = 0; i < c->interfacescount; i++) {
1860 ic = c->interfaces[i];
1862 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1869 /* class_get_annotations *******************************************************
1871 Get the unparsed declared annotations in a byte array
1875 c........the class of which the annotations should be returned
1878 The unparsed declared annotations in a byte array
1879 (or NULL if there aren't any).
1881 *******************************************************************************/
1883 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1885 #if defined(ENABLE_ANNOTATIONS)
1886 java_handle_t *annotations; /* unparsed annotations */
1888 LLNI_classinfo_field_get(c, annotations, annotations);
1890 return (java_handle_bytearray_t*)annotations;
1897 /* class_get_modifiers *********************************************************
1899 Get the modifier flags of the given class.
1902 c....the class of which the modifier flags should be returned
1903 ignoreInnerClassesAttrib
1907 *******************************************************************************/
1909 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
1911 classref_or_classinfo inner;
1912 classref_or_classinfo outer;
1916 if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
1917 /* search for passed class as inner class */
1919 for (i = 0; i < c->innerclasscount; i++) {
1920 inner = c->innerclass[i].inner_class;
1921 outer = c->innerclass[i].outer_class;
1923 /* Check if inner is a classref or a real class and get
1924 the name of the structure */
1926 innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
1928 /* innerclass is this class */
1930 if (innername == c->name) {
1931 /* has the class actually an outer class? */
1934 /* return flags got from the outer class file */
1935 return c->innerclass[i].flags & ACC_CLASS_REFLECT_MASK;
1937 return c->flags & ACC_CLASS_REFLECT_MASK;
1942 /* passed class is no inner class or it was not requested */
1944 return c->flags & ACC_CLASS_REFLECT_MASK;
1948 /* class_get_signature *********************************************************
1950 Return the signature of the given class. For array and primitive
1951 classes return NULL.
1953 *******************************************************************************/
1955 #if defined(ENABLE_JAVASE)
1956 utf *class_get_signature(classinfo *c)
1958 /* For array and primitive classes return NULL. */
1960 if (class_is_array(c) || class_is_primitive(c))
1963 return c->signature;
1968 /* class_printflags ************************************************************
1970 Prints flags of a class.
1972 *******************************************************************************/
1974 #if !defined(NDEBUG)
1975 void class_printflags(classinfo *c)
1982 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1983 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1984 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1985 if (c->flags & ACC_STATIC) printf(" STATIC");
1986 if (c->flags & ACC_FINAL) printf(" FINAL");
1987 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1988 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1989 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1990 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1991 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1992 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1997 /* class_print *****************************************************************
1999 Prints classname plus flags.
2001 *******************************************************************************/
2003 #if !defined(NDEBUG)
2004 void class_print(classinfo *c)
2011 utf_display_printable_ascii(c->name);
2012 class_printflags(c);
2017 /* class_classref_print ********************************************************
2019 Prints classname plus referer class.
2021 *******************************************************************************/
2023 #if !defined(NDEBUG)
2024 void class_classref_print(constant_classref *cr)
2031 utf_display_printable_ascii(cr->name);
2034 class_print(cr->referer);
2042 /* class_println ***************************************************************
2044 Prints classname plus flags and new line.
2046 *******************************************************************************/
2048 #if !defined(NDEBUG)
2049 void class_println(classinfo *c)
2057 /* class_classref_println ******************************************************
2059 Prints classname plus referer class and new line.
2061 *******************************************************************************/
2063 #if !defined(NDEBUG)
2064 void class_classref_println(constant_classref *cr)
2066 class_classref_print(cr);
2072 /* class_classref_or_classinfo_print *******************************************
2074 Prints classname plus referer class.
2076 *******************************************************************************/
2078 #if !defined(NDEBUG)
2079 void class_classref_or_classinfo_print(classref_or_classinfo c)
2081 if (c.any == NULL) {
2082 printf("(classref_or_classinfo) NULL");
2086 class_classref_print(c.ref);
2093 /* class_classref_or_classinfo_println *****************************************
2095 Prints classname plus referer class and a newline.
2097 *******************************************************************************/
2099 #if !defined(NDEBUG)
2100 void class_classref_or_classinfo_println(classref_or_classinfo c)
2102 class_classref_or_classinfo_print(c);
2108 /* class_showconstantpool ******************************************************
2110 Dump the constant pool of the given class to stdout.
2112 *******************************************************************************/
2114 #if !defined(NDEBUG)
2115 void class_showconstantpool (classinfo *c)
2120 printf ("---- dump of constant pool ----\n");
2122 for (i=0; i<c->cpcount; i++) {
2123 printf ("#%d: ", (int) i);
2125 e = c -> cpinfos [i];
2128 switch (c -> cptags [i]) {
2129 case CONSTANT_Class:
2130 printf ("Classreference -> ");
2131 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2133 case CONSTANT_Fieldref:
2134 printf ("Fieldref -> ");
2135 field_fieldref_print((constant_FMIref *) e);
2137 case CONSTANT_Methodref:
2138 printf ("Methodref -> ");
2139 method_methodref_print((constant_FMIref *) e);
2141 case CONSTANT_InterfaceMethodref:
2142 printf ("InterfaceMethod -> ");
2143 method_methodref_print((constant_FMIref *) e);
2145 case CONSTANT_String:
2146 printf ("String -> ");
2147 utf_display_printable_ascii (e);
2149 case CONSTANT_Integer:
2150 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2152 case CONSTANT_Float:
2153 printf ("Float -> %f", ((constant_float*)e) -> value);
2155 case CONSTANT_Double:
2156 printf ("Double -> %f", ((constant_double*)e) -> value);
2160 u8 v = ((constant_long*)e) -> value;
2162 printf ("Long -> %ld", (long int) v);
2164 printf ("Long -> HI: %ld, LO: %ld\n",
2165 (long int) v.high, (long int) v.low);
2169 case CONSTANT_NameAndType:
2171 constant_nameandtype *cnt = e;
2172 printf ("NameAndType: ");
2173 utf_display_printable_ascii (cnt->name);
2175 utf_display_printable_ascii (cnt->descriptor);
2179 printf ("Utf8 -> ");
2180 utf_display_printable_ascii (e);
2183 log_text("Invalid type of ConstantPool-Entry");
2191 #endif /* !defined(NDEBUG) */
2194 /* class_showmethods ***********************************************************
2196 Dump info about the fields and methods of the given class to stdout.
2198 *******************************************************************************/
2200 #if !defined(NDEBUG)
2201 void class_showmethods (classinfo *c)
2205 printf("--------- Fields and Methods ----------------\n");
2207 class_printflags(c);
2211 utf_display_printable_ascii(c->name);
2216 utf_display_printable_ascii(c->super->name);
2220 printf("Index: %d\n", c->index);
2222 printf("Interfaces:\n");
2223 for (i = 0; i < c->interfacescount; i++) {
2225 utf_display_printable_ascii(c->interfaces[i]->name);
2226 printf (" (%d)\n", c->interfaces[i]->index);
2229 printf("Fields:\n");
2230 for (i = 0; i < c->fieldscount; i++)
2231 field_println(&(c->fields[i]));
2233 printf("Methods:\n");
2234 for (i = 0; i < c->methodscount; i++) {
2235 methodinfo *m = &(c->methods[i]);
2237 if (!(m->flags & ACC_STATIC))
2238 printf("vftblindex: %d ", m->vftblindex);
2243 printf ("Virtual function table:\n");
2244 for (i = 0; i < c->vftbl->vftbllength; i++)
2245 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2247 #endif /* !defined(NDEBUG) */
2251 * These are local overrides for various environment variables in Emacs.
2252 * Please do not remove this and leave it at the end of the file, where
2253 * Emacs will automagically detect them.
2254 * ---------------------------------------------------------------------
2257 * indent-tabs-mode: t
2261 * vim:noexpandtab:sw=4:ts=4: