1 /* src/vmcore/class.c - class related functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 #include "mm/memory.h"
39 #include "native/llni.h"
41 #include "threads/lock-common.h"
43 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/resolve.h"
51 #include "vm/jit/asmpart.h"
53 #include "vmcore/class.h"
54 #include "vmcore/classcache.h"
55 #include "vmcore/linker.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/statistics.h"
63 #include "vmcore/suck.h"
64 #include "vmcore/utf8.h"
67 /* global variables ***********************************************************/
69 /* frequently used classes ****************************************************/
71 /* Important system classes. */
73 classinfo *class_java_lang_Object;
74 classinfo *class_java_lang_Class;
75 classinfo *class_java_lang_ClassLoader;
76 classinfo *class_java_lang_Cloneable;
77 classinfo *class_java_lang_SecurityManager;
78 classinfo *class_java_lang_String;
79 classinfo *class_java_lang_System;
80 classinfo *class_java_lang_Thread;
81 classinfo *class_java_lang_ThreadGroup;
82 classinfo *class_java_lang_Throwable;
83 classinfo *class_java_io_Serializable;
85 #if defined(WITH_CLASSPATH_GNU)
86 classinfo *class_java_lang_VMSystem;
87 classinfo *class_java_lang_VMThread;
88 classinfo *class_java_lang_VMThrowable;
91 /* Important system exceptions. */
93 classinfo *class_java_lang_Exception;
94 classinfo *class_java_lang_ClassNotFoundException;
95 classinfo *class_java_lang_RuntimeException;
97 #if defined(WITH_CLASSPATH_SUN)
98 classinfo *class_sun_reflect_MagicAccessorImpl;
101 #if defined(ENABLE_JAVASE)
102 classinfo *class_java_lang_Void;
104 classinfo *class_java_lang_Boolean;
105 classinfo *class_java_lang_Byte;
106 classinfo *class_java_lang_Character;
107 classinfo *class_java_lang_Short;
108 classinfo *class_java_lang_Integer;
109 classinfo *class_java_lang_Long;
110 classinfo *class_java_lang_Float;
111 classinfo *class_java_lang_Double;
113 /* some classes which may be used more often */
115 #if defined(ENABLE_JAVASE)
116 classinfo *class_java_lang_StackTraceElement;
117 classinfo *class_java_lang_reflect_Constructor;
118 classinfo *class_java_lang_reflect_Field;
119 classinfo *class_java_lang_reflect_Method;
120 classinfo *class_java_security_PrivilegedAction;
121 classinfo *class_java_util_Vector;
122 classinfo *class_java_util_HashMap;
124 classinfo *arrayclass_java_lang_Object;
126 # if defined(ENABLE_ANNOTATIONS)
127 classinfo *class_sun_reflect_ConstantPool;
128 # if defined(WITH_CLASSPATH_GNU)
129 classinfo *class_sun_reflect_annotation_AnnotationParser;
134 /* pseudo classes for the typechecker */
136 classinfo *pseudo_class_Arraystub;
137 classinfo *pseudo_class_Null;
138 classinfo *pseudo_class_New;
141 /* class_set_packagename *******************************************************
143 Derive the package name from the class name and store it in the
146 An internal package name consists of the package name plus the
147 trailing '/', e.g. "java/lang/".
149 For classes in the unnamed package, the package name is set to
152 *******************************************************************************/
154 void class_set_packagename(classinfo *c)
159 p = UTF_END(c->name) - 1;
160 start = c->name->text;
162 if (c->name->text[0] == '[') {
163 /* Set packagename of arrays to the element's package. */
165 for (; *start == '['; start++);
167 /* Skip the 'L' in arrays of references. */
173 /* Search for last '/'. */
175 for (; (p > start) && (*p != '/'); --p);
177 /* If we found a '/' we set the package name plus the trailing
178 '/'. Otherwise we set the packagename to NULL. */
181 c->packagename = utf_new(start, p - start + 1);
183 c->packagename = NULL;
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 #if !defined(ENABLE_GC_BOEHM)
218 c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
219 /*c = NEW(classinfo);
220 MZERO(c, classinfo, 1);*/
222 c = GCNEW_UNCOLLECTABLE(classinfo, 1);
223 /* GCNEW_UNCOLLECTABLE clears the allocated memory */
228 /* Set the header.vftbl of all loaded classes to the one of
229 java.lang.Class, so Java code can use a class as object. */
231 if (class_java_lang_Class != NULL)
232 if (class_java_lang_Class->vftbl != NULL)
233 c->object.header.vftbl = class_java_lang_Class->vftbl;
235 #if defined(ENABLE_JAVASE)
236 /* check if the class is a reference class and flag it */
238 if (classname == utf_java_lang_ref_SoftReference) {
239 c->flags |= ACC_CLASS_REFERENCE_SOFT;
241 else if (classname == utf_java_lang_ref_WeakReference) {
242 c->flags |= ACC_CLASS_REFERENCE_WEAK;
244 else if (classname == utf_java_lang_ref_PhantomReference) {
245 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
249 if (classname != utf_not_named_yet)
250 class_set_packagename(c);
252 LOCK_INIT_OBJECT_LOCK(&c->object.header);
258 /* class_postset_header_vftbl **************************************************
260 Set the header.vftbl of all classes created before java.lang.Class
261 was linked. This is necessary that Java code can use a class as
264 *******************************************************************************/
266 void class_postset_header_vftbl(void)
270 classcache_name_entry *nmen;
271 classcache_class_entry *clsen;
273 assert(class_java_lang_Class);
275 for (slot = 0; slot < hashtable_classcache.size; slot++) {
276 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
278 for (; nmen; nmen = nmen->hashlink) {
279 /* iterate over all class entries */
281 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
284 /* now set the the vftbl */
286 if (c->object.header.vftbl == NULL)
287 c->object.header.vftbl = class_java_lang_Class->vftbl;
293 /* class_define ****************************************************************
295 Calls the loader and defines a class in the VM.
297 *******************************************************************************/
299 classinfo *class_define(utf *name, classloader *cl, int32_t length, uint8_t *data, java_handle_t *pd)
306 /* check if this class has already been defined */
308 c = classcache_lookup_defined_or_initiated(cl, name);
311 exceptions_throw_linkageerror("duplicate class definition: ", c);
316 /* create a new classinfo struct */
318 c = class_create_classinfo(name);
320 #if defined(ENABLE_STATISTICS)
323 if (opt_getloadingtime)
327 /* build a classbuffer with the given data */
329 cb = NEW(classbuffer);
336 /* preset the defining classloader */
340 /* load the class from this buffer */
342 r = load_class_from_classbuffer(cb);
346 FREE(cb, classbuffer);
348 #if defined(ENABLE_STATISTICS)
351 if (opt_getloadingtime)
356 /* If return value is NULL, we had a problem and the class is
357 not loaded. Now free the allocated memory, otherwise we
358 could run into a DOS. */
365 #if defined(ENABLE_JAVASE)
366 # if defined(WITH_CLASSPATH_SUN)
367 /* Store the protection domain. */
369 c->protectiondomain = pd;
373 /* Store the newly defined class in the class cache. This call
374 also checks whether a class of the same name has already been
375 defined by the same defining loader, and if so, replaces the
376 newly created class by the one defined earlier. */
378 /* Important: The classinfo given to classcache_store must be
379 fully prepared because another thread may return
380 this pointer after the lookup at to top of this
381 function directly after the class cache lock has
384 c = classcache_store(cl, c, true);
390 /* class_load_attribute_sourcefile *********************************************
392 SourceFile_attribute {
393 u2 attribute_name_index;
398 *******************************************************************************/
400 static bool class_load_attribute_sourcefile(classbuffer *cb)
411 /* check buffer size */
413 if (!suck_check_classbuffer_size(cb, 4 + 2))
416 /* check attribute length */
418 attribute_length = suck_u4(cb);
420 if (attribute_length != 2) {
421 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
425 /* there can be no more than one SourceFile attribute */
427 if (c->sourcefile != NULL) {
428 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
434 sourcefile_index = suck_u2(cb);
435 sourcefile = class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
437 if (sourcefile == NULL)
440 /* store sourcefile */
442 c->sourcefile = sourcefile;
448 /* class_load_attribute_enclosingmethod ****************************************
450 EnclosingMethod_attribute {
451 u2 attribute_name_index;
457 *******************************************************************************/
459 #if defined(ENABLE_JAVASE)
460 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
466 classref_or_classinfo cr;
467 constant_nameandtype *cn;
473 /* check buffer size */
475 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
478 /* check attribute length */
480 attribute_length = suck_u4(cb);
482 if (attribute_length != 4) {
483 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
487 /* there can be no more than one EnclosingMethod attribute */
489 if (c->enclosingmethod != NULL) {
490 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
494 /* get class index */
496 class_index = suck_u2(cb);
497 cr.ref = innerclass_getconstant(c, class_index, CONSTANT_Class);
499 /* get method index */
501 method_index = suck_u2(cb);
502 cn = innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
504 /* store info in classinfo */
506 c->enclosingclass.any = cr.any;
507 c->enclosingmethod = cn;
511 #endif /* defined(ENABLE_JAVASE) */
514 /* class_load_attributes *******************************************************
516 Read attributes from ClassFile.
519 u2 attribute_name_index;
521 u1 info[attribute_length];
524 InnerClasses_attribute {
525 u2 attribute_name_index;
529 *******************************************************************************/
531 bool class_load_attributes(classbuffer *cb)
534 uint16_t attributes_count;
535 uint16_t attribute_name_index;
537 innerclassinfo *info;
538 classref_or_classinfo inner;
539 classref_or_classinfo outer;
546 /* get attributes count */
548 if (!suck_check_classbuffer_size(cb, 2))
551 attributes_count = suck_u2(cb);
553 for (i = 0; i < attributes_count; i++) {
554 /* get attribute name */
556 if (!suck_check_classbuffer_size(cb, 2))
559 attribute_name_index = suck_u2(cb);
561 class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
563 if (attribute_name == NULL)
566 if (attribute_name == utf_InnerClasses) {
569 if (c->innerclass != NULL) {
570 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
574 if (!suck_check_classbuffer_size(cb, 4 + 2))
577 /* skip attribute length */
580 /* number of records */
581 c->innerclasscount = suck_u2(cb);
583 if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
586 /* allocate memory for innerclass structure */
587 c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
589 for (j = 0; j < c->innerclasscount; j++) {
590 /* The innerclass structure contains a class with an encoded
591 name, its defining scope, its simple name and a bitmask of
594 info = c->innerclass + j;
596 inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
597 outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
598 name = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
601 /* If the current inner-class is the currently loaded
602 class check for some special flags. */
604 if (inner.ref->name == c->name) {
605 /* If an inner-class is not a member, its
606 outer-class is NULL. */
608 if (outer.ref != NULL) {
609 c->flags |= ACC_CLASS_MEMBER;
611 /* A member class doesn't have an
612 EnclosingMethod attribute, so set the
613 enclosing-class to be the same as the
616 c->declaringclass = outer;
617 c->enclosingclass = outer;
620 /* If an inner-class is anonymous, its name is
624 c->flags |= ACC_CLASS_ANONYMOUS;
627 info->inner_class = inner;
628 info->outer_class = outer;
633 else if (attribute_name == utf_SourceFile) {
636 if (!class_load_attribute_sourcefile(cb))
639 #if defined(ENABLE_JAVASE)
640 else if (attribute_name == utf_EnclosingMethod) {
641 /* EnclosingMethod */
643 if (!class_load_attribute_enclosingmethod(cb))
646 else if (attribute_name == utf_Signature) {
649 if (!loader_load_attribute_signature(cb, &(c->signature)))
654 #if defined(ENABLE_ANNOTATIONS)
655 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
656 /* RuntimeVisibleAnnotations */
657 if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
660 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
661 /* RuntimeInvisibleAnnotations */
662 if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
668 /* unknown attribute */
670 if (!loader_skip_attribute_body(cb))
679 /* class_freepool **************************************************************
681 Frees all resources used by this classes Constant Pool.
683 *******************************************************************************/
685 static void class_freecpool(classinfo *c)
691 if (c->cptags && c->cpinfos) {
692 for (idx = 0; idx < c->cpcount; idx++) {
693 tag = c->cptags[idx];
694 info = c->cpinfos[idx];
698 case CONSTANT_Fieldref:
699 case CONSTANT_Methodref:
700 case CONSTANT_InterfaceMethodref:
701 FREE(info, constant_FMIref);
703 case CONSTANT_Integer:
704 FREE(info, constant_integer);
707 FREE(info, constant_float);
710 FREE(info, constant_long);
712 case CONSTANT_Double:
713 FREE(info, constant_double);
715 case CONSTANT_NameAndType:
716 FREE(info, constant_nameandtype);
724 MFREE(c->cptags, u1, c->cpcount);
727 MFREE(c->cpinfos, voidptr, c->cpcount);
731 /* class_getconstant ***********************************************************
733 Retrieves the value at position 'pos' of the constantpool of a
734 class. If the type of the value is other than 'ctype', an error is
737 *******************************************************************************/
739 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
741 /* check index and type of constantpool entry */
742 /* (pos == 0 is caught by type comparison) */
744 if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
745 exceptions_throw_classformaterror(c, "Illegal constant pool index");
749 return c->cpinfos[pos];
753 /* innerclass_getconstant ******************************************************
755 Like class_getconstant, but if cptags is ZERO, null is returned.
757 *******************************************************************************/
759 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
761 /* invalid position in constantpool */
763 if (pos >= c->cpcount) {
764 exceptions_throw_classformaterror(c, "Illegal constant pool index");
768 /* constantpool entry of type 0 */
770 if (c->cptags[pos] == 0)
773 /* check type of constantpool entry */
775 if (c->cptags[pos] != ctype) {
776 exceptions_throw_classformaterror(c, "Illegal constant pool index");
780 return c->cpinfos[pos];
784 /* class_free ******************************************************************
786 Frees all resources used by the class.
788 *******************************************************************************/
790 void class_free(classinfo *c)
797 if (c->interfaces != NULL)
798 MFREE(c->interfaces, classinfo*, c->interfacescount);
801 for (i = 0; i < c->fieldscount; i++)
802 field_free(&(c->fields[i]));
803 MFREE(c->fields, fieldinfo, c->fieldscount);
807 for (i = 0; i < c->methodscount; i++)
808 method_free(&(c->methods[i]));
809 MFREE(c->methods, methodinfo, c->methodscount);
812 if ((v = c->vftbl) != NULL) {
814 mem_free(v->arraydesc,sizeof(arraydescriptor));
816 for (i = 0; i < v->interfacetablelength; i++) {
817 MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
819 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
821 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
822 sizeof(methodptr*) * (v->interfacetablelength -
823 (v->interfacetablelength > 0));
824 v = (vftbl_t*) (((methodptr*) v) -
825 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
830 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
832 /* if (c->classvftbl)
833 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
839 /* get_array_class *************************************************************
841 Returns the array class with the given name for the given
842 classloader, or NULL if an exception occurred.
844 Note: This function does eager loading.
846 *******************************************************************************/
848 static classinfo *get_array_class(utf *name,classloader *initloader,
849 classloader *defloader,bool link)
853 /* lookup this class in the classcache */
854 c = classcache_lookup(initloader,name);
856 c = classcache_lookup_defined(defloader,name);
859 /* we have to create it */
860 c = class_create_classinfo(name);
861 c = load_newly_created_array(c,initloader);
867 assert(c->state & CLASS_LOADED);
868 assert(c->classloader == defloader);
870 if (link && !(c->state & CLASS_LINKED))
874 assert(!link || (c->state & CLASS_LINKED));
880 /* class_array_of **************************************************************
882 Returns an array class with the given component class. The array
883 class is dynamically created if neccessary.
885 *******************************************************************************/
887 classinfo *class_array_of(classinfo *component, bool link)
896 cl = component->classloader;
900 /* Assemble the array class name */
901 namelen = component->name->blength;
903 if (component->name->text[0] == '[') {
904 /* the component is itself an array */
905 namebuf = DMNEW(char, namelen + 1);
907 MCOPY(namebuf + 1, component->name->text, char, namelen);
911 /* the component is a non-array class */
912 namebuf = DMNEW(char, namelen + 3);
915 MCOPY(namebuf + 2, component->name->text, char, namelen);
916 namebuf[2 + namelen] = ';';
920 u = utf_new(namebuf, namelen);
922 c = get_array_class(u, cl, cl, link);
930 /* class_multiarray_of *********************************************************
932 Returns an array class with the given dimension and element class.
933 The array class is dynamically created if neccessary.
935 *******************************************************************************/
937 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
947 log_text("Invalid array dimension requested");
951 /* Assemble the array class name */
952 namelen = element->name->blength;
954 if (element->name->text[0] == '[') {
955 /* the element is itself an array */
956 namebuf = DMNEW(char, namelen + dim);
957 memcpy(namebuf + dim, element->name->text, namelen);
961 /* the element is a non-array class */
962 namebuf = DMNEW(char, namelen + 2 + dim);
964 memcpy(namebuf + dim + 1, element->name->text, namelen);
965 namelen += (2 + dim);
966 namebuf[namelen - 1] = ';';
968 memset(namebuf, '[', dim);
970 c = get_array_class(utf_new(namebuf, namelen),
971 element->classloader,
972 element->classloader,
981 /* class_lookup_classref *******************************************************
983 Looks up the constant_classref for a given classname in the classref
987 cls..............the class containing the reference
988 name.............the name of the class refered to
991 a pointer to a constant_classref, or
992 NULL if the reference was not found
994 *******************************************************************************/
996 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
998 constant_classref *ref;
999 extra_classref *xref;
1004 assert(!cls->classrefcount || cls->classrefs);
1006 /* first search the main classref table */
1007 count = cls->classrefcount;
1008 ref = cls->classrefs;
1009 for (; count; --count, ++ref)
1010 if (ref->name == name)
1013 /* next try the list of extra classrefs */
1014 for (xref = cls->extclassrefs; xref; xref = xref->next) {
1015 if (xref->classref.name == name)
1016 return &(xref->classref);
1024 /* class_get_classref **********************************************************
1026 Returns the constant_classref for a given classname.
1029 cls..............the class containing the reference
1030 name.............the name of the class refered to
1033 a pointer to a constant_classref (never NULL)
1036 The given name is not checked for validity!
1038 *******************************************************************************/
1040 constant_classref *class_get_classref(classinfo *cls, utf *name)
1042 constant_classref *ref;
1043 extra_classref *xref;
1048 ref = class_lookup_classref(cls,name);
1052 xref = NEW(extra_classref);
1053 CLASSREF_INIT(xref->classref,cls,name);
1055 xref->next = cls->extclassrefs;
1056 cls->extclassrefs = xref;
1058 return &(xref->classref);
1062 /* class_get_self_classref *****************************************************
1064 Returns the constant_classref to the class itself.
1067 cls..............the class containing the reference
1070 a pointer to a constant_classref (never NULL)
1072 *******************************************************************************/
1074 constant_classref *class_get_self_classref(classinfo *cls)
1076 /* XXX this should be done in a faster way. Maybe always make */
1077 /* the classref of index 0 a self reference. */
1078 return class_get_classref(cls,cls->name);
1081 /* class_get_classref_multiarray_of ********************************************
1083 Returns an array type reference with the given dimension and element class
1087 dim..............the requested dimension
1088 dim must be in [1;255]. This is NOT checked!
1089 ref..............the component class reference
1092 a pointer to the class reference for the array type
1095 The referer of `ref` is used as the referer for the new classref.
1097 *******************************************************************************/
1099 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1103 constant_classref *cr;
1107 assert(dim >= 1 && dim <= 255);
1111 /* Assemble the array class name */
1112 namelen = ref->name->blength;
1114 if (ref->name->text[0] == '[') {
1115 /* the element is itself an array */
1116 namebuf = DMNEW(char, namelen + dim);
1117 memcpy(namebuf + dim, ref->name->text, namelen);
1121 /* the element is a non-array class */
1122 namebuf = DMNEW(char, namelen + 2 + dim);
1124 memcpy(namebuf + dim + 1, ref->name->text, namelen);
1125 namelen += (2 + dim);
1126 namebuf[namelen - 1] = ';';
1128 memset(namebuf, '[', dim);
1130 cr = class_get_classref(ref->referer,utf_new(namebuf, namelen));
1138 /* class_get_classref_component_of *********************************************
1140 Returns the component classref of a given array type reference
1143 ref..............the array type reference
1146 a reference to the component class, or
1147 NULL if `ref` is not an object array type reference
1150 The referer of `ref` is used as the referer for the new classref.
1152 *******************************************************************************/
1154 constant_classref *class_get_classref_component_of(constant_classref *ref)
1161 name = ref->name->text;
1165 namelen = ref->name->blength - 1;
1170 else if (*name != '[') {
1174 return class_get_classref(ref->referer, utf_new(name, namelen));
1178 /* class_findmethod ************************************************************
1180 Searches a 'classinfo' structure for a method having the given name
1181 and descriptor. If descriptor is NULL, it is ignored.
1183 *******************************************************************************/
1185 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1190 for (i = 0; i < c->methodscount; i++) {
1191 m = &(c->methods[i]);
1193 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1201 /* class_resolvemethod *********************************************************
1203 Searches a class and it's super classes for a method.
1205 Superinterfaces are *not* searched.
1207 *******************************************************************************/
1209 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1214 m = class_findmethod(c, name, desc);
1219 /* JVM Specification bug:
1221 It is important NOT to resolve special <init> and <clinit>
1222 methods to super classes or interfaces; yet, this is not
1223 explicited in the specification. Section 5.4.3.3 should be
1224 updated appropriately. */
1226 if (name == utf_init || name == utf_clinit)
1236 /* class_resolveinterfacemethod_intern *****************************************
1238 Internally used helper function. Do not use this directly.
1240 *******************************************************************************/
1242 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1243 utf *name, utf *desc)
1248 /* try to find the method in the class */
1250 m = class_findmethod(c, name, desc);
1255 /* No method found? Try the super interfaces. */
1257 for (i = 0; i < c->interfacescount; i++) {
1258 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1264 /* no method found */
1270 /* class_resolveclassmethod ****************************************************
1272 Resolves a reference from REFERER to a method with NAME and DESC in
1275 If the method cannot be resolved the return value is NULL. If
1276 EXCEPT is true *exceptionptr is set, too.
1278 *******************************************************************************/
1280 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1281 classinfo *referer, bool throwexception)
1287 /* if (c->flags & ACC_INTERFACE) { */
1288 /* if (throwexception) */
1289 /* *exceptionptr = */
1290 /* new_exception(string_java_lang_IncompatibleClassChangeError); */
1294 /* try class c and its superclasses */
1298 m = class_resolvemethod(cls, name, desc);
1303 /* Try the super interfaces. */
1305 for (i = 0; i < c->interfacescount; i++) {
1306 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1313 exceptions_throw_nosuchmethoderror(c, name, desc);
1318 if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1320 exceptions_throw_abstractmethoderror();
1325 /* XXX check access rights */
1331 /* class_resolveinterfacemethod ************************************************
1333 Resolves a reference from REFERER to a method with NAME and DESC in
1336 If the method cannot be resolved the return value is NULL. If
1337 EXCEPT is true *exceptionptr is set, too.
1339 *******************************************************************************/
1341 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1342 classinfo *referer, bool throwexception)
1346 if (!(c->flags & ACC_INTERFACE)) {
1348 exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1353 mi = class_resolveinterfacemethod_intern(c, name, desc);
1358 /* try class java.lang.Object */
1360 mi = class_findmethod(class_java_lang_Object, name, desc);
1366 exceptions_throw_nosuchmethoderror(c, name, desc);
1372 /* class_findfield *************************************************************
1374 Searches for field with specified name and type in a classinfo
1375 structure. If no such field is found NULL is returned.
1377 *******************************************************************************/
1379 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1383 for (i = 0; i < c->fieldscount; i++)
1384 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1385 return &(c->fields[i]);
1387 if (c->super != NULL)
1388 return class_findfield(c->super, name, desc);
1394 /* class_findfield_approx ******************************************************
1396 Searches in 'classinfo'-structure for a field with the specified
1399 *******************************************************************************/
1401 fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
1405 /* get field index */
1407 i = class_findfield_index_by_name(c, name);
1409 /* field was not found, return */
1414 /* return field address */
1416 return &(c->fields[i]);
1420 s4 class_findfield_index_by_name(classinfo *c, utf *name)
1424 for (i = 0; i < c->fieldscount; i++) {
1425 /* compare field names */
1427 if ((c->fields[i].name == name))
1431 /* field was not found, raise exception */
1433 exceptions_throw_nosuchfielderror(c, name);
1439 /****************** Function: class_resolvefield_int ***************************
1441 This is an internally used helper function. Do not use this directly.
1443 Tries to resolve a field having the given name and type.
1444 If the field cannot be resolved, NULL is returned.
1446 *******************************************************************************/
1448 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1453 /* search for field in class c */
1455 for (i = 0; i < c->fieldscount; i++) {
1456 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1457 return &(c->fields[i]);
1461 /* Try super interfaces recursively. */
1463 for (i = 0; i < c->interfacescount; i++) {
1464 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1470 /* Try super class. */
1472 if (c->super != NULL)
1473 return class_resolvefield_int(c->super, name, desc);
1481 /********************* Function: class_resolvefield ***************************
1483 Resolves a reference from REFERER to a field with NAME and DESC in class C.
1485 If the field cannot be resolved the return value is NULL. If EXCEPT is
1486 true *exceptionptr is set, too.
1488 *******************************************************************************/
1490 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
1491 classinfo *referer, bool throwexception)
1495 fi = class_resolvefield_int(c, name, desc);
1499 exceptions_throw_nosuchfielderror(c, name);
1504 /* XXX check access rights */
1510 /* class_issubclass ************************************************************
1512 Checks if sub is a descendant of super.
1514 *******************************************************************************/
1516 bool class_issubclass(classinfo *sub, classinfo *super)
1523 /* We reached java/lang/Object and did not find the requested
1529 /* We found the requested super class. */
1539 /* class_isanysubclass *********************************************************
1541 Checks a subclass relation between two classes. Implemented
1542 interfaces are interpreted as super classes.
1544 Return value: 1 ... sub is subclass of super
1547 *******************************************************************************/
1549 bool class_isanysubclass(classinfo *sub, classinfo *super)
1554 /* This is the trivial case. */
1559 /* Primitive classes are only subclasses of themselves. */
1561 if (class_is_primitive(sub) || class_is_primitive(super))
1564 /* Check for interfaces. */
1566 if (super->flags & ACC_INTERFACE) {
1567 result = (sub->vftbl->interfacetablelength > super->index) &&
1568 (sub->vftbl->interfacetable[-super->index] != NULL);
1571 /* java.lang.Object is the only super class of any
1574 if (sub->flags & ACC_INTERFACE)
1575 return (super == class_java_lang_Object);
1577 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1579 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1580 result = diffval <= (uint32_t) super->vftbl->diffval;
1582 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1589 /* class_get_componenttype *****************************************************
1591 Return the component class of the given class. If the given class
1592 is not an array, return NULL.
1594 *******************************************************************************/
1596 classinfo *class_get_componenttype(classinfo *c)
1598 classinfo *component;
1599 arraydescriptor *ad;
1601 /* XXX maybe we could find a way to do this without linking. */
1602 /* This way should be safe and easy, however. */
1604 if (!(c->state & CLASS_LINKED))
1608 ad = c->vftbl->arraydesc;
1613 if (ad->arraytype == ARRAYTYPE_OBJECT)
1614 component = ad->componentvftbl->class;
1616 component = primitive_class_get_by_type(ad->arraytype);
1622 /* class_get_declaredclasses ***************************************************
1624 Return an array of declared classes of the given class.
1626 *******************************************************************************/
1628 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1630 classref_or_classinfo inner;
1631 classref_or_classinfo outer;
1633 int declaredclasscount; /* number of declared classes */
1634 int pos; /* current declared class */
1635 java_handle_objectarray_t *oa; /* array of declared classes */
1639 declaredclasscount = 0;
1641 if (!class_is_primitive(c) && !class_is_array(c)) {
1642 /* Determine number of declared classes. */
1644 for (i = 0; i < c->innerclasscount; i++) {
1645 /* Get outer-class. If the inner-class is not a member
1646 class, the outer-class is NULL. */
1648 outer = c->innerclass[i].outer_class;
1650 if (outer.any == NULL)
1653 /* Check if outer-class is a classref or a real class and
1654 get the class name from the structure. */
1656 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1658 /* Outer class is this class. */
1660 if ((outername == c->name) &&
1661 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1662 declaredclasscount++;
1666 /* Allocate Class[] and check for OOM. */
1668 oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1673 for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1674 inner = c->innerclass[i].inner_class;
1675 outer = c->innerclass[i].outer_class;
1677 /* Get outer-class. If the inner-class is not a member class,
1678 the outer-class is NULL. */
1680 if (outer.any == NULL)
1683 /* Check if outer_class is a classref or a real class and get
1684 the class name from the structure. */
1686 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1688 /* Outer class is this class. */
1690 if ((outername == c->name) &&
1691 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1693 ic = resolve_classref_or_classinfo_eager(inner, false);
1698 if (!(ic->state & CLASS_LINKED))
1699 if (!link_class(ic))
1702 LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1710 /* class_get_declaringclass ****************************************************
1712 If the class or interface given is a member of another class,
1713 return the declaring class. For array and primitive classes return
1716 *******************************************************************************/
1718 classinfo *class_get_declaringclass(classinfo *c)
1720 classref_or_classinfo cr;
1723 /* Get declaring class. */
1725 cr = c->declaringclass;
1730 /* Resolve the class if necessary. */
1732 if (IS_CLASSREF(cr)) {
1733 /* dc = resolve_classref_eager(cr.ref); */
1734 dc = resolve_classref_or_classinfo_eager(cr, true);
1739 /* Store the resolved class in the class structure. */
1750 /* class_get_enclosingclass ****************************************************
1752 Return the enclosing class for the given class.
1754 *******************************************************************************/
1756 classinfo *class_get_enclosingclass(classinfo *c)
1758 classref_or_classinfo cr;
1761 /* Get enclosing class. */
1763 cr = c->enclosingclass;
1768 /* Resolve the class if necessary. */
1770 if (IS_CLASSREF(cr)) {
1771 /* ec = resolve_classref_eager(cr.ref); */
1772 ec = resolve_classref_or_classinfo_eager(cr, true);
1777 /* Store the resolved class in the class structure. */
1788 /* class_get_enclosingmethod ***************************************************
1790 Return the enclosing method for the given class.
1793 c ... class to return the enclosing method for
1796 methodinfo of the enclosing method
1798 *******************************************************************************/
1800 methodinfo *class_get_enclosingmethod(classinfo *c)
1802 constant_nameandtype *cn;
1806 /* get enclosing class and method */
1808 ec = class_get_enclosingclass(c);
1809 cn = c->enclosingmethod;
1811 /* check for enclosing class and method */
1819 /* find method in enclosing class */
1821 m = class_findmethod(ec, cn->name, cn->descriptor);
1824 exceptions_throw_internalerror("Enclosing method doesn't exist");
1832 /* class_get_interfaces ********************************************************
1834 Return an array of interfaces of the given class.
1836 *******************************************************************************/
1838 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
1841 java_handle_objectarray_t *oa;
1844 if (!(c->state & CLASS_LINKED))
1848 oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
1853 for (i = 0; i < c->interfacescount; i++) {
1854 ic = c->interfaces[i];
1856 LLNI_array_direct(oa, i) = (java_object_t *) ic;
1863 /* class_get_annotations *******************************************************
1865 Get the unparsed declared annotations in a byte array
1869 c........the class of which the annotations should be returned
1872 The unparsed declared annotations in a byte array
1873 (or NULL if there aren't any).
1875 *******************************************************************************/
1877 java_handle_bytearray_t *class_get_annotations(classinfo *c)
1879 #if defined(ENABLE_ANNOTATIONS)
1880 java_handle_t *annotations; /* unparsed annotations */
1882 LLNI_classinfo_field_get(c, annotations, annotations);
1884 return (java_handle_bytearray_t*)annotations;
1891 /* class_get_modifiers *********************************************************
1893 Get the modifier flags of the given class.
1896 c....the class of which the modifier flags should be returned
1897 ignoreInnerClassesAttrib
1901 *******************************************************************************/
1903 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
1905 classref_or_classinfo inner;
1906 classref_or_classinfo outer;
1910 if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
1911 /* search for passed class as inner class */
1913 for (i = 0; i < c->innerclasscount; i++) {
1914 inner = c->innerclass[i].inner_class;
1915 outer = c->innerclass[i].outer_class;
1917 /* Check if inner is a classref or a real class and get
1918 the name of the structure */
1920 innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
1922 /* innerclass is this class */
1924 if (innername == c->name) {
1925 /* has the class actually an outer class? */
1928 /* return flags got from the outer class file */
1929 return c->innerclass[i].flags & ACC_CLASS_REFLECT_MASK;
1931 return c->flags & ACC_CLASS_REFLECT_MASK;
1936 /* passed class is no inner class or it was not requested */
1938 return c->flags & ACC_CLASS_REFLECT_MASK;
1942 /* class_get_signature *********************************************************
1944 Return the signature of the given class. For array and primitive
1945 classes return NULL.
1947 *******************************************************************************/
1949 #if defined(ENABLE_JAVASE)
1950 utf *class_get_signature(classinfo *c)
1952 /* For array and primitive classes return NULL. */
1954 if (class_is_array(c) || class_is_primitive(c))
1957 return c->signature;
1962 /* class_printflags ************************************************************
1964 Prints flags of a class.
1966 *******************************************************************************/
1968 #if !defined(NDEBUG)
1969 void class_printflags(classinfo *c)
1976 if (c->flags & ACC_PUBLIC) printf(" PUBLIC");
1977 if (c->flags & ACC_PRIVATE) printf(" PRIVATE");
1978 if (c->flags & ACC_PROTECTED) printf(" PROTECTED");
1979 if (c->flags & ACC_STATIC) printf(" STATIC");
1980 if (c->flags & ACC_FINAL) printf(" FINAL");
1981 if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
1982 if (c->flags & ACC_VOLATILE) printf(" VOLATILE");
1983 if (c->flags & ACC_TRANSIENT) printf(" TRANSIENT");
1984 if (c->flags & ACC_NATIVE) printf(" NATIVE");
1985 if (c->flags & ACC_INTERFACE) printf(" INTERFACE");
1986 if (c->flags & ACC_ABSTRACT) printf(" ABSTRACT");
1991 /* class_print *****************************************************************
1993 Prints classname plus flags.
1995 *******************************************************************************/
1997 #if !defined(NDEBUG)
1998 void class_print(classinfo *c)
2005 utf_display_printable_ascii(c->name);
2006 class_printflags(c);
2011 /* class_classref_print ********************************************************
2013 Prints classname plus referer class.
2015 *******************************************************************************/
2017 #if !defined(NDEBUG)
2018 void class_classref_print(constant_classref *cr)
2025 utf_display_printable_ascii(cr->name);
2028 class_print(cr->referer);
2036 /* class_println ***************************************************************
2038 Prints classname plus flags and new line.
2040 *******************************************************************************/
2042 #if !defined(NDEBUG)
2043 void class_println(classinfo *c)
2051 /* class_classref_println ******************************************************
2053 Prints classname plus referer class and new line.
2055 *******************************************************************************/
2057 #if !defined(NDEBUG)
2058 void class_classref_println(constant_classref *cr)
2060 class_classref_print(cr);
2066 /* class_classref_or_classinfo_print *******************************************
2068 Prints classname plus referer class.
2070 *******************************************************************************/
2072 #if !defined(NDEBUG)
2073 void class_classref_or_classinfo_print(classref_or_classinfo c)
2075 if (c.any == NULL) {
2076 printf("(classref_or_classinfo) NULL");
2080 class_classref_print(c.ref);
2087 /* class_classref_or_classinfo_println *****************************************
2089 Prints classname plus referer class and a newline.
2091 *******************************************************************************/
2093 #if !defined(NDEBUG)
2094 void class_classref_or_classinfo_println(classref_or_classinfo c)
2096 class_classref_or_classinfo_print(c);
2102 /* class_showconstantpool ******************************************************
2104 Dump the constant pool of the given class to stdout.
2106 *******************************************************************************/
2108 #if !defined(NDEBUG)
2109 void class_showconstantpool (classinfo *c)
2114 printf ("---- dump of constant pool ----\n");
2116 for (i=0; i<c->cpcount; i++) {
2117 printf ("#%d: ", (int) i);
2119 e = c -> cpinfos [i];
2122 switch (c -> cptags [i]) {
2123 case CONSTANT_Class:
2124 printf ("Classreference -> ");
2125 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2127 case CONSTANT_Fieldref:
2128 printf ("Fieldref -> ");
2129 field_fieldref_print((constant_FMIref *) e);
2131 case CONSTANT_Methodref:
2132 printf ("Methodref -> ");
2133 method_methodref_print((constant_FMIref *) e);
2135 case CONSTANT_InterfaceMethodref:
2136 printf ("InterfaceMethod -> ");
2137 method_methodref_print((constant_FMIref *) e);
2139 case CONSTANT_String:
2140 printf ("String -> ");
2141 utf_display_printable_ascii (e);
2143 case CONSTANT_Integer:
2144 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2146 case CONSTANT_Float:
2147 printf ("Float -> %f", ((constant_float*)e) -> value);
2149 case CONSTANT_Double:
2150 printf ("Double -> %f", ((constant_double*)e) -> value);
2154 u8 v = ((constant_long*)e) -> value;
2156 printf ("Long -> %ld", (long int) v);
2158 printf ("Long -> HI: %ld, LO: %ld\n",
2159 (long int) v.high, (long int) v.low);
2163 case CONSTANT_NameAndType:
2165 constant_nameandtype *cnt = e;
2166 printf ("NameAndType: ");
2167 utf_display_printable_ascii (cnt->name);
2169 utf_display_printable_ascii (cnt->descriptor);
2173 printf ("Utf8 -> ");
2174 utf_display_printable_ascii (e);
2177 log_text("Invalid type of ConstantPool-Entry");
2185 #endif /* !defined(NDEBUG) */
2188 /* class_showmethods ***********************************************************
2190 Dump info about the fields and methods of the given class to stdout.
2192 *******************************************************************************/
2194 #if !defined(NDEBUG)
2195 void class_showmethods (classinfo *c)
2199 printf("--------- Fields and Methods ----------------\n");
2201 class_printflags(c);
2205 utf_display_printable_ascii(c->name);
2210 utf_display_printable_ascii(c->super->name);
2214 printf("Index: %d\n", c->index);
2216 printf("Interfaces:\n");
2217 for (i = 0; i < c->interfacescount; i++) {
2219 utf_display_printable_ascii(c->interfaces[i]->name);
2220 printf (" (%d)\n", c->interfaces[i]->index);
2223 printf("Fields:\n");
2224 for (i = 0; i < c->fieldscount; i++)
2225 field_println(&(c->fields[i]));
2227 printf("Methods:\n");
2228 for (i = 0; i < c->methodscount; i++) {
2229 methodinfo *m = &(c->methods[i]);
2231 if (!(m->flags & ACC_STATIC))
2232 printf("vftblindex: %d ", m->vftblindex);
2237 printf ("Virtual function table:\n");
2238 for (i = 0; i < c->vftbl->vftbllength; i++)
2239 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]));
2241 #endif /* !defined(NDEBUG) */
2245 * These are local overrides for various environment variables in Emacs.
2246 * Please do not remove this and leave it at the end of the file, where
2247 * Emacs will automagically detect them.
2248 * ---------------------------------------------------------------------
2251 * indent-tabs-mode: t
2255 * vim:noexpandtab:sw=4:ts=4: