OSDN Git Service

Formatting fixes.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / java-io / java_io_VMObjectStreamClass.c
1 /* java_io_VMObjectStreamClass.c -- Native methods for VMObjectStreamClass.java
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10  
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 #include <jni.h>
40 #include <jcl.h>
41
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "java_io_VMObjectStreamClass.h"
46
47 /*
48  * Class:     java_io_VMObjectOutputStream
49  * Method:    hasClassInitializer
50  * Signature: (Ljava/lang/Class;)Z
51  */
52 JNIEXPORT jboolean JNICALL
53 Java_java_io_VMObjectStreamClass_hasClassInitializer (JNIEnv * env,
54                                                       jclass vmosklass
55                                                       __attribute__ ((__unused__)), jclass klass)
56 {
57   jmethodID mid = (*env)->GetStaticMethodID (env, klass, "<clinit>", "()V");
58   if (mid == NULL)
59     {
60       (*env)->ExceptionClear (env);
61       return JNI_FALSE;
62     }
63   return JNI_TRUE;
64 }
65
66 static void
67 throwInternalError (JNIEnv * env)
68 {
69   jclass internalErrorClass;
70   jthrowable previousException, newException;
71   jmethodID initException, getMessageID, initCauseID;
72   jstring message;
73
74   internalErrorClass = (*env)->FindClass (env, "java/lang/InternalError");
75   /** Just give up if this also fails. */
76   if (internalErrorClass == NULL)
77     return;
78
79   previousException = (*env)->ExceptionOccurred (env);
80
81   if (previousException == NULL)
82     {
83       (*env)->ThrowNew (env, internalErrorClass,
84                         "Unknown error raised by the VM");
85       return;
86     }
87
88   initException = (*env)->GetMethodID
89     (env, internalErrorClass, "<init>", "(Ljava/lang/String;)V");
90   getMessageID = (*env)->GetMethodID
91     (env, (*env)->GetObjectClass (env, previousException),
92      "getMessage", "()Ljava/lang/String;");
93   initCauseID = (*env)->GetMethodID
94     (env, internalErrorClass, "initCause", "(Ljava/lang/Throwable;)V");
95
96   message = (*env)->CallObjectMethod (env, previousException, getMessageID);
97
98   newException = (*env)->NewObject (env, internalErrorClass, initException,
99                                     message);
100   (*env)->CallVoidMethod (env, newException, initCauseID, previousException);
101
102   (*env)->ExceptionClear (env);
103   (*env)->Throw (env, newException);
104 }
105
106 static jfieldID
107 getFieldReference (JNIEnv * env, jobject field, const char *type)
108 {
109   jclass classClass;
110   jclass fieldClass;
111   jclass declaringClass;
112   jclass typeClass;
113   jfieldID fid;
114   const char *field_name;
115   const char *type_name;
116   int type_len;
117   jmethodID mid;
118   jstring name;
119   jstring tname;
120   int i;
121
122   fieldClass = (*env)->GetObjectClass (env, field);
123
124   mid =
125     (*env)->GetMethodID (env, fieldClass, "getName", "()Ljava/lang/String;");
126   if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
127     {
128       throwInternalError (env);
129       return NULL;
130     }
131
132   name = (*env)->CallObjectMethod (env, field, mid);
133   field_name = (*env)->GetStringUTFChars (env, name, NULL);
134
135   mid = (*env)->GetMethodID (env, fieldClass,
136                              "getDeclaringClass", "()Ljava/lang/Class;");
137   if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
138     {
139       throwInternalError (env);
140       return NULL;
141     }
142
143   declaringClass = (*env)->CallObjectMethod (env, field, mid);
144
145   /* Do we need to find out the exact type descriptor of the field? */
146   if (type == NULL)
147     {
148       char *the_type;
149
150       mid = (*env)->GetMethodID (env, fieldClass,
151                                  "getType", "()Ljava/lang/Class;");
152
153       if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
154         {
155           throwInternalError (env);
156           return NULL;
157         }
158
159       typeClass = (*env)->CallObjectMethod (env, field, mid);
160       classClass = (*env)->FindClass (env, "java/lang/Class");
161
162       mid = (*env)->GetMethodID (env, classClass,
163                                  "getName", "()Ljava/lang/String;");
164
165       if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
166         {
167           throwInternalError (env);
168           return NULL;
169         }
170
171       tname = (*env)->CallObjectMethod (env, typeClass, mid);
172       type_name = (*env)->GetStringUTFChars (env, tname, NULL);
173
174       /*
175        * If it isn't an array class then the actual field type descriptor
176        * starts with 'L', ends with ';' and has '/' instead of '.'.
177        */
178       type_len = strlen (type_name);
179       if (type_name[0] != '[')
180         {
181           /* XXX - FIXME - should not use dynamic allocation in core lib. */
182           the_type = (char *) malloc (type_len + 3);
183           the_type[0] = 'L';
184           the_type[type_len + 1] = ';';
185           the_type[type_len + 2] = '\0';
186           the_type++;
187         }
188       else
189         {
190           /* XXX - FIXME - should not use dynamic allocation in core lib. */
191           the_type = (char *) malloc (type_len + 1);
192           the_type[type_len] = '\0';
193         }
194
195       for (i = 0; i < type_len; i++)
196         if (type_name[i] == '.')
197           the_type[i] = '/';
198         else
199           the_type[i] = type_name[i];
200
201       if (type_name[0] != '[')
202         the_type--;
203
204       (*env)->ReleaseStringUTFChars (env, tname, type_name);
205       fid = (*env)->GetFieldID (env, declaringClass, field_name, the_type);
206       free (the_type);
207     }
208   else
209     {
210       type_len = -1;
211       fid = (*env)->GetFieldID (env, declaringClass, field_name, type);
212     }
213
214   if (fid == NULL)
215     {
216       throwInternalError (env);
217       return NULL;
218     }
219   (*env)->ReleaseStringUTFChars (env, name, field_name);
220
221   return fid;
222 }
223
224 /*
225  * Class:     java_io_VMObjectOutputStream
226  * Method:    setBooleanNative
227  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;Z)V
228  */
229 JNIEXPORT void JNICALL
230 Java_java_io_VMObjectStreamClass_setBooleanNative (JNIEnv * env,
231                                                    jclass vmosklass
232                                                    __attribute__ ((__unused__)), jobject field, jobject object, jboolean value)
233 {
234   jfieldID fid = getFieldReference (env, field, "Z");
235
236   if (fid != NULL)
237     (*env)->SetBooleanField (env, object, fid, value);
238 }
239
240 /*
241  * Class:     java_io_VMObjectOutputStream
242  * Method:    setCharNative
243  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;C)V
244  */
245 JNIEXPORT void JNICALL
246 Java_java_io_VMObjectStreamClass_setCharNative (JNIEnv * env,
247                                                 jclass vmosklass
248                                                 __attribute__ ((__unused__)),
249                                                 jobject field,
250                                                 jobject object, jchar value)
251 {
252   jfieldID fid = getFieldReference (env, field, "C");
253
254   if (fid != NULL)
255     (*env)->SetCharField (env, object, fid, value);
256 }
257
258 /*
259  * Class:     java_io_VMObjectOutputStream
260  * Method:    setByteNative
261  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;B)V
262  */
263 JNIEXPORT void JNICALL
264 Java_java_io_VMObjectStreamClass_setByteNative (JNIEnv * env,
265                                                 jclass vmosklass
266                                                 __attribute__ ((__unused__)),
267                                                 jobject field,
268                                                 jobject object, jbyte value)
269 {
270   jfieldID fid = getFieldReference (env, field, "B");
271
272   if (fid != NULL)
273     (*env)->SetByteField (env, object, fid, value);
274 }
275
276
277 /*
278  * Class:     java_io_VMObjectOutputStream
279  * Method:    setShortNative
280  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;S)V
281  */
282 JNIEXPORT void JNICALL
283 Java_java_io_VMObjectStreamClass_setShortNative (JNIEnv * env,
284                                                  jclass vmosklass
285                                                  __attribute__ ((__unused__)),
286                                                  jobject field,
287                                                  jobject object, jshort value)
288 {
289   jfieldID fid = getFieldReference (env, field, "S");
290
291   if (fid != NULL)
292     (*env)->SetShortField (env, object, fid, value);
293 }
294
295 /*
296  * Class:     java_io_VMObjectOutputStream
297  * Method:    setIntNative
298  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;I)V
299  */
300 JNIEXPORT void JNICALL
301 Java_java_io_VMObjectStreamClass_setIntNative (JNIEnv * env,
302                                                jclass vmosklass
303                                                __attribute__ ((__unused__)),
304                                                jobject field,
305                                                jobject object, jint value)
306 {
307   jfieldID fid = getFieldReference (env, field, "I");
308
309   if (fid != NULL)
310     (*env)->SetIntField (env, object, fid, value);
311 }
312
313
314 /*
315  * Class:     java_io_VMObjectOutputStream
316  * Method:    setLongNative
317  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;J)V
318  */
319 JNIEXPORT void JNICALL
320 Java_java_io_VMObjectStreamClass_setLongNative (JNIEnv * env,
321                                                 jclass vmosklass
322                                                 __attribute__ ((__unused__)),
323                                                 jobject field,
324                                                 jobject object, jlong value)
325 {
326   jfieldID fid = getFieldReference (env, field, "J");
327
328   if (fid != NULL)
329     (*env)->SetLongField (env, object, fid, value);
330 }
331
332
333 /*
334  * Class:     java_io_VMObjectOutputStream
335  * Method:    setFloatNative
336  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;F)V
337  */
338 JNIEXPORT void JNICALL
339 Java_java_io_VMObjectStreamClass_setFloatNative (JNIEnv * env,
340                                                  jclass vmosklass
341                                                  __attribute__ ((__unused__)),
342                                                  jobject field,
343                                                  jobject object, jfloat value)
344 {
345   jfieldID fid = getFieldReference (env, field, "F");
346
347   if (fid != NULL)
348     (*env)->SetFloatField (env, object, fid, value);
349 }
350
351 /*
352  * Class:     java_io_VMObjectOutputStream
353  * Method:    setDoubleNative
354  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;D)V
355  */
356 JNIEXPORT void JNICALL
357 Java_java_io_VMObjectStreamClass_setDoubleNative (JNIEnv * env,
358                                                   jclass vmosklass
359                                                   __attribute__ ((__unused__)), jobject field, jobject object, jdouble value)
360 {
361   jfieldID fid = getFieldReference (env, field, "D");
362
363   if (fid != NULL)
364     (*env)->SetDoubleField (env, object, fid, value);
365 }
366
367 /*
368  * Class:     java_io_VMObjectOutputStream
369  * Method:    setObjectNative
370  * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;Ljava/lang/Object;)V
371  */
372 JNIEXPORT void JNICALL
373 Java_java_io_VMObjectStreamClass_setObjectNative (JNIEnv * env,
374                                                   jclass vmosklass
375                                                   __attribute__ ((__unused__)), jobject field, jobject object, jobject value)
376 {
377   jfieldID fid = getFieldReference (env, field, NULL);
378
379   if (fid != NULL)
380     (*env)->SetObjectField (env, object, fid, value);
381 }