OSDN Git Service

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