link.cc (ensure_class_linked): Removed #ifdef.
[gcc.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
35 // These are the possible values for the `state' field of the class
36 // structure. Note that ordering is important here. Whenever the
37 // state changes, one should notify all waiters of this class.
38 enum
39 {
40 JV_STATE_NOTHING = 0, // Set by compiler.
41
42 JV_STATE_PRELOADING = 1, // Can do _Jv_FindClass.
43 JV_STATE_LOADING = 3, // Has super installed.
44 JV_STATE_READ = 4, // Has been completely defined.
45 JV_STATE_LOADED = 5, // Has Miranda methods defined.
46
47 JV_STATE_COMPILED = 6, // This was a compiled class.
48
49 JV_STATE_PREPARED = 7, // Layout & static init done.
50 JV_STATE_LINKED = 9, // Strings interned.
51
52 JV_STATE_IN_PROGRESS = 10, // <clinit> running.
53
54 JV_STATE_ERROR = 12,
55
56 JV_STATE_DONE = 14 // Must be last.
57 };
58
59 struct _Jv_Field;
60 struct _Jv_VTable;
61 union _Jv_word;
62 struct _Jv_ArrayVTable;
63 class _Jv_Linker;
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 bool _Jv_getInterfaceMethod(jclass, jclass&, int&,
190 const _Jv_Utf8Const*,
191 const _Jv_Utf8Const*);
192
193 jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
194 jboolean);
195 jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
196 jboolean);
197 jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
198
199 jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
200 jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
201 jint JvNumMethods (jclass);
202 jmethodID JvGetFirstMethod (jclass);
203
204 // Friend classes and functions to implement the ClassLoader
205 class java::lang::ClassLoader;
206 class java::lang::VMClassLoader;
207
208 class java::io::ObjectOutputStream;
209 class java::io::ObjectInputStream;
210 class java::io::ObjectStreamClass;
211
212 void _Jv_RegisterClassHookDefault (jclass klass);
213 void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
214 void _Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
215 void _Jv_UnregisterClass (jclass);
216 jclass _Jv_FindClass (_Jv_Utf8Const *name,
217 java::lang::ClassLoader *loader);
218 jclass _Jv_FindClassInCache (_Jv_Utf8Const *name);
219 jclass _Jv_PopClass (void);
220 void _Jv_PushClass (jclass k);
221 void _Jv_NewArrayClass (jclass element,
222 java::lang::ClassLoader *loader,
223 _Jv_VTable *array_vtable = 0);
224 jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
225 java::lang::ClassLoader *loader);
226 void _Jv_InitNewClassFields (jclass klass);
227
228 // Friend functions and classes in prims.cc
229 void _Jv_InitPrimClass (jclass, char *, char, int);
230 jstring _Jv_GetMethodString (jclass, _Jv_Method *, jclass = NULL);
231
232 jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
233 jint flags);
234 jclass _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader);
235
236 jboolean _Jv_IsInterpretedClass (jclass);
237
238 #ifdef INTERPRETER
239 void _Jv_InitField (jobject, jclass, int);
240
241 class _Jv_ClassReader;
242 class _Jv_InterpClass;
243 class _Jv_InterpMethod;
244 #endif
245
246 class _Jv_StackTrace;
247 class _Jv_BytecodeVerifier;
248 class java::io::VMObjectStreamClass;
249
250 void _Jv_sharedlib_register_hook (jclass klass);
251
252
253 class java::lang::Class : public java::lang::Object
254 {
255 public:
256 static jclass forName (jstring className, jboolean initialize,
257 java::lang::ClassLoader *loader);
258 static jclass forName (jstring className);
259 JArray<jclass> *getClasses (void);
260
261 java::lang::ClassLoader *getClassLoader (void);
262
263 // This is an internal method that circumvents the usual security
264 // checks when getting the class loader.
265 java::lang::ClassLoader *getClassLoaderInternal (void)
266 {
267 return loader;
268 }
269
270 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
271 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
272 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
273 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
274 java::lang::reflect::Field *getDeclaredField (jstring);
275 JArray<java::lang::reflect::Field *> *getDeclaredFields ();
276 JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean);
277 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
278 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
279
280 JArray<jclass> *getDeclaredClasses (void);
281 jclass getDeclaringClass (void);
282
283 java::lang::reflect::Field *getField (jstring);
284 private:
285 JArray<java::lang::reflect::Field *> internalGetFields ();
286 JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
287 java::lang::reflect::Field *getField (jstring, jint);
288 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
289 jint offset);
290 java::lang::reflect::Field *getPrivateField (jstring);
291 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
292 java::security::ProtectionDomain *getProtectionDomain0 ();
293
294 java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
295 java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
296
297 public:
298 JArray<java::lang::reflect::Field *> *getFields (void);
299
300 JArray<jclass> *getInterfaces (void);
301
302 void getSignature (java::lang::StringBuffer *buffer);
303 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
304 JArray<java::lang::reflect::Method *> *getMethods (void);
305
306 inline jint getModifiers (void)
307 {
308 return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
309 }
310
311 jstring getName (void);
312
313 java::net::URL *getResource (jstring resourceName);
314 java::io::InputStream *getResourceAsStream (jstring resourceName);
315 JArray<jobject> *getSigners (void);
316 void setSigners(JArray<jobject> *);
317
318 inline jclass getSuperclass (void)
319 {
320 return superclass;
321 }
322
323 inline jclass getInterface (jint n)
324 {
325 return interfaces[n];
326 }
327
328 inline jboolean isArray (void)
329 {
330 return name->first() == '[';
331 }
332
333 inline jclass getComponentType (void)
334 {
335 return isArray () ? (* (jclass *) &methods) : 0;
336 }
337
338 jboolean isAssignableFrom (jclass cls);
339 jboolean isInstance (jobject obj);
340
341 inline jboolean isInterface (void)
342 {
343 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
344 }
345
346 inline jboolean isPrimitive (void)
347 {
348 return vtable == JV_PRIMITIVE_VTABLE;
349 }
350
351 jobject newInstance (void);
352 java::security::ProtectionDomain *getProtectionDomain (void);
353 java::lang::Package *getPackage (void);
354 jstring toString (void);
355 jboolean desiredAssertionStatus (void);
356
357 // FIXME: this probably shouldn't be public.
358 jint size (void)
359 {
360 return size_in_bytes;
361 }
362
363 // The index of the first method we declare ourself (as opposed to
364 // inheriting).
365 inline jint firstMethodIndex (void)
366 {
367 return vtable_method_count - method_count;
368 }
369
370 // finalization
371 void finalize ();
372
373 // This constructor is used to create Class object for the primitive
374 // types. See prims.cc.
375 Class ();
376
377 static java::lang::Class class$;
378
379 private:
380
381 void memberAccessCheck (jint flags);
382
383 void initializeClass (void);
384
385 static jstring getPackagePortion (jstring);
386
387 void set_state (jint nstate)
388 {
389 state = nstate;
390 notifyAll ();
391 }
392
393 // Friend functions implemented in natClass.cc.
394 friend _Jv_Method *::_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
395 _Jv_Utf8Const *signature);
396 friend jboolean (::_Jv_IsAssignableFrom) (jclass, jclass);
397 friend jboolean (::_Jv_IsAssignableFromSlow) (jclass, jclass);
398 friend jboolean (::_Jv_InterfaceAssignableFrom) (jclass, jclass);
399 friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
400 int method_idx);
401
402 friend void ::_Jv_InitClass (jclass klass);
403
404 friend _Jv_Method* ::_Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
405 _Jv_Utf8Const*, jclass *);
406 friend jfieldID (::JvGetFirstInstanceField) (jclass);
407 friend jint (::JvNumInstanceFields) (jclass);
408 friend jfieldID (::JvGetFirstStaticField) (jclass);
409 friend jint (::JvNumStaticFields) (jclass);
410
411 friend jobject (::_Jv_AllocObject) (jclass);
412 friend void *::_Jv_AllocObj (jint, jclass);
413 friend void *::_Jv_AllocPtrFreeObj (jint, jclass);
414 friend void *::_Jv_AllocArray (jint, jclass);
415
416 friend jobject (::_Jv_JNI_ToReflectedField) (_Jv_JNIEnv *, jclass, jfieldID,
417 jboolean);
418 friend jobject (::_Jv_JNI_ToReflectedMethod) (_Jv_JNIEnv *, jclass, jmethodID,
419 jboolean);
420 friend jfieldID (::_Jv_FromReflectedField) (java::lang::reflect::Field *);
421
422 friend jmethodID (::_Jv_FromReflectedMethod) (java::lang::reflect::Method *);
423 friend jmethodID (::_Jv_FromReflectedConstructor) (java::lang::reflect::Constructor *);
424 friend jint (::JvNumMethods) (jclass);
425 friend jmethodID (::JvGetFirstMethod) (jclass);
426
427 // Friends classes and functions to implement the ClassLoader
428 friend class java::lang::ClassLoader;
429 friend class java::lang::VMClassLoader;
430
431 friend class java::io::ObjectOutputStream;
432 friend class java::io::ObjectInputStream;
433 friend class java::io::ObjectStreamClass;
434
435 friend void ::_Jv_RegisterClasses (const jclass *classes);
436 friend void ::_Jv_RegisterClasses_Counted (const jclass *classes,
437 size_t count);
438 friend void ::_Jv_RegisterClassHookDefault (jclass klass);
439 friend void ::_Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
440 friend void ::_Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
441 friend void ::_Jv_UnregisterClass (jclass);
442 friend jclass (::_Jv_FindClass) (_Jv_Utf8Const *name,
443 java::lang::ClassLoader *loader);
444 friend jclass (::_Jv_FindClassInCache) (_Jv_Utf8Const *name);
445 friend jclass (::_Jv_PopClass) (void);
446 friend void ::_Jv_PushClass (jclass k);
447 friend void ::_Jv_NewArrayClass (jclass element,
448 java::lang::ClassLoader *loader,
449 _Jv_VTable *array_vtable);
450 friend jclass (::_Jv_NewClass) (_Jv_Utf8Const *name, jclass superclass,
451 java::lang::ClassLoader *loader);
452 friend void ::_Jv_InitNewClassFields (jclass klass);
453
454 // in prims.cc
455 friend void ::_Jv_InitPrimClass (jclass, char *, char, int);
456
457 friend jstring (::_Jv_GetMethodString) (jclass, _Jv_Method *, jclass);
458
459 friend jboolean (::_Jv_CheckAccess) (jclass self_klass, jclass other_klass,
460 jint flags);
461
462 friend bool (::_Jv_getInterfaceMethod) (jclass, jclass&, int&,
463 const _Jv_Utf8Const*,
464 const _Jv_Utf8Const*);
465
466 friend jclass (::_Jv_GetArrayClass) (jclass klass,
467 java::lang::ClassLoader *loader);
468
469 friend jboolean (::_Jv_IsInterpretedClass) (jclass);
470
471 #ifdef INTERPRETER
472 friend void ::_Jv_InitField (jobject, jclass, int);
473
474 friend class ::_Jv_ClassReader;
475 friend class ::_Jv_InterpClass;
476 friend class ::_Jv_InterpMethod;
477 #endif
478 friend class ::_Jv_StackTrace;
479
480 #ifdef JV_MARKOBJ_DECL
481 friend JV_MARKOBJ_DECL;
482 #endif
483
484 friend class ::_Jv_BytecodeVerifier;
485 friend class java::io::VMObjectStreamClass;
486
487 friend class ::_Jv_Linker;
488 friend class ::_Jv_ExecutionEngine;
489 friend class ::_Jv_CompiledEngine;
490 friend class ::_Jv_InterpreterEngine;
491
492 friend void ::_Jv_sharedlib_register_hook (jclass klass);
493
494 // Chain for class pool. This also doubles as the ABI version
495 // number. It is only used for this purpose at class registration
496 // time, and only for precompiled classes.
497 jclass next_or_version;
498 // Name of class.
499 _Jv_Utf8Const *name;
500 // Access flags for class.
501 _Jv_ushort accflags;
502 // The superclass, or null for Object.
503 jclass superclass;
504 // Class constants.
505 _Jv_Constants constants;
506 // Methods. If this is an array class, then this field holds a
507 // pointer to the element type.
508 _Jv_Method *methods;
509 // Number of methods. If this class is primitive, this holds the
510 // character used to represent this type in a signature.
511 jshort method_count;
512 // Number of methods in the vtable.
513 jshort vtable_method_count;
514 // The fields.
515 _Jv_Field *fields;
516 // Size of instance fields, in bytes.
517 jint size_in_bytes;
518 // Total number of fields (instance and static).
519 jshort field_count;
520 // Number of static fields.
521 jshort static_field_count;
522 // The vtbl for all objects of this class.
523 _Jv_VTable *vtable;
524 // Virtual method offset table.
525 _Jv_OffsetTable *otable;
526 // Offset table symbols.
527 _Jv_MethodSymbol *otable_syms;
528 // Address table
529 _Jv_AddressTable *atable;
530 _Jv_MethodSymbol *atable_syms;
531 // Interface table
532 _Jv_AddressTable *itable;
533 _Jv_MethodSymbol *itable_syms;
534 _Jv_CatchClass *catch_classes;
535 // Interfaces implemented by this class.
536 jclass *interfaces;
537 // The class loader for this class.
538 java::lang::ClassLoader *loader;
539 // Number of interfaces.
540 jshort interface_count;
541 // State of this class.
542 jbyte state;
543 // The thread which has locked this class. Used during class
544 // initialization.
545 java::lang::Thread *thread;
546 // How many levels of "extends" this class is removed from Object.
547 jshort depth;
548 // Vector of this class's superclasses, ordered by decreasing depth.
549 jclass *ancestors;
550 // Interface Dispatch Table.
551 _Jv_IDispatchTable *idt;
552 // Pointer to the class that represents an array of this class.
553 jclass arrayclass;
554 // Security Domain to which this class belongs (or null).
555 java::security::ProtectionDomain *protectionDomain;
556 // Pointer to the type assertion table for this class.
557 _Jv_TypeAssertion *assertion_table;
558 // Signers of this class (or null).
559 JArray<jobject> *hack_signers;
560 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
561 jclass chain;
562 // Additional data, specific to the generator (JIT, native,
563 // interpreter) of this class.
564 void *aux_info;
565 // Execution engine.
566 _Jv_ExecutionEngine *engine;
567 };
568
569 // Inline functions that are friends of java::lang::Class
570
571 inline void _Jv_InitClass (jclass klass)
572 {
573 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
574 return;
575 klass->initializeClass ();
576 }
577
578 // Return array class corresponding to element type KLASS, creating it if
579 // necessary.
580 inline jclass
581 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
582 {
583 extern void _Jv_NewArrayClass (jclass element,
584 java::lang::ClassLoader *loader,
585 _Jv_VTable *array_vtable = 0);
586 if (__builtin_expect (!klass->arrayclass, false))
587 _Jv_NewArrayClass (klass, loader);
588 return klass->arrayclass;
589 }
590
591 #endif /* __JAVA_LANG_CLASS_H__ */