1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 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 // 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
,
31 // This must be predefined with "C" linkage.
32 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
34 extern "C" void *_Jv_ResolvePoolEntry (jclass this_class
, jint index
);
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
;
65 class _Jv_ExecutionEngine
;
66 class _Jv_CompiledEngine
;
67 class _Jv_InterpreterEngine
;
81 _Jv_Utf8Const
*signature
;
84 // Method's index in the vtable.
86 // Pointer to underlying function.
88 // NULL-terminated list of exception class names; can be NULL if
89 // there are none such.
90 _Jv_Utf8Const
**throws
;
92 _Jv_Method
*getNextMethod ()
96 // Interface Dispatch Tables
97 union _Jv_IDispatchTable
101 // Index into interface's ioffsets.
103 jshort itable_length
;
104 // Class Interface dispatch table.
110 // Offsets into implementation class itables.
115 // Used by _Jv_Linker::get_interfaces ()
123 struct _Jv_MethodSymbol
125 _Jv_Utf8Const
*class_name
;
127 _Jv_Utf8Const
*signature
;
130 struct _Jv_OffsetTable
136 struct _Jv_AddressTable
142 struct _Jv_CatchClass
144 java::lang::Class
**address
;
145 _Jv_Utf8Const
*classname
;
148 // Possible values for the assertion_code field in the type assertion table.
151 JV_ASSERT_END_OF_TABLE
= 0,
152 JV_ASSERT_TYPES_COMPATIBLE
= 1,
153 JV_ASSERT_IS_INSTANTIABLE
= 2
156 // Entry in the type assertion table, used to validate type constraints
157 // for binary compatibility.
158 struct _Jv_TypeAssertion
165 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
167 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
169 // Forward declarations for friends of java::lang::Class
171 // Friend functions implemented in natClass.cc.
172 _Jv_Method
*_Jv_GetMethodLocal (jclass klass
, _Jv_Utf8Const
*name
,
173 _Jv_Utf8Const
*signature
);
174 jboolean
_Jv_IsAssignableFrom (jclass
, jclass
);
175 jboolean
_Jv_IsAssignableFromSlow (jclass
, jclass
);
176 jboolean
_Jv_InterfaceAssignableFrom (jclass
, jclass
);
178 _Jv_Method
* _Jv_LookupDeclaredMethod (jclass
, _Jv_Utf8Const
*,
179 _Jv_Utf8Const
*, jclass
* = NULL
);
180 jfieldID
JvGetFirstInstanceField (jclass
);
181 jint
JvNumInstanceFields (jclass
);
182 jfieldID
JvGetFirstStaticField (jclass
);
183 jint
JvNumStaticFields (jclass
);
185 jobject
_Jv_AllocObject (jclass
);
186 void *_Jv_AllocObj (jint
, jclass
);
187 void *_Jv_AllocPtrFreeObj (jint
, jclass
);
188 void *_Jv_AllocArray (jint
, jclass
);
190 bool _Jv_getInterfaceMethod(jclass
, jclass
&, int&,
191 const _Jv_Utf8Const
*,
192 const _Jv_Utf8Const
*);
194 jobject
_Jv_JNI_ToReflectedField (_Jv_JNIEnv
*, jclass
, jfieldID
,
196 jobject
_Jv_JNI_ToReflectedMethod (_Jv_JNIEnv
*, jclass
, jmethodID
,
198 jfieldID
_Jv_FromReflectedField (java::lang::reflect::Field
*);
200 jmethodID
_Jv_FromReflectedMethod (java::lang::reflect::Method
*);
201 jmethodID
_Jv_FromReflectedConstructor (java::lang::reflect::Constructor
*);
202 jint
JvNumMethods (jclass
);
203 jmethodID
JvGetFirstMethod (jclass
);
205 // Friend classes and functions to implement the ClassLoader
206 class java::lang::ClassLoader
;
207 class java::lang::VMClassLoader
;
209 class java::io::ObjectOutputStream
;
210 class java::io::ObjectInputStream
;
211 class java::io::ObjectStreamClass
;
213 void _Jv_RegisterClassHookDefault (jclass klass
);
214 void _Jv_RegisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
215 void _Jv_UnregisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
216 void _Jv_UnregisterClass (jclass
);
217 jclass
_Jv_FindClass (_Jv_Utf8Const
*name
,
218 java::lang::ClassLoader
*loader
);
219 jclass
_Jv_FindClassInCache (_Jv_Utf8Const
*name
);
220 jclass
_Jv_PopClass (void);
221 void _Jv_PushClass (jclass k
);
222 void _Jv_NewArrayClass (jclass element
,
223 java::lang::ClassLoader
*loader
,
224 _Jv_VTable
*array_vtable
= 0);
225 jclass
_Jv_NewClass (_Jv_Utf8Const
*name
, jclass superclass
,
226 java::lang::ClassLoader
*loader
);
227 void _Jv_InitNewClassFields (jclass klass
);
229 // Friend functions and classes in prims.cc
230 void _Jv_InitPrimClass (jclass
, char *, char, int);
231 jstring
_Jv_GetMethodString (jclass
, _Jv_Method
*, jclass
= NULL
);
233 jboolean
_Jv_CheckAccess (jclass self_klass
, jclass other_klass
,
235 jclass
_Jv_GetArrayClass (jclass klass
, java::lang::ClassLoader
*loader
);
237 jboolean
_Jv_IsInterpretedClass (jclass
);
239 void _Jv_CopyClassesToSystemLoader (java::lang::ClassLoader
*);
242 void _Jv_InitField (jobject
, jclass
, int);
244 class _Jv_ClassReader
;
245 class _Jv_InterpClass
;
246 class _Jv_InterpMethod
;
249 class _Jv_StackTrace
;
250 class _Jv_BytecodeVerifier
;
251 class java::io::VMObjectStreamClass
;
253 void _Jv_sharedlib_register_hook (jclass klass
);
256 class java::lang::Class
: public java::lang::Object
259 static jclass
forName (jstring className
, jboolean initialize
,
260 java::lang::ClassLoader
*loader
);
261 static jclass
forName (jstring className
);
262 JArray
<jclass
> *getClasses (void);
264 java::lang::ClassLoader
*getClassLoader (void);
266 // This is an internal method that circumvents the usual security
267 // checks when getting the class loader.
268 java::lang::ClassLoader
*getClassLoaderInternal (void)
273 java::lang::reflect::Constructor
*getConstructor (JArray
<jclass
> *);
274 JArray
<java::lang::reflect::Constructor
*> *getConstructors (void);
275 java::lang::reflect::Constructor
*getDeclaredConstructor (JArray
<jclass
> *);
276 JArray
<java::lang::reflect::Constructor
*> *getDeclaredConstructors (jboolean
);
277 java::lang::reflect::Field
*getDeclaredField (jstring
);
278 JArray
<java::lang::reflect::Field
*> *getDeclaredFields ();
279 JArray
<java::lang::reflect::Field
*> *getDeclaredFields (jboolean
);
280 java::lang::reflect::Method
*getDeclaredMethod (jstring
, JArray
<jclass
> *);
281 JArray
<java::lang::reflect::Method
*> *getDeclaredMethods (void);
283 JArray
<jclass
> *getDeclaredClasses (void);
284 JArray
<jclass
> *getDeclaredClasses (jboolean
);
285 jclass
getDeclaringClass (void);
287 java::lang::reflect::Field
*getField (jstring
);
289 JArray
<java::lang::reflect::Field
*> internalGetFields ();
290 java::lang::reflect::Field
*getField (jstring
, jint
);
291 jint
_getMethods (JArray
<java::lang::reflect::Method
*> *result
,
293 java::lang::reflect::Field
*getPrivateField (jstring
);
294 java::lang::reflect::Method
*getPrivateMethod (jstring
, JArray
<jclass
> *);
295 java::security::ProtectionDomain
*getProtectionDomain0 ();
297 java::lang::reflect::Method
*_getMethod (jstring
, JArray
<jclass
> *);
298 java::lang::reflect::Method
*_getDeclaredMethod (jstring
, JArray
<jclass
> *);
301 JArray
<java::lang::reflect::Field
*> *getFields (void);
303 JArray
<jclass
> *getInterfaces (void);
305 void getSignature (java::lang::StringBuffer
*buffer
);
306 static jstring
getSignature (JArray
<jclass
> *, jboolean is_constructor
);
307 JArray
<java::lang::reflect::Method
*> *getMethods (void);
309 inline jint
getModifiers (void)
311 return accflags
& java::lang::reflect::Modifier::ALL_FLAGS
;
314 jstring
getName (void);
316 java::net::URL
*getResource (jstring resourceName
);
317 java::io::InputStream
*getResourceAsStream (jstring resourceName
);
318 JArray
<jobject
> *getSigners (void);
319 void setSigners(JArray
<jobject
> *);
321 inline jclass
getSuperclass (void)
326 inline jclass
getInterface (jint n
)
328 return interfaces
[n
];
331 inline jboolean
isArray (void)
333 return name
->first() == '[';
336 inline jclass
getComponentType (void)
338 return isArray () ? (* (jclass
*) &methods
) : 0;
341 jboolean
isAssignableFrom (jclass cls
);
342 jboolean
isInstance (jobject obj
);
344 inline jboolean
isInterface (void)
346 return (accflags
& java::lang::reflect::Modifier::INTERFACE
) != 0;
349 inline jboolean
isPrimitive (void)
351 return vtable
== JV_PRIMITIVE_VTABLE
;
354 jobject
newInstance (void);
355 java::security::ProtectionDomain
*getProtectionDomain (void);
356 java::lang::Package
*getPackage (void);
357 jstring
toString (void);
358 jboolean
desiredAssertionStatus (void);
360 // FIXME: this probably shouldn't be public.
363 return size_in_bytes
;
366 // The index of the first method we declare ourself (as opposed to
368 inline jint
firstMethodIndex (void)
370 return vtable_method_count
- method_count
;
376 // This constructor is used to create Class object for the primitive
377 // types. See prims.cc.
380 static java::lang::Class
class$
;
384 void memberAccessCheck (jint flags
);
386 void initializeClass (void);
388 static jstring
getPackagePortion (jstring
);
390 void set_state (jint nstate
)
396 // Friend functions implemented in natClass.cc.
397 friend _Jv_Method
*::_Jv_GetMethodLocal (jclass klass
, _Jv_Utf8Const
*name
,
398 _Jv_Utf8Const
*signature
);
399 friend jboolean (::_Jv_IsAssignableFrom
) (jclass
, jclass
);
400 friend jboolean (::_Jv_IsAssignableFromSlow
) (jclass
, jclass
);
401 friend jboolean (::_Jv_InterfaceAssignableFrom
) (jclass
, jclass
);
402 friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
405 friend void ::_Jv_InitClass (jclass klass
);
407 friend _Jv_Method
* ::_Jv_LookupDeclaredMethod (jclass
, _Jv_Utf8Const
*,
408 _Jv_Utf8Const
*, jclass
*);
409 friend jfieldID (::JvGetFirstInstanceField
) (jclass
);
410 friend jint (::JvNumInstanceFields
) (jclass
);
411 friend jfieldID (::JvGetFirstStaticField
) (jclass
);
412 friend jint (::JvNumStaticFields
) (jclass
);
414 friend jobject (::_Jv_AllocObject
) (jclass
);
415 friend void *::_Jv_AllocObj (jint
, jclass
);
416 friend void *::_Jv_AllocPtrFreeObj (jint
, jclass
);
417 friend void *::_Jv_AllocArray (jint
, jclass
);
419 friend jobject (::_Jv_JNI_ToReflectedField
) (_Jv_JNIEnv
*, jclass
, jfieldID
,
421 friend jobject (::_Jv_JNI_ToReflectedMethod
) (_Jv_JNIEnv
*, jclass
, jmethodID
,
423 friend jfieldID (::_Jv_FromReflectedField
) (java::lang::reflect::Field
*);
425 friend jmethodID (::_Jv_FromReflectedMethod
) (java::lang::reflect::Method
*);
426 friend jmethodID (::_Jv_FromReflectedConstructor
) (java::lang::reflect::Constructor
*);
427 friend jint (::JvNumMethods
) (jclass
);
428 friend jmethodID (::JvGetFirstMethod
) (jclass
);
430 // Friends classes and functions to implement the ClassLoader
431 friend class java::lang::ClassLoader
;
432 friend class java::lang::VMClassLoader
;
434 friend class java::io::ObjectOutputStream
;
435 friend class java::io::ObjectInputStream
;
436 friend class java::io::ObjectStreamClass
;
438 friend void ::_Jv_RegisterClasses (const jclass
*classes
);
439 friend void ::_Jv_RegisterClasses_Counted (const jclass
*classes
,
441 friend void ::_Jv_RegisterClassHookDefault (jclass klass
);
442 friend void ::_Jv_RegisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
443 friend void ::_Jv_UnregisterInitiatingLoader (jclass
,java::lang::ClassLoader
*);
444 friend void ::_Jv_UnregisterClass (jclass
);
445 friend jclass (::_Jv_FindClass
) (_Jv_Utf8Const
*name
,
446 java::lang::ClassLoader
*loader
);
447 friend jclass (::_Jv_FindClassInCache
) (_Jv_Utf8Const
*name
);
448 friend jclass (::_Jv_PopClass
) (void);
449 friend void ::_Jv_PushClass (jclass k
);
450 friend void ::_Jv_NewArrayClass (jclass element
,
451 java::lang::ClassLoader
*loader
,
452 _Jv_VTable
*array_vtable
);
453 friend jclass (::_Jv_NewClass
) (_Jv_Utf8Const
*name
, jclass superclass
,
454 java::lang::ClassLoader
*loader
);
455 friend void ::_Jv_InitNewClassFields (jclass klass
);
458 friend void ::_Jv_InitPrimClass (jclass
, char *, char, int);
460 friend jstring (::_Jv_GetMethodString
) (jclass
, _Jv_Method
*, jclass
);
462 friend jboolean (::_Jv_CheckAccess
) (jclass self_klass
, jclass other_klass
,
465 friend bool (::_Jv_getInterfaceMethod
) (jclass
, jclass
&, int&,
466 const _Jv_Utf8Const
*,
467 const _Jv_Utf8Const
*);
469 friend jclass (::_Jv_GetArrayClass
) (jclass klass
,
470 java::lang::ClassLoader
*loader
);
472 friend jboolean (::_Jv_IsInterpretedClass
) (jclass
);
475 friend void ::_Jv_InitField (jobject
, jclass
, int);
477 friend class ::_Jv_ClassReader
;
478 friend class ::_Jv_InterpClass
;
479 friend class ::_Jv_InterpMethod
;
481 friend class ::_Jv_StackTrace
;
483 #ifdef JV_MARKOBJ_DECL
484 friend JV_MARKOBJ_DECL
;
487 friend class ::_Jv_BytecodeVerifier
;
488 friend class java::io::VMObjectStreamClass
;
490 friend class ::_Jv_Linker
;
491 friend class ::_Jv_ExecutionEngine
;
492 friend class ::_Jv_CompiledEngine
;
493 friend class ::_Jv_InterpreterEngine
;
495 friend void ::_Jv_sharedlib_register_hook (jclass klass
);
497 friend void *::_Jv_ResolvePoolEntry (jclass this_class
, jint index
);
499 friend void ::_Jv_CopyClassesToSystemLoader (java::lang::ClassLoader
*);
501 // Chain for class pool. This also doubles as the ABI version
502 // number. It is only used for this purpose at class registration
503 // time, and only for precompiled classes.
504 jclass next_or_version
;
507 // Access flags for class.
509 // The superclass, or null for Object.
512 _Jv_Constants constants
;
513 // Methods. If this is an array class, then this field holds a
514 // pointer to the element type.
516 // Number of methods. If this class is primitive, this holds the
517 // character used to represent this type in a signature.
519 // Number of methods in the vtable.
520 jshort vtable_method_count
;
523 // Size of instance fields, in bytes.
525 // Total number of fields (instance and static).
527 // Number of static fields.
528 jshort static_field_count
;
529 // The vtbl for all objects of this class.
531 // Virtual method offset table.
532 _Jv_OffsetTable
*otable
;
533 // Offset table symbols.
534 _Jv_MethodSymbol
*otable_syms
;
536 _Jv_AddressTable
*atable
;
537 _Jv_MethodSymbol
*atable_syms
;
539 _Jv_AddressTable
*itable
;
540 _Jv_MethodSymbol
*itable_syms
;
541 _Jv_CatchClass
*catch_classes
;
542 // Interfaces implemented by this class.
544 // The class loader for this class.
545 java::lang::ClassLoader
*loader
;
546 // Number of interfaces.
547 jshort interface_count
;
548 // State of this class.
550 // The thread which has locked this class. Used during class
552 java::lang::Thread
*thread
;
553 // How many levels of "extends" this class is removed from Object.
555 // Vector of this class's superclasses, ordered by decreasing depth.
557 // Interface Dispatch Table.
558 _Jv_IDispatchTable
*idt
;
559 // Pointer to the class that represents an array of this class.
561 // Security Domain to which this class belongs (or null).
562 java::security::ProtectionDomain
*protectionDomain
;
563 // Pointer to the type assertion table for this class.
564 _Jv_TypeAssertion
*assertion_table
;
565 // Signers of this class (or null).
566 JArray
<jobject
> *hack_signers
;
567 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
569 // Additional data, specific to the generator (JIT, native,
570 // interpreter) of this class.
573 _Jv_ExecutionEngine
*engine
;
576 // Inline functions that are friends of java::lang::Class
578 inline void _Jv_InitClass (jclass klass
)
580 if (__builtin_expect (klass
->state
== JV_STATE_DONE
, true))
582 klass
->initializeClass ();
585 // Return array class corresponding to element type KLASS, creating it if
588 _Jv_GetArrayClass (jclass klass
, java::lang::ClassLoader
*loader
)
590 extern void _Jv_NewArrayClass (jclass element
,
591 java::lang::ClassLoader
*loader
,
592 _Jv_VTable
*array_vtable
= 0);
593 if (__builtin_expect (!klass
->arrayclass
, false))
594 _Jv_NewArrayClass (klass
, loader
);
595 return klass
->arrayclass
;
598 #endif /* __JAVA_LANG_CLASS_H__ */