Class.h (_Jv_Self): New union type.
[gcc.git] / libjava / java / lang / Class.h
1 // Class.h - Header file for java.lang.Class. -*- c++ -*-
2
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 // Written primary using compiler source and Class.java as guides.
12 #ifndef __JAVA_LANG_CLASS_H__
13 #define __JAVA_LANG_CLASS_H__
14
15 #pragma interface
16
17 #include <stddef.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
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);
27
28 // This must be predefined with "C" linkage.
29 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
30 int meth_idx);
31
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.
35 enum
36 {
37 JV_STATE_NOTHING = 0, // Set by compiler.
38
39 JV_STATE_PRELOADING = 1, // Can do _Jv_FindClass.
40 JV_STATE_LOADING = 3, // Has super installed.
41 JV_STATE_LOADED = 5, // Is complete.
42
43 JV_STATE_COMPILED = 6, // This was a compiled class.
44
45 JV_STATE_PREPARED = 7, // Layout & static init done.
46 JV_STATE_LINKED = 9, // Strings interned.
47
48 JV_STATE_IN_PROGRESS = 10, // <Clinit> running.
49 JV_STATE_DONE = 12, //
50
51 JV_STATE_ERROR = 14 // must be last.
52 };
53
54 struct _Jv_Field;
55 struct _Jv_VTable;
56 union _Jv_word;
57
58 struct _Jv_Constants
59 {
60 jint size;
61 jbyte *tags;
62 _Jv_word *data;
63 };
64
65 struct _Jv_Method
66 {
67 _Jv_Utf8Const *name;
68 _Jv_Utf8Const *signature;
69 _Jv_ushort accflags;
70 void *ncode;
71 _Jv_Method *getNextMethod ()
72 { return this + 1; }
73 };
74
75 // Interface Dispatch Tables
76 union _Jv_IDispatchTable
77 {
78 struct
79 {
80 // Index into interface's ioffsets.
81 jshort iindex;
82 jshort itable_length;
83 // Class Interface dispatch table.
84 void **itable;
85 } cls;
86
87 struct
88 {
89 // Offsets into implementation class itables.
90 jshort *ioffsets;
91 } iface;
92 };
93
94 // Used by _Jv_GetInterfaces ()
95 struct _Jv_ifaces
96 {
97 jclass *list;
98 jshort len;
99 jshort count;
100 };
101
102 // Used for vtable pointer manipulation.
103 union _Jv_Self
104 {
105 char *vtable_ptr;
106 jclass self;
107 };
108
109 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
110
111 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
112
113 class java::lang::Class : public java::lang::Object
114 {
115 public:
116 static jclass forName (jstring className, jboolean initialize,
117 java::lang::ClassLoader *loader);
118 static jclass forName (jstring className);
119 JArray<jclass> *getClasses (void);
120
121 java::lang::ClassLoader *getClassLoader (void)
122 {
123 return loader;
124 }
125
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);
134
135 JArray<jclass> *getDeclaredClasses (void);
136 jclass getDeclaringClass (void);
137
138 java::lang::reflect::Field *getField (jstring);
139 private:
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,
144 jint offset);
145 java::lang::reflect::Field *getPrivateField (jstring);
146 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
147 java::security::ProtectionDomain *getProtectionDomain0 ();
148
149 public:
150 JArray<java::lang::reflect::Field *> *getFields (void);
151
152 JArray<jclass> *getInterfaces (void);
153
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);
158
159 inline jint getModifiers (void)
160 {
161 return accflags;
162 }
163
164 jstring getName (void);
165
166 java::net::URL *getResource (jstring resourceName);
167 java::io::InputStream *getResourceAsStream (jstring resourceName);
168 JArray<jobject> *getSigners (void);
169
170 inline jclass getSuperclass (void)
171 {
172 return superclass;
173 }
174
175 inline jboolean isArray (void)
176 {
177 return name->data[0] == '[';
178 }
179
180 inline jclass getComponentType (void)
181 {
182 return isArray () ? (* (jclass *) &methods) : 0;
183 }
184
185 jboolean isAssignableFrom (jclass cls);
186 jboolean isInstance (jobject obj);
187
188 inline jboolean isInterface (void)
189 {
190 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
191 }
192
193 inline jboolean isPrimitive (void)
194 {
195 return vtable == JV_PRIMITIVE_VTABLE;
196 }
197
198 jobject newInstance (void);
199 jstring toString (void);
200
201 // FIXME: this probably shouldn't be public.
202 jint size (void)
203 {
204 return size_in_bytes;
205 }
206
207 // finalization
208 void finalize ();
209
210 Class () {};
211
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)
215 {
216 using namespace java::lang::reflect;
217 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
218
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 *);
222
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;
228 method_count = sig;
229 size_in_bytes = len;
230 vtable = JV_PRIMITIVE_VTABLE;
231 state = JV_STATE_DONE;
232 depth = -1;
233 if (method_count != 'V')
234 _Jv_NewArrayClass (this, NULL, (_Jv_VTable *) array_vtable);
235 }
236
237 static java::lang::Class class$;
238
239 private:
240
241 void checkMemberAccess (jint flags);
242
243 void initializeClass (void);
244
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,
251 int method_idx);
252
253 inline friend void
254 _Jv_InitClass (jclass klass)
255 {
256 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
257 return;
258 klass->initializeClass ();
259 }
260
261 friend _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
262 _Jv_Utf8Const*);
263 friend jfieldID JvGetFirstInstanceField (jclass);
264 friend jint JvNumInstanceFields (jclass);
265 friend jfieldID JvGetFirstStaticField (jclass);
266 friend jint JvNumStaticFields (jclass);
267
268 friend jobject _Jv_AllocObject (jclass, jint);
269 friend void *_Jv_AllocObj (jint, jclass);
270 friend void *_Jv_AllocArray (jint, jclass);
271
272 friend jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
273 jboolean);
274 friend jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
275 jboolean);
276 friend jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
277
278 friend jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
279 friend jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
280 friend jint JvNumMethods (jclass);
281 friend jmethodID JvGetFirstMethod (jclass);
282
283 // Friends classes and functions to implement the ClassLoader
284 friend class java::lang::ClassLoader;
285
286 friend class java::io::ObjectOutputStream;
287 friend class java::io::ObjectInputStream;
288 friend class java::io::ObjectStreamClass;
289
290 friend void _Jv_WaitForState (jclass, int);
291 friend void _Jv_RegisterClasses (jclass *classes);
292 friend void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
293 friend void _Jv_UnregisterClass (jclass);
294 friend jclass _Jv_FindClass (_Jv_Utf8Const *name,
295 java::lang::ClassLoader *loader);
296 friend jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
297 java::lang::ClassLoader *loader);
298 friend void _Jv_NewArrayClass (jclass element,
299 java::lang::ClassLoader *loader,
300 _Jv_VTable *array_vtable = 0);
301 friend jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
302 java::lang::ClassLoader *loader);
303
304 friend void _Jv_PrepareCompiledClass (jclass);
305 friend void _Jv_PrepareConstantTimeTables (jclass);
306 friend jshort _Jv_GetInterfaces (jclass, _Jv_ifaces *);
307 friend void _Jv_GenerateITable (jclass, _Jv_ifaces *, jshort *);
308 friend jstring _Jv_GetMethodString(jclass, _Jv_Utf8Const *);
309 friend jshort _Jv_AppendPartialITable (jclass, jclass, void **, jshort);
310 friend jshort _Jv_FindIIndex (jclass *, jshort *, jshort);
311
312 // Return array class corresponding to element type KLASS, creating it if
313 // neccessary.
314 inline friend jclass
315 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
316 {
317 if (__builtin_expect (!klass->arrayclass, false))
318 _Jv_NewArrayClass (klass, loader);
319 return klass->arrayclass;
320 }
321
322 #ifdef INTERPRETER
323 friend jboolean _Jv_IsInterpretedClass (jclass);
324 friend void _Jv_InitField (jobject, jclass, _Jv_Field*);
325 friend int _Jv_DetermineVTableIndex (jclass, _Jv_Utf8Const *,
326 _Jv_Utf8Const*);
327 friend void _Jv_InitField (jobject, jclass, int);
328 friend _Jv_word _Jv_ResolvePoolEntry (jclass, int);
329 friend _Jv_Method *_Jv_SearchMethodInClass (jclass cls, jclass klass,
330 _Jv_Utf8Const *method_name,
331 _Jv_Utf8Const *method_signature);
332
333 friend void _Jv_PrepareClass (jclass);
334
335 friend class _Jv_ClassReader;
336 friend class _Jv_InterpClass;
337 friend class _Jv_InterpMethod;
338 friend class _Jv_InterpMethodInvocation;
339 #endif
340
341 #ifdef JV_MARKOBJ_DECL
342 friend JV_MARKOBJ_DECL;
343 #endif
344
345 // Chain for class pool.
346 jclass next;
347 // Name of class.
348 _Jv_Utf8Const *name;
349 // Access flags for class.
350 _Jv_ushort accflags;
351 // The superclass, or null for Object.
352 jclass superclass;
353 // Class constants.
354 _Jv_Constants constants;
355 // Methods. If this is an array class, then this field holds a
356 // pointer to the element type.
357 _Jv_Method *methods;
358 // Number of methods. If this class is primitive, this holds the
359 // character used to represent this type in a signature.
360 jshort method_count;
361 // Number of methods in the vtable.
362 jshort vtable_method_count;
363 // The fields.
364 _Jv_Field *fields;
365 // Size of instance fields, in bytes.
366 jint size_in_bytes;
367 // Total number of fields (instance and static).
368 jshort field_count;
369 // Number of static fields.
370 jshort static_field_count;
371 // The vtbl for all objects of this class.
372 _Jv_VTable *vtable;
373 // Interfaces implemented by this class.
374 jclass *interfaces;
375 // The class loader for this class.
376 java::lang::ClassLoader *loader;
377 // Number of interfaces.
378 jshort interface_count;
379 // State of this class.
380 jbyte state;
381 // The thread which has locked this class. Used during class
382 // initialization.
383 java::lang::Thread *thread;
384 // How many levels of "extends" this class is removed from Object.
385 jshort depth;
386 // Vector of this class's superclasses, ordered by decreasing depth.
387 jclass *ancestors;
388 // Interface Dispatch Table.
389 _Jv_IDispatchTable *idt;
390 // Pointer to the class that represents an array of this class.
391 jclass arrayclass;
392 // Security Domain to which this class belongs (or null).
393 java::security::ProtectionDomain *protectionDomain;
394 };
395
396 #endif /* __JAVA_LANG_CLASS_H__ */