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
25 $Id: class.c 8309 2007-08-15 16:42:52Z twisti $
41 #include "mm/memory.h"
43 #include "threads/lock-common.h"
45 #include "toolbox/logging.h"
47 #include "vm/builtin.h"
48 #include "vm/exceptions.h"
49 #include "vm/global.h"
50 #include "vm/resolve.h"
52 #include "vm/jit/asmpart.h"
54 #include "vmcore/class.h"
55 #include "vmcore/classcache.h"
56 #include "vmcore/linker.h"
57 #include "vmcore/loader.h"
58 #include "vmcore/options.h"
60 #if defined(ENABLE_STATISTICS)
61 # include "vmcore/statistics.h"
64 #include "vmcore/suck.h"
65 #include "vmcore/utf8.h"
68 /* global variables ***********************************************************/
70 /* frequently used classes ****************************************************/
72 /* important system classes */
74 classinfo *class_java_lang_Object;
75 classinfo *class_java_lang_Class;
76 classinfo *class_java_lang_ClassLoader;
77 classinfo *class_java_lang_Cloneable;
78 classinfo *class_java_lang_SecurityManager;
79 classinfo *class_java_lang_String;
80 classinfo *class_java_lang_System;
81 classinfo *class_java_lang_Thread;
82 classinfo *class_java_lang_ThreadGroup;
83 classinfo *class_java_lang_VMSystem;
84 classinfo *class_java_lang_VMThread;
85 classinfo *class_java_io_Serializable;
87 #if defined(WITH_CLASSPATH_SUN)
88 classinfo *class_sun_reflect_MagicAccessorImpl;
91 /* system exception classes required in cacao */
93 classinfo *class_java_lang_Throwable;
94 classinfo *class_java_lang_Error;
95 classinfo *class_java_lang_LinkageError;
96 classinfo *class_java_lang_NoClassDefFoundError;
97 classinfo *class_java_lang_OutOfMemoryError;
98 classinfo *class_java_lang_VirtualMachineError;
100 #if defined(WITH_CLASSPATH_GNU)
101 classinfo *class_java_lang_VMThrowable;
104 classinfo *class_java_lang_Exception;
105 classinfo *class_java_lang_ClassCastException;
106 classinfo *class_java_lang_ClassNotFoundException;
108 #if defined(ENABLE_JAVASE)
109 classinfo *class_java_lang_Void;
111 classinfo *class_java_lang_Boolean;
112 classinfo *class_java_lang_Byte;
113 classinfo *class_java_lang_Character;
114 classinfo *class_java_lang_Short;
115 classinfo *class_java_lang_Integer;
116 classinfo *class_java_lang_Long;
117 classinfo *class_java_lang_Float;
118 classinfo *class_java_lang_Double;
121 /* some runtime exception */
123 classinfo *class_java_lang_NullPointerException;
126 /* some classes which may be used more often */
128 #if defined(ENABLE_JAVASE)
129 classinfo *class_java_lang_StackTraceElement;
130 classinfo *class_java_lang_reflect_Constructor;
131 classinfo *class_java_lang_reflect_Field;
132 classinfo *class_java_lang_reflect_Method;
133 classinfo *class_java_security_PrivilegedAction;
134 classinfo *class_java_util_Vector;
136 classinfo *arrayclass_java_lang_Object;
138 #if defined(ENABLE_ANNOTATIONS)
139 classinfo *class_sun_reflect_ConstantPool;
140 classinfo *class_sun_reflect_annotation_AnnotationParser;
145 /* pseudo classes for the typechecker */
147 classinfo *pseudo_class_Arraystub;
148 classinfo *pseudo_class_Null;
149 classinfo *pseudo_class_New;
152 /* class_set_packagename *******************************************************
154 Derive the package name from the class name and store it in the struct.
156 *******************************************************************************/
158 void class_set_packagename(classinfo *c)
160 char *p = UTF_END(c->name) - 1;
161 char *start = c->name->text;
163 /* set the package name */
164 /* classes in the unnamed package keep packagename == NULL */
166 if (c->name->text[0] == '[') {
167 /* set packagename of arrays to the element's package */
169 for (; *start == '['; start++);
171 /* skip the 'L' in arrays of references */
175 for (; (p > start) && (*p != '/'); --p);
177 c->packagename = utf_new(start, p - start);
180 for (; (p > start) && (*p != '/'); --p);
182 c->packagename = utf_new(start, p - start);
187 /* class_create_classinfo ******************************************************
189 Create a new classinfo struct. The class name is set to the given utf *,
190 most other fields are initialized to zero.
192 Note: classname may be NULL. In this case a not-yet-named classinfo is
193 created. The name must be filled in later and class_set_packagename
194 must be called after that.
196 *******************************************************************************/
198 classinfo *class_create_classinfo(utf *classname)
202 #if defined(ENABLE_STATISTICS)
204 size_classinfo += sizeof(classinfo);
207 /* we use a safe name for temporarily unnamed classes */
209 if (classname == NULL)
210 classname = utf_not_named_yet;
214 log_message_utf("Creating class: ", classname);
217 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
219 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
220 /*c=NEW(classinfo);*/
223 /* Set the header.vftbl of all loaded classes to the one of
224 java.lang.Class, so Java code can use a class as object. */
226 if (class_java_lang_Class != NULL)
227 if (class_java_lang_Class->vftbl != NULL)
228 c->object.header.vftbl = class_java_lang_Class->vftbl;
230 #if defined(ENABLE_JAVASE)
231 /* check if the class is a reference class and flag it */
233 if (classname == utf_java_lang_ref_SoftReference) {
234 c->flags |= ACC_CLASS_REFERENCE_SOFT;
236 else if (classname == utf_java_lang_ref_WeakReference) {
237 c->flags |= ACC_CLASS_REFERENCE_WEAK;
239 else if (classname == utf_java_lang_ref_PhantomReference) {
240 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
244 if (classname != utf_not_named_yet)
245 class_set_packagename(c);
247 LOCK_INIT_OBJECT_LOCK(&c->object.header);
253 /* class_postset_header_vftbl **************************************************
255 Set the header.vftbl of all classes created before java.lang.Class
256 was linked. This is necessary that Java code can use a class as
259 *******************************************************************************/
261 void class_postset_header_vftbl(void)
265 classcache_name_entry *nmen;
266 classcache_class_entry *clsen;
268 assert(class_java_lang_Class);
270 for (slot = 0; slot < hashtable_classcache.size; slot++) {
271 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
273 for (; nmen; nmen = nmen->hashlink) {
274 /* iterate over all class entries */
276 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
279 /* now set the the vftbl */
281 if (c->object.header.vftbl == NULL)
282 c->object.header.vftbl = class_java_lang_Class->vftbl;
288 /* class_define ****************************************************************
290 Calls the loader and defines a class in the VM.
292 *******************************************************************************/
294 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
301 /* check if this class has already been defined */
303 c = classcache_lookup_defined_or_initiated(cl, name);
306 exceptions_throw_linkageerror("duplicate class definition: ", c);
311 /* create a new classinfo struct */
313 c = class_create_classinfo(name);
315 #if defined(ENABLE_STATISTICS)
318 if (opt_getloadingtime)
322 /* build a classbuffer with the given data */
324 cb = NEW(classbuffer);
331 /* preset the defining classloader */
335 /* load the class from this buffer */
337 r = load_class_from_classbuffer(cb);
341 FREE(cb, classbuffer);
343 #if defined(ENABLE_STATISTICS)
346 if (opt_getloadingtime)
351 /* If return value is NULL, we had a problem and the class is
352 not loaded. Now free the allocated memory, otherwise we
353 could run into a DOS. */
360 /* Store the newly defined class in the class cache. This call
361 also checks whether a class of the same name has already been
362 defined by the same defining loader, and if so, replaces the
363 newly created class by the one defined earlier. */
365 /* Important: The classinfo given to classcache_store must be
366 fully prepared because another thread may return
367 this pointer after the lookup at to top of this
368 function directly after the class cache lock has
371 c = classcache_store(cl, c, true);
377 /* class_load_attribute_sourcefile *********************************************
379 SourceFile_attribute {
380 u2 attribute_name_index;
385 *******************************************************************************/
387 static bool class_load_attribute_sourcefile(classbuffer *cb)
398 /* check buffer size */
400 if (!suck_check_classbuffer_size(cb, 4 + 2))
403 /* check attribute length */
405 attribute_length = suck_u4(cb);
407 if (attribute_length != 2) {
408 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
412 /* there can be no more than one SourceFile attribute */
414 if (c->sourcefile != NULL) {
415 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
421 sourcefile_index = suck_u2(cb);
422 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
424 if (sourcefile == NULL)
427 /* store sourcefile */
429 c->sourcefile = sourcefile;
435 /* class_load_attribute_enclosingmethod ****************************************
437 EnclosingMethod_attribute {
438 u2 attribute_name_index;
444 *******************************************************************************/
446 #if defined(ENABLE_JAVASE)
447 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
453 classref_or_classinfo cr;
454 constant_nameandtype *cn;
460 /* check buffer size */
462 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
465 /* check attribute length */
467 attribute_length = suck_u4(cb);
469 if (attribute_length != 4) {
470 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
474 /* there can be no more than one EnclosingMethod attribute */
476 if (c->enclosingmethod != NULL) {
477 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
481 /* get class index */
483 class_index = suck_u2(cb);
484 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
486 /* get method index */
488 method_index = suck_u2(cb);
489 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
491 /* store info in classinfo */
493 c->enclosingclass.any = cr.any;
494 c->enclosingmethod = cn;
498 #endif /* defined(ENABLE_JAVASE) */
501 /* class_load_attributes *******************************************************
503 Read attributes from ClassFile.
506 u2 attribute_name_index;
508 u1 info[attribute_length];
511 InnerClasses_attribute {
512 u2 attribute_name_index;
516 *******************************************************************************/
518 bool class_load_attributes(classbuffer *cb)
523 u2 attribute_name_index;
528 /* get attributes count */
530 if (!suck_check_classbuffer_size(cb, 2))
533 attributes_count = suck_u2(cb);
535 for (i = 0; i < attributes_count; i++) {
536 /* get attribute name */
538 if (!suck_check_classbuffer_size(cb, 2))
541 attribute_name_index = suck_u2(cb);
543 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
545 if (attribute_name == NULL)
548 if (attribute_name == utf_InnerClasses) {
551 if (c->innerclass != NULL) {
552 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
556 if (!suck_check_classbuffer_size(cb, 4 + 2))
559 /* skip attribute length */
562 /* number of records */
563 c->innerclasscount = suck_u2(cb);
565 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
568 /* allocate memory for innerclass structure */
569 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
571 for (j = 0; j < c->innerclasscount; j++) {
572 /* The innerclass structure contains a class with an encoded
573 name, its defining scope, its simple name and a bitmask of
574 the access flags. If an inner class is not a member, its
575 outer_class is NULL, if a class is anonymous, its name is
578 innerclassinfo *info = c->innerclass + j;
580 info->inner_class.ref =
581 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
582 info->outer_class.ref =
583 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
585 innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
586 info->flags = suck_u2(cb);
589 else if (attribute_name == utf_SourceFile) {
592 if (!class_load_attribute_sourcefile(cb))
595 #if defined(ENABLE_JAVASE)
596 else if (attribute_name == utf_EnclosingMethod) {
597 /* EnclosingMethod */
599 if (!class_load_attribute_enclosingmethod(cb))
602 else if (attribute_name == utf_Signature) {
605 if (!loader_load_attribute_signature(cb, &(c->signature)))
610 #if defined(ENABLE_ANNOTATIONS)
611 /* XXX We can't do a release with that enabled */
613 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
614 /* RuntimeVisibleAnnotations */
615 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
618 /* XXX RuntimeInvisibleAnnotations should only be loaded
619 * (or returned to Java) if some commandline options says so.
620 * Currently there is no such option available in cacao,
621 * therefore I load them allways (for testing purpose).
622 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
623 * generated if you tell javac to do so. So in most cases
624 * there won't be any.
626 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
627 /* RuntimeInvisibleAnnotations */
628 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
634 /* unknown attribute */
636 if (!loader_skip_attribute_body(cb))
645 /* class_freepool **************************************************************
647 Frees all resources used by this classes Constant Pool.
649 *******************************************************************************/
651 static void class_freecpool(classinfo *c)
657 if (c->cptags && c->cpinfos) {
658 for (idx = 0; idx < c->cpcount; idx++) {
659 tag = c->cptags[idx];
660 info = c->cpinfos[idx];
664 case CONSTANT_Fieldref:
665 case CONSTANT_Methodref:
666 case CONSTANT_InterfaceMethodref:
667 FREE(info, constant_FMIref);
669 case CONSTANT_Integer:
670 FREE(info, constant_integer);
673 FREE(info, constant_float);
676 FREE(info, constant_long);
678 case CONSTANT_Double:
679 FREE(info, constant_double);
681 case CONSTANT_NameAndType:
682 FREE(info, constant_nameandtype);
690 MFREE(c->cptags, u1, c->cpcount);
693 MFREE(c->cpinfos, voidptr, c->cpcount);
697 /* class_getconstant ***********************************************************
699 Retrieves the value at position 'pos' of the constantpool of a
700 class. If the type of the value is other than 'ctype', an error is
703 *******************************************************************************/
705 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
707 /* check index and type of constantpool entry */
708 /* (pos == 0 is caught by type comparison) */
710 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
711 exceptions_throw_classformaterror(c, "Illegal constant pool index");
715 return c->cpinfos[pos];
719 /* innerclass_getconstant ******************************************************
721 Like class_getconstant, but if cptags is ZERO, null is returned.
723 *******************************************************************************/
725 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
727 /* invalid position in constantpool */
729 if (pos >= c->cpcount) {
730 exceptions_throw_classformaterror(c, "Illegal constant pool index");
734 /* constantpool entry of type 0 */
736 if (c->cptags[pos] == 0)
739 /* check type of constantpool entry */
741 if (c->cptags[pos] != ctype) {
742 exceptions_throw_classformaterror(c, "Illegal constant pool index");
746 return c->cpinfos[pos];
750 /* class_free ******************************************************************
752 Frees all resources used by the class.
754 *******************************************************************************/
756 void class_free(classinfo *c)
764 MFREE(c->interfaces, classinfo*, c->interfacescount);
767 for (i = 0; i < c->fieldscount; i++)
768 field_free(&(c->fields[i]));
769 #if defined(ENABLE_CACAO_GC)
770 MFREE(c->fields, fieldinfo, c->fieldscount);
775 for (i = 0; i < c->methodscount; i++)
776 method_free(&(c->methods[i]));
777 MFREE(c->methods, methodinfo, c->methodscount);
780 if ((v = c->vftbl) != NULL) {
782 mem_free(v->arraydesc,sizeof(arraydescriptor));
784 for (i = 0; i < v->interfacetablelength; i++) {
785 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
787 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
789 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
790 sizeof(methodptr*) * (v->interfacetablelength -
791 (v->interfacetablelength > 0));
792 v = (vftbl_t*) (((methodptr*) v) -
793 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
798 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
800 /* if (c->classvftbl)
801 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
805 #if defined(ENABLE_ANNOTATIONS)
806 annotation_bytearray_free(c->annotations);
808 annotation_bytearrays_free(c->method_annotations);
809 annotation_bytearrays_free(c->method_parameterannotations);
810 annotation_bytearrays_free(c->method_annotationdefaults);
812 annotation_bytearrays_free(c->field_annotations);
817 /* get_array_class *************************************************************
819 Returns the array class with the given name for the given
820 classloader, or NULL if an exception occurred.
822 Note: This function does eager loading.
824 *******************************************************************************/
826 static classinfo *get_array_class(utf *name,classloader *initloader,
827 classloader *defloader,bool link)
831 /* lookup this class in the classcache */
832 c = classcache_lookup(initloader,name);
834 c = classcache_lookup_defined(defloader,name);
837 /* we have to create it */
838 c = class_create_classinfo(name);
839 c = load_newly_created_array(c,initloader);
845 assert(c->state & CLASS_LOADED);
846 assert(c->classloader == defloader);
848 if (link && !(c->state & CLASS_LINKED))
852 assert(!link || (c->state & CLASS_LINKED));
858 /* class_array_of **************************************************************
860 Returns an array class with the given component class. The array
861 class is dynamically created if neccessary.
863 *******************************************************************************/
865 classinfo *class_array_of(classinfo *component, bool link)
874 cl = component->classloader;
876 dumpsize = dump_size();
878 /* Assemble the array class name */
879 namelen = component->name->blength;
881 if (component->name->text[0] == '[') {
882 /* the component is itself an array */
883 namebuf = DMNEW(char, namelen + 1);
885 MCOPY(namebuf + 1, component->name->text, char, namelen);
889 /* the component is a non-array class */
890 namebuf = DMNEW(char, namelen + 3);
893 MCOPY(namebuf + 2, component->name->text, char, namelen);
894 namebuf[2 + namelen] = ';';
898 u = utf_new(namebuf, namelen);
900 c = get_array_class(u, cl, cl, link);
902 dump_release(dumpsize);
908 /* class_multiarray_of *********************************************************
910 Returns an array class with the given dimension and element class.
911 The array class is dynamically created if neccessary.
913 *******************************************************************************/
915 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
922 dumpsize = dump_size();
925 log_text("Invalid array dimension requested");
929 /* Assemble the array class name */
930 namelen = element->name->blength;
932 if (element->name->text[0] == '[') {
933 /* the element is itself an array */
934 namebuf = DMNEW(char, namelen + dim);
935 memcpy(namebuf + dim, element->name->text, namelen);
939 /* the element is a non-array class */
940 namebuf = DMNEW(char, namelen + 2 + dim);
942 memcpy(namebuf + dim + 1, element->name->text, namelen);
943 namelen += (2 + dim);
944 namebuf[namelen - 1] = ';';
946 memset(namebuf, '[', dim);
948 c = get_array_class(utf_new(namebuf, namelen),
949 element->classloader,
950 element->classloader,
953 dump_release(dumpsize);
959 /* class_lookup_classref *******************************************************
961 Looks up the constant_classref for a given classname in the classref
965 cls..............the class containing the reference
966 name.............the name of the class refered to
969 a pointer to a constant_classref, or
970 NULL if the reference was not found
972 *******************************************************************************/
974 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
976 constant_classref *ref;
977 extra_classref *xref;
982 assert(!cls->classrefcount || cls->classrefs);
984 /* first search the main classref table */
985 count = cls->classrefcount;
986 ref = cls->classrefs;
987 for (; count; --count, ++ref)
988 if (ref->name == name)
991 /* next try the list of extra classrefs */
992 for (xref = cls->extclassrefs; xref; xref = xref->next) {
993 if (xref->classref.name == name)
994 return &(xref->classref);
1002 /* class_get_classref **********************************************************
1004 Returns the constant_classref for a given classname.
1007 cls..............the class containing the reference
1008 name.............the name of the class refered to
1011 a pointer to a constant_classref (never NULL)
1014 The given name is not checked for validity!
1016 *******************************************************************************/
1018 constant_classref *class_get_classref(classinfo *cls, utf *name)
1020 constant_classref *ref;
1021 extra_classref *xref;
1026 ref = class_lookup_classref(cls,name);
1030 xref = NEW(extra_classref);
1031 CLASSREF_INIT(xref->classref,cls,name);
1033 xref->next = cls->extclassrefs;
1034 cls->extclassrefs = xref;
1036 return &(xref->classref);
1040 /* class_get_self_classref *****************************************************
1042 Returns the constant_classref to the class itself.
1045 cls..............the class containing the reference
1048 a pointer to a constant_classref (never NULL)
1050 *******************************************************************************/
1052 constant_classref *class_get_self_classref(classinfo *cls)
1054 /* XXX this should be done in a faster way. Maybe always make */
1055 /* the classref of index 0 a self reference. */
1056 return class_get_classref(cls,cls->name);
1059 /* class_get_classref_multiarray_of ********************************************
1061 Returns an array type reference with the given dimension and element class
1065 dim..............the requested dimension
1066 dim must be in [1;255]. This is NOT checked!
1067 ref..............the component class reference
1070 a pointer to the class reference for the array type
1073 The referer of `ref` is used as the referer for the new classref.
1075 *******************************************************************************/
1077 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1082 constant_classref *cr;
1085 assert(dim >= 1 && dim <= 255);
1087 dumpsize = dump_size();
1089 /* Assemble the array class name */
1090 namelen = ref->name->blength;
1092 if (ref->name->text[0] == '[') {
1093 /* the element is itself an array */
1094 namebuf = DMNEW(char, namelen + dim);
1095 memcpy(namebuf + dim, ref->name->text, namelen);
1099 /* the element is a non-array class */
1100 namebuf = DMNEW(char, namelen + 2 + dim);
1102 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1103 namelen += (2 + dim);
1104 namebuf[namelen - 1] = ';';
1106 memset(namebuf, '[', dim);
1108 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1110 dump_release(dumpsize);
1116 /* class_get_classref_component_of *********************************************
1118 Returns the component classref of a given array type reference
1121 ref..............the array type reference
1124 a reference to the component class, or
1125 NULL if `ref` is not an object array type reference
1128 The referer of `ref` is used as the referer for the new classref.
1130 *******************************************************************************/
1132 constant_classref *class_get_classref_component_of(constant_classref *ref)
1139 name = ref->name->text;
1143 namelen = ref->name->blength - 1;
1148 else if (*name != '[') {
1152 return class_get_classref(ref->referer, utf_new(name, namelen));
1156 /* class_findmethod ************************************************************
1158 Searches a 'classinfo' structure for a method having the given name
1159 and descriptor. If descriptor is NULL, it is ignored.
1161 *******************************************************************************/
1163 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1168 for (i = 0; i < c->methodscount; i++) {
1169 m = &(c->methods[i]);
1171 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1179 /* class_resolvemethod *********************************************************
1181 Searches a class and it's super classes for a method.
1183 Superinterfaces are *not* searched.
1185 *******************************************************************************/
1187 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1192 m = class_findmethod(c, name, desc);
1197 /* JVM Specification bug:
1199 It is important NOT to resolve special <init> and <clinit>
1200 methods to super classes or interfaces; yet, this is not
1201 explicited in the specification. Section 5.4.3.3 should be
1202 updated appropriately. */
1204 if (name == utf_init || name == utf_clinit)
1214 /* class_resolveinterfacemethod_intern *****************************************
1216 Internally used helper function. Do not use this directly.
1218 *******************************************************************************/
1220 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1221 utf *name, utf *desc)
1226 /* try to find the method in the class */
1228 m = class_findmethod(c, name, desc);
1233 /* no method found? try the superinterfaces */
1235 for (i = 0; i < c->interfacescount; i++) {
1236 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1243 /* no method found */
1249 /* class_resolveclassmethod ****************************************************
1251 Resolves a reference from REFERER to a method with NAME and DESC in
1254 If the method cannot be resolved the return value is NULL. If
1255 EXCEPT is true *exceptionptr is set, too.
1257 *******************************************************************************/
1259 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1260 classinfo *referer, bool throwexception)
1266 /* if (c->flags & ACC_INTERFACE) { */
1267 /* if (throwexception) */
1268 /* *exceptionptr = */
1269 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1273 /* try class c and its superclasses */
1277 m = class_resolvemethod(cls, name, desc);
1282 /* try the superinterfaces */
1284 for (i = 0; i < c->interfacescount; i++) {
1285 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1293 exceptions_throw_nosuchmethoderror(c, name, desc);
1298 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1300 exceptions_throw_abstractmethoderror();
1305 /* XXX check access rights */
1311 /* class_resolveinterfacemethod ************************************************
1313 Resolves a reference from REFERER to a method with NAME and DESC in
1316 If the method cannot be resolved the return value is NULL. If
1317 EXCEPT is true *exceptionptr is set, too.
1319 *******************************************************************************/
1321 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1322 classinfo *referer, bool throwexception)
1326 if (!(c->flags & ACC_INTERFACE)) {
1328 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1333 mi = class_resolveinterfacemethod_intern(c, name, desc);
1338 /* try class java.lang.Object */
1340 mi = class_findmethod(class_java_lang_Object, name, desc);
1346 exceptions_throw_nosuchmethoderror(c, name, desc);
1352 /* class_findfield *************************************************************
1354 Searches for field with specified name and type in a classinfo
1355 structure. If no such field is found NULL is returned.
1357 *******************************************************************************/
1359 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1363 for (i = 0; i < c->fieldscount; i++)
1364 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1365 return &(c->fields[i]);
1368 return class_findfield(c->super.cls, name, desc);
1374 /* class_findfield_approx ******************************************************
1376 Searches in 'classinfo'-structure for a field with the specified
1379 *******************************************************************************/
1381 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1385 /* get field index */
1387 i = class_findfield_index_by_name(c, name);
1389 /* field was not found, return */
1394 /* return field address */
1396 return &(c->fields[i]);
1400 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1404 for (i = 0; i < c->fieldscount; i++) {
1405 /* compare field names */
1407 if ((c->fields[i].name == name))
1411 /* field was not found, raise exception */
1413 exceptions_throw_nosuchfielderror(c, name);
1419 /****************** Function: class_resolvefield_int ***************************
1421 This is an internally used helper function. Do not use this directly.
1423 Tries to resolve a field having the given name and type.
1424 If the field cannot be resolved, NULL is returned.
1426 *******************************************************************************/
1428 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1433 /* search for field in class c */
1435 for (i = 0; i < c->fieldscount; i++) {
1436 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1437 return &(c->fields[i]);
1441 /* try superinterfaces recursively */
1443 for (i = 0; i < c->interfacescount; i++) {
1444 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1449 /* try superclass */
1452 return class_resolvefield_int(c->super.cls, name, desc);
1460 /********************* Function: class_resolvefield ***************************
1462 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1464 If the field cannot be resolved the return value is NULL. If EXCEPT is
1465 true *exceptionptr is set, too.
1467 *******************************************************************************/
1469 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1470 classinfo *referer, bool throwexception)
1474 fi = class_resolvefield_int(c, name, desc);
1478 exceptions_throw_nosuchfielderror(c, name);
1483 /* XXX check access rights */
1489 /* class_resolve_superclass ****************************************************
1491 Resolves the super class reference of the given class if necessary.
1493 *******************************************************************************/
1495 static classinfo *class_resolve_superclass(classinfo *c)
1499 if (c->super.any == NULL)
1502 /* Do we have a super class reference or is it already
1505 if (IS_CLASSREF(c->super)) {
1506 super = resolve_classref_or_classinfo_eager(c->super, true);
1511 /* Store the resolved super class in the class structure. */
1513 c->super.cls = super;
1516 return c->super.cls;
1520 /* class_issubclass ************************************************************
1522 Checks if sub is a descendant of super.
1524 *******************************************************************************/
1526 bool class_issubclass(classinfo *sub, classinfo *super)
1535 sub = class_resolve_superclass(sub);
1540 /* class_isanysubclass *********************************************************
1542 Checks a subclass relation between two classes. Implemented
1543 interfaces are interpreted as super classes.
1545 Return value: 1 ... sub is subclass of super
1548 *******************************************************************************/
1550 bool class_isanysubclass(classinfo *sub, classinfo *super)
1555 /* This is the trivial case. */
1560 /* Primitive classes are only subclasses of themselves. */
1562 if (class_is_primitive(sub) || class_is_primitive(super))
1565 /* Check for interfaces. */
1567 if (super->flags & ACC_INTERFACE) {
1568 result = (sub->vftbl->interfacetablelength > super->index) &&
1569 (sub->vftbl->interfacetable[-super->index] != NULL);
1572 /* java.lang.Object is the only super class of any
1575 if (sub->flags & ACC_INTERFACE)
1576 return (super == class_java_lang_Object);
1578 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1580 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1581 result = diffval <= (uint32_t) super->vftbl->diffval;
1583 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1590 /* class_is_primitive **********************************************************
1592 Checks if the given class is a primitive class.
1594 *******************************************************************************/
1596 bool class_is_primitive(classinfo *c)
1598 if (c->flags & ACC_CLASS_PRIMITIVE)
1605 /* class_is_array **************************************************************
1607 Checks if the given class is an array class.
1609 *******************************************************************************/
1611 bool class_is_array(classinfo *c)
1613 if (!(c->state & CLASS_LINKED))
1617 return (c->vftbl->arraydesc != NULL);
1621 /* class_is_interface **********************************************************
1623 Checks if the given class is an interface.
1625 *******************************************************************************/
1627 bool class_is_interface(classinfo *c)
1629 if (c->flags & ACC_INTERFACE)
1636 /* class_get_superclass ********************************************************
1638 Return the super class of the given class. If the super-field is a
1639 class-reference, resolve it and store it in the classinfo.
1641 *******************************************************************************/
1643 classinfo *class_get_superclass(classinfo *c)
1647 /* For java.lang.Object, primitive and Void classes we return
1650 if (c->super.any == NULL)
1653 /* For interfaces we also return NULL. */
1655 if (c->flags & ACC_INTERFACE)
1658 /* We may have to resolve the super class reference. */
1660 super = class_resolve_superclass(c);
1666 /* class_get_declaredclasses ***************************************************
1668 Return an array of declared classes of the given class.
1670 *******************************************************************************/
1672 java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
1674 classref_or_classinfo inner;
1675 classref_or_classinfo outer;
1677 int declaredclasscount; /* number of declared classes */
1678 int pos; /* current declared class */
1679 java_objectarray *oa; /* array of declared classes */
1683 declaredclasscount = 0;
1685 if (!class_is_primitive(c) && !class_is_array(c)) {
1686 /* Determine number of declared classes. */
1688 for (i = 0; i < c->innerclasscount; i++) {
1689 outer = c->innerclass[i].outer_class;
1691 /* Check if outer_class is a classref or a real class and
1692 get the class name from the structure. */
1694 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1696 /* Outer class is this class. */
1698 if ((outername == c->name) &&
1699 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1700 declaredclasscount++;
1704 /* Allocate Class[] and check for OOM. */
1706 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1711 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1712 inner = c->innerclass[i].inner_class;
1713 outer = c->innerclass[i].outer_class;
1715 /* Check if outer_class is a classref or a real class and get
1716 the class name from the structure. */
1718 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1720 /* Outer class is this class. */
1722 if ((outername == c->name) &&
1723 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1725 ic = resolve_classref_or_classinfo_eager(inner, false);
1730 if (!(ic->state & CLASS_LINKED))
1731 if (!link_class(ic))
1734 oa->data[pos++] = (java_object_t *) ic;
1742 /* class_get_declaringclass ****************************************************
1744 If the class or interface given is a member of another class,
1745 return the declaring class. For array and primitive classes return
1748 *******************************************************************************/
1750 classinfo *class_get_declaringclass(classinfo *c)
1752 classref_or_classinfo innercr;
1754 classref_or_classinfo outercr;
1758 /* return NULL for arrayclasses and primitive classes */
1760 if (class_is_primitive(c) || (c->name->text[0] == '['))
1763 /* no innerclasses exist */
1765 if (c->innerclasscount == 0)
1768 for (i = 0; i < c->innerclasscount; i++) {
1769 /* Check if inner_class is a classref or a real class and get
1770 the class name from the structure. */
1772 innercr = c->innerclass[i].inner_class;
1774 innername = IS_CLASSREF(innercr) ?
1775 innercr.ref->name : innercr.cls->name;
1777 /* Is the current innerclass this class? */
1779 if (innername == c->name) {
1780 /* Maybe the outer class is not loaded yet. */
1782 outercr = c->innerclass[i].outer_class;
1784 outer = resolve_classref_or_classinfo_eager(outercr, false);
1789 if (!(outer->state & CLASS_LINKED))
1790 if (!link_class(outer))
1801 /* class_get_interfaces ********************************************************
1803 Return an array of interfaces of the given class.
1805 *******************************************************************************/
1807 java_objectarray *class_get_interfaces(classinfo *c)
1810 java_objectarray *oa;
1813 if (!(c->state & CLASS_LINKED))
1817 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1822 for (i = 0; i < c->interfacescount; i++) {
1823 ic = c->interfaces[i].cls;
1825 oa->data[i] = (java_object_t *) ic;
1832 /* class_get_signature *********************************************************
1834 Return the signature of the given class. For array and primitive
1835 classes return NULL.
1837 *******************************************************************************/
1839 #if defined(ENABLE_JAVASE)
1840 utf *class_get_signature(classinfo *c)
1842 /* For array and primitive classes return NULL. */
1844 if (class_is_array(c) || class_is_primitive(c))
1847 return c->signature;
1852 /* class_printflags ************************************************************
1854 Prints flags of a class.
1856 *******************************************************************************/
1858 #if !defined(NDEBUG)
1859 void class_printflags(classinfo *c)
1866 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1867 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1868 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1869 if (c->flags & ACC_STATIC) printf(" STATIC");
1870 if (c->flags & ACC_FINAL) printf(" FINAL");
1871 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1872 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1873 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1874 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1875 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1876 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1881 /* class_print *****************************************************************
1883 Prints classname plus flags.
1885 *******************************************************************************/
1887 #if !defined(NDEBUG)
1888 void class_print(classinfo *c)
1895 utf_display_printable_ascii(c->name);
1896 class_printflags(c);
1901 /* class_classref_print ********************************************************
1903 Prints classname plus referer class.
1905 *******************************************************************************/
1907 #if !defined(NDEBUG)
1908 void class_classref_print(constant_classref *cr)
1915 utf_display_printable_ascii(cr->name);
1918 class_print(cr->referer);
1926 /* class_println ***************************************************************
1928 Prints classname plus flags and new line.
1930 *******************************************************************************/
1932 #if !defined(NDEBUG)
1933 void class_println(classinfo *c)
1941 /* class_classref_println ******************************************************
1943 Prints classname plus referer class and new line.
1945 *******************************************************************************/
1947 #if !defined(NDEBUG)
1948 void class_classref_println(constant_classref *cr)
1950 class_classref_print(cr);
1956 /* class_classref_or_classinfo_print *******************************************
1958 Prints classname plus referer class.
1960 *******************************************************************************/
1962 #if !defined(NDEBUG)
1963 void class_classref_or_classinfo_print(classref_or_classinfo c)
1965 if (c.any == NULL) {
1966 printf("(classref_or_classinfo) NULL");
1970 class_classref_print(c.ref);
1977 /* class_classref_or_classinfo_println *****************************************
1979 Prints classname plus referer class and a newline.
1981 *******************************************************************************/
1983 void class_classref_or_classinfo_println(classref_or_classinfo c)
1985 class_classref_or_classinfo_println(c);
1990 /* class_showconstantpool ******************************************************
1992 Dump the constant pool of the given class to stdout.
1994 *******************************************************************************/
1996 #if !defined(NDEBUG)
1997 void class_showconstantpool (classinfo *c)
2002 printf ("---- dump of constant pool ----\n");
2004 for (i=0; i<c->cpcount; i++) {
2005 printf ("#%d: ", (int) i);
2007 e = c -> cpinfos [i];
2010 switch (c -> cptags [i]) {
2011 case CONSTANT_Class:
2012 printf ("Classreference -> ");
2013 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2015 case CONSTANT_Fieldref:
2016 printf ("Fieldref -> ");
2017 field_fieldref_print((constant_FMIref *) e);
2019 case CONSTANT_Methodref:
2020 printf ("Methodref -> ");
2021 method_methodref_print((constant_FMIref *) e);
2023 case CONSTANT_InterfaceMethodref:
2024 printf ("InterfaceMethod -> ");
2025 method_methodref_print((constant_FMIref *) e);
2027 case CONSTANT_String:
2028 printf ("String -> ");
2029 utf_display_printable_ascii (e);
2031 case CONSTANT_Integer:
2032 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2034 case CONSTANT_Float:
2035 printf ("Float -> %f", ((constant_float*)e) -> value);
2037 case CONSTANT_Double:
2038 printf ("Double -> %f", ((constant_double*)e) -> value);
2042 u8 v = ((constant_long*)e) -> value;
2044 printf ("Long -> %ld", (long int) v);
2046 printf ("Long -> HI: %ld, LO: %ld\n",
2047 (long int) v.high, (long int) v.low);
2051 case CONSTANT_NameAndType:
2053 constant_nameandtype *cnt = e;
2054 printf ("NameAndType: ");
2055 utf_display_printable_ascii (cnt->name);
2057 utf_display_printable_ascii (cnt->descriptor);
2061 printf ("Utf8 -> ");
2062 utf_display_printable_ascii (e);
2065 log_text("Invalid type of ConstantPool-Entry");
2073 #endif /* !defined(NDEBUG) */
2076 /* class_showmethods ***********************************************************
2078 Dump info about the fields and methods of the given class to stdout.
2080 *******************************************************************************/
2082 #if !defined(NDEBUG)
2083 void class_showmethods (classinfo *c)
2087 printf("--------- Fields and Methods ----------------\n");
2089 class_printflags(c);
2093 utf_display_printable_ascii(c->name);
2098 utf_display_printable_ascii(c->super.cls->name);
2102 printf("Index: %d\n", c->index);
2104 printf("Interfaces:\n");
2105 for (i = 0; i < c->interfacescount; i++) {
2107 utf_display_printable_ascii(c->interfaces[i].cls->name);
2108 printf (" (%d)\n", c->interfaces[i].cls->index);
2111 printf("Fields:\n");
2112 for (i = 0; i < c->fieldscount; i++)
2113 field_println(&(c->fields[i]));
2115 printf("Methods:\n");
2116 for (i = 0; i < c->methodscount; i++) {
2117 methodinfo *m = &(c->methods[i]);
2119 if (!(m->flags & ACC_STATIC))
2120 printf("vftblindex: %d ", m->vftblindex);
2125 printf ("Virtual function table:\n");
2126 for (i = 0; i < c->vftbl->vftbllength; i++)
2127 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2129 #endif /* !defined(NDEBUG) */
2133 * These are local overrides for various environment variables in Emacs.
2134 * Please do not remove this and leave it at the end of the file, where
2135 * Emacs will automagically detect them.
2136 * ---------------------------------------------------------------------
2139 * indent-tabs-mode: t
2143 * vim:noexpandtab:sw=4:ts=4: