1 /* src/vmcore/class.c - class related functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
39 #include "mm/memory.h"
41 #include "native/llni.h"
43 #include "threads/lock-common.h"
45 #include "toolbox/logging.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/resolve.h"
53 #include "vm/jit/asmpart.h"
55 #include "vmcore/class.h"
56 #include "vmcore/classcache.h"
57 #include "vmcore/linker.h"
58 #include "vmcore/loader.h"
59 #include "vmcore/options.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
65 #include "vmcore/suck.h"
66 #include "vmcore/utf8.h"
69 /* global variables ***********************************************************/
71 /* frequently used classes ****************************************************/
73 /* important system classes */
75 classinfo *class_java_lang_Object;
76 classinfo *class_java_lang_Class;
77 classinfo *class_java_lang_ClassLoader;
78 classinfo *class_java_lang_Cloneable;
79 classinfo *class_java_lang_SecurityManager;
80 classinfo *class_java_lang_String;
81 classinfo *class_java_lang_System;
82 classinfo *class_java_lang_Thread;
83 classinfo *class_java_lang_ThreadGroup;
84 classinfo *class_java_lang_VMSystem;
85 classinfo *class_java_lang_VMThread;
86 classinfo *class_java_io_Serializable;
88 #if defined(WITH_CLASSPATH_SUN)
89 classinfo *class_sun_reflect_MagicAccessorImpl;
92 /* system exception classes required in cacao */
94 classinfo *class_java_lang_Throwable;
95 classinfo *class_java_lang_Error;
96 classinfo *class_java_lang_LinkageError;
97 classinfo *class_java_lang_NoClassDefFoundError;
98 classinfo *class_java_lang_OutOfMemoryError;
99 classinfo *class_java_lang_VirtualMachineError;
101 #if defined(WITH_CLASSPATH_GNU)
102 classinfo *class_java_lang_VMThrowable;
105 classinfo *class_java_lang_Exception;
106 classinfo *class_java_lang_ClassCastException;
107 classinfo *class_java_lang_ClassNotFoundException;
109 #if defined(ENABLE_JAVASE)
110 classinfo *class_java_lang_Void;
112 classinfo *class_java_lang_Boolean;
113 classinfo *class_java_lang_Byte;
114 classinfo *class_java_lang_Character;
115 classinfo *class_java_lang_Short;
116 classinfo *class_java_lang_Integer;
117 classinfo *class_java_lang_Long;
118 classinfo *class_java_lang_Float;
119 classinfo *class_java_lang_Double;
122 /* some runtime exception */
124 classinfo *class_java_lang_NullPointerException;
127 /* some classes which may be used more often */
129 #if defined(ENABLE_JAVASE)
130 classinfo *class_java_lang_StackTraceElement;
131 classinfo *class_java_lang_reflect_Constructor;
132 classinfo *class_java_lang_reflect_Field;
133 classinfo *class_java_lang_reflect_Method;
134 classinfo *class_java_security_PrivilegedAction;
135 classinfo *class_java_util_Vector;
137 classinfo *arrayclass_java_lang_Object;
139 #if defined(ENABLE_ANNOTATIONS)
140 classinfo *class_sun_reflect_ConstantPool;
141 #if defined(WITH_CLASSPATH_GNU)
142 classinfo *class_sun_reflect_annotation_AnnotationParser;
148 /* pseudo classes for the typechecker */
150 classinfo *pseudo_class_Arraystub;
151 classinfo *pseudo_class_Null;
152 classinfo *pseudo_class_New;
155 /* class_set_packagename *******************************************************
157 Derive the package name from the class name and store it in the struct.
159 *******************************************************************************/
161 void class_set_packagename(classinfo *c)
163 char *p = UTF_END(c->name) - 1;
164 char *start = c->name->text;
166 /* set the package name */
167 /* classes in the unnamed package keep packagename == NULL */
169 if (c->name->text[0] == '[') {
170 /* set packagename of arrays to the element's package */
172 for (; *start == '['; start++);
174 /* skip the 'L' in arrays of references */
178 for (; (p > start) && (*p != '/'); --p);
180 c->packagename = utf_new(start, p - start);
183 for (; (p > start) && (*p != '/'); --p);
185 c->packagename = utf_new(start, p - start);
190 /* class_create_classinfo ******************************************************
192 Create a new classinfo struct. The class name is set to the given utf *,
193 most other fields are initialized to zero.
195 Note: classname may be NULL. In this case a not-yet-named classinfo is
196 created. The name must be filled in later and class_set_packagename
197 must be called after that.
199 *******************************************************************************/
201 classinfo *class_create_classinfo(utf *classname)
205 #if defined(ENABLE_STATISTICS)
207 size_classinfo += sizeof(classinfo);
210 /* we use a safe name for temporarily unnamed classes */
212 if (classname == NULL)
213 classname = utf_not_named_yet;
217 log_message_utf("Creating class: ", classname);
220 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
222 #if defined(ENABLE_GC_CACAO)
223 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
225 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
226 /*c=NEW(classinfo);*/
230 /* Set the header.vftbl of all loaded classes to the one of
231 java.lang.Class, so Java code can use a class as object. */
233 if (class_java_lang_Class != NULL)
234 if (class_java_lang_Class->vftbl != NULL)
235 c->object.header.vftbl = class_java_lang_Class->vftbl;
237 #if defined(ENABLE_JAVASE)
238 /* check if the class is a reference class and flag it */
240 if (classname == utf_java_lang_ref_SoftReference) {
241 c->flags |= ACC_CLASS_REFERENCE_SOFT;
243 else if (classname == utf_java_lang_ref_WeakReference) {
244 c->flags |= ACC_CLASS_REFERENCE_WEAK;
246 else if (classname == utf_java_lang_ref_PhantomReference) {
247 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
251 if (classname != utf_not_named_yet)
252 class_set_packagename(c);
254 LOCK_INIT_OBJECT_LOCK(&c->object.header);
260 /* class_postset_header_vftbl **************************************************
262 Set the header.vftbl of all classes created before java.lang.Class
263 was linked. This is necessary that Java code can use a class as
266 *******************************************************************************/
268 void class_postset_header_vftbl(void)
272 classcache_name_entry *nmen;
273 classcache_class_entry *clsen;
275 assert(class_java_lang_Class);
277 for (slot = 0; slot < hashtable_classcache.size; slot++) {
278 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
280 for (; nmen; nmen = nmen->hashlink) {
281 /* iterate over all class entries */
283 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
286 /* now set the the vftbl */
288 if (c->object.header.vftbl == NULL)
289 c->object.header.vftbl = class_java_lang_Class->vftbl;
295 /* class_define ****************************************************************
297 Calls the loader and defines a class in the VM.
299 *******************************************************************************/
301 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
308 /* check if this class has already been defined */
310 c = classcache_lookup_defined_or_initiated(cl, name);
313 exceptions_throw_linkageerror("duplicate class definition: ", c);
318 /* create a new classinfo struct */
320 c = class_create_classinfo(name);
322 #if defined(ENABLE_STATISTICS)
325 if (opt_getloadingtime)
329 /* build a classbuffer with the given data */
331 cb = NEW(classbuffer);
338 /* preset the defining classloader */
342 /* load the class from this buffer */
344 r = load_class_from_classbuffer(cb);
348 FREE(cb, classbuffer);
350 #if defined(ENABLE_STATISTICS)
353 if (opt_getloadingtime)
358 /* If return value is NULL, we had a problem and the class is
359 not loaded. Now free the allocated memory, otherwise we
360 could run into a DOS. */
367 #if defined(ENABLE_JAVASE)
368 # if defined(WITH_CLASSPATH_SUN)
369 /* Store the protection domain. */
371 c->protectiondomain = pd;
375 /* Store the newly defined class in the class cache. This call
376 also checks whether a class of the same name has already been
377 defined by the same defining loader, and if so, replaces the
378 newly created class by the one defined earlier. */
380 /* Important: The classinfo given to classcache_store must be
381 fully prepared because another thread may return
382 this pointer after the lookup at to top of this
383 function directly after the class cache lock has
386 c = classcache_store(cl, c, true);
392 /* class_load_attribute_sourcefile *********************************************
394 SourceFile_attribute {
395 u2 attribute_name_index;
400 *******************************************************************************/
402 static bool class_load_attribute_sourcefile(classbuffer *cb)
413 /* check buffer size */
415 if (!suck_check_classbuffer_size(cb, 4 + 2))
418 /* check attribute length */
420 attribute_length = suck_u4(cb);
422 if (attribute_length != 2) {
423 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
427 /* there can be no more than one SourceFile attribute */
429 if (c->sourcefile != NULL) {
430 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
436 sourcefile_index = suck_u2(cb);
437 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
439 if (sourcefile == NULL)
442 /* store sourcefile */
444 c->sourcefile = sourcefile;
450 /* class_load_attribute_enclosingmethod ****************************************
452 EnclosingMethod_attribute {
453 u2 attribute_name_index;
459 *******************************************************************************/
461 #if defined(ENABLE_JAVASE)
462 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
468 classref_or_classinfo cr;
469 constant_nameandtype *cn;
475 /* check buffer size */
477 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
480 /* check attribute length */
482 attribute_length = suck_u4(cb);
484 if (attribute_length != 4) {
485 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
489 /* there can be no more than one EnclosingMethod attribute */
491 if (c->enclosingmethod != NULL) {
492 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
496 /* get class index */
498 class_index = suck_u2(cb);
499 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
501 /* get method index */
503 method_index = suck_u2(cb);
504 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
506 /* store info in classinfo */
508 c->enclosingclass.any = cr.any;
509 c->enclosingmethod = cn;
513 #endif /* defined(ENABLE_JAVASE) */
516 /* class_load_attributes *******************************************************
518 Read attributes from ClassFile.
521 u2 attribute_name_index;
523 u1 info[attribute_length];
526 InnerClasses_attribute {
527 u2 attribute_name_index;
531 *******************************************************************************/
533 bool class_load_attributes(classbuffer *cb)
536 uint16_t attributes_count;
537 uint16_t attribute_name_index;
539 innerclassinfo *info;
540 classref_or_classinfo inner;
541 classref_or_classinfo outer;
548 /* get attributes count */
550 if (!suck_check_classbuffer_size(cb, 2))
553 attributes_count = suck_u2(cb);
555 for (i = 0; i < attributes_count; i++) {
556 /* get attribute name */
558 if (!suck_check_classbuffer_size(cb, 2))
561 attribute_name_index = suck_u2(cb);
563 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
565 if (attribute_name == NULL)
568 if (attribute_name == utf_InnerClasses) {
571 if (c->innerclass != NULL) {
572 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
576 if (!suck_check_classbuffer_size(cb, 4 + 2))
579 /* skip attribute length */
582 /* number of records */
583 c->innerclasscount = suck_u2(cb);
585 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
588 /* allocate memory for innerclass structure */
589 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
591 for (j = 0; j < c->innerclasscount; j++) {
592 /* The innerclass structure contains a class with an encoded
593 name, its defining scope, its simple name and a bitmask of
596 info = c->innerclass + j;
598 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
599 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
600 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
603 /* If the current inner-class is the currently loaded
604 class check for some special flags. */
606 if (inner.ref->name == c->name) {
607 /* If an inner-class is not a member, its
608 outer-class is NULL. */
610 if (outer.ref != NULL) {
611 c->flags |= ACC_CLASS_MEMBER;
613 /* A member class doesn't have an
614 EnclosingMethod attribute, so set the
615 enclosing-class to be the same as the
618 c->declaringclass = outer;
619 c->enclosingclass = outer;
622 /* If an inner-class is anonymous, its name is
626 c->flags |= ACC_CLASS_ANONYMOUS;
629 info->inner_class = inner;
630 info->outer_class = outer;
635 else if (attribute_name == utf_SourceFile) {
638 if (!class_load_attribute_sourcefile(cb))
641 #if defined(ENABLE_JAVASE)
642 else if (attribute_name == utf_EnclosingMethod) {
643 /* EnclosingMethod */
645 if (!class_load_attribute_enclosingmethod(cb))
648 else if (attribute_name == utf_Signature) {
651 if (!loader_load_attribute_signature(cb, &(c->signature)))
656 #if defined(ENABLE_ANNOTATIONS)
657 /* XXX We can't do a release with that enabled */
659 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
660 /* RuntimeVisibleAnnotations */
661 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
664 /* XXX RuntimeInvisibleAnnotations should only be loaded
665 * (or returned to Java) if some commandline options says so.
666 * Currently there is no such option available in cacao,
667 * therefore I load them allways (for testing purpose).
668 * Anyway, bytecode for RuntimeInvisibleAnnotations is only
669 * generated if you tell javac to do so. So in most cases
670 * there won't be any.
672 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
673 /* RuntimeInvisibleAnnotations */
674 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
680 /* unknown attribute */
682 if (!loader_skip_attribute_body(cb))
691 /* class_freepool **************************************************************
693 Frees all resources used by this classes Constant Pool.
695 *******************************************************************************/
697 static void class_freecpool(classinfo *c)
703 if (c->cptags && c->cpinfos) {
704 for (idx = 0; idx < c->cpcount; idx++) {
705 tag = c->cptags[idx];
706 info = c->cpinfos[idx];
710 case CONSTANT_Fieldref:
711 case CONSTANT_Methodref:
712 case CONSTANT_InterfaceMethodref:
713 FREE(info, constant_FMIref);
715 case CONSTANT_Integer:
716 FREE(info, constant_integer);
719 FREE(info, constant_float);
722 FREE(info, constant_long);
724 case CONSTANT_Double:
725 FREE(info, constant_double);
727 case CONSTANT_NameAndType:
728 FREE(info, constant_nameandtype);
736 MFREE(c->cptags, u1, c->cpcount);
739 MFREE(c->cpinfos, voidptr, c->cpcount);
743 /* class_getconstant ***********************************************************
745 Retrieves the value at position 'pos' of the constantpool of a
746 class. If the type of the value is other than 'ctype', an error is
749 *******************************************************************************/
751 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
753 /* check index and type of constantpool entry */
754 /* (pos == 0 is caught by type comparison) */
756 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
757 exceptions_throw_classformaterror(c, "Illegal constant pool index");
761 return c->cpinfos[pos];
765 /* innerclass_getconstant ******************************************************
767 Like class_getconstant, but if cptags is ZERO, null is returned.
769 *******************************************************************************/
771 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
773 /* invalid position in constantpool */
775 if (pos >= c->cpcount) {
776 exceptions_throw_classformaterror(c, "Illegal constant pool index");
780 /* constantpool entry of type 0 */
782 if (c->cptags[pos] == 0)
785 /* check type of constantpool entry */
787 if (c->cptags[pos] != ctype) {
788 exceptions_throw_classformaterror(c, "Illegal constant pool index");
792 return c->cpinfos[pos];
796 /* class_free ******************************************************************
798 Frees all resources used by the class.
800 *******************************************************************************/
802 void class_free(classinfo *c)
810 MFREE(c->interfaces, classinfo*, c->interfacescount);
813 for (i = 0; i < c->fieldscount; i++)
814 field_free(&(c->fields[i]));
815 #if defined(ENABLE_CACAO_GC)
816 MFREE(c->fields, fieldinfo, c->fieldscount);
821 for (i = 0; i < c->methodscount; i++)
822 method_free(&(c->methods[i]));
823 MFREE(c->methods, methodinfo, c->methodscount);
826 if ((v = c->vftbl) != NULL) {
828 mem_free(v->arraydesc,sizeof(arraydescriptor));
830 for (i = 0; i < v->interfacetablelength; i++) {
831 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
833 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
835 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
836 sizeof(methodptr*) * (v->interfacetablelength -
837 (v->interfacetablelength > 0));
838 v = (vftbl_t*) (((methodptr*) v) -
839 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
844 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
846 /* if (c->classvftbl)
847 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
851 #if defined(ENABLE_ANNOTATIONS)
852 annotation_bytearray_free(c->annotations);
854 annotation_bytearrays_free(c->method_annotations);
855 annotation_bytearrays_free(c->method_parameterannotations);
856 annotation_bytearrays_free(c->method_annotationdefaults);
858 annotation_bytearrays_free(c->field_annotations);
863 /* get_array_class *************************************************************
865 Returns the array class with the given name for the given
866 classloader, or NULL if an exception occurred.
868 Note: This function does eager loading.
870 *******************************************************************************/
872 static classinfo *get_array_class(utf *name,classloader *initloader,
873 classloader *defloader,bool link)
877 /* lookup this class in the classcache */
878 c = classcache_lookup(initloader,name);
880 c = classcache_lookup_defined(defloader,name);
883 /* we have to create it */
884 c = class_create_classinfo(name);
885 c = load_newly_created_array(c,initloader);
891 assert(c->state & CLASS_LOADED);
892 assert(c->classloader == defloader);
894 if (link && !(c->state & CLASS_LINKED))
898 assert(!link || (c->state & CLASS_LINKED));
904 /* class_array_of **************************************************************
906 Returns an array class with the given component class. The array
907 class is dynamically created if neccessary.
909 *******************************************************************************/
911 classinfo *class_array_of(classinfo *component, bool link)
920 cl = component->classloader;
922 dumpsize = dump_size();
924 /* Assemble the array class name */
925 namelen = component->name->blength;
927 if (component->name->text[0] == '[') {
928 /* the component is itself an array */
929 namebuf = DMNEW(char, namelen + 1);
931 MCOPY(namebuf + 1, component->name->text, char, namelen);
935 /* the component is a non-array class */
936 namebuf = DMNEW(char, namelen + 3);
939 MCOPY(namebuf + 2, component->name->text, char, namelen);
940 namebuf[2 + namelen] = ';';
944 u = utf_new(namebuf, namelen);
946 c = get_array_class(u, cl, cl, link);
948 dump_release(dumpsize);
954 /* class_multiarray_of *********************************************************
956 Returns an array class with the given dimension and element class.
957 The array class is dynamically created if neccessary.
959 *******************************************************************************/
961 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
968 dumpsize = dump_size();
971 log_text("Invalid array dimension requested");
975 /* Assemble the array class name */
976 namelen = element->name->blength;
978 if (element->name->text[0] == '[') {
979 /* the element is itself an array */
980 namebuf = DMNEW(char, namelen + dim);
981 memcpy(namebuf + dim, element->name->text, namelen);
985 /* the element is a non-array class */
986 namebuf = DMNEW(char, namelen + 2 + dim);
988 memcpy(namebuf + dim + 1, element->name->text, namelen);
989 namelen += (2 + dim);
990 namebuf[namelen - 1] = ';';
992 memset(namebuf, '[', dim);
994 c = get_array_class(utf_new(namebuf, namelen),
995 element->classloader,
996 element->classloader,
999 dump_release(dumpsize);
1005 /* class_lookup_classref *******************************************************
1007 Looks up the constant_classref for a given classname in the classref
1011 cls..............the class containing the reference
1012 name.............the name of the class refered to
1015 a pointer to a constant_classref, or
1016 NULL if the reference was not found
1018 *******************************************************************************/
1020 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
1022 constant_classref *ref;
1023 extra_classref *xref;
1028 assert(!cls->classrefcount || cls->classrefs);
1030 /* first search the main classref table */
1031 count = cls->classrefcount;
1032 ref = cls->classrefs;
1033 for (; count; --count, ++ref)
1034 if (ref->name == name)
1037 /* next try the list of extra classrefs */
1038 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1039 if (xref->classref.name == name)
1040 return &(xref->classref);
1048 /* class_get_classref **********************************************************
1050 Returns the constant_classref for a given classname.
1053 cls..............the class containing the reference
1054 name.............the name of the class refered to
1057 a pointer to a constant_classref (never NULL)
1060 The given name is not checked for validity!
1062 *******************************************************************************/
1064 constant_classref *class_get_classref(classinfo *cls, utf *name)
1066 constant_classref *ref;
1067 extra_classref *xref;
1072 ref = class_lookup_classref(cls,name);
1076 xref = NEW(extra_classref);
1077 CLASSREF_INIT(xref->classref,cls,name);
1079 xref->next = cls->extclassrefs;
1080 cls->extclassrefs = xref;
1082 return &(xref->classref);
1086 /* class_get_self_classref *****************************************************
1088 Returns the constant_classref to the class itself.
1091 cls..............the class containing the reference
1094 a pointer to a constant_classref (never NULL)
1096 *******************************************************************************/
1098 constant_classref *class_get_self_classref(classinfo *cls)
1100 /* XXX this should be done in a faster way. Maybe always make */
1101 /* the classref of index 0 a self reference. */
1102 return class_get_classref(cls,cls->name);
1105 /* class_get_classref_multiarray_of ********************************************
1107 Returns an array type reference with the given dimension and element class
1111 dim..............the requested dimension
1112 dim must be in [1;255]. This is NOT checked!
1113 ref..............the component class reference
1116 a pointer to the class reference for the array type
1119 The referer of `ref` is used as the referer for the new classref.
1121 *******************************************************************************/
1123 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1128 constant_classref *cr;
1131 assert(dim >= 1 && dim <= 255);
1133 dumpsize = dump_size();
1135 /* Assemble the array class name */
1136 namelen = ref->name->blength;
1138 if (ref->name->text[0] == '[') {
1139 /* the element is itself an array */
1140 namebuf = DMNEW(char, namelen + dim);
1141 memcpy(namebuf + dim, ref->name->text, namelen);
1145 /* the element is a non-array class */
1146 namebuf = DMNEW(char, namelen + 2 + dim);
1148 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1149 namelen += (2 + dim);
1150 namebuf[namelen - 1] = ';';
1152 memset(namebuf, '[', dim);
1154 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1156 dump_release(dumpsize);
1162 /* class_get_classref_component_of *********************************************
1164 Returns the component classref of a given array type reference
1167 ref..............the array type reference
1170 a reference to the component class, or
1171 NULL if `ref` is not an object array type reference
1174 The referer of `ref` is used as the referer for the new classref.
1176 *******************************************************************************/
1178 constant_classref *class_get_classref_component_of(constant_classref *ref)
1185 name = ref->name->text;
1189 namelen = ref->name->blength - 1;
1194 else if (*name != '[') {
1198 return class_get_classref(ref->referer, utf_new(name, namelen));
1202 /* class_findmethod ************************************************************
1204 Searches a 'classinfo' structure for a method having the given name
1205 and descriptor. If descriptor is NULL, it is ignored.
1207 *******************************************************************************/
1209 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1214 for (i = 0; i < c->methodscount; i++) {
1215 m = &(c->methods[i]);
1217 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1225 /* class_resolvemethod *********************************************************
1227 Searches a class and it's super classes for a method.
1229 Superinterfaces are *not* searched.
1231 *******************************************************************************/
1233 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1238 m = class_findmethod(c, name, desc);
1243 /* JVM Specification bug:
1245 It is important NOT to resolve special <init> and <clinit>
1246 methods to super classes or interfaces; yet, this is not
1247 explicited in the specification. Section 5.4.3.3 should be
1248 updated appropriately. */
1250 if (name == utf_init || name == utf_clinit)
1260 /* class_resolveinterfacemethod_intern *****************************************
1262 Internally used helper function. Do not use this directly.
1264 *******************************************************************************/
1266 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1267 utf *name, utf *desc)
1272 /* try to find the method in the class */
1274 m = class_findmethod(c, name, desc);
1279 /* no method found? try the superinterfaces */
1281 for (i = 0; i < c->interfacescount; i++) {
1282 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1289 /* no method found */
1295 /* class_resolveclassmethod ****************************************************
1297 Resolves a reference from REFERER to a method with NAME and DESC in
1300 If the method cannot be resolved the return value is NULL. If
1301 EXCEPT is true *exceptionptr is set, too.
1303 *******************************************************************************/
1305 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1306 classinfo *referer, bool throwexception)
1312 /* if (c->flags & ACC_INTERFACE) { */
1313 /* if (throwexception) */
1314 /* *exceptionptr = */
1315 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1319 /* try class c and its superclasses */
1323 m = class_resolvemethod(cls, name, desc);
1328 /* try the superinterfaces */
1330 for (i = 0; i < c->interfacescount; i++) {
1331 m = class_resolveinterfacemethod_intern(c->interfaces[i].cls,
1339 exceptions_throw_nosuchmethoderror(c, name, desc);
1344 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1346 exceptions_throw_abstractmethoderror();
1351 /* XXX check access rights */
1357 /* class_resolveinterfacemethod ************************************************
1359 Resolves a reference from REFERER to a method with NAME and DESC in
1362 If the method cannot be resolved the return value is NULL. If
1363 EXCEPT is true *exceptionptr is set, too.
1365 *******************************************************************************/
1367 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1368 classinfo *referer, bool throwexception)
1372 if (!(c->flags & ACC_INTERFACE)) {
1374 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1379 mi = class_resolveinterfacemethod_intern(c, name, desc);
1384 /* try class java.lang.Object */
1386 mi = class_findmethod(class_java_lang_Object, name, desc);
1392 exceptions_throw_nosuchmethoderror(c, name, desc);
1398 /* class_findfield *************************************************************
1400 Searches for field with specified name and type in a classinfo
1401 structure. If no such field is found NULL is returned.
1403 *******************************************************************************/
1405 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1409 for (i = 0; i < c->fieldscount; i++)
1410 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1411 return &(c->fields[i]);
1414 return class_findfield(c->super.cls, name, desc);
1420 /* class_findfield_approx ******************************************************
1422 Searches in 'classinfo'-structure for a field with the specified
1425 *******************************************************************************/
1427 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1431 /* get field index */
1433 i = class_findfield_index_by_name(c, name);
1435 /* field was not found, return */
1440 /* return field address */
1442 return &(c->fields[i]);
1446 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1450 for (i = 0; i < c->fieldscount; i++) {
1451 /* compare field names */
1453 if ((c->fields[i].name == name))
1457 /* field was not found, raise exception */
1459 exceptions_throw_nosuchfielderror(c, name);
1465 /****************** Function: class_resolvefield_int ***************************
1467 This is an internally used helper function. Do not use this directly.
1469 Tries to resolve a field having the given name and type.
1470 If the field cannot be resolved, NULL is returned.
1472 *******************************************************************************/
1474 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1479 /* search for field in class c */
1481 for (i = 0; i < c->fieldscount; i++) {
1482 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1483 return &(c->fields[i]);
1487 /* try superinterfaces recursively */
1489 for (i = 0; i < c->interfacescount; i++) {
1490 fi = class_resolvefield_int(c->interfaces[i].cls, name, desc);
1495 /* try superclass */
1498 return class_resolvefield_int(c->super.cls, name, desc);
1506 /********************* Function: class_resolvefield ***************************
1508 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1510 If the field cannot be resolved the return value is NULL. If EXCEPT is
1511 true *exceptionptr is set, too.
1513 *******************************************************************************/
1515 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1516 classinfo *referer, bool throwexception)
1520 fi = class_resolvefield_int(c, name, desc);
1524 exceptions_throw_nosuchfielderror(c, name);
1529 /* XXX check access rights */
1535 /* class_resolve_superclass ****************************************************
1537 Resolves the super class reference of the given class if necessary.
1539 *******************************************************************************/
1541 static classinfo *class_resolve_superclass(classinfo *c)
1545 if (c->super.any == NULL)
1548 /* Check if the super class is a reference. */
1550 if (IS_CLASSREF(c->super)) {
1551 /* XXX I'm very sure this is not correct. */
1552 super = resolve_classref_or_classinfo_eager(c->super, true);
1553 /* super = resolve_classref_or_classinfo_eager(c->super, false); */
1558 /* Store the resolved super class in the class structure. */
1560 c->super.cls = super;
1563 return c->super.cls;
1567 /* class_issubclass ************************************************************
1569 Checks if sub is a descendant of super.
1571 *******************************************************************************/
1573 bool class_issubclass(classinfo *sub, classinfo *super)
1582 /* sub = class_resolve_superclass(sub); */
1583 if (sub->super.any == NULL)
1586 assert(IS_CLASSREF(sub->super) == 0);
1588 sub = sub->super.cls;
1593 /* class_isanysubclass *********************************************************
1595 Checks a subclass relation between two classes. Implemented
1596 interfaces are interpreted as super classes.
1598 Return value: 1 ... sub is subclass of super
1601 *******************************************************************************/
1603 bool class_isanysubclass(classinfo *sub, classinfo *super)
1608 /* This is the trivial case. */
1613 /* Primitive classes are only subclasses of themselves. */
1615 if (class_is_primitive(sub) || class_is_primitive(super))
1618 /* Check for interfaces. */
1620 if (super->flags & ACC_INTERFACE) {
1621 result = (sub->vftbl->interfacetablelength > super->index) &&
1622 (sub->vftbl->interfacetable[-super->index] != NULL);
1625 /* java.lang.Object is the only super class of any
1628 if (sub->flags & ACC_INTERFACE)
1629 return (super == class_java_lang_Object);
1631 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1633 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1634 result = diffval <= (uint32_t) super->vftbl->diffval;
1636 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1643 /* class_is_primitive **********************************************************
1645 Checks if the given class is a primitive class.
1647 *******************************************************************************/
1649 bool class_is_primitive(classinfo *c)
1651 if (c->flags & ACC_CLASS_PRIMITIVE)
1658 /* class_is_anonymousclass *****************************************************
1660 Checks if the given class is an anonymous class.
1662 *******************************************************************************/
1664 bool class_is_anonymousclass(classinfo *c)
1666 if (c->flags & ACC_CLASS_ANONYMOUS)
1673 /* class_is_array **************************************************************
1675 Checks if the given class is an array class.
1677 *******************************************************************************/
1679 bool class_is_array(classinfo *c)
1681 if (!(c->state & CLASS_LINKED))
1685 return (c->vftbl->arraydesc != NULL);
1689 /* class_is_interface **********************************************************
1691 Checks if the given class is an interface.
1693 *******************************************************************************/
1695 bool class_is_interface(classinfo *c)
1697 if (c->flags & ACC_INTERFACE)
1704 /* class_is_localclass *********************************************************
1706 Checks if the given class is a local class.
1708 *******************************************************************************/
1710 bool class_is_localclass(classinfo *c)
1712 if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
1719 /* class_is_memberclass ********************************************************
1721 Checks if the given class is a member class.
1723 *******************************************************************************/
1725 bool class_is_memberclass(classinfo *c)
1727 if (c->flags & ACC_CLASS_MEMBER)
1734 /* class_get_superclass ********************************************************
1736 Return the super class of the given class. If the super-field is a
1737 class-reference, resolve it and store it in the classinfo.
1739 *******************************************************************************/
1741 classinfo *class_get_superclass(classinfo *c)
1745 /* For java.lang.Object, primitive and Void classes we return
1748 if (c->super.any == NULL)
1751 /* For interfaces we also return NULL. */
1753 if (c->flags & ACC_INTERFACE)
1756 /* We may have to resolve the super class reference. */
1758 super = class_resolve_superclass(c);
1764 /* class_get_componenttype *****************************************************
1766 Return the component class of the given class. If the given class
1767 is not an array, return NULL.
1769 *******************************************************************************/
1771 classinfo *class_get_componenttype(classinfo *c)
1773 classinfo *component;
1774 arraydescriptor *ad;
1776 /* XXX maybe we could find a way to do this without linking. */
1777 /* This way should be safe and easy, however. */
1779 if (!(c->state & CLASS_LINKED))
1783 ad = c->vftbl->arraydesc;
1788 if (ad->arraytype == ARRAYTYPE_OBJECT)
1789 component = ad->componentvftbl->class;
1791 component = primitive_class_get_by_type(ad->arraytype);
1797 /* class_get_declaredclasses ***************************************************
1799 Return an array of declared classes of the given class.
1801 *******************************************************************************/
1803 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1805 classref_or_classinfo inner;
1806 classref_or_classinfo outer;
1808 int declaredclasscount; /* number of declared classes */
1809 int pos; /* current declared class */
1810 java_handle_objectarray_t *oa; /* array of declared classes */
1814 declaredclasscount = 0;
1816 if (!class_is_primitive(c) && !class_is_array(c)) {
1817 /* Determine number of declared classes. */
1819 for (i = 0; i < c->innerclasscount; i++) {
1820 /* Get outer-class. If the inner-class is not a member
1821 class, the outer-class is NULL. */
1823 outer = c->innerclass[i].outer_class;
1825 if (outer.any == NULL)
1828 /* Check if outer-class is a classref or a real class and
1829 get the class name from the structure. */
1831 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1833 /* Outer class is this class. */
1835 if ((outername == c->name) &&
1836 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1837 declaredclasscount++;
1841 /* Allocate Class[] and check for OOM. */
1843 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1848 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1849 inner = c->innerclass[i].inner_class;
1850 outer = c->innerclass[i].outer_class;
1852 /* Get outer-class. If the inner-class is not a member class,
1853 the outer-class is NULL. */
1855 if (outer.any == NULL)
1858 /* Check if outer_class is a classref or a real class and get
1859 the class name from the structure. */
1861 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1863 /* Outer class is this class. */
1865 if ((outername == c->name) &&
1866 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1868 ic = resolve_classref_or_classinfo_eager(inner, false);
1873 if (!(ic->state & CLASS_LINKED))
1874 if (!link_class(ic))
1877 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1885 /* class_get_declaringclass ****************************************************
1887 If the class or interface given is a member of another class,
1888 return the declaring class. For array and primitive classes return
1891 *******************************************************************************/
1893 classinfo *class_get_declaringclass(classinfo *c)
1895 classref_or_classinfo cr;
1898 /* Get declaring class. */
1900 cr = c->declaringclass;
1905 /* Resolve the class if necessary. */
1907 if (IS_CLASSREF(cr)) {
1908 /* dc = resolve_classref_eager(cr.ref); */
1909 dc = resolve_classref_or_classinfo_eager(cr, true);
1914 /* Store the resolved class in the class structure. */
1925 /* class_get_enclosingclass ****************************************************
1927 Return the enclosing class for the given class.
1929 *******************************************************************************/
1931 classinfo *class_get_enclosingclass(classinfo *c)
1933 classref_or_classinfo cr;
1936 /* Get enclosing class. */
1938 cr = c->enclosingclass;
1943 /* Resolve the class if necessary. */
1945 if (IS_CLASSREF(cr)) {
1946 /* ec = resolve_classref_eager(cr.ref); */
1947 ec = resolve_classref_or_classinfo_eager(cr, true);
1952 /* Store the resolved class in the class structure. */
1963 /* class_get_interfaces ********************************************************
1965 Return an array of interfaces of the given class.
1967 *******************************************************************************/
1969 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1972 java_handle_objectarray_t *oa;
1975 if (!(c->state & CLASS_LINKED))
1979 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1984 for (i = 0; i < c->interfacescount; i++) {
1985 ic = c->interfaces[i].cls;
1987 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1994 /* class_get_annotations *******************************************************
1996 Return the unparsed declared annotations in an byte array
1997 of the given class (or NULL if there aren't any).
1999 *******************************************************************************/
2001 java_handle_bytearray_t *class_get_annotations(classinfo *c)
2003 #if defined(ENABLE_ANNOTATIONS)
2004 return c->annotations;
2011 /* class_get_signature *********************************************************
2013 Return the signature of the given class. For array and primitive
2014 classes return NULL.
2016 *******************************************************************************/
2018 #if defined(ENABLE_JAVASE)
2019 utf *class_get_signature(classinfo *c)
2021 /* For array and primitive classes return NULL. */
2023 if (class_is_array(c) || class_is_primitive(c))
2026 return c->signature;
2031 /* class_printflags ************************************************************
2033 Prints flags of a class.
2035 *******************************************************************************/
2037 #if !defined(NDEBUG)
2038 void class_printflags(classinfo *c)
2045 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
2046 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
2047 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
2048 if (c->flags & ACC_STATIC) printf(" STATIC");
2049 if (c->flags & ACC_FINAL) printf(" FINAL");
2050 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2051 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
2052 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
2053 if (c->flags & ACC_NATIVE) printf(" NATIVE");
2054 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
2055 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
2060 /* class_print *****************************************************************
2062 Prints classname plus flags.
2064 *******************************************************************************/
2066 #if !defined(NDEBUG)
2067 void class_print(classinfo *c)
2074 utf_display_printable_ascii(c->name);
2075 class_printflags(c);
2080 /* class_classref_print ********************************************************
2082 Prints classname plus referer class.
2084 *******************************************************************************/
2086 #if !defined(NDEBUG)
2087 void class_classref_print(constant_classref *cr)
2094 utf_display_printable_ascii(cr->name);
2097 class_print(cr->referer);
2105 /* class_println ***************************************************************
2107 Prints classname plus flags and new line.
2109 *******************************************************************************/
2111 #if !defined(NDEBUG)
2112 void class_println(classinfo *c)
2120 /* class_classref_println ******************************************************
2122 Prints classname plus referer class and new line.
2124 *******************************************************************************/
2126 #if !defined(NDEBUG)
2127 void class_classref_println(constant_classref *cr)
2129 class_classref_print(cr);
2135 /* class_classref_or_classinfo_print *******************************************
2137 Prints classname plus referer class.
2139 *******************************************************************************/
2141 #if !defined(NDEBUG)
2142 void class_classref_or_classinfo_print(classref_or_classinfo c)
2144 if (c.any == NULL) {
2145 printf("(classref_or_classinfo) NULL");
2149 class_classref_print(c.ref);
2156 /* class_classref_or_classinfo_println *****************************************
2158 Prints classname plus referer class and a newline.
2160 *******************************************************************************/
2162 void class_classref_or_classinfo_println(classref_or_classinfo c)
2164 class_classref_or_classinfo_println(c);
2169 /* class_showconstantpool ******************************************************
2171 Dump the constant pool of the given class to stdout.
2173 *******************************************************************************/
2175 #if !defined(NDEBUG)
2176 void class_showconstantpool (classinfo *c)
2181 printf ("---- dump of constant pool ----\n");
2183 for (i=0; i<c->cpcount; i++) {
2184 printf ("#%d: ", (int) i);
2186 e = c -> cpinfos [i];
2189 switch (c -> cptags [i]) {
2190 case CONSTANT_Class:
2191 printf ("Classreference -> ");
2192 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2194 case CONSTANT_Fieldref:
2195 printf ("Fieldref -> ");
2196 field_fieldref_print((constant_FMIref *) e);
2198 case CONSTANT_Methodref:
2199 printf ("Methodref -> ");
2200 method_methodref_print((constant_FMIref *) e);
2202 case CONSTANT_InterfaceMethodref:
2203 printf ("InterfaceMethod -> ");
2204 method_methodref_print((constant_FMIref *) e);
2206 case CONSTANT_String:
2207 printf ("String -> ");
2208 utf_display_printable_ascii (e);
2210 case CONSTANT_Integer:
2211 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2213 case CONSTANT_Float:
2214 printf ("Float -> %f", ((constant_float*)e) -> value);
2216 case CONSTANT_Double:
2217 printf ("Double -> %f", ((constant_double*)e) -> value);
2221 u8 v = ((constant_long*)e) -> value;
2223 printf ("Long -> %ld", (long int) v);
2225 printf ("Long -> HI: %ld, LO: %ld\n",
2226 (long int) v.high, (long int) v.low);
2230 case CONSTANT_NameAndType:
2232 constant_nameandtype *cnt = e;
2233 printf ("NameAndType: ");
2234 utf_display_printable_ascii (cnt->name);
2236 utf_display_printable_ascii (cnt->descriptor);
2240 printf ("Utf8 -> ");
2241 utf_display_printable_ascii (e);
2244 log_text("Invalid type of ConstantPool-Entry");
2252 #endif /* !defined(NDEBUG) */
2255 /* class_showmethods ***********************************************************
2257 Dump info about the fields and methods of the given class to stdout.
2259 *******************************************************************************/
2261 #if !defined(NDEBUG)
2262 void class_showmethods (classinfo *c)
2266 printf("--------- Fields and Methods ----------------\n");
2268 class_printflags(c);
2272 utf_display_printable_ascii(c->name);
2277 utf_display_printable_ascii(c->super.cls->name);
2281 printf("Index: %d\n", c->index);
2283 printf("Interfaces:\n");
2284 for (i = 0; i < c->interfacescount; i++) {
2286 utf_display_printable_ascii(c->interfaces[i].cls->name);
2287 printf (" (%d)\n", c->interfaces[i].cls->index);
2290 printf("Fields:\n");
2291 for (i = 0; i < c->fieldscount; i++)
2292 field_println(&(c->fields[i]));
2294 printf("Methods:\n");
2295 for (i = 0; i < c->methodscount; i++) {
2296 methodinfo *m = &(c->methods[i]);
2298 if (!(m->flags & ACC_STATIC))
2299 printf("vftblindex: %d ", m->vftblindex);
2304 printf ("Virtual function table:\n");
2305 for (i = 0; i < c->vftbl->vftbllength; i++)
2306 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2308 #endif /* !defined(NDEBUG) */
2312 * These are local overrides for various environment variables in Emacs.
2313 * Please do not remove this and leave it at the end of the file, where
2314 * Emacs will automagically detect them.
2315 * ---------------------------------------------------------------------
2318 * indent-tabs-mode: t
2322 * vim:noexpandtab:sw=4:ts=4: