1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001 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>
24 // We declare these here to avoid including gcj/cni.h.
25 extern "C" void _Jv_InitClass (jclass klass);
26 extern "C" void _Jv_RegisterClasses (jclass *classes);
28 // This must be predefined with "C" linkage.
29 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
32 // These are the possible values for the `state' field of the class
33 // structure. Note that ordering is important here. Whenever the
34 // state changes, one should notify all waiters of this class.
37 JV_STATE_NOTHING = 0, // Set by compiler.
39 JV_STATE_PRELOADING = 1, // Can do _Jv_FindClass.
40 JV_STATE_LOADING = 3, // Has super installed.
41 JV_STATE_LOADED = 5, // Is complete.
43 JV_STATE_COMPILED = 6, // This was a compiled class.
45 JV_STATE_PREPARED = 7, // Layout & static init done.
46 JV_STATE_LINKED = 9, // Strings interned.
48 JV_STATE_IN_PROGRESS = 10, // <Clinit> running.
49 JV_STATE_DONE = 12, //
51 JV_STATE_ERROR = 14 // must be last.
68 _Jv_Utf8Const *signature;
71 _Jv_Method *getNextMethod ()
75 // Interface Dispatch Tables
76 union _Jv_IDispatchTable
80 // Index into interface's ioffsets.
83 // Class Interface dispatch table.
89 // Offsets into implementation class itables.
94 // Used by _Jv_GetInterfaces ()
102 // Used for vtable pointer manipulation.
109 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
111 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
113 class java::lang::Class : public java::lang::Object
116 static jclass forName (jstring className, jboolean initialize,
117 java::lang::ClassLoader *loader);
118 static jclass forName (jstring className);
119 JArray<jclass> *getClasses (void);
121 java::lang::ClassLoader *getClassLoader (void)
126 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
127 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
128 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
129 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
130 java::lang::reflect::Field *getDeclaredField (jstring);
131 JArray<java::lang::reflect::Field *> *getDeclaredFields (void);
132 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
133 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
135 JArray<jclass> *getDeclaredClasses (void);
136 jclass getDeclaringClass (void);
138 java::lang::reflect::Field *getField (jstring);
140 jint _getFields (JArray<java::lang::reflect::Field *> *result, jint offset);
141 JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
142 java::lang::reflect::Field *getField (jstring, jint);
143 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
145 java::lang::reflect::Field *getPrivateField (jstring);
146 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
147 java::security::ProtectionDomain *getProtectionDomain0 ();
150 JArray<java::lang::reflect::Field *> *getFields (void);
152 JArray<jclass> *getInterfaces (void);
154 void getSignature (java::lang::StringBuffer *buffer);
155 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
156 java::lang::reflect::Method *getMethod (jstring, JArray<jclass> *);
157 JArray<java::lang::reflect::Method *> *getMethods (void);
159 inline jint getModifiers (void)
164 jstring getName (void);
166 java::net::URL *getResource (jstring resourceName);
167 java::io::InputStream *getResourceAsStream (jstring resourceName);
168 JArray<jobject> *getSigners (void);
170 inline jclass getSuperclass (void)
175 inline jboolean isArray (void)
177 return name->data[0] == '[';
180 inline jclass getComponentType (void)
182 return isArray () ? (* (jclass *) &methods) : 0;
185 jboolean isAssignableFrom (jclass cls);
186 jboolean isInstance (jobject obj);
188 inline jboolean isInterface (void)
190 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
193 inline jboolean isPrimitive (void)
195 return vtable == JV_PRIMITIVE_VTABLE;
198 jobject newInstance (void);
199 jstring toString (void);
201 // FIXME: this probably shouldn't be public.
204 return size_in_bytes;
212 // This constructor is used to create Class object for the primitive
213 // types. See prims.cc.
214 Class (jobject cname, jbyte sig, jint len, jobject array_vtable)
216 using namespace java::lang::reflect;
217 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
219 // C++ ctors set the vtbl pointer to point at an offset inside the vtable
220 // object. That doesn't work for Java, so this hack adjusts it back.
221 ((_Jv_Self *)this)->vtable_ptr -= 2 * sizeof (void *);
223 // We must initialize every field of the class. We do this in the
224 // same order they are declared in Class.h, except for fields that
225 // are initialized to NULL.
226 name = _Jv_makeUtf8Const ((char *) cname, -1);
227 accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
230 vtable = JV_PRIMITIVE_VTABLE;
231 state = JV_STATE_DONE;
233 if (method_count != 'V')
234 _Jv_NewArrayClass (this, NULL, (_Jv_VTable *) array_vtable);
237 static java::lang::Class class$;
241 void checkMemberAccess (jint flags);
243 void initializeClass (void);
245 // Friend functions implemented in natClass.cc.
246 friend _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
247 _Jv_Utf8Const *signature);
248 friend jboolean _Jv_IsAssignableFrom(jclass, jclass);
249 friend jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
250 friend void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
254 _Jv_InitClass (jclass klass)
256 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
258 klass->initializeClass ();
261 friend _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
263 friend jfieldID JvGetFirstInstanceField (jclass);
264 friend jint JvNumInstanceFields (jclass);
265 friend jfieldID JvGetFirstStaticField (jclass);
266 friend jint JvNumStaticFields (jclass);
268 friend jobject _Jv_AllocObject (jclass, jint);
269 friend void *_Jv_AllocObj (jint, jclass);
270 friend void *_Jv_AllocPtrFreeObj (jint, jclass);
271 friend void *_Jv_AllocArray (jint, jclass);
273 friend jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
275 friend jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
277 friend jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
279 friend jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
280 friend jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
281 friend jint JvNumMethods (jclass);
282 friend jmethodID JvGetFirstMethod (jclass);
284 // Friends classes and functions to implement the ClassLoader
285 friend class java::lang::ClassLoader;
287 friend class java::io::ObjectOutputStream;
288 friend class java::io::ObjectInputStream;
289 friend class java::io::ObjectStreamClass;
291 friend void _Jv_WaitForState (jclass, int);
292 friend void _Jv_RegisterClasses (jclass *classes);
293 friend void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
294 friend void _Jv_UnregisterClass (jclass);
295 friend jclass _Jv_FindClass (_Jv_Utf8Const *name,
296 java::lang::ClassLoader *loader);
297 friend jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
298 java::lang::ClassLoader *loader);
299 friend void _Jv_NewArrayClass (jclass element,
300 java::lang::ClassLoader *loader,
301 _Jv_VTable *array_vtable = 0);
302 friend jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
303 java::lang::ClassLoader *loader);
305 friend void _Jv_PrepareCompiledClass (jclass);
306 friend void _Jv_PrepareConstantTimeTables (jclass);
307 friend jshort _Jv_GetInterfaces (jclass, _Jv_ifaces *);
308 friend void _Jv_GenerateITable (jclass, _Jv_ifaces *, jshort *);
309 friend jstring _Jv_GetMethodString(jclass, _Jv_Utf8Const *);
310 friend jshort _Jv_AppendPartialITable (jclass, jclass, void **, jshort);
311 friend jshort _Jv_FindIIndex (jclass *, jshort *, jshort);
313 // Return array class corresponding to element type KLASS, creating it if
316 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
318 if (__builtin_expect (!klass->arrayclass, false))
319 _Jv_NewArrayClass (klass, loader);
320 return klass->arrayclass;
324 friend jboolean _Jv_IsInterpretedClass (jclass);
325 friend void _Jv_InitField (jobject, jclass, _Jv_Field*);
326 friend int _Jv_DetermineVTableIndex (jclass, _Jv_Utf8Const *,
328 friend void _Jv_InitField (jobject, jclass, int);
329 friend _Jv_word _Jv_ResolvePoolEntry (jclass, int);
330 friend _Jv_Method *_Jv_SearchMethodInClass (jclass cls, jclass klass,
331 _Jv_Utf8Const *method_name,
332 _Jv_Utf8Const *method_signature);
334 friend void _Jv_PrepareClass (jclass);
336 friend class _Jv_ClassReader;
337 friend class _Jv_InterpClass;
338 friend class _Jv_InterpMethod;
339 friend class _Jv_InterpMethodInvocation;
342 #ifdef JV_MARKOBJ_DECL
343 friend JV_MARKOBJ_DECL;
346 // Chain for class pool.
350 // Access flags for class.
352 // The superclass, or null for Object.
355 _Jv_Constants constants;
356 // Methods. If this is an array class, then this field holds a
357 // pointer to the element type.
359 // Number of methods. If this class is primitive, this holds the
360 // character used to represent this type in a signature.
362 // Number of methods in the vtable.
363 jshort vtable_method_count;
366 // Size of instance fields, in bytes.
368 // Total number of fields (instance and static).
370 // Number of static fields.
371 jshort static_field_count;
372 // The vtbl for all objects of this class.
374 // Interfaces implemented by this class.
376 // The class loader for this class.
377 java::lang::ClassLoader *loader;
378 // Number of interfaces.
379 jshort interface_count;
380 // State of this class.
382 // The thread which has locked this class. Used during class
384 java::lang::Thread *thread;
385 // How many levels of "extends" this class is removed from Object.
387 // Vector of this class's superclasses, ordered by decreasing depth.
389 // Interface Dispatch Table.
390 _Jv_IDispatchTable *idt;
391 // Pointer to the class that represents an array of this class.
393 // Security Domain to which this class belongs (or null).
394 java::security::ProtectionDomain *protectionDomain;
397 #endif /* __JAVA_LANG_CLASS_H__ */