OSDN Git Service

gcc/java:
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Class.h
1 // Class.h - Header file for java.lang.Class.  -*- c++ -*-
2
3 /* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 // Written primary using compiler source and Class.java as guides.
12 #ifndef __JAVA_LANG_CLASS_H__
13 #define __JAVA_LANG_CLASS_H__
14
15 #pragma interface
16
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
23 // We declare these here to avoid including gcj/cni.h.
24 extern "C" void _Jv_InitClass (jclass klass);
25 extern "C" void _Jv_RegisterClasses (jclass *classes);
26
27 // This must be predefined with "C" linkage.
28 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
29                                                int meth_idx);
30
31 // These are the possible values for the `state' field of the class
32 // structure.  Note that ordering is important here.  Whenever the
33 // state changes, one should notify all waiters of this class.
34 enum
35 {
36   JV_STATE_NOTHING = 0,         // Set by compiler.
37
38   JV_STATE_PRELOADING = 1,      // Can do _Jv_FindClass.
39   JV_STATE_LOADING = 3,         // Has super installed.
40   JV_STATE_LOADED = 5,          // Is complete.
41     
42   JV_STATE_COMPILED = 6,        // This was a compiled class.
43
44   JV_STATE_PREPARED = 7,        // Layout & static init done.
45   JV_STATE_LINKED = 9,          // Strings interned.
46
47   JV_STATE_IN_PROGRESS = 10,    // <Clinit> running.
48   JV_STATE_DONE = 12,           // 
49
50   JV_STATE_ERROR = 14           // must be last.
51 };
52
53 struct _Jv_Field;
54 struct _Jv_VTable;
55 union _Jv_word;
56 struct _Jv_ArrayVTable;
57
58 struct _Jv_Constants
59 {
60   jint size;
61   jbyte *tags;
62   _Jv_word *data;
63 };
64
65 struct _Jv_Method
66 {
67   // Method name.
68   _Jv_Utf8Const *name;
69   // Method signature.
70   _Jv_Utf8Const *signature;
71   // Access flags.
72   _Jv_ushort accflags;
73   // Method's index in the vtable.
74   _Jv_ushort index;
75   // Pointer to underlying function.
76   void *ncode;
77   // NULL-terminated list of exception class names; can be NULL if
78   // there are none such.
79   _Jv_Utf8Const **throws;
80
81   _Jv_Method *getNextMethod ()
82   { return this + 1; }
83 };
84
85 // Interface Dispatch Tables 
86 union _Jv_IDispatchTable
87 {
88   struct
89   {
90     // Index into interface's ioffsets.
91     jshort iindex;
92     jshort itable_length;
93     // Class Interface dispatch table.
94     void **itable;
95   } cls;
96
97   struct
98   {
99     // Offsets into implementation class itables.
100     jshort *ioffsets;
101   } iface;
102 };
103
104 // Used by _Jv_GetInterfaces ()
105 struct _Jv_ifaces
106 {
107   jclass *list;
108   jshort len;
109   jshort count;
110 };
111
112 // Used for vtable pointer manipulation.
113 union _Jv_Self
114 {
115   char *vtable_ptr;
116   jclass self;
117 };
118
119 struct _Jv_MethodSymbol
120 {
121   _Jv_Utf8Const *class_name;
122   _Jv_Utf8Const *name;
123   _Jv_Utf8Const *signature;
124 };
125
126 struct _Jv_OffsetTable
127 {
128   jint state;
129   jint offsets[];
130 };
131
132 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
133
134 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
135
136 class java::lang::Class : public java::lang::Object
137 {
138 public:
139   static jclass forName (jstring className, jboolean initialize, 
140                          java::lang::ClassLoader *loader);
141   static jclass forName (jstring className);
142   JArray<jclass> *getClasses (void);
143
144   java::lang::ClassLoader *getClassLoader (void)
145     {
146       return loader;
147     }
148
149   java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
150   JArray<java::lang::reflect::Constructor *> *getConstructors (void);
151   java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
152   JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
153   java::lang::reflect::Field *getDeclaredField (jstring);
154   JArray<java::lang::reflect::Field *> *getDeclaredFields (void);
155   java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
156   JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
157
158   JArray<jclass> *getDeclaredClasses (void);
159   jclass getDeclaringClass (void);
160
161   java::lang::reflect::Field *getField (jstring);
162 private:
163   jint _getFields (JArray<java::lang::reflect::Field *> *result, jint offset);
164   JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
165   java::lang::reflect::Field *getField (jstring, jint);
166   jint _getMethods (JArray<java::lang::reflect::Method *> *result,
167                     jint offset);
168   java::lang::reflect::Field *getPrivateField (jstring);
169   java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
170   java::security::ProtectionDomain *getProtectionDomain0 ();
171
172 public:
173   JArray<java::lang::reflect::Field *> *getFields (void);
174
175   JArray<jclass> *getInterfaces (void);
176
177   void getSignature (java::lang::StringBuffer *buffer);
178   static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
179   java::lang::reflect::Method *getMethod (jstring, JArray<jclass> *);
180   JArray<java::lang::reflect::Method *> *getMethods (void);
181
182   inline jint getModifiers (void)
183     {
184       return accflags;
185     }
186
187   jstring getName (void);
188
189   java::net::URL        *getResource (jstring resourceName);
190   java::io::InputStream *getResourceAsStream (jstring resourceName);
191   JArray<jobject> *getSigners (void);
192
193   inline jclass getSuperclass (void)
194     {
195       return superclass;
196     }
197
198   inline jboolean isArray (void)
199     {
200       return name->data[0] == '[';
201     }
202
203   inline jclass getComponentType (void)
204     {
205       return isArray () ? (* (jclass *) &methods) : 0;
206     }
207
208   jboolean isAssignableFrom (jclass cls);
209   jboolean isInstance (jobject obj);
210
211   inline jboolean isInterface (void)
212   {
213     return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
214   }
215   
216   inline jboolean isPrimitive (void)
217     {
218       return vtable == JV_PRIMITIVE_VTABLE;
219     }
220
221   jobject newInstance (void);
222   jstring toString (void);
223
224   // FIXME: this probably shouldn't be public.
225   jint size (void)
226     {
227       return size_in_bytes;
228     }
229     
230   // finalization
231   void finalize ();
232
233   // This constructor is used to create Class object for the primitive
234   // types. See prims.cc.
235   Class ()
236   {
237     // C++ ctors set the vtbl pointer to point at an offset inside the vtable
238     // object. That doesn't work for Java, so this hack adjusts it back.
239     ((_Jv_Self *)this)->vtable_ptr -= 2 * sizeof (void *);
240   }
241
242   static java::lang::Class class$;
243
244 private:   
245
246   void checkMemberAccess (jint flags);
247
248   void initializeClass (void);
249
250   // Friend functions implemented in natClass.cc.
251   friend _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
252                                          _Jv_Utf8Const *signature);
253   friend jboolean _Jv_IsAssignableFrom(jclass, jclass);
254   friend jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
255   friend void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
256                                              int method_idx);
257
258   inline friend void 
259   _Jv_InitClass (jclass klass)
260   {
261     if (__builtin_expect (klass->state == JV_STATE_DONE, true))
262       return;
263     klass->initializeClass ();  
264   }
265
266   friend _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *, 
267                                                _Jv_Utf8Const*);
268   friend jfieldID JvGetFirstInstanceField (jclass);
269   friend jint JvNumInstanceFields (jclass);
270   friend jfieldID JvGetFirstStaticField (jclass);
271   friend jint JvNumStaticFields (jclass);
272
273   friend jobject _Jv_AllocObject (jclass, jint);
274   friend void *_Jv_AllocObj (jint, jclass);
275   friend void *_Jv_AllocPtrFreeObj (jint, jclass);
276   friend void *_Jv_AllocArray (jint, jclass);
277
278   friend jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
279                                            jboolean);
280   friend jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
281                                             jboolean);
282   friend jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
283
284   friend jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
285   friend jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
286   friend jint JvNumMethods (jclass);
287   friend jmethodID JvGetFirstMethod (jclass);
288
289   // Friends classes and functions to implement the ClassLoader
290   friend class java::lang::ClassLoader;
291
292   friend class java::io::ObjectOutputStream;
293   friend class java::io::ObjectInputStream;
294   friend class java::io::ObjectStreamClass;
295
296   friend void _Jv_WaitForState (jclass, int);
297   friend void _Jv_RegisterClasses (jclass *classes);
298   friend void _Jv_RegisterClassHookDefault (jclass klass);
299   friend void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
300   friend void _Jv_UnregisterClass (jclass);
301   friend jclass _Jv_FindClass (_Jv_Utf8Const *name,
302                                java::lang::ClassLoader *loader);
303   friend jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
304                                       java::lang::ClassLoader *loader);
305   friend void _Jv_NewArrayClass (jclass element,
306                                  java::lang::ClassLoader *loader,
307                                  _Jv_VTable *array_vtable = 0);
308   friend jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
309                               java::lang::ClassLoader *loader);
310
311   // in prims.cc
312   friend void _Jv_InitPrimClass (jclass, char *, char, int, _Jv_ArrayVTable *);
313
314   friend void _Jv_PrepareCompiledClass (jclass);
315   friend void _Jv_PrepareConstantTimeTables (jclass);
316   friend jshort _Jv_GetInterfaces (jclass, _Jv_ifaces *);
317   friend void _Jv_GenerateITable (jclass, _Jv_ifaces *, jshort *);
318   friend jstring _Jv_GetMethodString(jclass, _Jv_Utf8Const *);
319   friend jshort _Jv_AppendPartialITable (jclass, jclass, void **, jshort);
320   friend jshort _Jv_FindIIndex (jclass *, jshort *, jshort);
321   friend void _Jv_LinkOffsetTable (jclass);
322   friend void _Jv_LayoutVTableMethods (jclass klass);
323   friend void _Jv_SetVTableEntries (jclass, _Jv_VTable *);
324   friend void _Jv_MakeVTable (jclass);
325
326   // Return array class corresponding to element type KLASS, creating it if
327   // necessary.
328   inline friend jclass
329   _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
330   {
331     if (__builtin_expect (!klass->arrayclass, false))
332       _Jv_NewArrayClass (klass, loader);
333     return klass->arrayclass;
334   }
335
336 #ifdef INTERPRETER
337   friend jboolean _Jv_IsInterpretedClass (jclass);
338   friend void _Jv_InitField (jobject, jclass, _Jv_Field*);
339   friend int _Jv_DetermineVTableIndex (jclass, _Jv_Utf8Const *, 
340                                        _Jv_Utf8Const*);
341   friend void _Jv_InitField (jobject, jclass, int);
342   friend _Jv_word _Jv_ResolvePoolEntry (jclass, int);
343   friend _Jv_Method *_Jv_SearchMethodInClass (jclass cls, jclass klass, 
344                                               _Jv_Utf8Const *method_name, 
345                                               _Jv_Utf8Const *method_signature);
346
347   friend void _Jv_PrepareClass (jclass);
348
349   friend class _Jv_ClassReader; 
350   friend class _Jv_InterpClass;
351   friend class _Jv_InterpMethod;
352   friend class _Jv_InterpMethodInvocation;
353 #endif
354
355 #ifdef JV_MARKOBJ_DECL
356   friend JV_MARKOBJ_DECL;
357 #endif
358
359   friend class _Jv_BytecodeVerifier;
360
361   // Chain for class pool.
362   jclass next;
363   // Name of class.
364   _Jv_Utf8Const *name;
365   // Access flags for class.
366   _Jv_ushort accflags;
367   // The superclass, or null for Object.
368   jclass superclass;
369   // Class constants.
370   _Jv_Constants constants;
371   // Methods.  If this is an array class, then this field holds a
372   // pointer to the element type.
373   _Jv_Method *methods;
374   // Number of methods.  If this class is primitive, this holds the
375   // character used to represent this type in a signature.
376   jshort method_count;
377   // Number of methods in the vtable.
378   jshort vtable_method_count;
379   // The fields.
380   _Jv_Field *fields;
381   // Size of instance fields, in bytes.
382   jint size_in_bytes;
383   // Total number of fields (instance and static).
384   jshort field_count;
385   // Number of static fields.
386   jshort static_field_count;
387   // The vtbl for all objects of this class.
388   _Jv_VTable *vtable;
389   // Virtual method offset table.
390   _Jv_OffsetTable *otable;
391   // Offset table symbols.
392   _Jv_MethodSymbol *otable_syms;
393   // Interfaces implemented by this class.
394   jclass *interfaces;
395   // The class loader for this class.
396   java::lang::ClassLoader *loader;
397   // Number of interfaces.
398   jshort interface_count;
399   // State of this class.
400   jbyte state;
401   // The thread which has locked this class.  Used during class
402   // initialization.
403   java::lang::Thread *thread;
404   // How many levels of "extends" this class is removed from Object.
405   jshort depth;
406   // Vector of this class's superclasses, ordered by decreasing depth.
407   jclass *ancestors;
408   // Interface Dispatch Table.
409   _Jv_IDispatchTable *idt;
410   // Pointer to the class that represents an array of this class.
411   jclass arrayclass;
412   // Security Domain to which this class belongs (or null).
413   java::security::ProtectionDomain *protectionDomain;
414 };
415
416 #endif /* __JAVA_LANG_CLASS_H__ */