* src/native/jni.c [ENABLE_JAVASE] (native/vm/reflect.h): Added.
[cacao.git] / src / native / vm / reflect.c
1 /* src/native/vm/reflect.c - helper functions for java/lang/reflect
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: java_lang_Class.c 8132 2007-06-22 11:15:47Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34 #include <string.h>
35
36 #include "native/jni.h"
37 #include "native/native.h"
38
39 /* keep this order of the native includes */
40
41 #include "native/include/java_lang_String.h"
42
43 #if defined(ENABLE_JAVASE)
44 # if defined(WITH_CLASSPATH_SUN)
45 #  include "native/include/java_nio_ByteBuffer.h"       /* required by j.l.CL */
46 # endif
47 # include "native/include/java_lang_ClassLoader.h"
48 #endif
49
50 #include "native/include/java_lang_Object.h"
51 #include "native/include/java_lang_Class.h"
52
53 #if defined(ENABLE_JAVASE)
54 # include "native/include/java_lang_reflect_Constructor.h"
55 # include "native/include/java_lang_reflect_Field.h"
56 # include "native/include/java_lang_reflect_Method.h"
57 #endif
58
59 #include "native/vm/reflect.h"
60
61 #include "vm/builtin.h"
62 #include "vm/global.h"
63
64 #include "vmcore/method.h"
65
66
67 /* reflect_constructor_new *****************************************************
68
69    Allocates a new java.lang.reflect.Constructor object and
70    initializes the fields with the method passed.
71
72 *******************************************************************************/
73
74 java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
75 {
76         classinfo                     *c;
77         java_objectheader             *o;
78         java_lang_reflect_Constructor *rc;
79         int32_t                        slot;
80
81         /* get declaring class */
82
83         c = (classinfo *) m->class;
84
85         /* allocate a new object */
86
87         o = builtin_new(class_java_lang_reflect_Constructor);
88
89         if (o == NULL)
90                 return NULL;
91
92         /* initialize instance fields */
93
94         rc = (java_lang_reflect_Constructor *) o;
95
96         /* calculate the slot */
97
98         slot = m - c->methods;
99
100 #if defined(WITH_CLASSPATH_GNU)
101
102         rc->clazz                = (java_lang_Class *) c;
103         rc->slot                 = slot;
104
105 #elif defined(WITH_CLASSPATH_SUN)
106
107         rc->clazz                = (java_lang_Class *) c;
108         rc->parameterTypes       = method_get_parametertypearray(m);
109         rc->exceptionTypes       = method_get_exceptionarray(m);
110         rc->modifiers            = m->flags & ACC_CLASS_REFLECT_MASK;
111         rc->slot                 = slot;
112         rc->signature            = m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL;
113         rc->annotations          = NULL;
114         rc->parameterAnnotations = NULL;
115
116 #else
117 # error unknown classpath configuration
118 #endif
119
120         return rc;
121 }
122
123
124 /* reflect_field_new ***********************************************************
125
126    Allocates a new java.lang.reflect.Field object and initializes the
127    fields with the field passed.
128
129 *******************************************************************************/
130
131 java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
132 {
133         classinfo               *c;
134         java_objectheader       *o;
135         java_lang_reflect_Field *rf;
136         int32_t                  slot;
137
138         /* get declaring class */
139
140         c = (classinfo *) f->class;
141
142         /* allocate a new object */
143
144         o = builtin_new(class_java_lang_reflect_Field);
145
146         if (o == NULL)
147                 return NULL;
148
149         /* initialize instance fields */
150
151         rf = (java_lang_reflect_Field *) o;
152
153         /* calculate the slot */
154
155         slot = f - c->fields;
156
157 #if defined(WITH_CLASSPATH_GNU)
158
159         rf->clazz = (java_lang_Class *) c;
160
161         /* The name needs to be interned */
162         /* XXX implement me better! */
163
164         rf->name           = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name));
165         rf->slot           = slot;
166
167 #elif defined(WITH_CLASSPATH_SUN)
168
169         rf->clazz          = (java_lang_Class *) c;
170
171         /* The name needs to be interned */
172         /* XXX implement me better! */
173
174         rf->name           = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name));
175         rf->type           = (java_lang_Class *) field_get_type(f);
176         rf->modifiers      = f->flags;
177         rf->slot           = slot;
178         rf->signature      = f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL;
179         rf->annotations    = NULL;
180
181 #else
182 # error unknown classpath configuration
183 #endif
184
185         return rf;
186 }
187
188
189 /* reflect_method_new **********************************************************
190
191    Allocates a new java.lang.reflect.Method object and initializes the
192    fields with the method passed.
193
194 *******************************************************************************/
195
196 java_lang_reflect_Method *reflect_method_new(methodinfo *m)
197 {
198         classinfo                *c;
199         java_objectheader        *o;
200         java_lang_reflect_Method *rm;
201         int32_t                   slot;
202
203         /* get declaring class */
204
205         c = (classinfo *) m->class;
206
207         /* allocate a new object */
208
209         o = builtin_new(class_java_lang_reflect_Method);
210
211         if (o == NULL)
212                 return NULL;
213
214         /* initialize instance fields */
215
216         rm = (java_lang_reflect_Method *) o;
217
218         /* calculate the slot */
219
220         slot = m - c->methods;
221
222 #if defined(WITH_CLASSPATH_GNU)
223
224         rm->clazz                = (java_lang_Class *) m->class;
225
226         /* The name needs to be interned */
227         /* XXX implement me better! */
228
229         rm->name                 = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name));
230         rm->slot                 = slot;
231
232 #elif defined(WITH_CLASSPATH_SUN)
233
234         rm->clazz                = (java_lang_Class *) m->class;
235
236         /* The name needs to be interned */
237         /* XXX implement me better! */
238
239         rm->name                 = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name));
240         rm->parameterTypes       = method_get_parametertypearray(m);
241         rm->returnType           = (java_lang_Class *) method_returntype_get(m);
242         rm->exceptionTypes       = method_get_exceptionarray(m);
243         rm->modifiers            = m->flags & ACC_CLASS_REFLECT_MASK;
244         rm->slot                 = slot;
245         rm->signature            = m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL;
246         rm->annotations          = NULL;
247         rm->parameterAnnotations = NULL;
248         rm->annotationDefault    = NULL;
249
250 #else
251 # error unknown classpath configuration
252 #endif
253
254         return rm;
255 }
256
257
258 /*
259  * These are local overrides for various environment variables in Emacs.
260  * Please do not remove this and leave it at the end of the file, where
261  * Emacs will automagically detect them.
262  * ---------------------------------------------------------------------
263  * Local variables:
264  * mode: c
265  * indent-tabs-mode: t
266  * c-basic-offset: 4
267  * tab-width: 4
268  * End:
269  * vim:noexpandtab:sw=4:ts=4:
270  */