1 /* java_io_VMObjectStreamClass.c -- Native methods for VMObjectStreamClass.java
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
45 #include "java_io_VMObjectStreamClass.h"
48 * Class: java_io_VMObjectOutputStream
49 * Method: hasClassInitializer
50 * Signature: (Ljava/lang/Class;)Z
52 JNIEXPORT jboolean JNICALL
53 Java_java_io_VMObjectStreamClass_hasClassInitializer (JNIEnv * env,
55 __attribute__ ((__unused__)), jclass klass)
57 jmethodID mid = (*env)->GetStaticMethodID (env, klass, "<clinit>", "()V");
60 (*env)->ExceptionClear (env);
67 throwInternalError (JNIEnv * env)
69 jclass internalErrorClass;
70 jthrowable previousException, newException;
71 jmethodID initException, getMessageID, initCauseID;
74 internalErrorClass = (*env)->FindClass (env, "java/lang/InternalError");
75 /** Just give up if this also fails. */
76 if (internalErrorClass == NULL)
79 previousException = (*env)->ExceptionOccurred (env);
81 if (previousException == NULL)
83 (*env)->ThrowNew (env, internalErrorClass,
84 "Unknown error raised by the VM");
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");
96 message = (*env)->CallObjectMethod (env, previousException, getMessageID);
98 newException = (*env)->NewObject (env, internalErrorClass, initException,
100 (*env)->CallVoidMethod (env, newException, initCauseID, previousException);
102 (*env)->ExceptionClear (env);
103 (*env)->Throw (env, newException);
107 getFieldReference (JNIEnv * env, jobject field, const char *type)
111 jclass declaringClass;
114 const char *field_name;
115 const char *type_name;
122 fieldClass = (*env)->GetObjectClass (env, field);
125 (*env)->GetMethodID (env, fieldClass, "getName", "()Ljava/lang/String;");
126 if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
128 throwInternalError (env);
132 name = (*env)->CallObjectMethod (env, field, mid);
133 field_name = (*env)->GetStringUTFChars (env, name, NULL);
135 mid = (*env)->GetMethodID (env, fieldClass,
136 "getDeclaringClass", "()Ljava/lang/Class;");
137 if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
139 throwInternalError (env);
143 declaringClass = (*env)->CallObjectMethod (env, field, mid);
145 /* Do we need to find out the exact type descriptor of the field? */
150 mid = (*env)->GetMethodID (env, fieldClass,
151 "getType", "()Ljava/lang/Class;");
153 if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
155 throwInternalError (env);
159 typeClass = (*env)->CallObjectMethod (env, field, mid);
160 classClass = (*env)->FindClass (env, "java/lang/Class");
162 mid = (*env)->GetMethodID (env, classClass,
163 "getName", "()Ljava/lang/String;");
165 if (mid == NULL || (*env)->ExceptionOccurred (env) != NULL)
167 throwInternalError (env);
171 tname = (*env)->CallObjectMethod (env, typeClass, mid);
172 type_name = (*env)->GetStringUTFChars (env, tname, NULL);
175 * If it isn't an array class then the actual field type descriptor
176 * starts with 'L', ends with ';' and has '/' instead of '.'.
178 type_len = strlen (type_name);
179 if (type_name[0] != '[')
181 /* XXX - FIXME - should not use dynamic allocation in core lib. */
182 the_type = (char *) malloc (type_len + 3);
184 the_type[type_len + 1] = ';';
185 the_type[type_len + 2] = '\0';
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';
195 for (i = 0; i < type_len; i++)
196 if (type_name[i] == '.')
199 the_type[i] = type_name[i];
201 if (type_name[0] != '[')
204 (*env)->ReleaseStringUTFChars (env, tname, type_name);
205 fid = (*env)->GetFieldID (env, declaringClass, field_name, the_type);
211 fid = (*env)->GetFieldID (env, declaringClass, field_name, type);
216 throwInternalError (env);
219 (*env)->ReleaseStringUTFChars (env, name, field_name);
225 * Class: java_io_VMObjectOutputStream
226 * Method: setBooleanNative
227 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;Z)V
229 JNIEXPORT void JNICALL
230 Java_java_io_VMObjectStreamClass_setBooleanNative (JNIEnv * env,
232 __attribute__ ((__unused__)), jobject field, jobject object, jboolean value)
234 jfieldID fid = getFieldReference (env, field, "Z");
237 (*env)->SetBooleanField (env, object, fid, value);
241 * Class: java_io_VMObjectOutputStream
242 * Method: setCharNative
243 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;C)V
245 JNIEXPORT void JNICALL
246 Java_java_io_VMObjectStreamClass_setCharNative (JNIEnv * env,
248 __attribute__ ((__unused__)),
250 jobject object, jchar value)
252 jfieldID fid = getFieldReference (env, field, "C");
255 (*env)->SetCharField (env, object, fid, value);
259 * Class: java_io_VMObjectOutputStream
260 * Method: setByteNative
261 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;B)V
263 JNIEXPORT void JNICALL
264 Java_java_io_VMObjectStreamClass_setByteNative (JNIEnv * env,
266 __attribute__ ((__unused__)),
268 jobject object, jbyte value)
270 jfieldID fid = getFieldReference (env, field, "B");
273 (*env)->SetByteField (env, object, fid, value);
278 * Class: java_io_VMObjectOutputStream
279 * Method: setShortNative
280 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;S)V
282 JNIEXPORT void JNICALL
283 Java_java_io_VMObjectStreamClass_setShortNative (JNIEnv * env,
285 __attribute__ ((__unused__)),
287 jobject object, jshort value)
289 jfieldID fid = getFieldReference (env, field, "S");
292 (*env)->SetShortField (env, object, fid, value);
296 * Class: java_io_VMObjectOutputStream
297 * Method: setIntNative
298 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;I)V
300 JNIEXPORT void JNICALL
301 Java_java_io_VMObjectStreamClass_setIntNative (JNIEnv * env,
303 __attribute__ ((__unused__)),
305 jobject object, jint value)
307 jfieldID fid = getFieldReference (env, field, "I");
310 (*env)->SetIntField (env, object, fid, value);
315 * Class: java_io_VMObjectOutputStream
316 * Method: setLongNative
317 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;J)V
319 JNIEXPORT void JNICALL
320 Java_java_io_VMObjectStreamClass_setLongNative (JNIEnv * env,
322 __attribute__ ((__unused__)),
324 jobject object, jlong value)
326 jfieldID fid = getFieldReference (env, field, "J");
329 (*env)->SetLongField (env, object, fid, value);
334 * Class: java_io_VMObjectOutputStream
335 * Method: setFloatNative
336 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;F)V
338 JNIEXPORT void JNICALL
339 Java_java_io_VMObjectStreamClass_setFloatNative (JNIEnv * env,
341 __attribute__ ((__unused__)),
343 jobject object, jfloat value)
345 jfieldID fid = getFieldReference (env, field, "F");
348 (*env)->SetFloatField (env, object, fid, value);
352 * Class: java_io_VMObjectOutputStream
353 * Method: setDoubleNative
354 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;D)V
356 JNIEXPORT void JNICALL
357 Java_java_io_VMObjectStreamClass_setDoubleNative (JNIEnv * env,
359 __attribute__ ((__unused__)), jobject field, jobject object, jdouble value)
361 jfieldID fid = getFieldReference (env, field, "D");
364 (*env)->SetDoubleField (env, object, fid, value);
368 * Class: java_io_VMObjectOutputStream
369 * Method: setObjectNative
370 * Signature: (Ljava/lang/reflect/Field;Ljava/lang/Object;Ljava/lang/Object;)V
372 JNIEXPORT void JNICALL
373 Java_java_io_VMObjectStreamClass_setObjectNative (JNIEnv * env,
375 __attribute__ ((__unused__)), jobject field, jobject object, jobject value)
377 jfieldID fid = getFieldReference (env, field, NULL);
380 (*env)->SetObjectField (env, object, fid, value);