OSDN Git Service

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