java_io_VMObjectStreamClass.c 11 KB

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