1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 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
11 // Written primary using compiler source and Class.java as guides.
12 #ifndef __JAVA_LANG_CLASS_H__
13 #define __JAVA_LANG_CLASS_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>
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,
32 // This must be predefined with "C" linkage.
33 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
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.
41 JV_STATE_NOTHING = 0, // Set by compiler.
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.
48 JV_STATE_COMPILED = 6, // This was a compiled class.
50 JV_STATE_PREPARED = 7, // Layout & static init done.
51 JV_STATE_LINKED = 9, // Strings interned.
53 JV_STATE_IN_PROGRESS = 10, // <clinit> running.
57 JV_STATE_DONE = 14 // Must be last.
63 struct _Jv_ArrayVTable;
64 class _Jv_ExecutionEngine;
65 class _Jv_CompiledEngine;
66 class _Jv_InterpreterEngine;
80 _Jv_Utf8Const *signature;
83 // Method's index in the vtable.
85 // Pointer to underlying function.
87 // NULL-terminated list of exception class names; can be NULL if
88 // there are none such.
89 _Jv_Utf8Const **throws;
91 _Jv_Method *getNextMethod ()
95 // Interface Dispatch Tables
96 union _Jv_IDispatchTable
100 // Index into interface's ioffsets.
102 jshort itable_length;
103 // Class Interface dispatch table.
109 // Offsets into implementation class itables.
114 // Used by _Jv_Linker::get_interfaces ()
122 struct _Jv_MethodSymbol
124 _Jv_Utf8Const *class_name;
126 _Jv_Utf8Const *signature;
129 struct _Jv_OffsetTable
135 struct _Jv_AddressTable
141 struct _Jv_CatchClass
143 java::lang::Class **address;
144 _Jv_Utf8Const *classname;
147 // Possible values for the assertion_code field in the type assertion table.
150 JV_ASSERT_END_OF_TABLE = 0,
151 JV_ASSERT_TYPES_COMPATIBLE = 1,
152 JV_ASSERT_IS_INSTANTIABLE = 2
155 // Entry in the type assertion table, used to validate type constraints
156 // for binary compatibility.
157 struct _Jv_TypeAssertion
164 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
166 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
168 // Forward declarations for friends of java::lang::Class
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);
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);
184 jobject _Jv_AllocObject (jclass);
185 void *_Jv_AllocObj (jint, jclass);
186 void *_Jv_AllocPtrFreeObj (jint, jclass);
187 void *_Jv_AllocArray (jint, jclass);
189 jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
191 jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
193 jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
195 jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
196 jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
197 jint JvNumMethods (jclass);
198 jmethodID JvGetFirstMethod (jclass);
200 // Friend classes and functions to implement the ClassLoader
201 class java::lang::ClassLoader;
202 class java::lang::VMClassLoader;
204 class java::io::ObjectOutputStream;
205 class java::io::ObjectInputStream;
206 class java::io::ObjectStreamClass;
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);
224 // Friend functions and classes in prims.cc
225 void _Jv_InitPrimClass (jclass, char *, char, int);
226 jstring _Jv_GetMethodString (jclass, _Jv_Utf8Const *);
228 jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
230 jclass _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader);
233 jboolean _Jv_IsInterpretedClass (jclass);
234 void _Jv_InitField (jobject, jclass, int);
236 class _Jv_ClassReader;
237 class _Jv_InterpClass;
238 class _Jv_InterpMethod;
241 class _Jv_BytecodeVerifier;
242 class gnu::gcj::runtime::StackTrace;
243 class java::io::VMObjectStreamClass;
245 void _Jv_sharedlib_register_hook (jclass klass);
248 class java::lang::Class : public java::lang::Object
251 static jclass forName (jstring className, jboolean initialize,
252 java::lang::ClassLoader *loader);
253 static jclass forName (jstring className);
254 JArray<jclass> *getClasses (void);
256 java::lang::ClassLoader *getClassLoader (void);
258 // This is an internal method that circumvents the usual security
259 // checks when getting the class loader.
260 java::lang::ClassLoader *getClassLoaderInternal (void)
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);
275 JArray<jclass> *getDeclaredClasses (void);
276 jclass getDeclaringClass (void);
278 java::lang::reflect::Field *getField (jstring);
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,
285 java::lang::reflect::Field *getPrivateField (jstring);
286 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
287 java::security::ProtectionDomain *getProtectionDomain0 ();
289 java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
290 java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
293 JArray<java::lang::reflect::Field *> *getFields (void);
295 JArray<jclass> *getInterfaces (void);
297 void getSignature (java::lang::StringBuffer *buffer);
298 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
299 JArray<java::lang::reflect::Method *> *getMethods (void);
301 inline jint getModifiers (void)
303 return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
306 jstring getName (void);
308 java::net::URL *getResource (jstring resourceName);
309 java::io::InputStream *getResourceAsStream (jstring resourceName);
310 JArray<jobject> *getSigners (void);
311 void setSigners(JArray<jobject> *);
313 inline jclass getSuperclass (void)
318 inline jclass getInterface (jint n)
320 return interfaces[n];
323 inline jboolean isArray (void)
325 return name->first() == '[';
328 inline jclass getComponentType (void)
330 return isArray () ? (* (jclass *) &methods) : 0;
333 jboolean isAssignableFrom (jclass cls);
334 jboolean isInstance (jobject obj);
336 inline jboolean isInterface (void)
338 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
341 inline jboolean isPrimitive (void)
343 return vtable == JV_PRIMITIVE_VTABLE;
346 jobject newInstance (void);
347 java::security::ProtectionDomain *getProtectionDomain (void);
348 java::lang::Package *getPackage (void);
349 jstring toString (void);
350 jboolean desiredAssertionStatus (void);
352 // FIXME: this probably shouldn't be public.
355 return size_in_bytes;
358 // The index of the first method we declare ourself (as opposed to
360 inline jint firstMethodIndex (void)
362 return vtable_method_count - method_count;
368 // This constructor is used to create Class object for the primitive
369 // types. See prims.cc.
372 static java::lang::Class class$;
376 void memberAccessCheck (jint flags);
378 void initializeClass (void);
380 static jstring getPackagePortion (jstring);
382 void set_state (jint nstate)
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,
397 friend void ::_Jv_InitClass (jclass klass);
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);
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);
411 friend jobject (::_Jv_JNI_ToReflectedField) (_Jv_JNIEnv *, jclass, jfieldID,
413 friend jobject (::_Jv_JNI_ToReflectedMethod) (_Jv_JNIEnv *, jclass, jmethodID,
415 friend jfieldID (::_Jv_FromReflectedField) (java::lang::reflect::Field *);
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);
422 // Friends classes and functions to implement the ClassLoader
423 friend class java::lang::ClassLoader;
424 friend class java::lang::VMClassLoader;
426 friend class java::io::ObjectOutputStream;
427 friend class java::io::ObjectInputStream;
428 friend class java::io::ObjectStreamClass;
430 friend void ::_Jv_RegisterClasses (const jclass *classes);
431 friend void ::_Jv_RegisterClasses_Counted (const jclass *classes,
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);
450 friend void ::_Jv_InitPrimClass (jclass, char *, char, int);
452 friend jstring (::_Jv_GetMethodString) (jclass, _Jv_Utf8Const *);
454 friend jboolean (::_Jv_CheckAccess) (jclass self_klass, jclass other_klass,
457 friend bool _Jv_getInterfaceMethod(jclass, jclass&, int&,
458 const _Jv_Utf8Const*,
459 const _Jv_Utf8Const*);
461 friend jclass (::_Jv_GetArrayClass) (jclass klass,
462 java::lang::ClassLoader *loader);
465 friend jboolean (::_Jv_IsInterpretedClass) (jclass);
466 friend void ::_Jv_InitField (jobject, jclass, int);
468 friend class ::_Jv_ClassReader;
469 friend class ::_Jv_InterpClass;
470 friend class ::_Jv_InterpMethod;
473 #ifdef JV_MARKOBJ_DECL
474 friend JV_MARKOBJ_DECL;
477 friend class ::_Jv_BytecodeVerifier;
478 friend class gnu::gcj::runtime::StackTrace;
479 friend class java::io::VMObjectStreamClass;
481 friend class _Jv_Linker;
482 friend class _Jv_ExecutionEngine;
483 friend class _Jv_CompiledEngine;
484 friend class _Jv_InterpreterEngine;
486 friend void ::_Jv_sharedlib_register_hook (jclass klass);
488 // Chain for class pool.
492 // Access flags for class.
494 // The superclass, or null for Object.
497 _Jv_Constants constants;
498 // Methods. If this is an array class, then this field holds a
499 // pointer to the element type.
501 // Number of methods. If this class is primitive, this holds the
502 // character used to represent this type in a signature.
504 // Number of methods in the vtable.
505 jshort vtable_method_count;
508 // Size of instance fields, in bytes.
510 // Total number of fields (instance and static).
512 // Number of static fields.
513 jshort static_field_count;
514 // The vtbl for all objects of this class.
516 // Virtual method offset table.
517 _Jv_OffsetTable *otable;
518 // Offset table symbols.
519 _Jv_MethodSymbol *otable_syms;
521 _Jv_AddressTable *atable;
522 _Jv_MethodSymbol *atable_syms;
524 _Jv_AddressTable *itable;
525 _Jv_MethodSymbol *itable_syms;
526 _Jv_CatchClass *catch_classes;
527 // Interfaces implemented by this class.
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.
535 // The thread which has locked this class. Used during class
537 java::lang::Thread *thread;
538 // How many levels of "extends" this class is removed from Object.
540 // Vector of this class's superclasses, ordered by decreasing depth.
542 // Interface Dispatch Table.
543 _Jv_IDispatchTable *idt;
544 // Pointer to the class that represents an array of this class.
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.
554 // Additional data, specific to the generator (JIT, native,
555 // interpreter) of this class.
558 _Jv_ExecutionEngine *engine;
561 // Inline functions that are friends of java::lang::Class
563 inline void _Jv_InitClass (jclass klass)
565 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
567 klass->initializeClass ();
570 // Return array class corresponding to element type KLASS, creating it if
573 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
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;
583 #endif /* __JAVA_LANG_CLASS_H__ */