Mark as release
[official-gcc.git] / libjava / java / lang / Class.h
bloba884cd6c59b765c52d00a915a741be8d67ddbdf8
1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 // Written primary using compiler source and Class.java as guides.
12 #ifndef __JAVA_LANG_CLASS_H__
13 #define __JAVA_LANG_CLASS_H__
15 #pragma interface
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>
25 // Avoid including SystemClassLoader.h.
26 extern "Java"
28 namespace gnu
30 namespace gcj
32 namespace runtime
34 class SystemClassLoader;
40 // We declare these here to avoid including gcj/cni.h.
41 extern "C" void _Jv_InitClass (jclass klass);
42 extern "C" jclass _Jv_NewClassFromInitializer
43 (const char *class_initializer);
44 extern "C" void _Jv_RegisterNewClasses (char **classes);
45 extern "C" void _Jv_RegisterClasses (const jclass *classes);
46 extern "C" void _Jv_RegisterClasses_Counted (const jclass *classes,
47 size_t count);
49 // This must be predefined with "C" linkage.
50 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
51 int meth_idx);
52 extern "C" void *_Jv_ResolvePoolEntry (jclass this_class, jint index);
54 // These are the possible values for the `state' field of the class
55 // structure. Note that ordering is important here. Whenever the
56 // state changes, one should notify all waiters of this class.
57 enum
59 JV_STATE_NOTHING = 0, // Set by compiler.
61 JV_STATE_PRELOADING = 1, // Can do _Jv_FindClass.
63 // There is an invariant through libgcj that a class will always be
64 // at a state greater than or equal to JV_STATE_LOADING when it is
65 // returned by a class loader to user code. Hence, defineclass.cc
66 // installs supers before returning a class, C++-ABI-compiled
67 // classes are created with supers installed, and BC-ABI-compiled
68 // classes are linked to this state before being returned by their
69 // class loader.
70 JV_STATE_LOADING = 3, // Has super installed.
71 JV_STATE_READ = 4, // Has been completely defined.
72 JV_STATE_LOADED = 5, // Has Miranda methods defined.
74 JV_STATE_COMPILED = 6, // This was a compiled class.
76 JV_STATE_PREPARED = 7, // Layout & static init done.
77 JV_STATE_LINKED = 9, // Strings interned.
79 JV_STATE_IN_PROGRESS = 10, // <clinit> running.
81 JV_STATE_ERROR = 12,
83 JV_STATE_PHANTOM = 13, // Bytecode is missing. In many cases we can
84 // work around that. If not, throw a
85 // NoClassDefFoundError.
87 JV_STATE_DONE = 14, // Must be last.
92 struct _Jv_Field;
93 struct _Jv_VTable;
94 union _Jv_word;
95 struct _Jv_ArrayVTable;
96 class _Jv_Linker;
97 class _Jv_ExecutionEngine;
98 class _Jv_CompiledEngine;
99 class _Jv_IndirectCompiledEngine;
100 class _Jv_InterpreterEngine;
102 #ifdef INTERPRETER
103 class _Jv_ClassReader;
104 class _Jv_InterpClass;
105 class _Jv_InterpMethod;
106 #endif
108 struct _Jv_Constants
110 jint size;
111 jbyte *tags;
112 _Jv_word *data;
115 struct _Jv_Method
117 // Method name.
118 _Jv_Utf8Const *name;
119 // Method signature.
120 _Jv_Utf8Const *signature;
121 // Access flags.
122 _Jv_ushort accflags;
123 // Method's index in the vtable.
124 _Jv_ushort index;
125 // Pointer to underlying function.
126 void *ncode;
127 // NULL-terminated list of exception class names; can be NULL if
128 // there are none such.
129 _Jv_Utf8Const **throws;
131 _Jv_Method *getNextMethod ()
132 { return this + 1; }
135 // The table used to resolve interface calls.
136 struct _Jv_IDispatchTable
138 // Index into interface's ioffsets.
139 jshort iindex;
140 jshort itable_length;
141 // Class Interface dispatch table.
142 void *itable[0];
145 // Used by _Jv_Linker::get_interfaces ()
146 struct _Jv_ifaces
148 jclass *list;
149 jshort len;
150 jshort count;
153 struct _Jv_MethodSymbol
155 _Jv_Utf8Const *class_name;
156 _Jv_Utf8Const *name;
157 _Jv_Utf8Const *signature;
160 struct _Jv_OffsetTable
162 jint state;
163 jint offsets[];
166 struct _Jv_AddressTable
168 jint state;
169 void *addresses[];
172 struct _Jv_CatchClass
174 java::lang::Class **address;
175 _Jv_Utf8Const *classname;
178 // Possible values for the assertion_code field in the type assertion table.
179 enum
181 JV_ASSERT_END_OF_TABLE = 0,
182 JV_ASSERT_TYPES_COMPATIBLE = 1,
183 JV_ASSERT_IS_INSTANTIABLE = 2
186 // Entry in the type assertion table, used to validate type constraints
187 // for binary compatibility.
188 struct _Jv_TypeAssertion
190 jint assertion_code;
191 _Jv_Utf8Const *op1;
192 _Jv_Utf8Const *op2;
195 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
197 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
199 // Forward declarations for friends of java::lang::Class
201 // Friend functions implemented in natClass.cc.
202 _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
203 _Jv_Utf8Const *signature);
204 jboolean _Jv_IsAssignableFrom (jclass, jclass);
205 jboolean _Jv_IsAssignableFromSlow (jclass, jclass);
206 jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
208 _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
209 _Jv_Utf8Const*, jclass * = NULL);
210 jfieldID JvGetFirstInstanceField (jclass);
211 jint JvNumInstanceFields (jclass);
212 jfieldID JvGetFirstStaticField (jclass);
213 jint JvNumStaticFields (jclass);
215 jobject _Jv_AllocObject (jclass);
216 void *_Jv_AllocObj (jint, jclass);
217 void *_Jv_AllocPtrFreeObj (jint, jclass);
218 void *_Jv_AllocArray (jint, jclass);
220 bool _Jv_getInterfaceMethod(jclass, jclass&, int&,
221 const _Jv_Utf8Const*,
222 const _Jv_Utf8Const*);
224 jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
225 jboolean);
226 jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
227 jboolean);
228 jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
230 jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
231 jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
232 jint JvNumMethods (jclass);
233 jmethodID JvGetFirstMethod (jclass);
234 _Jv_Utf8Const *_Jv_GetClassNameUtf8 (jclass);
236 #ifdef INTERPRETER
237 // Finds a desired interpreter method in the given class or NULL if not found
238 class _Jv_MethodBase;
239 _Jv_MethodBase *_Jv_FindInterpreterMethod (jclass, jmethodID);
240 #endif
242 // Friend classes and functions to implement the ClassLoader
243 class java::lang::ClassLoader;
244 class java::lang::VMClassLoader;
246 class java::io::ObjectOutputStream;
247 class java::io::ObjectInputStream;
248 class java::io::ObjectStreamClass;
250 void _Jv_RegisterClassHookDefault (jclass klass);
251 void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
252 void _Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
253 void _Jv_UnregisterClass (jclass);
254 jclass _Jv_FindClassNoException (_Jv_Utf8Const *name,
255 java::lang::ClassLoader *loader);
256 jclass _Jv_FindClass (_Jv_Utf8Const *name,
257 java::lang::ClassLoader *loader);
258 jclass _Jv_FindClassInCache (_Jv_Utf8Const *name);
259 jclass _Jv_PopClass (void);
260 void _Jv_PushClass (jclass k);
261 void _Jv_NewArrayClass (jclass element,
262 java::lang::ClassLoader *loader,
263 _Jv_VTable *array_vtable = 0);
264 jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
265 java::lang::ClassLoader *loader);
266 void _Jv_InitNewClassFields (jclass klass);
268 // Friend functions and classes in prims.cc
269 void _Jv_InitPrimClass (jclass, const char *, char, int);
270 jstring _Jv_GetMethodString (jclass, _Jv_Method *, jclass = NULL);
272 jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
273 jint flags);
274 jclass _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader);
276 jboolean _Jv_IsInterpretedClass (jclass);
277 jboolean _Jv_IsBinaryCompatibilityABI (jclass);
279 jboolean _Jv_IsPhantomClass (jclass);
281 void _Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *);
283 #ifdef INTERPRETER
284 void _Jv_InitField (jobject, jclass, int);
285 #endif
287 class _Jv_StackTrace;
288 class _Jv_BytecodeVerifier;
289 class java::io::VMObjectStreamClass;
291 void _Jv_sharedlib_register_hook (jclass klass);
294 class java::lang::Class : public java::lang::Object
296 public:
297 static jclass forName (jstring className, jboolean initialize,
298 java::lang::ClassLoader *loader);
299 static jclass forName (jstring className);
300 JArray<jclass> *getClasses (void);
302 java::lang::ClassLoader *getClassLoader (void);
303 private:
304 java::lang::ClassLoader *getClassLoader (jclass caller);
305 public:
306 // This is an internal method that circumvents the usual security
307 // checks when getting the class loader.
308 java::lang::ClassLoader *getClassLoaderInternal (void)
310 return loader;
313 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
314 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
315 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
316 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (jboolean);
317 java::lang::reflect::Field *getDeclaredField (jstring);
318 JArray<java::lang::reflect::Field *> *getDeclaredFields ();
319 JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean);
320 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
321 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
323 JArray<jclass> *getDeclaredClasses (void);
324 JArray<jclass> *getDeclaredClasses (jboolean);
325 jclass getDeclaringClass (void);
327 java::lang::reflect::Field *getField (jstring);
328 private:
329 JArray<java::lang::reflect::Field *> internalGetFields ();
330 java::lang::reflect::Field *getField (jstring, jint);
331 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
332 jint offset);
333 java::lang::reflect::Field *getPrivateField (jstring);
334 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
335 java::security::ProtectionDomain *getProtectionDomain0 ();
337 java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
338 java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
340 public:
341 JArray<java::lang::reflect::Field *> *getFields (void);
343 JArray<jclass> *getInterfaces (void);
345 void getSignature (java::lang::StringBuffer *buffer);
346 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
347 JArray<java::lang::reflect::Method *> *getMethods (void);
349 inline jint getModifiers (void)
351 return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
354 jstring getName (void);
356 java::net::URL *getResource (jstring resourceName);
357 java::io::InputStream *getResourceAsStream (jstring resourceName);
358 JArray<jobject> *getSigners (void);
359 void setSigners(JArray<jobject> *);
361 inline jclass getSuperclass (void)
363 return superclass;
366 inline jclass getInterface (jint n)
368 return interfaces[n];
371 inline jboolean isArray (void)
373 return name->first() == '[';
376 inline jclass getComponentType (void)
378 return isArray () ? element_type : 0;
381 jboolean isAssignableFrom (jclass cls);
382 jboolean isInstance (jobject obj);
384 inline jboolean isInterface (void)
386 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
389 inline jboolean isPrimitive (void)
391 return vtable == JV_PRIMITIVE_VTABLE;
394 jobject newInstance (void);
395 java::security::ProtectionDomain *getProtectionDomain (void);
396 java::lang::Package *getPackage (void);
397 jstring toString (void);
398 jboolean desiredAssertionStatus (void);
400 JArray<java::lang::reflect::TypeVariable *> *getTypeParameters (void);
402 java::lang::Class *getEnclosingClass (void);
403 java::lang::reflect::Constructor *getEnclosingConstructor (void);
404 java::lang::reflect::Method *getEnclosingMethod (void);
405 jboolean isEnum (void);
407 // FIXME: this probably shouldn't be public.
408 jint size (void)
410 return size_in_bytes;
413 // The index of the first method we declare ourself (as opposed to
414 // inheriting).
415 inline jint firstMethodIndex (void)
417 return vtable_method_count - method_count;
420 // finalization
421 void finalize ();
423 // This constructor is used to create Class object for the primitive
424 // types. See prims.cc.
425 Class ();
427 static java::lang::Class class$;
429 private:
431 void memberAccessCheck (jint flags);
433 void initializeClass (void);
435 static jstring getPackagePortion (jstring);
437 void set_state (jint nstate)
439 state = nstate;
440 notifyAll ();
443 // Friend functions implemented in natClass.cc.
444 friend _Jv_Method *::_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
445 _Jv_Utf8Const *signature);
446 friend jboolean (::_Jv_IsAssignableFrom) (jclass, jclass);
447 friend jboolean (::_Jv_IsAssignableFromSlow) (jclass, jclass);
448 friend jboolean (::_Jv_InterfaceAssignableFrom) (jclass, jclass);
449 friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
450 int method_idx);
452 friend void ::_Jv_InitClass (jclass klass);
453 friend java::lang::Class* ::_Jv_NewClassFromInitializer (const char *class_initializer);
454 friend void _Jv_RegisterNewClasses (void **classes);
456 friend _Jv_Method* ::_Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
457 _Jv_Utf8Const*, jclass *);
458 friend jfieldID (::JvGetFirstInstanceField) (jclass);
459 friend jint (::JvNumInstanceFields) (jclass);
460 friend jfieldID (::JvGetFirstStaticField) (jclass);
461 friend jint (::JvNumStaticFields) (jclass);
463 friend jobject (::_Jv_AllocObject) (jclass);
464 friend void *::_Jv_AllocObj (jint, jclass);
465 friend void *::_Jv_AllocPtrFreeObj (jint, jclass);
466 friend void *::_Jv_AllocArray (jint, jclass);
468 friend jobject (::_Jv_JNI_ToReflectedField) (_Jv_JNIEnv *, jclass, jfieldID,
469 jboolean);
470 friend jobject (::_Jv_JNI_ToReflectedMethod) (_Jv_JNIEnv *, jclass, jmethodID,
471 jboolean);
472 friend jfieldID (::_Jv_FromReflectedField) (java::lang::reflect::Field *);
474 friend jmethodID (::_Jv_FromReflectedMethod) (java::lang::reflect::Method *);
475 friend jmethodID (::_Jv_FromReflectedConstructor) (java::lang::reflect::Constructor *);
476 friend jint (::JvNumMethods) (jclass);
477 friend jmethodID (::JvGetFirstMethod) (jclass);
478 friend _Jv_Utf8Const *::_Jv_GetClassNameUtf8 (jclass);
479 #ifdef INTERPRETER
480 friend _Jv_MethodBase *(::_Jv_FindInterpreterMethod) (jclass klass,
481 jmethodID desired_method);
482 #endif
484 // Friends classes and functions to implement the ClassLoader
485 friend class java::lang::ClassLoader;
486 friend class java::lang::VMClassLoader;
488 friend class java::io::ObjectOutputStream;
489 friend class java::io::ObjectInputStream;
490 friend class java::io::ObjectStreamClass;
492 friend void ::_Jv_RegisterClasses (const jclass *classes);
493 friend void ::_Jv_RegisterClasses_Counted (const jclass *classes,
494 size_t count);
495 friend void ::_Jv_RegisterClassHookDefault (jclass klass);
496 friend void ::_Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
497 friend void ::_Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
498 friend void ::_Jv_UnregisterClass (jclass);
499 friend jclass (::_Jv_FindClassNoException) (_Jv_Utf8Const *name,
500 java::lang::ClassLoader *loader);
501 friend jclass (::_Jv_FindClass) (_Jv_Utf8Const *name,
502 java::lang::ClassLoader *loader);
503 friend jclass (::_Jv_FindClassInCache) (_Jv_Utf8Const *name);
504 friend jclass (::_Jv_PopClass) (void);
505 friend void ::_Jv_PushClass (jclass k);
506 friend void ::_Jv_NewArrayClass (jclass element,
507 java::lang::ClassLoader *loader,
508 _Jv_VTable *array_vtable);
509 friend jclass (::_Jv_NewClass) (_Jv_Utf8Const *name, jclass superclass,
510 java::lang::ClassLoader *loader);
511 friend void ::_Jv_InitNewClassFields (jclass klass);
513 // in prims.cc
514 friend void ::_Jv_InitPrimClass (jclass, const char *, char, int);
516 friend jstring (::_Jv_GetMethodString) (jclass, _Jv_Method *, jclass);
518 friend jboolean (::_Jv_CheckAccess) (jclass self_klass, jclass other_klass,
519 jint flags);
521 friend bool (::_Jv_getInterfaceMethod) (jclass, jclass&, int&,
522 const _Jv_Utf8Const*,
523 const _Jv_Utf8Const*);
525 friend jclass (::_Jv_GetArrayClass) (jclass klass,
526 java::lang::ClassLoader *loader);
528 friend jboolean (::_Jv_IsInterpretedClass) (jclass);
529 friend jboolean (::_Jv_IsBinaryCompatibilityABI) (jclass);
531 friend jboolean (::_Jv_IsPhantomClass) (jclass);
533 #ifdef INTERPRETER
534 friend void ::_Jv_InitField (jobject, jclass, int);
536 friend class ::_Jv_ClassReader;
537 friend class ::_Jv_InterpClass;
538 friend class ::_Jv_InterpMethod;
539 #endif
540 friend class ::_Jv_StackTrace;
542 #ifdef JV_MARKOBJ_DECL
543 friend JV_MARKOBJ_DECL;
544 #endif
546 friend class ::_Jv_BytecodeVerifier;
547 friend class java::io::VMObjectStreamClass;
549 friend class ::_Jv_Linker;
550 friend class ::_Jv_ExecutionEngine;
551 friend class ::_Jv_CompiledEngine;
552 friend class ::_Jv_IndirectCompiledEngine;
553 friend class ::_Jv_InterpreterEngine;
555 friend void ::_Jv_sharedlib_register_hook (jclass klass);
557 friend void *::_Jv_ResolvePoolEntry (jclass this_class, jint index);
559 friend void ::_Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *);
561 // Chain for class pool. This also doubles as the ABI version
562 // number. It is only used for this purpose at class registration
563 // time, and only for precompiled classes.
564 jclass next_or_version;
565 // Name of class.
566 _Jv_Utf8Const *name;
567 // Access flags for class.
568 _Jv_ushort accflags;
569 // The superclass, or null for Object.
570 jclass superclass;
571 // Class constants.
572 _Jv_Constants constants;
573 // Methods. If this is an array class, then this field holds a
574 // pointer to the element type.
575 union
577 _Jv_Method *methods;
578 jclass element_type;
580 // Number of methods. If this class is primitive, this holds the
581 // character used to represent this type in a signature.
582 jshort method_count;
583 // Number of methods in the vtable.
584 jshort vtable_method_count;
585 // The fields.
586 _Jv_Field *fields;
587 // Size of instance fields, in bytes.
588 jint size_in_bytes;
589 // Total number of fields (instance and static).
590 jshort field_count;
591 // Number of static fields.
592 jshort static_field_count;
593 // The vtbl for all objects of this class.
594 _Jv_VTable *vtable;
595 // Virtual method offset table.
596 _Jv_OffsetTable *otable;
597 // Offset table symbols.
598 _Jv_MethodSymbol *otable_syms;
599 // Address table
600 _Jv_AddressTable *atable;
601 _Jv_MethodSymbol *atable_syms;
602 // Interface table
603 _Jv_AddressTable *itable;
604 _Jv_MethodSymbol *itable_syms;
605 _Jv_CatchClass *catch_classes;
606 // Interfaces implemented by this class.
607 jclass *interfaces;
608 // The class loader for this class.
609 java::lang::ClassLoader *loader;
610 // Number of interfaces.
611 jshort interface_count;
612 // State of this class.
613 jbyte state;
614 // The thread which has locked this class. Used during class
615 // initialization.
616 java::lang::Thread *thread;
617 // How many levels of "extends" this class is removed from Object.
618 jshort depth;
619 // Vector of this class's superclasses, ordered by decreasing depth.
620 jclass *ancestors;
621 // In a regular class, this field points to the Class Interface Dispatch
622 // Table. In an interface, it points to the ioffsets table.
623 union
625 _Jv_IDispatchTable *idt;
626 jshort *ioffsets;
628 // Pointer to the class that represents an array of this class.
629 jclass arrayclass;
630 // Security Domain to which this class belongs (or null).
631 java::security::ProtectionDomain *protectionDomain;
632 // Pointer to the type assertion table for this class.
633 _Jv_TypeAssertion *assertion_table;
634 // Signers of this class (or null).
635 JArray<jobject> *hack_signers;
636 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
637 jclass chain;
638 // Additional data, specific to the generator (JIT, native,
639 // interpreter) of this class.
640 void *aux_info;
641 // Execution engine.
642 _Jv_ExecutionEngine *engine;
645 // Inline functions that are friends of java::lang::Class
647 inline void _Jv_InitClass (jclass klass)
649 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
650 return;
651 klass->initializeClass ();
654 // Return array class corresponding to element type KLASS, creating it if
655 // necessary.
656 inline jclass
657 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
659 extern void _Jv_NewArrayClass (jclass element,
660 java::lang::ClassLoader *loader,
661 _Jv_VTable *array_vtable = 0);
662 if (__builtin_expect (!klass->arrayclass, false))
663 _Jv_NewArrayClass (klass, loader);
664 return klass->arrayclass;
667 #endif /* __JAVA_LANG_CLASS_H__ */