OSDN Git Service

* Merged gcj-abi-2-dev-branch to trunk.
[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, 2002, 2003, 2004  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 #include <java/lang/Package.h>
24 #include <gnu/gcj/runtime/StackTrace.h>
25
26 // We declare these here to avoid including gcj/cni.h.
27 extern "C" void _Jv_InitClass (jclass klass);
28 extern "C" void _Jv_RegisterClasses (const jclass *classes);
29 extern "C" void _Jv_RegisterClasses_Counted (const jclass *classes,
30                                              size_t count);
31
32 // This must be predefined with "C" linkage.
33 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
34                                                int meth_idx);
35
36 // These are the possible values for the `state' field of the class
37 // structure.  Note that ordering is important here.  Whenever the
38 // state changes, one should notify all waiters of this class.
39 enum
40 {
41   JV_STATE_NOTHING = 0,         // Set by compiler.
42
43   JV_STATE_PRELOADING = 1,      // Can do _Jv_FindClass.
44   JV_STATE_LOADING = 3,         // Has super installed.
45   JV_STATE_READ = 4,            // Has been completely defined.
46   JV_STATE_LOADED = 5,          // Has Miranda methods defined.
47
48   JV_STATE_COMPILED = 6,        // This was a compiled class.
49
50   JV_STATE_PREPARED = 7,        // Layout & static init done.
51   JV_STATE_LINKED = 9,          // Strings interned.
52
53   JV_STATE_IN_PROGRESS = 10,    // <clinit> running.
54
55   JV_STATE_ERROR = 12,
56
57   JV_STATE_DONE = 14            // Must be last.
58 };
59
60 struct _Jv_Field;
61 struct _Jv_VTable;
62 union _Jv_word;
63 struct _Jv_ArrayVTable;
64 class _Jv_ExecutionEngine;
65 class _Jv_CompiledEngine;
66 class _Jv_InterpreterEngine;
67
68 struct _Jv_Constants
69 {
70   jint size;
71   jbyte *tags;
72   _Jv_word *data;
73 };
74
75 struct _Jv_Method
76 {
77   // Method name.
78   _Jv_Utf8Const *name;
79   // Method signature.
80   _Jv_Utf8Const *signature;
81   // Access flags.
82   _Jv_ushort accflags;
83   // Method's index in the vtable.
84   _Jv_ushort index;
85   // Pointer to underlying function.
86   void *ncode;
87   // NULL-terminated list of exception class names; can be NULL if
88   // there are none such.
89   _Jv_Utf8Const **throws;
90
91   _Jv_Method *getNextMethod ()
92   { return this + 1; }
93 };
94
95 // Interface Dispatch Tables 
96 union _Jv_IDispatchTable
97 {
98   struct
99   {
100     // Index into interface's ioffsets.
101     jshort iindex;
102     jshort itable_length;
103     // Class Interface dispatch table.
104     void **itable;
105   } cls;
106
107   struct
108   {
109     // Offsets into implementation class itables.
110     jshort *ioffsets;
111   } iface;
112 };
113
114 // Used by _Jv_Linker::get_interfaces ()
115 struct _Jv_ifaces
116 {
117   jclass *list;
118   jshort len;
119   jshort count;
120 };
121
122 struct _Jv_MethodSymbol
123 {
124   _Jv_Utf8Const *class_name;
125   _Jv_Utf8Const *name;
126   _Jv_Utf8Const *signature;
127 };
128
129 struct _Jv_OffsetTable
130 {
131   jint state;
132   jint offsets[];
133 };
134
135 struct _Jv_AddressTable
136 {
137   jint state;
138   void *addresses[];
139 };
140
141 struct _Jv_CatchClass
142 {
143   java::lang::Class **address;
144   _Jv_Utf8Const *classname;
145 };
146
147 // Possible values for the assertion_code field in the type assertion table.
148 enum
149 {
150   JV_ASSERT_END_OF_TABLE = 0,
151   JV_ASSERT_TYPES_COMPATIBLE = 1,
152   JV_ASSERT_IS_INSTANTIABLE = 2
153 };
154
155 // Entry in the type assertion table, used to validate type constraints
156 // for binary compatibility.
157 struct _Jv_TypeAssertion
158 {
159   jint assertion_code;
160   _Jv_Utf8Const *op1;
161   _Jv_Utf8Const *op2;
162 };
163
164 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
165
166 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
167
168 // Forward declarations for friends of java::lang::Class
169
170 // Friend functions implemented in natClass.cc.
171 _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
172                                 _Jv_Utf8Const *signature);
173 jboolean _Jv_IsAssignableFrom (jclass, jclass);
174 jboolean _Jv_IsAssignableFromSlow (jclass, jclass);
175 jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
176
177 _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *, 
178                                       _Jv_Utf8Const*, jclass * = NULL);
179 jfieldID JvGetFirstInstanceField (jclass);
180 jint JvNumInstanceFields (jclass);
181 jfieldID JvGetFirstStaticField (jclass);
182 jint JvNumStaticFields (jclass);
183
184 jobject _Jv_AllocObject (jclass);
185 void *_Jv_AllocObj (jint, jclass);
186 void *_Jv_AllocPtrFreeObj (jint, jclass);
187 void *_Jv_AllocArray (jint, jclass);
188
189 jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
190                                   jboolean);
191 jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
192                                    jboolean);
193 jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
194
195 jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
196 jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
197 jint JvNumMethods (jclass);
198 jmethodID JvGetFirstMethod (jclass);
199
200 // Friend classes and functions to implement the ClassLoader
201 class java::lang::ClassLoader;
202 class java::lang::VMClassLoader;
203
204 class java::io::ObjectOutputStream;
205 class java::io::ObjectInputStream;
206 class java::io::ObjectStreamClass;
207
208 void _Jv_RegisterClassHookDefault (jclass klass);
209 void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
210 void _Jv_UnregisterClass (jclass);
211 jclass _Jv_FindClass (_Jv_Utf8Const *name,
212                       java::lang::ClassLoader *loader);
213 jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
214                              java::lang::ClassLoader *loader);
215 jclass _Jv_PopClass (void);
216 void _Jv_PushClass (jclass k);
217 void _Jv_NewArrayClass (jclass element,
218                         java::lang::ClassLoader *loader,
219                         _Jv_VTable *array_vtable = 0);
220 jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
221                      java::lang::ClassLoader *loader);
222 void _Jv_InitNewClassFields (jclass klass);
223
224 // Friend functions and classes in prims.cc
225 void _Jv_InitPrimClass (jclass, char *, char, int);
226 jstring _Jv_GetMethodString (jclass, _Jv_Utf8Const *);
227
228 jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
229                           jint flags);
230 jclass _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader);
231
232 #ifdef INTERPRETER
233 jboolean _Jv_IsInterpretedClass (jclass);
234 void _Jv_InitField (jobject, jclass, int);
235
236 class _Jv_ClassReader;  
237 class _Jv_InterpClass;
238 class _Jv_InterpMethod;
239 #endif
240
241 class _Jv_BytecodeVerifier;
242 class gnu::gcj::runtime::StackTrace;
243 class java::io::VMObjectStreamClass;
244
245 void _Jv_sharedlib_register_hook (jclass klass);
246
247
248 class java::lang::Class : public java::lang::Object
249 {
250 public:
251   static jclass forName (jstring className, jboolean initialize, 
252                          java::lang::ClassLoader *loader);
253   static jclass forName (jstring className);
254   JArray<jclass> *getClasses (void);
255
256   java::lang::ClassLoader *getClassLoader (void);
257
258   // This is an internal method that circumvents the usual security
259   // checks when getting the class loader.
260   java::lang::ClassLoader *getClassLoaderInternal (void)
261   {
262     return loader;
263   }
264
265   java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
266   JArray<java::lang::reflect::Constructor *> *getConstructors (void);
267   java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
268   JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
269   java::lang::reflect::Field *getDeclaredField (jstring);
270   JArray<java::lang::reflect::Field *> *getDeclaredFields ();
271   JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean);
272   java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
273   JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
274
275   JArray<jclass> *getDeclaredClasses (void);
276   jclass getDeclaringClass (void);
277
278   java::lang::reflect::Field *getField (jstring);
279 private:
280   JArray<java::lang::reflect::Field *> internalGetFields ();
281   JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
282   java::lang::reflect::Field *getField (jstring, jint);
283   jint _getMethods (JArray<java::lang::reflect::Method *> *result,
284                     jint offset);
285   java::lang::reflect::Field *getPrivateField (jstring);
286   java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
287   java::security::ProtectionDomain *getProtectionDomain0 ();
288
289   java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
290   java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
291
292 public:
293   JArray<java::lang::reflect::Field *> *getFields (void);
294
295   JArray<jclass> *getInterfaces (void);
296
297   void getSignature (java::lang::StringBuffer *buffer);
298   static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
299   JArray<java::lang::reflect::Method *> *getMethods (void);
300
301   inline jint getModifiers (void)
302   {
303     return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
304   }
305
306   jstring getName (void);
307
308   java::net::URL        *getResource (jstring resourceName);
309   java::io::InputStream *getResourceAsStream (jstring resourceName);
310   JArray<jobject> *getSigners (void);
311   void setSigners(JArray<jobject> *);
312
313   inline jclass getSuperclass (void)
314   {
315     return superclass;
316   }
317
318   inline jclass getInterface (jint n)
319   {
320     return interfaces[n];
321   }
322
323   inline jboolean isArray (void)
324     {
325       return name->first() == '[';
326     }
327
328   inline jclass getComponentType (void)
329     {
330       return isArray () ? (* (jclass *) &methods) : 0;
331     }
332
333   jboolean isAssignableFrom (jclass cls);
334   jboolean isInstance (jobject obj);
335
336   inline jboolean isInterface (void)
337   {
338     return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
339   }
340   
341   inline jboolean isPrimitive (void)
342     {
343       return vtable == JV_PRIMITIVE_VTABLE;
344     }
345
346   jobject newInstance (void);
347   java::security::ProtectionDomain *getProtectionDomain (void);
348   java::lang::Package *getPackage (void);
349   jstring toString (void);
350   jboolean desiredAssertionStatus (void);
351
352   // FIXME: this probably shouldn't be public.
353   jint size (void)
354   {
355     return size_in_bytes;
356   }
357
358   // The index of the first method we declare ourself (as opposed to
359   // inheriting).
360   inline jint firstMethodIndex (void)
361   {
362     return vtable_method_count - method_count;
363   }
364     
365   // finalization
366   void finalize ();
367
368   // This constructor is used to create Class object for the primitive
369   // types. See prims.cc.
370   Class ();
371
372   static java::lang::Class class$;
373
374 private:   
375
376   void memberAccessCheck (jint flags);
377
378   void initializeClass (void);
379
380   static jstring getPackagePortion (jstring);
381
382   void set_state (jint nstate)
383   {
384     state = nstate;
385     notifyAll ();
386   }
387
388   // Friend functions implemented in natClass.cc.
389   friend _Jv_Method *::_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
390                                            _Jv_Utf8Const *signature);
391   friend jboolean (::_Jv_IsAssignableFrom) (jclass, jclass);
392   friend jboolean (::_Jv_IsAssignableFromSlow) (jclass, jclass);
393   friend jboolean (::_Jv_InterfaceAssignableFrom) (jclass, jclass);
394   friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
395                                                int method_idx);
396
397   friend void ::_Jv_InitClass (jclass klass);
398
399   friend _Jv_Method* ::_Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *, 
400                                                  _Jv_Utf8Const*, jclass *);
401   friend jfieldID (::JvGetFirstInstanceField) (jclass);
402   friend jint (::JvNumInstanceFields) (jclass);
403   friend jfieldID (::JvGetFirstStaticField) (jclass);
404   friend jint (::JvNumStaticFields) (jclass);
405
406   friend jobject (::_Jv_AllocObject) (jclass);
407   friend void *::_Jv_AllocObj (jint, jclass);
408   friend void *::_Jv_AllocPtrFreeObj (jint, jclass);
409   friend void *::_Jv_AllocArray (jint, jclass);
410
411   friend jobject (::_Jv_JNI_ToReflectedField) (_Jv_JNIEnv *, jclass, jfieldID,
412                                                jboolean);
413   friend jobject (::_Jv_JNI_ToReflectedMethod) (_Jv_JNIEnv *, jclass, jmethodID,
414                                                 jboolean);
415   friend jfieldID (::_Jv_FromReflectedField) (java::lang::reflect::Field *);
416
417   friend jmethodID (::_Jv_FromReflectedMethod) (java::lang::reflect::Method *);
418   friend jmethodID (::_Jv_FromReflectedConstructor) (java::lang::reflect::Constructor *);
419   friend jint (::JvNumMethods) (jclass);
420   friend jmethodID (::JvGetFirstMethod) (jclass);
421
422   // Friends classes and functions to implement the ClassLoader
423   friend class java::lang::ClassLoader;
424   friend class java::lang::VMClassLoader;
425
426   friend class java::io::ObjectOutputStream;
427   friend class java::io::ObjectInputStream;
428   friend class java::io::ObjectStreamClass;
429
430   friend void ::_Jv_RegisterClasses (const jclass *classes);
431   friend void ::_Jv_RegisterClasses_Counted (const jclass *classes, 
432                                              size_t count);
433   friend void ::_Jv_RegisterClassHookDefault (jclass klass);
434   friend void ::_Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
435   friend void ::_Jv_UnregisterClass (jclass);
436   friend jclass (::_Jv_FindClass) (_Jv_Utf8Const *name,
437                                    java::lang::ClassLoader *loader);
438   friend jclass (::_Jv_FindClassInCache) (_Jv_Utf8Const *name,
439                                           java::lang::ClassLoader *loader);
440   friend jclass (::_Jv_PopClass) (void);
441   friend void ::_Jv_PushClass (jclass k);
442   friend void ::_Jv_NewArrayClass (jclass element,
443                                    java::lang::ClassLoader *loader,
444                                    _Jv_VTable *array_vtable);
445   friend jclass (::_Jv_NewClass) (_Jv_Utf8Const *name, jclass superclass,
446                                   java::lang::ClassLoader *loader);
447   friend void ::_Jv_InitNewClassFields (jclass klass);
448
449   // in prims.cc
450   friend void ::_Jv_InitPrimClass (jclass, char *, char, int);
451
452   friend jstring (::_Jv_GetMethodString) (jclass, _Jv_Utf8Const *);
453
454   friend jboolean (::_Jv_CheckAccess) (jclass self_klass, jclass other_klass,
455                                    jint flags);
456   
457   friend bool _Jv_getInterfaceMethod(jclass, jclass&, int&, 
458                                      const _Jv_Utf8Const*,
459                                      const _Jv_Utf8Const*);
460
461   friend jclass (::_Jv_GetArrayClass) (jclass klass,
462                                        java::lang::ClassLoader *loader);
463
464 #ifdef INTERPRETER
465   friend jboolean (::_Jv_IsInterpretedClass) (jclass);
466   friend void ::_Jv_InitField (jobject, jclass, int);
467
468   friend class ::_Jv_ClassReader;       
469   friend class ::_Jv_InterpClass;
470   friend class ::_Jv_InterpMethod;
471 #endif
472
473 #ifdef JV_MARKOBJ_DECL
474   friend JV_MARKOBJ_DECL;
475 #endif
476
477   friend class ::_Jv_BytecodeVerifier;
478   friend class gnu::gcj::runtime::StackTrace;
479   friend class java::io::VMObjectStreamClass;
480
481   friend class _Jv_Linker;
482   friend class _Jv_ExecutionEngine;
483   friend class _Jv_CompiledEngine;
484   friend class _Jv_InterpreterEngine;
485
486   friend void ::_Jv_sharedlib_register_hook (jclass klass);
487
488   // Chain for class pool.
489   jclass next;
490   // Name of class.
491   _Jv_Utf8Const *name;
492   // Access flags for class.
493   _Jv_ushort accflags;
494   // The superclass, or null for Object.
495   jclass superclass;
496   // Class constants.
497   _Jv_Constants constants;
498   // Methods.  If this is an array class, then this field holds a
499   // pointer to the element type.
500   _Jv_Method *methods;
501   // Number of methods.  If this class is primitive, this holds the
502   // character used to represent this type in a signature.
503   jshort method_count;
504   // Number of methods in the vtable.
505   jshort vtable_method_count;
506   // The fields.
507   _Jv_Field *fields;
508   // Size of instance fields, in bytes.
509   jint size_in_bytes;
510   // Total number of fields (instance and static).
511   jshort field_count;
512   // Number of static fields.
513   jshort static_field_count;
514   // The vtbl for all objects of this class.
515   _Jv_VTable *vtable;
516   // Virtual method offset table.
517   _Jv_OffsetTable *otable;
518   // Offset table symbols.
519   _Jv_MethodSymbol *otable_syms;
520   // Address table
521   _Jv_AddressTable *atable;
522   _Jv_MethodSymbol *atable_syms;
523   // Interface table
524   _Jv_AddressTable *itable;
525   _Jv_MethodSymbol *itable_syms;
526   _Jv_CatchClass *catch_classes;
527   // Interfaces implemented by this class.
528   jclass *interfaces;
529   // The class loader for this class.
530   java::lang::ClassLoader *loader;
531   // Number of interfaces.
532   jshort interface_count;
533   // State of this class.
534   jbyte state;
535   // The thread which has locked this class.  Used during class
536   // initialization.
537   java::lang::Thread *thread;
538   // How many levels of "extends" this class is removed from Object.
539   jshort depth;
540   // Vector of this class's superclasses, ordered by decreasing depth.
541   jclass *ancestors;
542   // Interface Dispatch Table.
543   _Jv_IDispatchTable *idt;
544   // Pointer to the class that represents an array of this class.
545   jclass arrayclass;
546   // Security Domain to which this class belongs (or null).
547   java::security::ProtectionDomain *protectionDomain;
548   // Pointer to the type assertion table for this class.
549   _Jv_TypeAssertion *assertion_table;
550   // Signers of this class (or null).
551   JArray<jobject> *hack_signers;
552   // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
553   jclass chain;
554   // Additional data, specific to the generator (JIT, native,
555   // interpreter) of this class.
556   void *aux_info;
557   // Execution engine.
558   _Jv_ExecutionEngine *engine;
559 };
560
561 // Inline functions that are friends of java::lang::Class
562
563 inline void _Jv_InitClass (jclass klass)
564 {
565   if (__builtin_expect (klass->state == JV_STATE_DONE, true))
566     return;
567   klass->initializeClass ();  
568 }
569
570 // Return array class corresponding to element type KLASS, creating it if
571 // necessary.
572 inline jclass
573 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
574 {
575   extern void _Jv_NewArrayClass (jclass element,
576                                  java::lang::ClassLoader *loader,
577                                  _Jv_VTable *array_vtable = 0);
578   if (__builtin_expect (!klass->arrayclass, false))
579     _Jv_NewArrayClass (klass, loader);
580   return klass->arrayclass;
581 }
582
583 #endif /* __JAVA_LANG_CLASS_H__ */