natArray.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // natField.cc - Implementation of java.lang.reflect.Field native methods.
  2. /* Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #include <config.h>
  8. #include <stdlib.h>
  9. #include <jvm.h>
  10. #include <gcj/cni.h>
  11. #include <java-stack.h>
  12. #include <java/lang/reflect/Array.h>
  13. #include <java/lang/ArrayIndexOutOfBoundsException.h>
  14. #include <java/lang/IllegalArgumentException.h>
  15. #include <java/lang/Byte.h>
  16. #include <java/lang/Short.h>
  17. #include <java/lang/Integer.h>
  18. #include <java/lang/Long.h>
  19. #include <java/lang/Float.h>
  20. #include <java/lang/Double.h>
  21. #include <java/lang/Boolean.h>
  22. #include <java/lang/Character.h>
  23. jobject
  24. java::lang::reflect::Array::newInstance (jclass componentType, jint length)
  25. {
  26. if (componentType->isPrimitive())
  27. {
  28. // We could check for this in _Jv_NewPrimArray, but that seems
  29. // like needless overhead when the only real route to this
  30. // problem is here.
  31. if (componentType == JvPrimClass (void))
  32. throw new java::lang::IllegalArgumentException ();
  33. return _Jv_NewPrimArray (componentType, length);
  34. }
  35. else
  36. // FIXME: class loader?
  37. return JvNewObjectArray (length, componentType, NULL);
  38. }
  39. jobject
  40. java::lang::reflect::Array::newInstance (jclass componentType,
  41. jintArray dimensions)
  42. {
  43. jint ndims = dimensions->length;
  44. if (ndims == 0)
  45. throw new java::lang::IllegalArgumentException ();
  46. jint* dims = elements (dimensions);
  47. if (ndims == 1)
  48. return newInstance (componentType, dims[0]);
  49. Class *caller = _Jv_StackTrace::GetCallingClass (&Array::class$);
  50. ClassLoader *caller_loader = NULL;
  51. if (caller)
  52. caller_loader = caller->getClassLoaderInternal();
  53. jclass arrayType = componentType;
  54. for (int i = 0; i < ndims; i++)
  55. arrayType = _Jv_GetArrayClass (arrayType, caller_loader);
  56. return _Jv_NewMultiArray (arrayType, ndims, dims);
  57. }
  58. jint
  59. java::lang::reflect::Array::getLength (jobject array)
  60. {
  61. jclass arrayType = array->getClass();
  62. if (! arrayType->isArray ())
  63. throw new java::lang::IllegalArgumentException;
  64. return ((__JArray*) array)->length;
  65. }
  66. jclass
  67. java::lang::reflect::Array::getElementType (jobject array, jint index)
  68. {
  69. jclass arrayType = array->getClass();
  70. if (! arrayType->isArray ())
  71. throw new java::lang::IllegalArgumentException;
  72. jint length = ((__JArray*) array)->length;
  73. if ((_Jv_uint) index >= (_Jv_uint) length)
  74. _Jv_ThrowBadArrayIndex(index);
  75. return arrayType->getComponentType ();
  76. }
  77. jboolean
  78. java::lang::reflect::Array::getBoolean (jobject array, jint index)
  79. {
  80. jclass elementType = getElementType (array, index);
  81. if (elementType == JvPrimClass (boolean))
  82. return elements ((jbooleanArray) array) [index];
  83. throw new java::lang::IllegalArgumentException;
  84. }
  85. jchar
  86. java::lang::reflect::Array::getChar (jobject array, jint index)
  87. {
  88. jclass elementType = getElementType (array, index);
  89. if (elementType == JvPrimClass (char))
  90. return elements ((jcharArray) array) [index];
  91. throw new java::lang::IllegalArgumentException;
  92. }
  93. jbyte
  94. java::lang::reflect::Array::getByte (jobject array, jint index)
  95. {
  96. jclass elementType = getElementType (array, index);
  97. if (elementType == JvPrimClass (byte))
  98. return elements ((jbyteArray) array) [index];
  99. throw new java::lang::IllegalArgumentException;
  100. }
  101. jshort
  102. java::lang::reflect::Array::getShort (jobject array, jint index)
  103. {
  104. jclass elementType = getElementType (array, index);
  105. if (elementType == JvPrimClass (short))
  106. return elements ((jshortArray) array) [index];
  107. if (elementType == JvPrimClass (byte))
  108. return elements ((jbyteArray) array) [index];
  109. throw new java::lang::IllegalArgumentException;
  110. }
  111. jint
  112. java::lang::reflect::Array::getInt (jobject array, jint index)
  113. {
  114. jclass elementType = getElementType (array, index);
  115. if (elementType == JvPrimClass (int))
  116. return elements ((jintArray) array) [index];
  117. if (elementType == JvPrimClass (short))
  118. return elements ((jshortArray) array) [index];
  119. if (elementType == JvPrimClass (byte))
  120. return elements ((jbyteArray) array) [index];
  121. if (elementType == JvPrimClass (char))
  122. return elements ((jcharArray) array) [index];
  123. throw new java::lang::IllegalArgumentException;
  124. }
  125. jlong
  126. java::lang::reflect::Array::getLong (jobject array, jint index)
  127. {
  128. jclass elementType = getElementType (array, index);
  129. if (elementType == JvPrimClass (long))
  130. return elements ((jlongArray) array) [index];
  131. if (elementType == JvPrimClass (int))
  132. return elements ((jintArray) array) [index];
  133. if (elementType == JvPrimClass (short))
  134. return elements ((jshortArray) array) [index];
  135. if (elementType == JvPrimClass (byte))
  136. return elements ((jbyteArray) array) [index];
  137. if (elementType == JvPrimClass (char))
  138. return elements ((jcharArray) array) [index];
  139. throw new java::lang::IllegalArgumentException;
  140. }
  141. jfloat
  142. java::lang::reflect::Array::getFloat (jobject array, jint index)
  143. {
  144. jclass elementType = getElementType (array, index);
  145. if (elementType == JvPrimClass (float))
  146. return elements ((jfloatArray) array) [index];
  147. if (elementType == JvPrimClass (long))
  148. return elements ((jlongArray) array) [index];
  149. if (elementType == JvPrimClass (int))
  150. return elements ((jintArray) array) [index];
  151. if (elementType == JvPrimClass (short))
  152. return elements ((jshortArray) array) [index];
  153. if (elementType == JvPrimClass (byte))
  154. return elements ((jbyteArray) array) [index];
  155. if (elementType == JvPrimClass (char))
  156. return elements ((jcharArray) array) [index];
  157. throw new java::lang::IllegalArgumentException;
  158. }
  159. jdouble
  160. java::lang::reflect::Array::getDouble (jobject array, jint index)
  161. {
  162. jclass elementType = getElementType (array, index);
  163. if (elementType == JvPrimClass (double))
  164. return elements ((jdoubleArray) array) [index];
  165. if (elementType == JvPrimClass (float))
  166. return elements ((jfloatArray) array) [index];
  167. if (elementType == JvPrimClass (long))
  168. return elements ((jlongArray) array) [index];
  169. if (elementType == JvPrimClass (int))
  170. return elements ((jintArray) array) [index];
  171. if (elementType == JvPrimClass (short))
  172. return elements ((jshortArray) array) [index];
  173. if (elementType == JvPrimClass (byte))
  174. return elements ((jbyteArray) array) [index];
  175. if (elementType == JvPrimClass (char))
  176. return elements ((jcharArray) array) [index];
  177. throw new java::lang::IllegalArgumentException;
  178. }
  179. jobject
  180. java::lang::reflect::Array::get (jobject array, jint index)
  181. {
  182. jclass elementType = getElementType (array, index);
  183. if (! elementType->isPrimitive ())
  184. return elements ((jobjectArray) array) [index];
  185. if (elementType == JvPrimClass (double))
  186. return new java::lang::Double (elements ((jdoubleArray) array) [index]);
  187. if (elementType == JvPrimClass (float))
  188. return new java::lang::Float (elements ((jfloatArray) array) [index]);
  189. if (elementType == JvPrimClass (long))
  190. return new java::lang::Long (elements ((jlongArray) array) [index]);
  191. if (elementType == JvPrimClass (int))
  192. return new java::lang::Integer (elements ((jintArray) array) [index]);
  193. if (elementType == JvPrimClass (short))
  194. return new java::lang::Short (elements ((jshortArray) array) [index]);
  195. if (elementType == JvPrimClass (byte))
  196. return new java::lang::Byte (elements ((jbyteArray) array) [index]);
  197. if (elementType == JvPrimClass (char))
  198. return new java::lang::Character (elements ((jcharArray) array) [index]);
  199. if (elementType == JvPrimClass (boolean))
  200. {
  201. _Jv_InitClass (&java::lang::Boolean::class$);
  202. if (elements ((jbooleanArray) array) [index])
  203. return java::lang::Boolean::TRUE;
  204. else
  205. return java::lang::Boolean::FALSE;
  206. }
  207. throw new java::lang::IllegalArgumentException;
  208. }
  209. void
  210. java::lang::reflect::Array::setChar (jobject array, jint index, jchar value)
  211. {
  212. jclass elementType = getElementType (array, index);
  213. if (elementType == JvPrimClass (char))
  214. elements ((jcharArray) array) [index] = value;
  215. else if (elementType == JvPrimClass (int))
  216. elements ((jintArray) array) [index] = value;
  217. else if (elementType == JvPrimClass (long))
  218. elements ((jlongArray) array) [index] = value;
  219. else if (elementType == JvPrimClass (float))
  220. elements ((jfloatArray) array) [index] = value;
  221. else if (elementType == JvPrimClass (double))
  222. elements ((jdoubleArray) array) [index] = value;
  223. else
  224. throw new java::lang::IllegalArgumentException;
  225. }
  226. void
  227. java::lang::reflect::Array::setByte (jobject array, jint index, jbyte value)
  228. {
  229. jclass elementType = getElementType (array, index);
  230. if (elementType == JvPrimClass (byte))
  231. elements ((jbyteArray) array) [index] = value;
  232. else if (elementType == JvPrimClass (short))
  233. elements ((jshortArray) array) [index] = value;
  234. else if (elementType == JvPrimClass (int))
  235. elements ((jintArray) array) [index] = value;
  236. else if (elementType == JvPrimClass (long))
  237. elements ((jlongArray) array) [index] = value;
  238. else if (elementType == JvPrimClass (float))
  239. elements ((jfloatArray) array) [index] = value;
  240. else if (elementType == JvPrimClass (double))
  241. elements ((jdoubleArray) array) [index] = value;
  242. else
  243. throw new java::lang::IllegalArgumentException;
  244. }
  245. void
  246. java::lang::reflect::Array::setShort (jobject array, jint index, jshort value)
  247. {
  248. jclass elementType = getElementType (array, index);
  249. if (elementType == JvPrimClass (short))
  250. elements ((jshortArray) array) [index] = value;
  251. else if (elementType == JvPrimClass (int))
  252. elements ((jintArray) array) [index] = value;
  253. else if (elementType == JvPrimClass (long))
  254. elements ((jlongArray) array) [index] = value;
  255. else if (elementType == JvPrimClass (float))
  256. elements ((jfloatArray) array) [index] = value;
  257. else if (elementType == JvPrimClass (double))
  258. elements ((jdoubleArray) array) [index] = value;
  259. else
  260. throw new java::lang::IllegalArgumentException;
  261. }
  262. void
  263. java::lang::reflect::Array::setInt (jobject array, jint index, jint value)
  264. {
  265. jclass elementType = getElementType (array, index);
  266. if (elementType == JvPrimClass (int))
  267. elements ((jintArray) array) [index] = value;
  268. else if (elementType == JvPrimClass (long))
  269. elements ((jlongArray) array) [index] = value;
  270. else if (elementType == JvPrimClass (float))
  271. elements ((jfloatArray) array) [index] = value;
  272. else if (elementType == JvPrimClass (double))
  273. elements ((jdoubleArray) array) [index] = value;
  274. else
  275. throw new java::lang::IllegalArgumentException;
  276. }
  277. void
  278. java::lang::reflect::Array::setLong (jobject array, jint index, jlong value)
  279. {
  280. jclass elementType = getElementType (array, index);
  281. if (elementType == JvPrimClass (long))
  282. elements ((jlongArray) array) [index] = value;
  283. else if (elementType == JvPrimClass (float))
  284. elements ((jfloatArray) array) [index] = value;
  285. else if (elementType == JvPrimClass (double))
  286. elements ((jdoubleArray) array) [index] = value;
  287. else
  288. throw new java::lang::IllegalArgumentException;
  289. }
  290. void
  291. java::lang::reflect::Array::setFloat (jobject array, jint index, jfloat value)
  292. {
  293. jclass elementType = getElementType (array, index);
  294. if (elementType == JvPrimClass (float))
  295. elements ((jfloatArray) array) [index] = value;
  296. else if (elementType == JvPrimClass (double))
  297. elements ((jdoubleArray) array) [index] = value;
  298. else
  299. throw new java::lang::IllegalArgumentException;
  300. }
  301. void
  302. java::lang::reflect::Array::setDouble (jobject array, jint index, jdouble value)
  303. {
  304. jclass elementType = getElementType (array, index);
  305. if (elementType == JvPrimClass (double))
  306. elements ((jdoubleArray) array) [index] = value;
  307. else
  308. throw new java::lang::IllegalArgumentException;
  309. }
  310. void
  311. java::lang::reflect::Array::setBoolean (jobject array,
  312. jint index, jboolean value)
  313. {
  314. jclass elementType = getElementType (array, index);
  315. if (elementType == JvPrimClass (boolean))
  316. elements ((jbooleanArray) array) [index] = value;
  317. else
  318. throw new java::lang::IllegalArgumentException;
  319. }
  320. void
  321. java::lang::reflect::Array::set (jobject array, jint index,
  322. jobject value, jclass elType)
  323. {
  324. // We don't have to call getElementType here, or check INDEX,
  325. // because it was already done in the Java wrapper.
  326. if (value != NULL && ! _Jv_IsInstanceOf (value, elType))
  327. throw new java::lang::IllegalArgumentException;
  328. elements ((jobjectArray) array) [index] = value;
  329. }