Field.java (toString): Use Method.appendClassName.
[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 // Method name.
68 _Jv_Utf8Const *name;
69 // Method signature.
70 _Jv_Utf8Const *signature;
71 // Access flags.
72 _Jv_ushort accflags;
73 // Pointer to underlying function.
74 void *ncode;
75 // NULL-terminated list of exception class names; can be NULL if
76 // there are none such.
77 _Jv_Utf8Const **throws;
78
79 _Jv_Method *getNextMethod ()
80 { return this + 1; }
81 };
82
83 // Interface Dispatch Tables
84 union _Jv_IDispatchTable
85 {
86 struct
87 {
88 // Index into interface's ioffsets.
89 jshort iindex;
90 jshort itable_length;
91 // Class Interface dispatch table.
92 void **itable;
93 } cls;
94
95 struct
96 {
97 // Offsets into implementation class itables.
98 jshort *ioffsets;
99 } iface;
100 };
101
102 // Used by _Jv_GetInterfaces ()
103 struct _Jv_ifaces
104 {
105 jclass *list;
106 jshort len;
107 jshort count;
108 };
109
110 // Used for vtable pointer manipulation.
111 union _Jv_Self
112 {
113 char *vtable_ptr;
114 jclass self;
115 };
116
117 #define JV_PRIMITIVE_VTABLE ((_Jv_VTable *) -1)
118
119 #define JV_CLASS(Obj) ((jclass) (*(_Jv_VTable **) Obj)->clas)
120
121 class java::lang::Class : public java::lang::Object
122 {
123 public:
124 static jclass forName (jstring className, jboolean initialize,
125 java::lang::ClassLoader *loader);
126 static jclass forName (jstring className);
127 JArray<jclass> *getClasses (void);
128
129 java::lang::ClassLoader *getClassLoader (void)
130 {
131 return loader;
132 }
133
134 java::lang::reflect::Constructor *getConstructor (JArray<jclass> *);
135 JArray<java::lang::reflect::Constructor *> *getConstructors (void);
136 java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *);
137 JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void);
138 java::lang::reflect::Field *getDeclaredField (jstring);
139 JArray<java::lang::reflect::Field *> *getDeclaredFields (void);
140 java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *);
141 JArray<java::lang::reflect::Method *> *getDeclaredMethods (void);
142
143 JArray<jclass> *getDeclaredClasses (void);
144 jclass getDeclaringClass (void);
145
146 java::lang::reflect::Field *getField (jstring);
147 private:
148 jint _getFields (JArray<java::lang::reflect::Field *> *result, jint offset);
149 JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean);
150 java::lang::reflect::Field *getField (jstring, jint);
151 jint _getMethods (JArray<java::lang::reflect::Method *> *result,
152 jint offset);
153 java::lang::reflect::Field *getPrivateField (jstring);
154 java::lang::reflect::Method *getPrivateMethod (jstring, JArray<jclass> *);
155 java::security::ProtectionDomain *getProtectionDomain0 ();
156
157 public:
158 JArray<java::lang::reflect::Field *> *getFields (void);
159
160 JArray<jclass> *getInterfaces (void);
161
162 void getSignature (java::lang::StringBuffer *buffer);
163 static jstring getSignature (JArray<jclass> *, jboolean is_constructor);
164 java::lang::reflect::Method *getMethod (jstring, JArray<jclass> *);
165 JArray<java::lang::reflect::Method *> *getMethods (void);
166
167 inline jint getModifiers (void)
168 {
169 return accflags;
170 }
171
172 jstring getName (void);
173
174 java::net::URL *getResource (jstring resourceName);
175 java::io::InputStream *getResourceAsStream (jstring resourceName);
176 JArray<jobject> *getSigners (void);
177
178 inline jclass getSuperclass (void)
179 {
180 return superclass;
181 }
182
183 inline jboolean isArray (void)
184 {
185 return name->data[0] == '[';
186 }
187
188 inline jclass getComponentType (void)
189 {
190 return isArray () ? (* (jclass *) &methods) : 0;
191 }
192
193 jboolean isAssignableFrom (jclass cls);
194 jboolean isInstance (jobject obj);
195
196 inline jboolean isInterface (void)
197 {
198 return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
199 }
200
201 inline jboolean isPrimitive (void)
202 {
203 return vtable == JV_PRIMITIVE_VTABLE;
204 }
205
206 jobject newInstance (void);
207 jstring toString (void);
208
209 // FIXME: this probably shouldn't be public.
210 jint size (void)
211 {
212 return size_in_bytes;
213 }
214
215 // finalization
216 void finalize ();
217
218 // This constructor is used to create Class object for the primitive
219 // types. See prims.cc.
220 Class (jobject cname, jbyte sig, jint len, jobject array_vtable)
221 {
222 using namespace java::lang::reflect;
223 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
224
225 // C++ ctors set the vtbl pointer to point at an offset inside the vtable
226 // object. That doesn't work for Java, so this hack adjusts it back.
227 ((_Jv_Self *)this)->vtable_ptr -= 2 * sizeof (void *);
228
229 // We must initialize every field of the class. We do this in the
230 // same order they are declared in Class.h, except for fields that
231 // are initialized to NULL.
232 name = _Jv_makeUtf8Const ((char *) cname, -1);
233 accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
234 method_count = sig;
235 size_in_bytes = len;
236 vtable = JV_PRIMITIVE_VTABLE;
237 state = JV_STATE_DONE;
238 depth = -1;
239 if (method_count != 'V')
240 _Jv_NewArrayClass (this, NULL, (_Jv_VTable *) array_vtable);
241 }
242
243 static java::lang::Class class$;
244
245 private:
246
247 Class ();
248
249 void checkMemberAccess (jint flags);
250
251 void initializeClass (void);
252
253 // Friend functions implemented in natClass.cc.
254 friend _Jv_Method *_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
255 _Jv_Utf8Const *signature);
256 friend jboolean _Jv_IsAssignableFrom(jclass, jclass);
257 friend jboolean _Jv_InterfaceAssignableFrom (jclass, jclass);
258 friend void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
259 int method_idx);
260
261 inline friend void
262 _Jv_InitClass (jclass klass)
263 {
264 if (__builtin_expect (klass->state == JV_STATE_DONE, true))
265 return;
266 klass->initializeClass ();
267 }
268
269 friend _Jv_Method* _Jv_LookupDeclaredMethod (jclass, _Jv_Utf8Const *,
270 _Jv_Utf8Const*);
271 friend jfieldID JvGetFirstInstanceField (jclass);
272 friend jint JvNumInstanceFields (jclass);
273 friend jfieldID JvGetFirstStaticField (jclass);
274 friend jint JvNumStaticFields (jclass);
275
276 friend jobject _Jv_AllocObject (jclass, jint);
277 friend void *_Jv_AllocObj (jint, jclass);
278 friend void *_Jv_AllocPtrFreeObj (jint, jclass);
279 friend void *_Jv_AllocArray (jint, jclass);
280
281 friend jobject _Jv_JNI_ToReflectedField (_Jv_JNIEnv *, jclass, jfieldID,
282 jboolean);
283 friend jobject _Jv_JNI_ToReflectedMethod (_Jv_JNIEnv *, jclass, jmethodID,
284 jboolean);
285 friend jfieldID _Jv_FromReflectedField (java::lang::reflect::Field *);
286
287 friend jmethodID _Jv_FromReflectedMethod (java::lang::reflect::Method *);
288 friend jmethodID _Jv_FromReflectedConstructor (java::lang::reflect::Constructor *);
289 friend jint JvNumMethods (jclass);
290 friend jmethodID JvGetFirstMethod (jclass);
291
292 // Friends classes and functions to implement the ClassLoader
293 friend class java::lang::ClassLoader;
294
295 friend class java::io::ObjectOutputStream;
296 friend class java::io::ObjectInputStream;
297 friend class java::io::ObjectStreamClass;
298
299 friend void _Jv_WaitForState (jclass, int);
300 friend void _Jv_RegisterClasses (jclass *classes);
301 friend void _Jv_RegisterInitiatingLoader (jclass,java::lang::ClassLoader*);
302 friend void _Jv_UnregisterClass (jclass);
303 friend jclass _Jv_FindClass (_Jv_Utf8Const *name,
304 java::lang::ClassLoader *loader);
305 friend jclass _Jv_FindClassInCache (_Jv_Utf8Const *name,
306 java::lang::ClassLoader *loader);
307 friend void _Jv_NewArrayClass (jclass element,
308 java::lang::ClassLoader *loader,
309 _Jv_VTable *array_vtable = 0);
310 friend jclass _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
311 java::lang::ClassLoader *loader);
312
313 friend void _Jv_PrepareCompiledClass (jclass);
314 friend void _Jv_PrepareConstantTimeTables (jclass);
315 friend jshort _Jv_GetInterfaces (jclass, _Jv_ifaces *);
316 friend void _Jv_GenerateITable (jclass, _Jv_ifaces *, jshort *);
317 friend jstring _Jv_GetMethodString(jclass, _Jv_Utf8Const *);
318 friend jshort _Jv_AppendPartialITable (jclass, jclass, void **, jshort);
319 friend jshort _Jv_FindIIndex (jclass *, jshort *, jshort);
320
321 // Return array class corresponding to element type KLASS, creating it if
322 // neccessary.
323 inline friend jclass
324 _Jv_GetArrayClass (jclass klass, java::lang::ClassLoader *loader)
325 {
326 if (__builtin_expect (!klass->arrayclass, false))
327 _Jv_NewArrayClass (klass, loader);
328 return klass->arrayclass;
329 }
330
331 #ifdef INTERPRETER
332 friend jboolean _Jv_IsInterpretedClass (jclass);
333 friend void _Jv_InitField (jobject, jclass, _Jv_Field*);
334 friend int _Jv_DetermineVTableIndex (jclass, _Jv_Utf8Const *,
335 _Jv_Utf8Const*);
336 friend void _Jv_InitField (jobject, jclass, int);
337 friend _Jv_word _Jv_ResolvePoolEntry (jclass, int);
338 friend _Jv_Method *_Jv_SearchMethodInClass (jclass cls, jclass klass,
339 _Jv_Utf8Const *method_name,
340 _Jv_Utf8Const *method_signature);
341
342 friend void _Jv_PrepareClass (jclass);
343
344 friend class _Jv_ClassReader;
345 friend class _Jv_InterpClass;
346 friend class _Jv_InterpMethod;
347 friend class _Jv_InterpMethodInvocation;
348 #endif
349
350 #ifdef JV_MARKOBJ_DECL
351 friend JV_MARKOBJ_DECL;
352 #endif
353
354 // Chain for class pool.
355 jclass next;
356 // Name of class.
357 _Jv_Utf8Const *name;
358 // Access flags for class.
359 _Jv_ushort accflags;
360 // The superclass, or null for Object.
361 jclass superclass;
362 // Class constants.
363 _Jv_Constants constants;
364 // Methods. If this is an array class, then this field holds a
365 // pointer to the element type.
366 _Jv_Method *methods;
367 // Number of methods. If this class is primitive, this holds the
368 // character used to represent this type in a signature.
369 jshort method_count;
370 // Number of methods in the vtable.
371 jshort vtable_method_count;
372 // The fields.
373 _Jv_Field *fields;
374 // Size of instance fields, in bytes.
375 jint size_in_bytes;
376 // Total number of fields (instance and static).
377 jshort field_count;
378 // Number of static fields.
379 jshort static_field_count;
380 // The vtbl for all objects of this class.
381 _Jv_VTable *vtable;
382 // Interfaces implemented by this class.
383 jclass *interfaces;
384 // The class loader for this class.
385 java::lang::ClassLoader *loader;
386 // Number of interfaces.
387 jshort interface_count;
388 // State of this class.
389 jbyte state;
390 // The thread which has locked this class. Used during class
391 // initialization.
392 java::lang::Thread *thread;
393 // How many levels of "extends" this class is removed from Object.
394 jshort depth;
395 // Vector of this class's superclasses, ordered by decreasing depth.
396 jclass *ancestors;
397 // Interface Dispatch Table.
398 _Jv_IDispatchTable *idt;
399 // Pointer to the class that represents an array of this class.
400 jclass arrayclass;
401 // Security Domain to which this class belongs (or null).
402 java::security::ProtectionDomain *protectionDomain;
403 };
404
405 #endif /* __JAVA_LANG_CLASS_H__ */