OSDN Git Service

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