1 /* src/vmcore/class.h - class related functions header
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
29 /* forward typedefs ***********************************************************/
31 typedef struct classinfo classinfo;
32 typedef struct innerclassinfo innerclassinfo;
33 typedef struct extra_classref extra_classref;
34 typedef struct castinfo castinfo;
43 #include "toolbox/list.h"
45 #include "vm/global.h"
46 #include "vm/stringlocal.h"
48 #if defined(ENABLE_JAVASE)
49 # include "vmcore/annotation.h"
52 #include "vmcore/field.h"
53 #include "vmcore/linker.h"
54 #include "vmcore/loader.h"
55 #include "vmcore/method.h"
56 #include "vmcore/references.h"
57 #include "vmcore/utf8.h"
59 /* class state defines ********************************************************/
61 #define CLASS_LOADING 0x0001
62 #define CLASS_LOADED 0x0002
63 #define CLASS_LINKING 0x0004
64 #define CLASS_LINKED 0x0008
65 #define CLASS_INITIALIZING 0x0010
66 #define CLASS_INITIALIZED 0x0020
67 #define CLASS_ERROR 0x0040
70 /* some macros ****************************************************************/
72 #define CLASS_IS_OR_ALMOST_INITIALIZED(c) \
73 (((c)->state & CLASS_INITIALIZING) || ((c)->state & CLASS_INITIALIZED))
76 /* classinfo ******************************************************************/
78 /* We define this dummy structure of java_lang_Class so we can
79 bootstrap cacaoh without needing a java_lang_Class.h file. Whether
80 the size of the dummy structure is big enough is checked during
81 runtime in vm_create. */
85 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
87 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
89 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
92 # error unknown classpath configuration
94 } dummy_java_lang_Class;
96 struct classinfo { /* class structure */
97 dummy_java_lang_Class object;
99 s4 flags; /* ACC flags */
100 utf *name; /* class name */
102 s4 cpcount; /* number of entries in constant pool */
103 u1 *cptags; /* constant pool tags */
104 voidptr *cpinfos; /* pointer to constant pool info structures */
106 s4 classrefcount; /* number of symbolic class references */
107 constant_classref *classrefs; /* table of symbolic class references */
108 extra_classref *extclassrefs; /* additional classrefs */
109 s4 parseddescsize; /* size of the parsed descriptors block */
110 u1 *parseddescs; /* parsed descriptors */
112 classinfo *super; /* super class */
113 classinfo *sub; /* sub class pointer */
114 classinfo *nextsub; /* pointer to next class in sub class list */
116 int32_t interfacescount; /* number of interfaces */
117 classinfo **interfaces; /* super interfaces */
119 int32_t fieldscount; /* number of fields */
120 fieldinfo *fields; /* field table */
122 int32_t methodscount; /* number of methods */
123 methodinfo *methods; /* method table */
125 s4 state; /* current class state */
126 s4 index; /* hierarchy depth (classes) or index */
128 s4 instancesize; /* size of an instance of this class */
130 vftbl_t *vftbl; /* pointer to virtual function table */
132 methodinfo *finalizer; /* finalizer method */
134 u2 innerclasscount; /* number of inner classes */
135 innerclassinfo *innerclass;
137 classref_or_classinfo declaringclass;
138 classref_or_classinfo enclosingclass; /* enclosing class */
139 constant_nameandtype *enclosingmethod; /* enclosing method */
141 utf *packagename; /* full name of the package */
142 utf *sourcefile; /* SourceFile attribute */
143 #if defined(ENABLE_JAVASE)
144 utf *signature; /* Signature attribute */
145 #if defined(ENABLE_ANNOTATIONS)
146 /* All the annotation attributes are NULL (and not a zero length array) */
147 /* if there is nothing. */
148 java_object_t *annotations; /* annotations of this class */
150 java_object_t *method_annotations; /* array of annotations of the methods */
151 java_object_t *method_parameterannotations; /* array of parameter */
152 /* annotations of the methods */
153 java_object_t *method_annotationdefaults; /* array of annotation default */
154 /* values of the methods */
156 java_object_t *field_annotations; /* array of annotations of the fields */
160 classloader_t *classloader; /* NULL for bootstrap classloader */
162 #if defined(ENABLE_JAVASE)
163 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
164 java_object_t *protectiondomain;
165 java_objectarray_t *signers;
168 #if defined(ENABLE_JITCACHE)
174 /* innerclassinfo *************************************************************/
176 struct innerclassinfo {
177 classref_or_classinfo inner_class; /* inner class pointer */
178 classref_or_classinfo outer_class; /* outer class pointer */
179 utf *name; /* innerclass name */
180 s4 flags; /* ACC flags */
184 /* extra_classref **************************************************************
186 for classrefs not occurring within descriptors
188 *******************************************************************************/
190 struct extra_classref {
191 extra_classref *next;
192 constant_classref classref;
196 /* castinfo *******************************************************************/
205 /* global variables ***********************************************************/
207 /* frequently used classes ****************************************************/
209 /* Important system classes. */
211 extern classinfo *class_java_lang_Object;
212 extern classinfo *class_java_lang_Class;
213 extern classinfo *class_java_lang_ClassLoader;
214 extern classinfo *class_java_lang_Cloneable;
215 extern classinfo *class_java_lang_SecurityManager;
216 extern classinfo *class_java_lang_String;
217 extern classinfo *class_java_lang_System;
218 extern classinfo *class_java_lang_Thread;
219 extern classinfo *class_java_lang_ThreadGroup;
220 extern classinfo *class_java_lang_Throwable;
221 extern classinfo *class_java_io_Serializable;
223 /* Important system exceptions. */
225 extern classinfo *class_java_lang_Exception;
226 extern classinfo *class_java_lang_ClassNotFoundException;
227 extern classinfo *class_java_lang_RuntimeException;
229 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
230 extern classinfo *class_java_lang_VMSystem;
231 extern classinfo *class_java_lang_VMThread;
232 extern classinfo *class_java_lang_VMThrowable;
235 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
236 extern classinfo *class_sun_reflect_MagicAccessorImpl;
239 #if defined(ENABLE_JAVASE)
240 extern classinfo *class_java_lang_Void;
243 extern classinfo *class_java_lang_Boolean;
244 extern classinfo *class_java_lang_Byte;
245 extern classinfo *class_java_lang_Character;
246 extern classinfo *class_java_lang_Short;
247 extern classinfo *class_java_lang_Integer;
248 extern classinfo *class_java_lang_Long;
249 extern classinfo *class_java_lang_Float;
250 extern classinfo *class_java_lang_Double;
252 /* some classes which may be used more often */
254 #if defined(ENABLE_JAVASE)
255 extern classinfo *class_java_lang_StackTraceElement;
256 extern classinfo *class_java_lang_reflect_Constructor;
257 extern classinfo *class_java_lang_reflect_Field;
258 extern classinfo *class_java_lang_reflect_Method;
259 extern classinfo *class_java_security_PrivilegedAction;
260 extern classinfo *class_java_util_Vector;
261 extern classinfo *class_java_util_HashMap;
263 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
264 extern classinfo *class_java_lang_reflect_VMConstructor;
265 extern classinfo *class_java_lang_reflect_VMField;
266 extern classinfo *class_java_lang_reflect_VMMethod;
269 extern classinfo *arrayclass_java_lang_Object;
271 # if defined(ENABLE_ANNOTATIONS)
272 extern classinfo *class_sun_reflect_ConstantPool;
273 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
274 extern classinfo *class_sun_reflect_annotation_AnnotationParser;
280 /* pseudo classes for the type checker ****************************************/
283 * pseudo_class_Arraystub
284 * (extends Object implements Cloneable, java.io.Serializable)
286 * If two arrays of incompatible component types are merged,
287 * the resulting reference has no accessible components.
288 * The result does, however, implement the interfaces Cloneable
289 * and java.io.Serializable. This pseudo class is used internally
290 * to represent such results. (They are *not* considered arrays!)
294 * This pseudo class is used internally to represent the
299 * This pseudo class is used internally to represent the
300 * the 'uninitialized object' type.
303 extern classinfo *pseudo_class_Arraystub;
304 extern classinfo *pseudo_class_Null;
305 extern classinfo *pseudo_class_New;
308 /* inline functions ***********************************************************/
311 * Returns the classname of the class, where slashes ('/') are
312 * replaced by dots ('.').
314 * @param c class to get name of
317 inline static java_handle_t* class_get_classname(classinfo* c)
321 /* Create a java string. */
323 s = javastring_new_slash_to_dot(c->name);
329 /* class_is_primitive **********************************************************
331 Checks if the given class is a primitive class.
333 *******************************************************************************/
335 static inline bool class_is_primitive(classinfo *c)
337 if (c->flags & ACC_CLASS_PRIMITIVE)
344 /* class_is_anonymousclass *****************************************************
346 Checks if the given class is an anonymous class.
348 *******************************************************************************/
350 static inline bool class_is_anonymousclass(classinfo *c)
352 if (c->flags & ACC_CLASS_ANONYMOUS)
359 /* class_is_array **************************************************************
361 Checks if the given class is an array class.
363 *******************************************************************************/
365 static inline bool class_is_array(classinfo *c)
367 if (!(c->state & CLASS_LINKED))
371 return (c->vftbl->arraydesc != NULL);
375 /* class_is_interface **********************************************************
377 Checks if the given class is an interface.
379 *******************************************************************************/
381 static inline bool class_is_interface(classinfo *c)
383 if (c->flags & ACC_INTERFACE)
390 /* class_is_localclass *********************************************************
392 Checks if the given class is a local class.
394 *******************************************************************************/
396 static inline bool class_is_localclass(classinfo *c)
398 if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
405 /* class_is_memberclass ********************************************************
407 Checks if the given class is a member class.
409 *******************************************************************************/
411 static inline bool class_is_memberclass(classinfo *c)
413 if (c->flags & ACC_CLASS_MEMBER)
420 /* class_get_classloader *******************************************************
422 Return the classloader of the given class.
424 *******************************************************************************/
426 static inline classloader_t *class_get_classloader(classinfo *c)
432 /* The classloader may be NULL. */
438 /* class_get_superclass ********************************************************
440 Return the super class of the given class.
442 *******************************************************************************/
444 static inline classinfo *class_get_superclass(classinfo *c)
446 /* For interfaces we return NULL. */
448 if (c->flags & ACC_INTERFACE)
451 /* For java/lang/Object, primitive-type and Void classes c->super
452 is NULL and we return NULL. */
458 /* function prototypes ********************************************************/
460 classinfo *class_create_classinfo(utf *u);
461 void class_postset_header_vftbl(void);
462 classinfo *class_define(utf *name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd);
463 void class_set_packagename(classinfo *c);
465 bool class_load_attributes(classbuffer *cb);
467 /* retrieve constantpool element */
468 voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype);
469 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
471 /* frees all resources used by the class */
472 void class_free(classinfo *);
474 /* return an array class with the given component class */
475 classinfo *class_array_of(classinfo *component,bool link);
477 /* return an array class with the given dimension and element class */
478 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
480 /* return a classref for the given class name */
481 /* (does a linear search!) */
482 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
484 /* return a classref for the given class name */
485 /* (does a linear search!) */
486 constant_classref *class_get_classref(classinfo *cls,utf *name);
488 /* return a classref to the class itself */
489 /* (does a linear search!) */
490 constant_classref *class_get_self_classref(classinfo *cls);
492 /* return a classref for an array with the given dimension of with the */
493 /* given component type */
494 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
496 /* return a classref for the component type of the given array type */
497 constant_classref *class_get_classref_component_of(constant_classref *ref);
499 /* get a class' field by name and descriptor */
500 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
502 /* search 'classinfo'-structure for a field with the specified name */
503 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
504 s4 class_findfield_index_by_name(classinfo *c, utf *name);
506 /* search class for a field */
507 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
509 /* search for a method with a specified name and descriptor */
510 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
511 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
512 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
513 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
515 bool class_issubclass(classinfo *sub, classinfo *super);
516 bool class_isanysubclass(classinfo *sub, classinfo *super);
517 bool class_is_assignable_from(classinfo *to, classinfo *from);
518 bool class_is_instance(classinfo *c, java_handle_t *h);
520 classloader_t *class_get_classloader(classinfo *c);
521 classinfo *class_get_superclass(classinfo *c);
522 classinfo *class_get_componenttype(classinfo *c);
523 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
524 java_handle_objectarray_t *class_get_declaredconstructors(classinfo *c, bool publicOnly);
525 java_handle_objectarray_t *class_get_declaredfields(classinfo *c, bool publicOnly);
526 java_handle_objectarray_t *class_get_declaredmethods(classinfo *c, bool publicOnly);
527 classinfo *class_get_declaringclass(classinfo *c);
528 classinfo *class_get_enclosingclass(classinfo *c);
529 java_handle_t* class_get_enclosingconstructor(classinfo *c);
530 methodinfo* class_get_enclosingmethod_raw(classinfo *c);
531 java_handle_t* class_get_enclosingmethod(classinfo *c);
532 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
533 java_handle_bytearray_t *class_get_annotations(classinfo *c);
534 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
535 java_handle_t *class_get_name(classinfo *c);
537 #if defined(ENABLE_JAVASE)
538 utf *class_get_signature(classinfo *c);
541 /* some debugging functions */
544 void class_printflags(classinfo *c);
545 void class_print(classinfo *c);
546 void class_println(classinfo *c);
547 void class_classref_print(constant_classref *cr);
548 void class_classref_println(constant_classref *cr);
549 void class_classref_or_classinfo_print(classref_or_classinfo c);
550 void class_classref_or_classinfo_println(classref_or_classinfo c);
554 void class_showmethods(classinfo *c);
555 void class_showconstantpool(classinfo *c);
557 #endif /* _CLASS_H */
561 * These are local overrides for various environment variables in Emacs.
562 * Please do not remove this and leave it at the end of the file, where
563 * Emacs will automagically detect them.
564 * ---------------------------------------------------------------------
567 * indent-tabs-mode: t