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
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>
25 // Avoid including SystemClassLoader.h.
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 jclass class_initializer
);
44 extern "C" void _Jv_RegisterNewClasses (void **classes
);
45 extern "C" void _Jv_RegisterClasses (const jclass
*classes
);
46 extern "C" void _Jv_RegisterClasses_Counted (const jclass
*classes
,
49 // This must be predefined with "C" linkage.
50 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
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.
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
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.
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.
95 struct _Jv_ArrayVTable
;
97 class _Jv_ExecutionEngine
;
98 class _Jv_CompiledEngine
;
99 class _Jv_IndirectCompiledEngine
;
100 class _Jv_InterpreterEngine
;
103 class _Jv_ClassReader
;
104 class _Jv_InterpClass
;
105 class _Jv_InterpMethod
;
120 _Jv_Utf8Const
*signature
;
123 // Method's index in the vtable.
125 // Pointer to underlying function.
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 ()
135 // The table used to resolve interface calls.
136 struct _Jv_IDispatchTable
138 // Index into interface's ioffsets.
140 jshort itable_length
;
141 // Class Interface dispatch table.
145 // Used by _Jv_Linker::get_interfaces ()
153 struct _Jv_MethodSymbol
155 _Jv_Utf8Const
*class_name
;
157 _Jv_Utf8Const
*signature
;
160 struct _Jv_OffsetTable
166 struct _Jv_AddressTable
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.
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
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
,
226 jobject
_Jv_JNI_ToReflectedMethod (_Jv_JNIEnv
*, jclass
, jmethodID
,
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
);
236 // Finds a desired interpreter method in the given class or NULL if not found
237 _Jv_InterpMethod
* _Jv_FindInterpreterMethod (jclass
, jmethodID
);
240 // Friend classes and functions to implement the ClassLoader
241 class java::lang::ClassLoader
;
242 class java::lang::VMClassLoader
;
244 class java::io::ObjectOutputStream
;
245 class java::io::ObjectInputStream
;
246 class java::io::ObjectStreamClass
;
248 void _Jv_RegisterClassHookDefault (jclass klass
);
249 void _Jv_RegisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
250 void _Jv_UnregisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
251 void _Jv_UnregisterClass (jclass
);
252 jclass
_Jv_FindClassNoException (_Jv_Utf8Const
*name
,
253 java::lang::ClassLoader
*loader
);
254 jclass
_Jv_FindClass (_Jv_Utf8Const
*name
,
255 java::lang::ClassLoader
*loader
);
256 jclass
_Jv_FindClassInCache (_Jv_Utf8Const
*name
);
257 jclass
_Jv_PopClass (void);
258 void _Jv_PushClass (jclass k
);
259 void _Jv_NewArrayClass (jclass element
,
260 java::lang::ClassLoader
*loader
,
261 _Jv_VTable
*array_vtable
= 0);
262 jclass
_Jv_NewClass (_Jv_Utf8Const
*name
, jclass superclass
,
263 java::lang::ClassLoader
*loader
);
264 void _Jv_InitNewClassFields (jclass klass
);
266 // Friend functions and classes in prims.cc
267 void _Jv_InitPrimClass (jclass
, const char *, char, int);
268 jstring
_Jv_GetMethodString (jclass
, _Jv_Method
*, jclass
= NULL
);
270 jboolean
_Jv_CheckAccess (jclass self_klass
, jclass other_klass
,
272 jclass
_Jv_GetArrayClass (jclass klass
, java::lang::ClassLoader
*loader
);
274 jboolean
_Jv_IsInterpretedClass (jclass
);
275 jboolean
_Jv_IsBinaryCompatibilityABI (jclass
);
277 jboolean
_Jv_IsPhantomClass (jclass
);
279 void _Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader
*);
282 void _Jv_InitField (jobject
, jclass
, int);
285 class _Jv_StackTrace
;
286 class _Jv_BytecodeVerifier
;
287 class java::io::VMObjectStreamClass
;
289 void _Jv_sharedlib_register_hook (jclass klass
);
292 class java::lang::Class
: public java::lang::Object
295 static jclass
forName (jstring className
, jboolean initialize
,
296 java::lang::ClassLoader
*loader
);
297 static jclass
forName (jstring className
);
298 JArray
<jclass
> *getClasses (void);
300 java::lang::ClassLoader
*getClassLoader (void);
302 java::lang::ClassLoader
*getClassLoader (jclass caller
);
304 // This is an internal method that circumvents the usual security
305 // checks when getting the class loader.
306 java::lang::ClassLoader
*getClassLoaderInternal (void)
311 java::lang::reflect::Constructor
*getConstructor (JArray
<jclass
> *);
312 JArray
<java::lang::reflect::Constructor
*> *getConstructors (void);
313 java::lang::reflect::Constructor
*getDeclaredConstructor (JArray
<jclass
> *);
314 JArray
<java::lang::reflect::Constructor
*> *getDeclaredConstructors (jboolean
);
315 java::lang::reflect::Field
*getDeclaredField (jstring
);
316 JArray
<java::lang::reflect::Field
*> *getDeclaredFields ();
317 JArray
<java::lang::reflect::Field
*> *getDeclaredFields (jboolean
);
318 java::lang::reflect::Method
*getDeclaredMethod (jstring
, JArray
<jclass
> *);
319 JArray
<java::lang::reflect::Method
*> *getDeclaredMethods (void);
321 JArray
<jclass
> *getDeclaredClasses (void);
322 JArray
<jclass
> *getDeclaredClasses (jboolean
);
323 jclass
getDeclaringClass (void);
325 java::lang::reflect::Field
*getField (jstring
);
327 JArray
<java::lang::reflect::Field
*> internalGetFields ();
328 java::lang::reflect::Field
*getField (jstring
, jint
);
329 jint
_getMethods (JArray
<java::lang::reflect::Method
*> *result
,
331 java::lang::reflect::Field
*getPrivateField (jstring
);
332 java::lang::reflect::Method
*getPrivateMethod (jstring
, JArray
<jclass
> *);
333 java::security::ProtectionDomain
*getProtectionDomain0 ();
335 java::lang::reflect::Method
*_getMethod (jstring
, JArray
<jclass
> *);
336 java::lang::reflect::Method
*_getDeclaredMethod (jstring
, JArray
<jclass
> *);
339 JArray
<java::lang::reflect::Field
*> *getFields (void);
341 JArray
<jclass
> *getInterfaces (void);
343 void getSignature (java::lang::StringBuffer
*buffer
);
344 static jstring
getSignature (JArray
<jclass
> *, jboolean is_constructor
);
345 JArray
<java::lang::reflect::Method
*> *getMethods (void);
347 inline jint
getModifiers (void)
349 return accflags
& java::lang::reflect::Modifier::ALL_FLAGS
;
352 jstring
getName (void);
354 java::net::URL
*getResource (jstring resourceName
);
355 java::io::InputStream
*getResourceAsStream (jstring resourceName
);
356 JArray
<jobject
> *getSigners (void);
357 void setSigners(JArray
<jobject
> *);
359 inline jclass
getSuperclass (void)
364 inline jclass
getInterface (jint n
)
366 return interfaces
[n
];
369 inline jboolean
isArray (void)
371 return name
->first() == '[';
374 inline jclass
getComponentType (void)
376 return isArray () ? element_type
: 0;
379 jboolean
isAssignableFrom (jclass cls
);
380 jboolean
isInstance (jobject obj
);
382 inline jboolean
isInterface (void)
384 return (accflags
& java::lang::reflect::Modifier::INTERFACE
) != 0;
387 inline jboolean
isPrimitive (void)
389 return vtable
== JV_PRIMITIVE_VTABLE
;
392 jobject
newInstance (void);
393 java::security::ProtectionDomain
*getProtectionDomain (void);
394 java::lang::Package
*getPackage (void);
395 jstring
toString (void);
396 jboolean
desiredAssertionStatus (void);
398 JArray
<java::lang::reflect::TypeVariable
*> *getTypeParameters (void);
400 java::lang::Class
*getEnclosingClass (void);
401 java::lang::reflect::Constructor
*getEnclosingConstructor (void);
402 java::lang::reflect::Method
*getEnclosingMethod (void);
404 // FIXME: this probably shouldn't be public.
407 return size_in_bytes
;
410 // The index of the first method we declare ourself (as opposed to
412 inline jint
firstMethodIndex (void)
414 return vtable_method_count
- method_count
;
420 // This constructor is used to create Class object for the primitive
421 // types. See prims.cc.
424 static java::lang::Class
class$
;
428 void memberAccessCheck (jint flags
);
430 void initializeClass (void);
432 static jstring
getPackagePortion (jstring
);
434 void set_state (jint nstate
)
440 // Friend functions implemented in natClass.cc.
441 friend _Jv_Method
*::_Jv_GetMethodLocal (jclass klass
, _Jv_Utf8Const
*name
,
442 _Jv_Utf8Const
*signature
);
443 friend jboolean (::_Jv_IsAssignableFrom
) (jclass
, jclass
);
444 friend jboolean (::_Jv_IsAssignableFromSlow
) (jclass
, jclass
);
445 friend jboolean (::_Jv_InterfaceAssignableFrom
) (jclass
, jclass
);
446 friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
449 friend void ::_Jv_InitClass (jclass klass
);
450 friend java::lang::Class
* ::_Jv_NewClassFromInitializer (const jclass class_initializer
);
451 friend void _Jv_RegisterNewClasses (void **classes
);
453 friend _Jv_Method
* ::_Jv_LookupDeclaredMethod (jclass
, _Jv_Utf8Const
*,
454 _Jv_Utf8Const
*, jclass
*);
455 friend jfieldID (::JvGetFirstInstanceField
) (jclass
);
456 friend jint (::JvNumInstanceFields
) (jclass
);
457 friend jfieldID (::JvGetFirstStaticField
) (jclass
);
458 friend jint (::JvNumStaticFields
) (jclass
);
460 friend jobject (::_Jv_AllocObject
) (jclass
);
461 friend void *::_Jv_AllocObj (jint
, jclass
);
462 friend void *::_Jv_AllocPtrFreeObj (jint
, jclass
);
463 friend void *::_Jv_AllocArray (jint
, jclass
);
465 friend jobject (::_Jv_JNI_ToReflectedField
) (_Jv_JNIEnv
*, jclass
, jfieldID
,
467 friend jobject (::_Jv_JNI_ToReflectedMethod
) (_Jv_JNIEnv
*, jclass
, jmethodID
,
469 friend jfieldID (::_Jv_FromReflectedField
) (java::lang::reflect::Field
*);
471 friend jmethodID (::_Jv_FromReflectedMethod
) (java::lang::reflect::Method
*);
472 friend jmethodID (::_Jv_FromReflectedConstructor
) (java::lang::reflect::Constructor
*);
473 friend jint (::JvNumMethods
) (jclass
);
474 friend jmethodID (::JvGetFirstMethod
) (jclass
);
476 friend _Jv_InterpMethod
* (::_Jv_FindInterpreterMethod
) (jclass klass
,
477 jmethodID desired_method
);
480 // Friends classes and functions to implement the ClassLoader
481 friend class java::lang::ClassLoader
;
482 friend class java::lang::VMClassLoader
;
484 friend class java::io::ObjectOutputStream
;
485 friend class java::io::ObjectInputStream
;
486 friend class java::io::ObjectStreamClass
;
488 friend void ::_Jv_RegisterClasses (const jclass
*classes
);
489 friend void ::_Jv_RegisterClasses_Counted (const jclass
*classes
,
491 friend void ::_Jv_RegisterClassHookDefault (jclass klass
);
492 friend void ::_Jv_RegisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
493 friend void ::_Jv_UnregisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
494 friend void ::_Jv_UnregisterClass (jclass
);
495 friend jclass (::_Jv_FindClassNoException
) (_Jv_Utf8Const
*name
,
496 java::lang::ClassLoader
*loader
);
497 friend jclass (::_Jv_FindClass
) (_Jv_Utf8Const
*name
,
498 java::lang::ClassLoader
*loader
);
499 friend jclass (::_Jv_FindClassInCache
) (_Jv_Utf8Const
*name
);
500 friend jclass (::_Jv_PopClass
) (void);
501 friend void ::_Jv_PushClass (jclass k
);
502 friend void ::_Jv_NewArrayClass (jclass element
,
503 java::lang::ClassLoader
*loader
,
504 _Jv_VTable
*array_vtable
);
505 friend jclass (::_Jv_NewClass
) (_Jv_Utf8Const
*name
, jclass superclass
,
506 java::lang::ClassLoader
*loader
);
507 friend void ::_Jv_InitNewClassFields (jclass klass
);
510 friend void ::_Jv_InitPrimClass (jclass
, const char *, char, int);
512 friend jstring (::_Jv_GetMethodString
) (jclass
, _Jv_Method
*, jclass
);
514 friend jboolean (::_Jv_CheckAccess
) (jclass self_klass
, jclass other_klass
,
517 friend bool (::_Jv_getInterfaceMethod
) (jclass
, jclass
&, int&,
518 const _Jv_Utf8Const
*,
519 const _Jv_Utf8Const
*);
521 friend jclass (::_Jv_GetArrayClass
) (jclass klass
,
522 java::lang::ClassLoader
*loader
);
524 friend jboolean (::_Jv_IsInterpretedClass
) (jclass
);
525 friend jboolean (::_Jv_IsBinaryCompatibilityABI
) (jclass
);
527 friend jboolean (::_Jv_IsPhantomClass
) (jclass
);
530 friend void ::_Jv_InitField (jobject
, jclass
, int);
532 friend class ::_Jv_ClassReader
;
533 friend class ::_Jv_InterpClass
;
534 friend class ::_Jv_InterpMethod
;
536 friend class ::_Jv_StackTrace
;
538 #ifdef JV_MARKOBJ_DECL
539 friend JV_MARKOBJ_DECL
;
542 friend class ::_Jv_BytecodeVerifier
;
543 friend class java::io::VMObjectStreamClass
;
545 friend class ::_Jv_Linker
;
546 friend class ::_Jv_ExecutionEngine
;
547 friend class ::_Jv_CompiledEngine
;
548 friend class ::_Jv_IndirectCompiledEngine
;
549 friend class ::_Jv_InterpreterEngine
;
551 friend void ::_Jv_sharedlib_register_hook (jclass klass
);
553 friend void *::_Jv_ResolvePoolEntry (jclass this_class
, jint index
);
555 friend void ::_Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader
*);
557 // Chain for class pool. This also doubles as the ABI version
558 // number. It is only used for this purpose at class registration
559 // time, and only for precompiled classes.
560 jclass next_or_version
;
563 // Access flags for class.
565 // The superclass, or null for Object.
568 _Jv_Constants constants
;
569 // Methods. If this is an array class, then this field holds a
570 // pointer to the element type.
576 // Number of methods. If this class is primitive, this holds the
577 // character used to represent this type in a signature.
579 // Number of methods in the vtable.
580 jshort vtable_method_count
;
583 // Size of instance fields, in bytes.
585 // Total number of fields (instance and static).
587 // Number of static fields.
588 jshort static_field_count
;
589 // The vtbl for all objects of this class.
591 // Virtual method offset table.
592 _Jv_OffsetTable
*otable
;
593 // Offset table symbols.
594 _Jv_MethodSymbol
*otable_syms
;
596 _Jv_AddressTable
*atable
;
597 _Jv_MethodSymbol
*atable_syms
;
599 _Jv_AddressTable
*itable
;
600 _Jv_MethodSymbol
*itable_syms
;
601 _Jv_CatchClass
*catch_classes
;
602 // Interfaces implemented by this class.
604 // The class loader for this class.
605 java::lang::ClassLoader
*loader
;
606 // Number of interfaces.
607 jshort interface_count
;
608 // State of this class.
610 // The thread which has locked this class. Used during class
612 java::lang::Thread
*thread
;
613 // How many levels of "extends" this class is removed from Object.
615 // Vector of this class's superclasses, ordered by decreasing depth.
617 // In a regular class, this field points to the Class Interface Dispatch
618 // Table. In an interface, it points to the ioffsets table.
621 _Jv_IDispatchTable
*idt
;
624 // Pointer to the class that represents an array of this class.
626 // Security Domain to which this class belongs (or null).
627 java::security::ProtectionDomain
*protectionDomain
;
628 // Pointer to the type assertion table for this class.
629 _Jv_TypeAssertion
*assertion_table
;
630 // Signers of this class (or null).
631 JArray
<jobject
> *hack_signers
;
632 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
634 // Additional data, specific to the generator (JIT, native,
635 // interpreter) of this class.
638 _Jv_ExecutionEngine
*engine
;
641 // Inline functions that are friends of java::lang::Class
643 inline void _Jv_InitClass (jclass klass
)
645 if (__builtin_expect (klass
->state
== JV_STATE_DONE
, true))
647 klass
->initializeClass ();
650 // Return array class corresponding to element type KLASS, creating it if
653 _Jv_GetArrayClass (jclass klass
, java::lang::ClassLoader
*loader
)
655 extern void _Jv_NewArrayClass (jclass element
,
656 java::lang::ClassLoader
*loader
,
657 _Jv_VTable
*array_vtable
= 0);
658 if (__builtin_expect (!klass
->arrayclass
, false))
659 _Jv_NewArrayClass (klass
, loader
);
660 return klass
->arrayclass
;
663 #endif /* __JAVA_LANG_CLASS_H__ */