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_InterpreterEngine;
102 class _Jv_ClassReader;
103 class _Jv_InterpClass;
104 class _Jv_InterpMethod;
119 _Jv_Utf8Const *signature;
122 // Method's index in the vtable.
124 // Pointer to underlying function.
126 // NULL-terminated list of exception class names; can be NULL if
127 // there are none such.
128 _Jv_Utf8Const **throws;
130 _Jv_Method *getNextMethod ()
134 // The table used to resolve interface calls.
135 struct _Jv_IDispatchTable
137 // Index into interface's ioffsets.
139 jshort itable_length;
140 // Class Interface dispatch table.
144 // Used by _Jv_Linker::get_interfaces ()
152 struct _Jv_MethodSymbol
154 _Jv_Utf8Const *class_name;
156 _Jv_Utf8Const *signature;
159 struct _Jv_OffsetTable
165 struct _Jv_AddressTable
171 struct _Jv_CatchClass
173 java::lang::Class **address;
174 _Jv_Utf8Const *classname;
177 // Possible values for the assertion_code field in the type assertion table.
180 JV_ASSERT_END_OF_TABLE = 0,
181 JV_ASSERT_TYPES_COMPATIBLE = 1,
182 JV_ASSERT_IS_INSTANTIABLE = 2
185 // Entry in the type assertion table, used to validate type constraints
186 // for binary compatibility.
187 struct _Jv_TypeAssertion
194 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
196 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
198 // Forward declarations for friends of java::lang::Class
200 // Friend functions implemented in natClass.cc.
201 _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
202 _Jv_Utf8Const *signature);
203 jboolean _Jv_IsAssignableFrom (jclass, jclass);
204 jboolean _Jv_IsAssignableFromSlow (jclass, jclass);
205 jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
207 _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
208 _Jv_Utf8Const*, jclass * = NULL);
209 jfieldID JvGetFirstInstanceField (jclass);
210 jint JvNumInstanceFields (jclass);
211 jfieldID JvGetFirstStaticField (jclass);
212 jint JvNumStaticFields (jclass);
214 jobject _Jv_AllocObject (jclass);
215 void *_Jv_AllocObj (jint, jclass);
216 void *_Jv_AllocPtrFreeObj (jint, jclass);
217 void *_Jv_AllocArray (jint, jclass);
219 bool _Jv_getInterfaceMethod(jclass, jclass&, int&,
220 const _Jv_Utf8Const*,
221 const _Jv_Utf8Const*);
223 jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
225 jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
227 jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
229 jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
230 jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
231 jint JvNumMethods (jclass);
232 jmethodID JvGetFirstMethod (jclass);
235 // Finds a desired interpreter method in the given class or NULL if not found
236 _Jv_InterpMethod* _Jv_FindInterpreterMethod (jclass, jmethodID);
239 // Friend classes and functions to implement the ClassLoader
240 class java::lang::ClassLoader;
241 class java::lang::VMClassLoader;
243 class java::io::ObjectOutputStream;
244 class java::io::ObjectInputStream;
245 class java::io::ObjectStreamClass;
247 void _Jv_RegisterClassHookDefault (jclass klass);
248 void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
249 void _Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
250 void _Jv_UnregisterClass (jclass);
251 jclass _Jv_FindClassNoException (_Jv_Utf8Const *name,
252 java::lang::ClassLoader *loader);
253 jclass _Jv_FindClass (_Jv_Utf8Const *name,
254 java::lang::ClassLoader *loader);
255 jclass _Jv_FindClassInCache (_Jv_Utf8Const *name);
256 jclass _Jv_PopClass (void);
257 void _Jv_PushClass (jclass k);
258 void _Jv_NewArrayClass (jclass element,
259 java::lang::ClassLoader *loader,
260 _Jv_VTable *array_vtable = 0);
261 jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
262 java::lang::ClassLoader *loader);
263 void _Jv_InitNewClassFields (jclass klass);
265 // Friend functions and classes in prims.cc
266 void _Jv_InitPrimClass (jclass, const char *, char, int);
267 jstring _Jv_GetMethodString (jclass, _Jv_Method *, jclass = NULL);
269 jboolean _Jv_CheckAccess (jclass self_klass, jclass other_klass,
271 jclass _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader);
273 jboolean _Jv_IsInterpretedClass (jclass);
274 jboolean _Jv_IsBinaryCompatibilityABI (jclass);
276 jboolean _Jv_IsPhantomClass (jclass);
278 void _Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *);
281 void _Jv_InitField (jobject, jclass, int);
284 class _Jv_StackTrace;
285 class _Jv_BytecodeVerifier;
286 class java::io::VMObjectStreamClass;
288 void _Jv_sharedlib_register_hook (jclass klass);
291 class java::lang::Class : public java::lang::Object
294 static jclass forName (jstring className, jboolean initialize,
295 java::lang::ClassLoader *loader);
296 static jclass forName (jstring className);
297 JArray<jclass> *getClasses (void);
299 java::lang::ClassLoader *getClassLoader (void);
301 // This is an internal method that circumvents the usual security
302 // checks when getting the class loader.
303 java::lang::ClassLoader *getClassLoaderInternal (void)
308 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
309 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
310 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
311 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (jboolean);
312 java::lang::reflect::Field *getDeclaredField (jstring);
313 JArray<java::lang::reflect::Field *> *getDeclaredFields ();
314 JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean);
315 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
316 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
318 JArray<jclass> *getDeclaredClasses (void);
319 JArray<jclass> *getDeclaredClasses (jboolean);
320 jclass getDeclaringClass (void);
322 java::lang::reflect::Field *getField (jstring);
324 JArray<java::lang::reflect::Field *> internalGetFields ();
325 java::lang::reflect::Field *getField (jstring, jint);
326 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
328 java::lang::reflect::Field *getPrivateField (jstring);
329 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
330 java::security::ProtectionDomain *getProtectionDomain0 ();
332 java::lang::reflect::Method *_getMethod (jstring, JArray<jclass> *);
333 java::lang::reflect::Method *_getDeclaredMethod (jstring, JArray<jclass> *);
336 JArray<java::lang::reflect::Field *> *getFields (void);
338 JArray<jclass> *getInterfaces (void);
340 void getSignature (java::lang::StringBuffer *buffer);
341 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
342 JArray<java::lang::reflect::Method *> *getMethods (void);
344 inline jint getModifiers (void)
346 return accflags & java::lang::reflect::Modifier::ALL_FLAGS;
349 jstring getName (void);
351 java::net::URL *getResource (jstring resourceName);
352 java::io::InputStream *getResourceAsStream (jstring resourceName);
353 JArray<jobject> *getSigners (void);
354 void setSigners(JArray<jobject> *);
356 inline jclass getSuperclass (void)
361 inline jclass getInterface (jint n)
363 return interfaces[n];
366 inline jboolean isArray (void)
368 return name->first() == '[';
371 inline jclass getComponentType (void)
373 return isArray () ? element_type : 0;
376 jboolean isAssignableFrom (jclass cls);
377 jboolean isInstance (jobject obj);
379 inline jboolean isInterface (void)
381 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
384 inline jboolean isPrimitive (void)
386 return vtable == JV_PRIMITIVE_VTABLE;
389 jobject newInstance (void);
390 java::security::ProtectionDomain *getProtectionDomain (void);
391 java::lang::Package *getPackage (void);
392 jstring toString (void);
393 jboolean desiredAssertionStatus (void);
395 // FIXME: this probably shouldn't be public.
398 return size_in_bytes;
401 // The index of the first method we declare ourself (as opposed to
403 inline jint firstMethodIndex (void)
405 return vtable_method_count - method_count;
411 // This constructor is used to create Class object for the primitive
412 // types. See prims.cc.
415 static java::lang::Class class$;
419 void memberAccessCheck (jint flags);
421 void initializeClass (void);
423 static jstring getPackagePortion (jstring);
425 void set_state (jint nstate)
431 // Friend functions implemented in natClass.cc.
432 friend _Jv_Method *::_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
433 _Jv_Utf8Const *signature);
434 friend jboolean (::_Jv_IsAssignableFrom) (jclass, jclass);
435 friend jboolean (::_Jv_IsAssignableFromSlow) (jclass, jclass);
436 friend jboolean (::_Jv_InterfaceAssignableFrom) (jclass, jclass);
437 friend void *::_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
440 friend void ::_Jv_InitClass (jclass klass);
441 friend java::lang::Class* ::_Jv_NewClassFromInitializer (const jclass class_initializer);
442 friend void _Jv_RegisterNewClasses (void **classes);
444 friend _Jv_Method* ::_Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
445 _Jv_Utf8Const*, jclass *);
446 friend jfieldID (::JvGetFirstInstanceField) (jclass);
447 friend jint (::JvNumInstanceFields) (jclass);
448 friend jfieldID (::JvGetFirstStaticField) (jclass);
449 friend jint (::JvNumStaticFields) (jclass);
451 friend jobject (::_Jv_AllocObject) (jclass);
452 friend void *::_Jv_AllocObj (jint, jclass);
453 friend void *::_Jv_AllocPtrFreeObj (jint, jclass);
454 friend void *::_Jv_AllocArray (jint, jclass);
456 friend jobject (::_Jv_JNI_ToReflectedField) (_Jv_JNIEnv *, jclass, jfieldID,
458 friend jobject (::_Jv_JNI_ToReflectedMethod) (_Jv_JNIEnv *, jclass, jmethodID,
460 friend jfieldID (::_Jv_FromReflectedField) (java::lang::reflect::Field *);
462 friend jmethodID (::_Jv_FromReflectedMethod) (java::lang::reflect::Method *);
463 friend jmethodID (::_Jv_FromReflectedConstructor) (java::lang::reflect::Constructor *);
464 friend jint (::JvNumMethods) (jclass);
465 friend jmethodID (::JvGetFirstMethod) (jclass);
467 friend _Jv_InterpMethod* (::_Jv_FindInterpreterMethod) (jclass klass,
468 jmethodID desired_method);
471 // Friends classes and functions to implement the ClassLoader
472 friend class java::lang::ClassLoader;
473 friend class java::lang::VMClassLoader;
475 friend class java::io::ObjectOutputStream;
476 friend class java::io::ObjectInputStream;
477 friend class java::io::ObjectStreamClass;
479 friend void ::_Jv_RegisterClasses (const jclass *classes);
480 friend void ::_Jv_RegisterClasses_Counted (const jclass *classes,
482 friend void ::_Jv_RegisterClassHookDefault (jclass klass);
483 friend void ::_Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
484 friend void ::_Jv_UnregisterInitiatingLoader (jclass,java::lang::ClassLoader*);
485 friend void ::_Jv_UnregisterClass (jclass);
486 friend jclass (::_Jv_FindClassNoException) (_Jv_Utf8Const *name,
487 java::lang::ClassLoader *loader);
488 friend jclass (::_Jv_FindClass) (_Jv_Utf8Const *name,
489 java::lang::ClassLoader *loader);
490 friend jclass (::_Jv_FindClassInCache) (_Jv_Utf8Const *name);
491 friend jclass (::_Jv_PopClass) (void);
492 friend void ::_Jv_PushClass (jclass k);
493 friend void ::_Jv_NewArrayClass (jclass element,
494 java::lang::ClassLoader *loader,
495 _Jv_VTable *array_vtable);
496 friend jclass (::_Jv_NewClass) (_Jv_Utf8Const *name, jclass superclass,
497 java::lang::ClassLoader *loader);
498 friend void ::_Jv_InitNewClassFields (jclass klass);
501 friend void ::_Jv_InitPrimClass (jclass, const char *, char, int);
503 friend jstring (::_Jv_GetMethodString) (jclass, _Jv_Method *, jclass);
505 friend jboolean (::_Jv_CheckAccess) (jclass self_klass, jclass other_klass,
508 friend bool (::_Jv_getInterfaceMethod) (jclass, jclass&, int&,
509 const _Jv_Utf8Const*,
510 const _Jv_Utf8Const*);
512 friend jclass (::_Jv_GetArrayClass) (jclass klass,
513 java::lang::ClassLoader *loader);
515 friend jboolean (::_Jv_IsInterpretedClass) (jclass);
516 friend jboolean (::_Jv_IsBinaryCompatibilityABI) (jclass);
518 friend jboolean (::_Jv_IsPhantomClass) (jclass);
521 friend void ::_Jv_InitField (jobject, jclass, int);
523 friend class ::_Jv_ClassReader;
524 friend class ::_Jv_InterpClass;
525 friend class ::_Jv_InterpMethod;
527 friend class ::_Jv_StackTrace;
529 #ifdef JV_MARKOBJ_DECL
530 friend JV_MARKOBJ_DECL;
533 friend class ::_Jv_BytecodeVerifier;
534 friend class java::io::VMObjectStreamClass;
536 friend class ::_Jv_Linker;
537 friend class ::_Jv_ExecutionEngine;
538 friend class ::_Jv_CompiledEngine;
539 friend class ::_Jv_InterpreterEngine;
541 friend void ::_Jv_sharedlib_register_hook (jclass klass);
543 friend void *::_Jv_ResolvePoolEntry (jclass this_class, jint index);
545 friend void ::_Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *);
547 // Chain for class pool. This also doubles as the ABI version
548 // number. It is only used for this purpose at class registration
549 // time, and only for precompiled classes.
550 jclass next_or_version;
553 // Access flags for class.
555 // The superclass, or null for Object.
558 _Jv_Constants constants;
559 // Methods. If this is an array class, then this field holds a
560 // pointer to the element type.
566 // Number of methods. If this class is primitive, this holds the
567 // character used to represent this type in a signature.
569 // Number of methods in the vtable.
570 jshort vtable_method_count;
573 // Size of instance fields, in bytes.
575 // Total number of fields (instance and static).
577 // Number of static fields.
578 jshort static_field_count;
579 // The vtbl for all objects of this class.
581 // Virtual method offset table.
582 _Jv_OffsetTable *otable;
583 // Offset table symbols.
584 _Jv_MethodSymbol *otable_syms;
586 _Jv_AddressTable *atable;
587 _Jv_MethodSymbol *atable_syms;
589 _Jv_AddressTable *itable;
590 _Jv_MethodSymbol *itable_syms;
591 _Jv_CatchClass *catch_classes;
592 // Interfaces implemented by this class.
594 // The class loader for this class.
595 java::lang::ClassLoader *loader;
596 // Number of interfaces.
597 jshort interface_count;
598 // State of this class.
600 // The thread which has locked this class. Used during class
602 java::lang::Thread *thread;
603 // How many levels of "extends" this class is removed from Object.
605 // Vector of this class's superclasses, ordered by decreasing depth.
607 // In a regular class, this field points to the Class Interface Dispatch
608 // Table. In an interface, it points to the ioffsets table.
611 _Jv_IDispatchTable *idt;
614 // Pointer to the class that represents an array of this class.
616 // Security Domain to which this class belongs (or null).
617 java::security::ProtectionDomain *protectionDomain;
618 // Pointer to the type assertion table for this class.
619 _Jv_TypeAssertion *assertion_table;
620 // Signers of this class (or null).
621 JArray<jobject> *hack_signers;
622 // Used by Jv_PopClass and _Jv_PushClass to communicate with StackTrace.
624 // Additional data, specific to the generator (JIT, native,
625 // interpreter) of this class.
628 _Jv_ExecutionEngine *engine;
631 // Inline functions that are friends of java::lang::Class
633 inline void _Jv_InitClass (jclass klass)
635 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
637 klass->initializeClass ();
640 // Return array class corresponding to element type KLASS, creating it if
643 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
645 extern void _Jv_NewArrayClass (jclass element,
646 java::lang::ClassLoader *loader,
647 _Jv_VTable *array_vtable = 0);
648 if (__builtin_expect (!klass->arrayclass, false))
649 _Jv_NewArrayClass (klass, loader);
650 return klass->arrayclass;
653 #endif /* __JAVA_LANG_CLASS_H__ */