2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / Class.h
blob1577628cb98e445930c8048ebe5316a95e3f8cfe
1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 // Written primary using compiler source and Class.java as guides.
12 #ifndef __JAVA_LANG_CLASS_H__
13 #define __JAVA_LANG_CLASS_H__
15 #pragma interface
17 #include <java/lang/Object.h>
18 #include <java/lang/String.h>
19 #include <java/net/URL.h>
20 #include <java/lang/reflect/Modifier.h>
21 #include <java/security/ProtectionDomain.h>
22 #include <java/lang/Package.h>
23 #include <gnu/gcj/runtime/StackTrace.h>
25 // We declare these here to avoid including gcj/cni.h.
26 extern "C" void _Jv_InitClass (jclass klass);
27 extern "C" void _Jv_RegisterClasses (jclass *classes);
29 // This must be predefined with "C" linkage.
30 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
31 int meth_idx);
33 // These are the possible values for the `state' field of the class
34 // structure. Note that ordering is important here. Whenever the
35 // state changes, one should notify all waiters of this class.
36 enum
38 JV_STATE_NOTHING = 0, // Set by compiler.
40 JV_STATE_PRELOADING = 1, // Can do _Jv_FindClass.
41 JV_STATE_LOADING = 3, // Has super installed.
42 JV_STATE_LOADED = 5, // Is complete.
44 JV_STATE_COMPILED = 6, // This was a compiled class.
46 JV_STATE_PREPARED = 7, // Layout & static init done.
47 JV_STATE_LINKED = 9, // Strings interned.
49 JV_STATE_IN_PROGRESS = 10, // <Clinit> running.
51 JV_STATE_ERROR = 12,
53 JV_STATE_DONE = 14 // Must be last.
56 struct _Jv_Field;
57 struct _Jv_VTable;
58 union _Jv_word;
59 struct _Jv_ArrayVTable;
61 struct _Jv_Constants
63 jint size;
64 jbyte *tags;
65 _Jv_word *data;
68 struct _Jv_Method
70 // Method name.
71 _Jv_Utf8Const *name;
72 // Method signature.
73 _Jv_Utf8Const *signature;
74 // Access flags.
75 _Jv_ushort accflags;
76 // Method's index in the vtable.
77 _Jv_ushort index;
78 // Pointer to underlying function.
79 void *ncode;
80 // NULL-terminated list of exception class names; can be NULL if
81 // there are none such.
82 _Jv_Utf8Const **throws;
84 _Jv_Method *getNextMethod ()
85 { return this + 1; }
88 // Interface Dispatch Tables
89 union _Jv_IDispatchTable
91 struct
93 // Index into interface's ioffsets.
94 jshort iindex;
95 jshort itable_length;
96 // Class Interface dispatch table.
97 void **itable;
98 } cls;
100 struct
102 // Offsets into implementation class itables.
103 jshort *ioffsets;
104 } iface;
107 // Used by _Jv_GetInterfaces ()
108 struct _Jv_ifaces
110 jclass *list;
111 jshort len;
112 jshort count;
115 struct _Jv_MethodSymbol
117 _Jv_Utf8Const *class_name;
118 _Jv_Utf8Const *name;
119 _Jv_Utf8Const *signature;
122 struct _Jv_OffsetTable
124 jint state;
125 jint offsets[];
128 struct _Jv_AddressTable
130 jint state;
131 void *addresses[];
134 struct _Jv_CatchClass
136 java::lang::Class **address;
137 _Jv_Utf8Const *classname;
140 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
142 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
144 class java::lang::Class : public java::lang::Object
146 public:
147 static jclass forName (jstring className, jboolean initialize,
148 java::lang::ClassLoader *loader);
149 static jclass forName (jstring className);
150 JArray<jclass> *getClasses (void);
152 java::lang::ClassLoader *getClassLoader (void);
154 // This is an internal method that circumvents the usual security
155 // checks when getting the class loader.
156 java::lang::ClassLoader *getClassLoaderInternal (void)
158 return loader;
161 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
162 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
163 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
164 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
165 java::lang::reflect::Field *getDeclaredField (jstring);
166 JArray<java::lang::reflect::Field *> *getDeclaredFields ();
167 JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean);
168 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
169 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
171 JArray<jclass> *getDeclaredClasses (void);
172 jclass getDeclaringClass (void);
174 java::lang::reflect::Field *getField (jstring);
175 private:
176 JArray<java::lang::reflect::Field *> internalGetFields ();
177 JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
178 java::lang::reflect::Field *getField (jstring, jint);
179 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
180 jint offset);
181 java::lang::reflect::Field *getPrivateField (jstring);
182 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
183 java::security::ProtectionDomain *getProtectionDomain0 ();
185 java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
186 java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
188 public:
189 JArray<java::lang::reflect::Field *> *getFields (void);
191 JArray<jclass> *getInterfaces (void);
193 void getSignature (java::lang::StringBuffer *buffer);
194 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
195 JArray<java::lang::reflect::Method *> *getMethods (void);
197 inline jint getModifiers (void)
199 return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
202 jstring getName (void);
204 java::net::URL *getResource (jstring resourceName);
205 java::io::InputStream *getResourceAsStream (jstring resourceName);
206 JArray<jobject> *getSigners (void);
207 void setSigners(JArray<jobject> *);
209 inline jclass getSuperclass (void)
211 return superclass;
214 inline jboolean isArray (void)
216 return name->data[0] == '[';
219 inline jclass getComponentType (void)
221 return isArray () ? (* (jclass *) &methods) : 0;
224 jboolean isAssignableFrom (jclass cls);
225 jboolean isInstance (jobject obj);
227 inline jboolean isInterface (void)
229 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
232 inline jboolean isPrimitive (void)
234 return vtable == JV_PRIMITIVE_VTABLE;
237 jobject newInstance (void);
238 java::security::ProtectionDomain *getProtectionDomain (void);
239 java::lang::Package *getPackage (void);
240 jstring toString (void);
241 jboolean desiredAssertionStatus (void);
243 // FIXME: this probably shouldn't be public.
244 jint size (void)
246 return size_in_bytes;
249 // finalization
250 void finalize ();
252 // This constructor is used to create Class object for the primitive
253 // types. See prims.cc.
254 Class ();
256 static java::lang::Class class$;
258 private:
260 void memberAccessCheck (jint flags);
262 void initializeClass (void);
264 static jstring getPackagePortion (jstring);
266 // Friend functions implemented in natClass.cc.
267 friend _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
268 _Jv_Utf8Const *signature);
269 friend jboolean _Jv_IsAssignableFrom(jclass, jclass);
270 friend jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
271 friend void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
272 int method_idx);
274 inline friend void
275 _Jv_InitClass (jclass klass)
277 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
278 return;
279 klass->initializeClass ();
282 friend _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
283 _Jv_Utf8Const*);
284 friend jfieldID JvGetFirstInstanceField (jclass);
285 friend jint JvNumInstanceFields (jclass);
286 friend jfieldID JvGetFirstStaticField (jclass);
287 friend jint JvNumStaticFields (jclass);
289 friend jobject _Jv_AllocObject (jclass, jint);
290 friend void *_Jv_AllocObj (jint, jclass);
291 friend void *_Jv_AllocPtrFreeObj (jint, jclass);
292 friend void *_Jv_AllocArray (jint, jclass);
294 friend jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
295 jboolean);
296 friend jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
297 jboolean);
298 friend jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
300 friend jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
301 friend jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
302 friend jint JvNumMethods (jclass);
303 friend jmethodID JvGetFirstMethod (jclass);
305 // Friends classes and functions to implement the ClassLoader
306 friend class java::lang::ClassLoader;
307 friend class java::lang::VMClassLoader;
309 friend class java::io::ObjectOutputStream;
310 friend class java::io::ObjectInputStream;
311 friend class java::io::ObjectStreamClass;
313 friend void _Jv_WaitForState (jclass, int);
314 friend void _Jv_RegisterClasses (jclass *classes);
315 friend void _Jv_RegisterClassHookDefault (jclass klass);
316 friend void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
317 friend void _Jv_UnregisterClass (jclass);
318 friend jclass _Jv_FindClass (_Jv_Utf8Const *name,
319 java::lang::ClassLoader *loader);
320 friend jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
321 java::lang::ClassLoader *loader);
322 friend jclass _Jv_PopClass (void);
323 friend void _Jv_PushClass (jclass k);
324 friend void _Jv_NewArrayClass (jclass element,
325 java::lang::ClassLoader *loader,
326 _Jv_VTable *array_vtable = 0);
327 friend jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
328 java::lang::ClassLoader *loader);
329 friend void _Jv_InitNewClassFields (jclass klass);
331 // in prims.cc
332 friend void _Jv_InitPrimClass (jclass, char *, char, int, _Jv_ArrayVTable *);
334 friend void _Jv_PrepareCompiledClass (jclass);
335 friend void _Jv_PrepareConstantTimeTables (jclass);
336 friend jshort _Jv_GetInterfaces (jclass, _Jv_ifaces *);
337 friend void _Jv_GenerateITable (jclass, _Jv_ifaces *, jshort *);
338 friend jstring _Jv_GetMethodString(jclass, _Jv_Utf8Const *);
339 friend jshort _Jv_AppendPartialITable (jclass, jclass, void **, jshort);
340 friend jshort _Jv_FindIIndex (jclass *, jshort *, jshort);
341 friend void _Jv_LinkSymbolTable (jclass);
342 friend void _Jv_LayoutVTableMethods (jclass klass);
343 friend void _Jv_SetVTableEntries (jclass, _Jv_VTable *, jboolean *);
344 friend void _Jv_MakeVTable (jclass);
345 friend void _Jv_linkExceptionClassTable (jclass);
347 friend jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
348 jint flags);
350 // Return array class corresponding to element type KLASS, creating it if
351 // necessary.
352 inline friend jclass
353 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
355 extern void _Jv_NewArrayClass (jclass element,
356 java::lang::ClassLoader *loader,
357 _Jv_VTable *array_vtable = 0);
358 if (__builtin_expect (!klass->arrayclass, false))
359 _Jv_NewArrayClass (klass, loader);
360 return klass->arrayclass;
363 #ifdef INTERPRETER
364 friend jboolean _Jv_IsInterpretedClass (jclass);
365 friend void _Jv_InitField (jobject, jclass, _Jv_Field*);
366 friend void _Jv_InitField (jobject, jclass, int);
367 friend _Jv_word _Jv_ResolvePoolEntry (jclass, int);
368 friend _Jv_Method *_Jv_SearchMethodInClass (jclass cls, jclass klass,
369 _Jv_Utf8Const *method_name,
370 _Jv_Utf8Const *method_signature);
372 friend void _Jv_PrepareClass (jclass);
373 friend void _Jv_PrepareMissingMethods (jclass base, jclass iface_class);
375 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
377 friend class _Jv_ClassReader;
378 friend class _Jv_InterpClass;
379 friend class _Jv_InterpMethod;
380 #endif
382 #ifdef JV_MARKOBJ_DECL
383 friend JV_MARKOBJ_DECL;
384 #endif
386 friend class _Jv_BytecodeVerifier;
387 friend class gnu::gcj::runtime::StackTrace;
388 friend class java::io::VMObjectStreamClass;
390 friend void _Jv_sharedlib_register_hook (jclass klass);
392 // Chain for class pool.
393 jclass next;
394 // Name of class.
395 _Jv_Utf8Const *name;
396 // Access flags for class.
397 _Jv_ushort accflags;
398 // The superclass, or null for Object.
399 jclass superclass;
400 // Class constants.
401 _Jv_Constants constants;
402 // Methods. If this is an array class, then this field holds a
403 // pointer to the element type.
404 _Jv_Method *methods;
405 // Number of methods. If this class is primitive, this holds the
406 // character used to represent this type in a signature.
407 jshort method_count;
408 // Number of methods in the vtable.
409 jshort vtable_method_count;
410 // The fields.
411 _Jv_Field *fields;
412 // Size of instance fields, in bytes.
413 jint size_in_bytes;
414 // Total number of fields (instance and static).
415 jshort field_count;
416 // Number of static fields.
417 jshort static_field_count;
418 // The vtbl for all objects of this class.
419 _Jv_VTable *vtable;
420 // Virtual method offset table.
421 _Jv_OffsetTable *otable;
422 // Offset table symbols.
423 _Jv_MethodSymbol *otable_syms;
424 _Jv_AddressTable *atable;
425 _Jv_MethodSymbol *atable_syms;
426 _Jv_CatchClass *catch_classes;
427 // Interfaces implemented by this class.
428 jclass *interfaces;
429 // The class loader for this class.
430 java::lang::ClassLoader *loader;
431 // Number of interfaces.
432 jshort interface_count;
433 // State of this class.
434 jbyte state;
435 // The thread which has locked this class. Used during class
436 // initialization.
437 java::lang::Thread *thread;
438 // How many levels of "extends" this class is removed from Object.
439 jshort depth;
440 // Vector of this class's superclasses, ordered by decreasing depth.
441 jclass *ancestors;
442 // Interface Dispatch Table.
443 _Jv_IDispatchTable *idt;
444 // Pointer to the class that represents an array of this class.
445 jclass arrayclass;
446 // Security Domain to which this class belongs (or null).
447 java::security::ProtectionDomain *protectionDomain;
448 // Signers of this class (or null).
449 JArray<jobject> *hack_signers;
450 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
451 jclass chain;
454 #endif /* __JAVA_LANG_CLASS_H__ */