Array.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* java.lang.reflect.Array - manipulate arrays by reflection
  2. Copyright (C) 1998, 1999, 2001, 2003, 2005, 2007 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. package java.lang.reflect;
  32. import gnu.classpath.Configuration;
  33. /**
  34. * Array holds static helper functions that allow you to create and
  35. * manipulate arrays by reflection. Operations know how to perform widening
  36. * conversions, but throw {@link IllegalArgumentException} if you attempt
  37. * a narrowing conversion. Also, when accessing primitive arrays, this
  38. * class performs object wrapping and unwrapping as necessary.<p>
  39. *
  40. * <B>Note:</B> This class returns and accepts types as Classes, even
  41. * primitive types; there are Class types defined that represent each
  42. * different primitive type. They are <code>java.lang.Boolean.TYPE,
  43. * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
  44. * byte.class</code>, etc. These are not to be confused with the
  45. * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
  46. * real classes. Note also that the shorthand <code>Object[].class</code>
  47. * is a convenient way to get array Classes.<p>
  48. *
  49. * <B>Performance note:</B> This class performs best when it does not have
  50. * to convert primitive types. The further along the chain it has to convert,
  51. * the worse performance will be. You're best off using the array as whatever
  52. * type it already is, and then converting the result. You will do even
  53. * worse if you do this and use the generic set() function.
  54. *
  55. * @author John Keiser
  56. * @author Eric Blake (ebb9@email.byu.edu)
  57. * @author Per Bothner (bothner@cygnus.com)
  58. * @see java.lang.Boolean#TYPE
  59. * @see java.lang.Byte#TYPE
  60. * @see java.lang.Short#TYPE
  61. * @see java.lang.Character#TYPE
  62. * @see java.lang.Integer#TYPE
  63. * @see java.lang.Long#TYPE
  64. * @see java.lang.Float#TYPE
  65. * @see java.lang.Double#TYPE
  66. * @since 1.1
  67. * @status updated to 1.4
  68. */
  69. public final class Array
  70. {
  71. static
  72. {
  73. if (Configuration.INIT_LOAD_LIBRARY)
  74. {
  75. System.loadLibrary("javalangreflect");
  76. }
  77. }
  78. /**
  79. * This class is uninstantiable.
  80. */
  81. private Array()
  82. {
  83. }
  84. /**
  85. * Creates a new single-dimensioned array.
  86. * @param componentType the type of the array to create
  87. * @param length the length of the array to create
  88. * @return the created array, cast to an Object
  89. * @throws NullPointerException if <code>componentType</code> is null
  90. * @throws IllegalArgumentException if <code>componentType</code> is
  91. * <code>Void.TYPE</code>
  92. * @throws NegativeArraySizeException when length is less than 0
  93. * @throws OutOfMemoryError if memory allocation fails
  94. */
  95. public static native Object newInstance(Class<?> componentType, int length);
  96. /**
  97. * Creates a new multi-dimensioned array. The new array has the same
  98. * component type as the argument class, and the number of dimensions
  99. * in the new array is the sum of the dimensions of the argument class
  100. * and the length of the argument dimensions. Virtual Machine limitations
  101. * forbid too many dimensions (usually 255 is the maximum); but even
  102. * 50 dimensions of 2 elements in each dimension would exceed your memory
  103. * long beforehand!
  104. *
  105. * @param componentType the type of the array to create.
  106. * @param dimensions the dimensions of the array to create. Each element
  107. * in <code>dimensions</code> makes another dimension of the new
  108. * array. Thus, <code>Array.newInstance(java.lang.Boolean,
  109. * new int[]{1,2,3})</code> is the same as
  110. * <code>new java.lang.Boolean[1][2][3]</code>
  111. * @return the created array, cast to an Object
  112. * @throws NullPointerException if componentType or dimension is null
  113. * @throws IllegalArgumentException if the the size of
  114. * <code>dimensions</code> is 0 or exceeds the maximum number of
  115. * array dimensions in the VM; or if componentType is Void.TYPE
  116. * @throws NegativeArraySizeException when any of the dimensions is less
  117. * than 0
  118. * @throws OutOfMemoryError if memory allocation fails
  119. */
  120. public static native Object newInstance(Class<?> elementType, int[] dimensions);
  121. /**
  122. * Gets the array length.
  123. * @param array the array
  124. * @return the length of the array
  125. * @throws IllegalArgumentException if <code>array</code> is not an array
  126. * @throws NullPointerException if <code>array</code> is null
  127. */
  128. public static native int getLength(Object array);
  129. /**
  130. * Gets an element of an array. Primitive elements will be wrapped in
  131. * the corresponding class type.
  132. *
  133. * @param array the array to access
  134. * @param index the array index to access
  135. * @return the element at <code>array[index]</code>
  136. * @throws IllegalArgumentException if <code>array</code> is not an array
  137. * @throws NullPointerException if <code>array</code> is null
  138. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  139. * bounds
  140. * @see #getBoolean(Object, int)
  141. * @see #getByte(Object, int)
  142. * @see #getChar(Object, int)
  143. * @see #getShort(Object, int)
  144. * @see #getInt(Object, int)
  145. * @see #getLong(Object, int)
  146. * @see #getFloat(Object, int)
  147. * @see #getDouble(Object, int)
  148. */
  149. public static native Object get(Object array, int index);
  150. /**
  151. * Gets an element of a boolean array.
  152. *
  153. * @param array the array to access
  154. * @param index the array index to access
  155. * @return the boolean element at <code>array[index]</code>
  156. * @throws IllegalArgumentException if <code>array</code> is not a boolean
  157. * array
  158. * @throws NullPointerException if <code>array</code> is null
  159. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  160. * bounds
  161. * @see #get(Object, int)
  162. */
  163. public static native boolean getBoolean(Object array, int index);
  164. /**
  165. * Gets an element of a byte array.
  166. *
  167. * @param array the array to access
  168. * @param index the array index to access
  169. * @return the byte element at <code>array[index]</code>
  170. * @throws IllegalArgumentException if <code>array</code> is not a byte
  171. * array
  172. * @throws NullPointerException if <code>array</code> is null
  173. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  174. * bounds
  175. * @see #get(Object, int)
  176. */
  177. public static native byte getByte(Object array, int index);
  178. /**
  179. * Gets an element of a char array.
  180. *
  181. * @param array the array to access
  182. * @param index the array index to access
  183. * @return the char element at <code>array[index]</code>
  184. * @throws IllegalArgumentException if <code>array</code> is not a char
  185. * array
  186. * @throws NullPointerException if <code>array</code> is null
  187. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  188. * bounds
  189. * @see #get(Object, int)
  190. */
  191. public static native char getChar(Object array, int index);
  192. /**
  193. * Gets an element of a short array.
  194. *
  195. * @param array the array to access
  196. * @param index the array index to access
  197. * @return the short element at <code>array[index]</code>
  198. * @throws IllegalArgumentException if <code>array</code> is not a byte
  199. * or char array
  200. * @throws NullPointerException if <code>array</code> is null
  201. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  202. * bounds
  203. * @see #get(Object, int)
  204. */
  205. public static native short getShort(Object array, int index);
  206. /**
  207. * Gets an element of an int array.
  208. *
  209. * @param array the array to access
  210. * @param index the array index to access
  211. * @return the int element at <code>array[index]</code>
  212. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  213. * char, short, or int array
  214. * @throws NullPointerException if <code>array</code> is null
  215. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  216. * bounds
  217. * @see #get(Object, int)
  218. */
  219. public static native int getInt(Object array, int index);
  220. /**
  221. * Gets an element of a long array.
  222. *
  223. * @param array the array to access
  224. * @param index the array index to access
  225. * @return the long element at <code>array[index]</code>
  226. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  227. * char, short, int, or long array
  228. * @throws NullPointerException if <code>array</code> is null
  229. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  230. * bounds
  231. * @see #get(Object, int)
  232. */
  233. public static native long getLong(Object array, int index);
  234. /**
  235. * Gets an element of a float array.
  236. *
  237. * @param array the array to access
  238. * @param index the array index to access
  239. * @return the float element at <code>array[index]</code>
  240. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  241. * char, short, int, long, or float array
  242. * @throws NullPointerException if <code>array</code> is null
  243. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  244. * bounds
  245. * @see #get(Object, int)
  246. */
  247. public static native float getFloat(Object array, int index);
  248. /**
  249. * Gets an element of a double array.
  250. *
  251. * @param array the array to access
  252. * @param index the array index to access
  253. * @return the double element at <code>array[index]</code>
  254. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  255. * char, short, int, long, float, or double array
  256. * @throws NullPointerException if <code>array</code> is null
  257. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  258. * bounds
  259. * @see #get(Object, int)
  260. */
  261. public static native double getDouble(Object array, int index);
  262. private static native Class getElementType(Object array, int index);
  263. private static native void set(Object array, int index,
  264. Object value, Class elType);
  265. /**
  266. * Sets an element of an array. If the array is primitive, then the new
  267. * value is unwrapped and widened.
  268. *
  269. * @param array the array to set a value of
  270. * @param index the array index to set the value to
  271. * @param value the value to set
  272. * @throws IllegalArgumentException if <code>array</code> is not an array,
  273. * or the array is primitive and unwrapping value fails, or the
  274. * value is not assignable to the array component type
  275. * @throws NullPointerException if array is null, or if array is primitive
  276. * and value is null
  277. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  278. * bounds
  279. * @see #setBoolean(Object, int, boolean)
  280. * @see #setByte(Object, int, byte)
  281. * @see #setChar(Object, int, char)
  282. * @see #setShort(Object, int, short)
  283. * @see #setInt(Object, int, int)
  284. * @see #setLong(Object, int, long)
  285. * @see #setFloat(Object, int, float)
  286. * @see #setDouble(Object, int, double)
  287. */
  288. public static void set(Object array, int index, Object value)
  289. {
  290. Class elType = getElementType(array, index);
  291. if (! elType.isPrimitive())
  292. set(array, index, value, elType);
  293. else if (value instanceof Byte)
  294. setByte(array, index, ((Byte) value).byteValue());
  295. else if (value instanceof Short)
  296. setShort(array, index, ((Short) value).shortValue());
  297. else if (value instanceof Integer)
  298. setInt(array, index, ((Integer) value).intValue());
  299. else if (value instanceof Long)
  300. setLong(array, index, ((Long) value).longValue());
  301. else if (value instanceof Float)
  302. setFloat(array, index, ((Float) value).floatValue());
  303. else if (value instanceof Double)
  304. setDouble(array, index, ((Double) value).doubleValue());
  305. else if (value instanceof Character)
  306. setChar(array, index, ((Character) value).charValue());
  307. else if (value instanceof Boolean)
  308. setBoolean(array, index, ((Boolean) value).booleanValue());
  309. else
  310. throw new IllegalArgumentException();
  311. }
  312. /**
  313. * Sets an element of a boolean array.
  314. *
  315. * @param array the array to set a value of
  316. * @param index the array index to set the value to
  317. * @param value the value to set
  318. * @throws IllegalArgumentException if <code>array</code> is not a boolean
  319. * array
  320. * @throws NullPointerException if <code>array</code> is null
  321. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  322. * bounds
  323. * @see #set(Object, int, Object)
  324. */
  325. public static native void setBoolean(Object array, int index, boolean value);
  326. /**
  327. * Sets an element of a byte array.
  328. *
  329. * @param array the array to set a value of
  330. * @param index the array index to set the value to
  331. * @param value the value to set
  332. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  333. * short, int, long, float, or double array
  334. * @throws NullPointerException if <code>array</code> is null
  335. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  336. * bounds
  337. * @see #set(Object, int, Object)
  338. */
  339. public static native void setByte(Object array, int index, byte value);
  340. /**
  341. * Sets an element of a char array.
  342. *
  343. * @param array the array to set a value of
  344. * @param index the array index to set the value to
  345. * @param value the value to set
  346. * @throws IllegalArgumentException if <code>array</code> is not a char,
  347. * int, long, float, or double array
  348. * @throws NullPointerException if <code>array</code> is null
  349. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  350. * bounds
  351. * @see #set(Object, int, Object)
  352. */
  353. public static native void setChar(Object array, int index, char value);
  354. /**
  355. * Sets an element of a short array.
  356. *
  357. * @param array the array to set a value of
  358. * @param index the array index to set the value to
  359. * @param value the value to set
  360. * @throws IllegalArgumentException if <code>array</code> is not a short,
  361. * int, long, float, or double array
  362. * @throws NullPointerException if <code>array</code> is null
  363. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  364. * bounds
  365. * @see #set(Object, int, Object)
  366. */
  367. public static native void setShort(Object array, int index, short value);
  368. /**
  369. * Sets an element of an int array.
  370. *
  371. * @param array the array to set a value of
  372. * @param index the array index to set the value to
  373. * @param value the value to set
  374. * @throws IllegalArgumentException if <code>array</code> is not an int,
  375. * long, float, or double array
  376. * @throws NullPointerException if <code>array</code> is null
  377. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  378. * bounds
  379. * @see #set(Object, int, Object)
  380. */
  381. public static native void setInt(Object array, int index, int value);
  382. /**
  383. * Sets an element of a long array.
  384. *
  385. * @param array the array to set a value of
  386. * @param index the array index to set the value to
  387. * @param value the value to set
  388. * @throws IllegalArgumentException if <code>array</code> is not a long,
  389. * float, or double array
  390. * @throws NullPointerException if <code>array</code> is null
  391. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  392. * bounds
  393. * @see #set(Object, int, Object)
  394. */
  395. public static native void setLong(Object array, int index, long value);
  396. /**
  397. * Sets an element of a float array.
  398. *
  399. * @param array the array to set a value of
  400. * @param index the array index to set the value to
  401. * @param value the value to set
  402. * @throws IllegalArgumentException if <code>array</code> is not a float
  403. * or double array
  404. * @throws NullPointerException if <code>array</code> is null
  405. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  406. * bounds
  407. * @see #set(Object, int, Object)
  408. */
  409. public static native void setFloat(Object array, int index, float value);
  410. /**
  411. * Sets an element of a double array.
  412. *
  413. * @param array the array to set a value of
  414. * @param index the array index to set the value to
  415. * @param value the value to set
  416. * @throws IllegalArgumentException if <code>array</code> is not a double
  417. * array
  418. * @throws NullPointerException if <code>array</code> is null
  419. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  420. * bounds
  421. * @see #set(Object, int, Object)
  422. */
  423. public static native void setDouble(Object array, int index, double value);
  424. }