CACAO
class.hpp
Go to the documentation of this file.
1 /* src/vm/class.hpp - class related functions header
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
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.
12 
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.
17 
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
21  02110-1301, USA.
22 
23 */
24 
25 
26 #ifndef CLASS_HPP_
27 #define CLASS_HPP_ 1
28 
29 #include <stddef.h> // for NULL
30 #include <stdint.h> // for intptr_t, uint8_t
31 #include <sys/types.h> // for int32_t
32 #include "config.h" // for ENABLE_JAVASE, etc
33 #include "vm/global.hpp" // for java_handle_t, etc
34 #include "vm/linker.hpp" // for link_class
35 #include "vm/loader.hpp" // for classloader_t
36 #include "vm/references.hpp" // for classref_or_classinfo, etc
37 #include "vm/types.hpp" // for s4, u4, u1, u2
38 #include "vm/utf8.hpp" // for Utf8String
39 #include "vm/vftbl.hpp" // for vftbl_t
40 
41 namespace cacao {
42  struct ClassBuffer;
43  class OStream;
44 }
45 
46 struct classinfo;
47 struct extra_classref;
48 struct fieldinfo;
49 struct innerclassinfo;
50 struct methodinfo;
51 
52 /* class state defines ********************************************************/
53 
54 enum ClassState {
55  CLASS_LOADING = 0x0001,
56  CLASS_LOADED = 0x0002,
57  CLASS_LINKING = 0x0004,
58  CLASS_LINKED = 0x0008,
61  CLASS_ERROR = 0x0040
62 };
63 
64 
65 /* classinfo ******************************************************************/
66 
67 /* We define this dummy structure of java_lang_Class so we can
68  bootstrap cacaoh without needing a java_lang_Class.h file. Whether
69  the size of the dummy structure is big enough is checked during
70  runtime in vm_create. */
71 
72 typedef struct {
74 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
75  intptr_t padding[4];
76 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
77  intptr_t padding[19];
78 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
79  intptr_t padding[3];
80 #else
81 # error unknown classpath configuration
82 #endif
84 
85 struct threadobject;
86 
87 struct classinfo { /* class structure */
89 
90  s4 flags; /* ACC flags */
91  Utf8String name; /* class name */
93 
94  s4 cpcount; /* number of entries in constant pool */
95  u1 *cptags; /* constant pool tags */
96  void* *cpinfos; /* pointer to constant pool info structures */
97 
98  s4 classrefcount; /* number of symbolic class references */
99  constant_classref *classrefs; /* table of symbolic class references */
100  extra_classref *extclassrefs; /* additional classrefs */
101 
102  classinfo *super; /* super class */
103  classinfo *sub; /* sub class pointer */
104  classinfo *nextsub; /* pointer to next class in sub class list */
105 
106  int32_t interfacescount; /* number of interfaces */
107  classinfo **interfaces; /* super interfaces */
108 
109  int32_t fieldscount; /* number of fields */
110  fieldinfo *fields; /* field table */
111 
112  int32_t methodscount; /* number of methods */
113  methodinfo *methods; /* method table */
114 
115  s4 state; /* current class state */
116  s4 index; /* hierarchy depth (classes) or index */
117  /* (interfaces) */
118  s4 instancesize; /* size of an instance of this class */
119 
121  vftbl_t *vftbl; /* pointer to virtual function table */
122 
123  methodinfo *finalizer; /* finalizer method */
124 
125  u2 innerclasscount; /* number of inner classes */
127 
129  classref_or_classinfo enclosingclass; /* enclosing class */
130  constant_nameandtype *enclosingmethod; /* enclosing method */
131 
132  Utf8String packagename; /* full name of the package */
133  Utf8String sourcefile; /* SourceFile attribute */
134 #if defined(ENABLE_JAVASE)
135  Utf8String signature; /* Signature attribute */
136 #if defined(ENABLE_ANNOTATIONS)
137  /* All the annotation attributes are NULL (and not a zero length array) */
138  /* if there is nothing. */
139  java_object_t *annotations; /* annotations of this class */
140 
141  java_object_t *method_annotations; /* array of annotations of the methods */
142  java_object_t *method_parameterannotations; /* array of parameter */
143  /* annotations of the methods */
144  java_object_t *method_annotationdefaults; /* array of annotation default */
145  /* values of the methods */
146 
147  java_object_t *field_annotations; /* array of annotations of the fields */
148 
149 #endif
150 #endif
151  classloader_t *classloader; /* NULL for bootstrap classloader */
152 
153 #if defined(ENABLE_JAVASE)
154 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
155  java_object_t *protectiondomain;
156  java_objectarray_t *signers;
157 # endif
158 #endif
159 };
160 
161 
162 /* innerclassinfo *************************************************************/
163 
165  classref_or_classinfo inner_class; /* inner class pointer */
166  classref_or_classinfo outer_class; /* outer class pointer */
167  Utf8String name; /* innerclass name */
168  s4 flags; /* ACC flags */
169 };
170 
171 
172 /* extra_classref **************************************************************
173 
174  for classrefs not occurring within descriptors
175 
176 *******************************************************************************/
177 
180  : next(next), classref(referer, name) {}
181 
184 };
185 
186 /* function prototypes ********************************************************/
187 
189 void class_postset_header_vftbl(void);
190 classinfo *class_define(Utf8String name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd);
192 
194 
196 
197 /* retrieve constantpool element */
200 
201 /* frees all resources used by the class */
202 void class_free(classinfo *);
203 
204 /* return an array class with the given component class */
205 classinfo *class_array_of(classinfo *component,bool link);
206 
207 /* return an array class with the given dimension and element class */
208 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
209 
210 /* return a classref for the given class name */
211 /* (does a linear search!) */
213 
214 /* return a classref for the given class name */
215 /* (does a linear search!) */
217 
218 /* return a classref to the class itself */
219 /* (does a linear search!) */
221 
222 /* return a classref for an array with the given dimension of with the */
223 /* given component type */
225 
226 /* return a classref for the component type of the given array type */
228 
229 /* get a class' field by name and descriptor */
231 
232 /* search 'classinfo'-structure for a field with the specified name */
233 fieldinfo *class_findfield_by_name(classinfo *c, Utf8String name, bool throwexception);
234 
235 /* search class for a field */
237 
238 /* search for a method with a specified name and descriptor */
241 methodinfo *class_resolveclassmethod(classinfo *c, Utf8String name, Utf8String dest, classinfo *referer, bool throwexception);
242 methodinfo *class_resolveinterfacemethod(classinfo *c, Utf8String name, Utf8String dest, classinfo *referer, bool throwexception);
243 
244 bool class_issubclass(classinfo *sub, classinfo *super);
245 bool class_isanysubclass(classinfo *sub, classinfo *super);
249 
251 static inline classinfo *class_get_superclass(classinfo *c);
264 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
266 
267 #if defined(ENABLE_JAVASE)
269 #endif
270 
271 /* some debugging functions */
272 
273 #if !defined(NDEBUG)
274 void class_printflags(classinfo *c);
275 void class_print(classinfo *c);
276 void class_println(classinfo *c);
281 #endif
282 
283 namespace cacao {
284 
285 /* OStream overloads */
286 OStream& operator<<(OStream& os, const classinfo *c);
287 
288 }
289 
290 /* debug purposes */
293 
294 /* inline functions ***********************************************************/
295 
296 /**
297  * Returns the classname of the class, where slashes ('/') are
298  * replaced by dots ('.').
299  *
300  * @param c class to get name of
301  * @return classname
302  */
304 
305 /* class_is_primitive **********************************************************
306 
307  Checks if the given class is a primitive class.
308 
309 *******************************************************************************/
310 
311 static inline bool class_is_primitive(classinfo *c)
312 {
313  if (c->flags & ACC_CLASS_PRIMITIVE)
314  return true;
315 
316  return false;
317 }
318 
319 
320 /* class_is_anonymousclass *****************************************************
321 
322  Checks if the given class is an anonymous class.
323 
324 *******************************************************************************/
325 
326 static inline bool class_is_anonymousclass(classinfo *c)
327 {
328  if (c->flags & ACC_CLASS_ANONYMOUS)
329  return true;
330 
331  return false;
332 }
333 
334 
335 /* class_is_array **************************************************************
336 
337  Checks if the given class is an array class.
338 
339 *******************************************************************************/
340 
341 static inline bool class_is_array(classinfo *c)
342 {
343  if (!(c->state & CLASS_LINKED))
344  if (!link_class(c))
345  return false;
346 
347  return (c->vftbl->arraydesc != NULL);
348 }
349 
350 
351 /* class_is_interface **********************************************************
352 
353  Checks if the given class is an interface.
354 
355 *******************************************************************************/
356 
357 static inline bool class_is_interface(classinfo *c)
358 {
359  if (c->flags & ACC_INTERFACE)
360  return true;
361 
362  return false;
363 }
364 
365 
366 /* class_is_localclass *********************************************************
367 
368  Checks if the given class is a local class.
369 
370 *******************************************************************************/
371 
372 static inline bool class_is_localclass(classinfo *c)
373 {
374  if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
375  return true;
376 
377  return false;
378 }
379 
380 
381 /* class_is_memberclass ********************************************************
382 
383  Checks if the given class is a member class.
384 
385 *******************************************************************************/
386 
387 static inline bool class_is_memberclass(classinfo *c)
388 {
389  if (c->flags & ACC_CLASS_MEMBER)
390  return true;
391 
392  return false;
393 }
394 
395 
396 /* class_get_classloader *******************************************************
397 
398  Return the classloader of the given class.
399 
400 *******************************************************************************/
401 
403 {
404  classloader_t *cl;
405 
406  cl = c->classloader;
407 
408  /* The classloader may be NULL. */
409 
410  return cl;
411 }
412 
413 
414 /* class_get_superclass ********************************************************
415 
416  Return the super class of the given class.
417 
418 *******************************************************************************/
419 
421 {
422  /* For interfaces we return NULL. */
423 
424  if (c->flags & ACC_INTERFACE)
425  return NULL;
426 
427  /* For java/lang/Object, primitive-type and Void classes c->super
428  is NULL and we return NULL. */
429 
430  return c->super;
431 }
432 
434  return ((c)->state & CLASS_INITIALIZED)
435  || ((c)->state & CLASS_INITIALIZING && class_initializing_thread_is_self((c)));
436 }
437 
438 #endif // CLASS_HPP_
439 
440 
441 /*
442  * These are local overrides for various environment variables in Emacs.
443  * Please do not remove this and leave it at the end of the file, where
444  * Emacs will automagically detect them.
445  * ---------------------------------------------------------------------
446  * Local variables:
447  * mode: c++
448  * indent-tabs-mode: t
449  * c-basic-offset: 4
450  * tab-width: 4
451  * End:
452  * vim:noexpandtab:sw=4:ts=4:
453  */
java_handle_t * class_get_name(classinfo *c)
java_handle_objectarray_t * class_get_declaredfields(classinfo *c, bool publicOnly)
Definition: class.cpp:1765
classref_or_classinfo outer_class
Definition: class.hpp:166
java_object_t header
Definition: class.hpp:73
dummy_java_lang_Class object
Definition: class.hpp:88
ConstantPoolTag
Types for entries of a classes constant pool.
Definition: global.hpp:162
fieldinfo * class_resolvefield(classinfo *c, Utf8String name, Utf8String desc, classinfo *referer)
Definition: class.cpp:1383
constant_classref * class_get_classref_component_of(constant_classref *ref)
Definition: class.cpp:1094
methodinfo * class_resolveclassmethod(classinfo *c, Utf8String name, Utf8String desc, classinfo *referer, bool throwexception)
Definition: class.cpp:1211
void * class_getconstant(classinfo *c, u4 pos, ConstantPoolTag ctype)
Definition: class.cpp:679
Definition: os.hpp:123
methodinfo * methods
Definition: class.hpp:113
java_handle_t * class_get_enclosingconstructor(classinfo *c)
Return the enclosing constructor as java.lang.reflect.Constructor object for the given class...
Definition: class.cpp:1962
argument_type from
classinfo * nextsub
Definition: class.hpp:104
methodinfo * class_get_enclosingmethod_raw(classinfo *c)
Definition: class.cpp:1995
static classinfo * class_get_superclass(classinfo *c)
Definition: class.hpp:420
classinfo * super
Definition: class.hpp:102
Utf8String packagename
Definition: class.hpp:132
extra_classref(extra_classref *next, classinfo *referer, Utf8String name)
Definition: class.hpp:179
void class_print(classinfo *c)
Definition: class.cpp:2231
int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
Definition: class.cpp:2126
classinfo * class_array_of(classinfo *component, bool link)
Definition: class.cpp:832
s4 state
Definition: class.hpp:115
u1 * cptags
Definition: class.hpp:95
Utf8String class_get_signature(classinfo *c)
Definition: class.cpp:2183
void * innerclass_getconstant(classinfo *c, u4 pos, ConstantPoolTag ctype)
Definition: class.cpp:708
classinfo * class_get_declaringclass(classinfo *c)
Definition: class.cpp:1882
classloader_t * classloader
Definition: class.hpp:151
void class_classref_or_classinfo_print(classref_or_classinfo c)
Definition: class.cpp:2306
s4 classrefcount
Definition: class.hpp:98
classref_or_classinfo enclosingclass
Definition: class.hpp:129
int32_t interfacescount
Definition: class.hpp:106
void class_classref_println(constant_classref *cr)
Definition: class.cpp:2291
s4 instancesize
Definition: class.hpp:118
bool class_is_instance(classinfo *c, java_handle_t *h)
Definition: class.cpp:1572
uint8_t u1
Definition: types.hpp:40
classinfo * class_get_enclosingclass(classinfo *c)
Definition: class.cpp:1920
static bool class_is_array(classinfo *c)
Definition: class.hpp:341
methodinfo * finalizer
Definition: class.hpp:123
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
java_object_t * method_annotationdefaults
Definition: class.hpp:144
constant_classref * classrefs
Definition: class.hpp:99
methodinfo * class_findmethod(classinfo *c, Utf8String name, Utf8String desc)
Definition: class.cpp:1124
bool class_is_arraycompatible(arraydescriptor *desc, arraydescriptor *target)
Definition: class.cpp:1488
static classloader_t * class_get_classloader(classinfo *c)
Definition: class.hpp:402
classinfo * class_multiarray_of(s4 dim, classinfo *element, bool link)
Definition: class.cpp:881
void class_set_packagename(classinfo *c)
Definition: class.cpp:102
constant_nameandtype * enclosingmethod
Definition: class.hpp:130
java_object_t * method_parameterannotations
Definition: class.hpp:142
cacao::ClassFileVersion version
Definition: class.hpp:92
ClassState
Definition: class.hpp:54
int32_t fieldscount
Definition: class.hpp:109
void link(basicblock *v, basicblock *w)
Definition: dominator.cpp:178
java_handle_objectarray_t * class_get_declaredconstructors(classinfo *c, bool publicOnly)
Return an array of declared constructors of the given class.
Definition: class.cpp:1708
java_handle_t * class_get_classname(classinfo *c)
Returns the classname of the class, where slashes (&#39;/&#39;) are replaced by dots (&#39;.
Definition: class.cpp:86
fieldinfo * fields
Definition: class.hpp:110
fieldinfo * class_findfield_by_name(classinfo *c, Utf8String name, bool throwexception)
Definition: class.cpp:1319
java_object_t * method_annotations
Definition: class.hpp:141
bool class_load_attributes(ClassBuffer &cb)
Definition: class.cpp:456
A version of the Java class file format.
Definition: loader.hpp:108
java_handle_objectarray_t * class_get_declaredclasses(classinfo *c, bool publicOnly)
Definition: class.cpp:1626
constant_classref * class_lookup_classref(classinfo *cls, Utf8String name)
Definition: class.cpp:941
threadobject * initializing_thread
Definition: class.hpp:120
java_handle_objectarray_t * class_get_interfaces(classinfo *c)
Definition: class.cpp:2065
java_handle_bytearray_t * class_get_annotations(classinfo *c)
Definition: class.cpp:2100
constant_classref * class_get_self_classref(classinfo *cls)
Definition: class.cpp:1013
uint16_t u2
Definition: types.hpp:43
java_handle_t * class_get_enclosingmethod(classinfo *c)
Return the enclosing method as java.lang.reflect.Method object for the given class.
Definition: class.cpp:2036
bool class_initializing_thread_is_self(classinfo *c)
Helper function for the function class_is_or_almost_initialized.
Definition: class.cpp:2169
java_handle_objectarray_t * class_get_declaredmethods(classinfo *c, bool publicOnly)
Definition: class.cpp:1818
classinfo * sub
Definition: class.hpp:103
Utf8String name
Definition: class.hpp:91
void class_println(classinfo *c)
Definition: class.cpp:2276
methodinfo * class_resolvemethod(classinfo *c, Utf8String name, Utf8String desc)
Definition: class.cpp:1145
u2 innerclasscount
Definition: class.hpp:125
void class_showconstantpool(classinfo *c)
Definition: class.cpp:2342
static bool class_is_interface(classinfo *c)
Definition: class.hpp:357
fieldinfo * class_findfield(classinfo *c, Utf8String name, Utf8String desc)
Definition: class.cpp:1299
void class_classref_print(constant_classref *cr)
Definition: class.cpp:2251
s4 flags
Definition: class.hpp:90
void class_printflags(classinfo *c)
Definition: class.cpp:2202
classref_or_classinfo inner_class
Definition: class.hpp:165
static bool class_is_memberclass(classinfo *c)
Definition: class.hpp:387
constant_classref * class_get_classref(classinfo *cls, Utf8String name)
Definition: class.cpp:985
int32_t s4
Definition: types.hpp:45
static bool class_is_anonymousclass(classinfo *c)
Definition: class.hpp:326
MIIterator pos
bool class_isanysubclass(classinfo *sub, classinfo *super)
Definition: class.cpp:1435
s4 cpcount
Definition: class.hpp:94
s4 index
Definition: class.hpp:116
extra_classref * next
Definition: class.hpp:182
Utf8String sourcefile
Definition: class.hpp:133
bool class_issubclass(classinfo *sub, classinfo *super)
Definition: class.cpp:1404
int32_t methodscount
Definition: class.hpp:112
OStream & operator<<(OStream &OS, const std::string &t)
Definition: OStream.hpp:459
classinfo ** interfaces
Definition: class.hpp:107
arraydescriptor * arraydesc
Definition: vftbl.hpp:101
void class_classref_or_classinfo_println(classref_or_classinfo c)
Definition: class.cpp:2327
uint32_t u4
Definition: types.hpp:46
vftbl_t * vftbl
Definition: class.hpp:121
java_object_t * field_annotations
Definition: class.hpp:147
static bool class_is_localclass(classinfo *c)
Definition: class.hpp:372
void class_showmethods(classinfo *c)
Definition: class.cpp:2439
void class_free(classinfo *c)
Definition: class.cpp:739
classinfo * link_class(classinfo *c)
Definition: linker.cpp:378
static bool class_is_or_almost_initialized(classinfo *c)
Definition: class.hpp:433
Utf8String signature
Definition: class.hpp:135
void class_postset_header_vftbl(void)
Definition: class.cpp:210
bool class_is_assignable_from(classinfo *to, classinfo *from)
Definition: class.cpp:1539
void ** cpinfos
Definition: class.hpp:96
extra_classref * extclassrefs
Definition: class.hpp:100
classref_or_classinfo declaringclass
Definition: class.hpp:128
BeginInst * target
static bool class_is_primitive(classinfo *c)
Definition: class.hpp:311
classinfo * class_create_classinfo(Utf8String classname)
Definition: class.cpp:145
constant_classref classref
Definition: class.hpp:183
java_object_t * annotations
Definition: class.hpp:139
innerclassinfo * innerclass
Definition: class.hpp:126
const char const void jint length
Definition: jvmti.h:352
classinfo * class_define(Utf8String name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd)
Definition: class.cpp:243
classinfo * class_get_componenttype(classinfo *c)
Definition: class.cpp:1594
constant_classref * class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
Definition: class.cpp:1038
Utf8String name
Definition: class.hpp:167
methodinfo * class_resolveinterfacemethod(classinfo *c, Utf8String name, Utf8String desc, classinfo *referer, bool throwexception)
Definition: class.cpp:1261