Array.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* java.lang.reflect.Array - manipulate arrays by reflection
  2. Copyright (C) 1998, 1999, 2001, 2003, 2005 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. /**
  33. * Array holds static helper functions that allow you to create and
  34. * manipulate arrays by reflection. Operations know how to perform widening
  35. * conversions, but throw {@link IllegalArgumentException} if you attempt
  36. * a narrowing conversion. Also, when accessing primitive arrays, this
  37. * class performs object wrapping and unwrapping as necessary.<p>
  38. *
  39. * <B>Note:</B> This class returns and accepts types as Classes, even
  40. * primitive types; there are Class types defined that represent each
  41. * different primitive type. They are <code>java.lang.Boolean.TYPE,
  42. * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
  43. * byte.class</code>, etc. These are not to be confused with the
  44. * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
  45. * real classes. Note also that the shorthand <code>Object[].class</code>
  46. * is a convenient way to get array Classes.<p>
  47. *
  48. * <B>Performance note:</B> This class performs best when it does not have
  49. * to convert primitive types. The further along the chain it has to convert,
  50. * the worse performance will be. You're best off using the array as whatever
  51. * type it already is, and then converting the result. You will do even
  52. * worse if you do this and use the generic set() function.
  53. *
  54. * @author John Keiser
  55. * @author Eric Blake (ebb9@email.byu.edu)
  56. * @author Per Bothner (bothner@cygnus.com)
  57. * @see java.lang.Boolean#TYPE
  58. * @see java.lang.Byte#TYPE
  59. * @see java.lang.Short#TYPE
  60. * @see java.lang.Character#TYPE
  61. * @see java.lang.Integer#TYPE
  62. * @see java.lang.Long#TYPE
  63. * @see java.lang.Float#TYPE
  64. * @see java.lang.Double#TYPE
  65. * @since 1.1
  66. * @status updated to 1.4
  67. */
  68. public final class Array
  69. {
  70. /**
  71. * This class is uninstantiable.
  72. */
  73. private Array()
  74. {
  75. }
  76. /**
  77. * Creates a new single-dimensioned array.
  78. * @param componentType the type of the array to create
  79. * @param length the length of the array to create
  80. * @return the created array, cast to an Object
  81. * @throws NullPointerException if <code>componentType</code> is null
  82. * @throws IllegalArgumentException if <code>componentType</code> is
  83. * <code>Void.TYPE</code>
  84. * @throws NegativeArraySizeException when length is less than 0
  85. * @throws OutOfMemoryError if memory allocation fails
  86. */
  87. public static Object newInstance(Class<?> componentType, int length)
  88. {
  89. if (! componentType.isPrimitive())
  90. return VMArray.createObjectArray(componentType, length);
  91. if (componentType == boolean.class)
  92. return new boolean[length];
  93. if (componentType == byte.class)
  94. return new byte[length];
  95. if (componentType == char.class)
  96. return new char[length];
  97. if (componentType == short.class)
  98. return new short[length];
  99. if (componentType == int.class)
  100. return new int[length];
  101. if (componentType == long.class)
  102. return new long[length];
  103. if (componentType == float.class)
  104. return new float[length];
  105. if (componentType == double.class)
  106. return new double[length];
  107. // assert componentType == void.class
  108. throw new IllegalArgumentException();
  109. }
  110. /**
  111. * Creates a new multi-dimensioned array. The new array has the same
  112. * component type as the argument class, and the number of dimensions
  113. * in the new array is the sum of the dimensions of the argument class
  114. * and the length of the argument dimensions. Virtual Machine limitations
  115. * forbid too many dimensions (usually 255 is the maximum); but even
  116. * 50 dimensions of 2 elements in each dimension would exceed your memory
  117. * long beforehand!
  118. *
  119. * @param componentType the type of the array to create.
  120. * @param dimensions the dimensions of the array to create. Each element
  121. * in <code>dimensions</code> makes another dimension of the new
  122. * array. Thus, <code>Array.newInstance(java.lang.Boolean,
  123. * new int[]{1,2,3})</code> is the same as
  124. * <code>new java.lang.Boolean[1][2][3]</code>
  125. * @return the created array, cast to an Object
  126. * @throws NullPointerException if componentType or dimension is null
  127. * @throws IllegalArgumentException if the the size of
  128. * <code>dimensions</code> is 0 or exceeds the maximum number of
  129. * array dimensions in the VM; or if componentType is Void.TYPE
  130. * @throws NegativeArraySizeException when any of the dimensions is less
  131. * than 0
  132. * @throws OutOfMemoryError if memory allocation fails
  133. */
  134. public static Object newInstance(Class<?> componentType, int[] dimensions)
  135. {
  136. if (dimensions.length <= 0)
  137. throw new IllegalArgumentException ("Empty dimensions array.");
  138. return createMultiArray(componentType, dimensions, 0);
  139. }
  140. /**
  141. * Gets the array length.
  142. * @param array the array
  143. * @return the length of the array
  144. * @throws IllegalArgumentException if <code>array</code> is not an array
  145. * @throws NullPointerException if <code>array</code> is null
  146. */
  147. public static int getLength(Object array)
  148. {
  149. if (array instanceof Object[])
  150. return ((Object[]) array).length;
  151. if (array instanceof boolean[])
  152. return ((boolean[]) array).length;
  153. if (array instanceof byte[])
  154. return ((byte[]) array). length;
  155. if (array instanceof char[])
  156. return ((char[]) array).length;
  157. if (array instanceof short[])
  158. return ((short[]) array).length;
  159. if (array instanceof int[])
  160. return ((int[]) array).length;
  161. if (array instanceof long[])
  162. return ((long[]) array).length;
  163. if (array instanceof float[])
  164. return ((float[]) array).length;
  165. if (array instanceof double[])
  166. return ((double[]) array).length;
  167. if (array == null)
  168. throw new NullPointerException();
  169. throw new IllegalArgumentException();
  170. }
  171. /**
  172. * Gets an element of an array. Primitive elements will be wrapped in
  173. * the corresponding class type.
  174. *
  175. * @param array the array to access
  176. * @param index the array index to access
  177. * @return the element at <code>array[index]</code>
  178. * @throws IllegalArgumentException if <code>array</code> is not an array
  179. * @throws NullPointerException if <code>array</code> is null
  180. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  181. * bounds
  182. * @see #getBoolean(Object, int)
  183. * @see #getByte(Object, int)
  184. * @see #getChar(Object, int)
  185. * @see #getShort(Object, int)
  186. * @see #getInt(Object, int)
  187. * @see #getLong(Object, int)
  188. * @see #getFloat(Object, int)
  189. * @see #getDouble(Object, int)
  190. */
  191. public static Object get(Object array, int index)
  192. {
  193. if (array instanceof Object[])
  194. return ((Object[]) array)[index];
  195. if (array instanceof boolean[])
  196. return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE;
  197. if (array instanceof byte[])
  198. return Byte.valueOf(((byte[]) array)[index]);
  199. if (array instanceof char[])
  200. return Character.valueOf(((char[]) array)[index]);
  201. if (array instanceof short[])
  202. return Short.valueOf(((short[]) array)[index]);
  203. if (array instanceof int[])
  204. return Integer.valueOf(((int[]) array)[index]);
  205. if (array instanceof long[])
  206. return Long.valueOf(((long[]) array)[index]);
  207. if (array instanceof float[])
  208. return Float.valueOf(((float[]) array)[index]);
  209. if (array instanceof double[])
  210. return Double.valueOf(((double[]) array)[index]);
  211. if (array == null)
  212. throw new NullPointerException();
  213. throw new IllegalArgumentException();
  214. }
  215. /**
  216. * Gets an element of a boolean array.
  217. *
  218. * @param array the array to access
  219. * @param index the array index to access
  220. * @return the boolean element at <code>array[index]</code>
  221. * @throws IllegalArgumentException if <code>array</code> is not a boolean
  222. * array
  223. * @throws NullPointerException if <code>array</code> is null
  224. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  225. * bounds
  226. * @see #get(Object, int)
  227. */
  228. public static boolean getBoolean(Object array, int index)
  229. {
  230. if (array instanceof boolean[])
  231. return ((boolean[]) array)[index];
  232. if (array == null)
  233. throw new NullPointerException();
  234. throw new IllegalArgumentException();
  235. }
  236. /**
  237. * Gets an element of a byte array.
  238. *
  239. * @param array the array to access
  240. * @param index the array index to access
  241. * @return the byte element at <code>array[index]</code>
  242. * @throws IllegalArgumentException if <code>array</code> is not a byte
  243. * array
  244. * @throws NullPointerException if <code>array</code> is null
  245. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  246. * bounds
  247. * @see #get(Object, int)
  248. */
  249. public static byte getByte(Object array, int index)
  250. {
  251. if (array instanceof byte[])
  252. return ((byte[]) array)[index];
  253. if (array == null)
  254. throw new NullPointerException();
  255. throw new IllegalArgumentException();
  256. }
  257. /**
  258. * Gets an element of a char array.
  259. *
  260. * @param array the array to access
  261. * @param index the array index to access
  262. * @return the char element at <code>array[index]</code>
  263. * @throws IllegalArgumentException if <code>array</code> is not a char
  264. * array
  265. * @throws NullPointerException if <code>array</code> is null
  266. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  267. * bounds
  268. * @see #get(Object, int)
  269. */
  270. public static char getChar(Object array, int index)
  271. {
  272. if (array instanceof char[])
  273. return ((char[]) array)[index];
  274. if (array == null)
  275. throw new NullPointerException();
  276. throw new IllegalArgumentException();
  277. }
  278. /**
  279. * Gets an element of a short array.
  280. *
  281. * @param array the array to access
  282. * @param index the array index to access
  283. * @return the short element at <code>array[index]</code>
  284. * @throws IllegalArgumentException if <code>array</code> is not a byte
  285. * or char array
  286. * @throws NullPointerException if <code>array</code> is null
  287. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  288. * bounds
  289. * @see #get(Object, int)
  290. */
  291. public static short getShort(Object array, int index)
  292. {
  293. if (array instanceof short[])
  294. return ((short[]) array)[index];
  295. return getByte(array, index);
  296. }
  297. /**
  298. * Gets an element of an int array.
  299. *
  300. * @param array the array to access
  301. * @param index the array index to access
  302. * @return the int element at <code>array[index]</code>
  303. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  304. * char, short, or int array
  305. * @throws NullPointerException if <code>array</code> is null
  306. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  307. * bounds
  308. * @see #get(Object, int)
  309. */
  310. public static int getInt(Object array, int index)
  311. {
  312. if (array instanceof int[])
  313. return ((int[]) array)[index];
  314. if (array instanceof char[])
  315. return ((char[]) array)[index];
  316. return getShort(array, index);
  317. }
  318. /**
  319. * Gets an element of a long array.
  320. *
  321. * @param array the array to access
  322. * @param index the array index to access
  323. * @return the long element at <code>array[index]</code>
  324. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  325. * char, short, int, or long array
  326. * @throws NullPointerException if <code>array</code> is null
  327. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  328. * bounds
  329. * @see #get(Object, int)
  330. */
  331. public static long getLong(Object array, int index)
  332. {
  333. if (array instanceof long[])
  334. return ((long[]) array)[index];
  335. return getInt(array, index);
  336. }
  337. /**
  338. * Gets an element of a float array.
  339. *
  340. * @param array the array to access
  341. * @param index the array index to access
  342. * @return the float element at <code>array[index]</code>
  343. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  344. * char, short, int, long, or float array
  345. * @throws NullPointerException if <code>array</code> is null
  346. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  347. * bounds
  348. * @see #get(Object, int)
  349. */
  350. public static float getFloat(Object array, int index)
  351. {
  352. if (array instanceof float[])
  353. return ((float[]) array)[index];
  354. return getLong(array, index);
  355. }
  356. /**
  357. * Gets an element of a double array.
  358. *
  359. * @param array the array to access
  360. * @param index the array index to access
  361. * @return the double element at <code>array[index]</code>
  362. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  363. * char, short, int, long, float, or double array
  364. * @throws NullPointerException if <code>array</code> is null
  365. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  366. * bounds
  367. * @see #get(Object, int)
  368. */
  369. public static double getDouble(Object array, int index)
  370. {
  371. if (array instanceof double[])
  372. return ((double[]) array)[index];
  373. return getFloat(array, index);
  374. }
  375. /**
  376. * Sets an element of an array. If the array is primitive, then the new
  377. * value is unwrapped and widened.
  378. *
  379. * @param array the array to set a value of
  380. * @param index the array index to set the value to
  381. * @param value the value to set
  382. * @throws IllegalArgumentException if <code>array</code> is not an array,
  383. * or the array is primitive and unwrapping value fails, or the
  384. * value is not assignable to the array component type
  385. * @throws NullPointerException if array is null, or if array is primitive
  386. * and value is null
  387. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  388. * bounds
  389. * @see #setBoolean(Object, int, boolean)
  390. * @see #setByte(Object, int, byte)
  391. * @see #setChar(Object, int, char)
  392. * @see #setShort(Object, int, short)
  393. * @see #setInt(Object, int, int)
  394. * @see #setLong(Object, int, long)
  395. * @see #setFloat(Object, int, float)
  396. * @see #setDouble(Object, int, double)
  397. */
  398. public static void set(Object array, int index, Object value)
  399. {
  400. if (array instanceof Object[])
  401. {
  402. // Too bad the API won't let us throw the easier ArrayStoreException!
  403. if (value != null
  404. && ! array.getClass().getComponentType().isInstance(value))
  405. throw new IllegalArgumentException();
  406. ((Object[]) array)[index] = value;
  407. }
  408. else if (value instanceof Byte)
  409. setByte(array, index, ((Byte) value).byteValue());
  410. else if (value instanceof Short)
  411. setShort(array, index, ((Short) value).shortValue());
  412. else if (value instanceof Integer)
  413. setInt(array, index, ((Integer) value).intValue());
  414. else if (value instanceof Long)
  415. setLong(array, index, ((Long) value).longValue());
  416. else if (value instanceof Float)
  417. setFloat(array, index, ((Float) value).floatValue());
  418. else if (value instanceof Double)
  419. setDouble(array, index, ((Double) value).doubleValue());
  420. else if (value instanceof Character)
  421. setChar(array, index, ((Character) value).charValue());
  422. else if (value instanceof Boolean)
  423. setBoolean(array, index, ((Boolean) value).booleanValue());
  424. else if (array == null)
  425. throw new NullPointerException();
  426. else
  427. throw new IllegalArgumentException();
  428. }
  429. /**
  430. * Sets an element of a boolean array.
  431. *
  432. * @param array the array to set a value of
  433. * @param index the array index to set the value to
  434. * @param value the value to set
  435. * @throws IllegalArgumentException if <code>array</code> is not a boolean
  436. * array
  437. * @throws NullPointerException if <code>array</code> is null
  438. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  439. * bounds
  440. * @see #set(Object, int, Object)
  441. */
  442. public static void setBoolean(Object array, int index, boolean value)
  443. {
  444. if (array instanceof boolean[])
  445. ((boolean[]) array)[index] = value;
  446. else if (array == null)
  447. throw new NullPointerException();
  448. else
  449. throw new IllegalArgumentException();
  450. }
  451. /**
  452. * Sets an element of a byte array.
  453. *
  454. * @param array the array to set a value of
  455. * @param index the array index to set the value to
  456. * @param value the value to set
  457. * @throws IllegalArgumentException if <code>array</code> is not a byte,
  458. * short, int, long, float, or double array
  459. * @throws NullPointerException if <code>array</code> is null
  460. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  461. * bounds
  462. * @see #set(Object, int, Object)
  463. */
  464. public static void setByte(Object array, int index, byte value)
  465. {
  466. if (array instanceof byte[])
  467. ((byte[]) array)[index] = value;
  468. else
  469. setShort(array, index, value);
  470. }
  471. /**
  472. * Sets an element of a char array.
  473. *
  474. * @param array the array to set a value of
  475. * @param index the array index to set the value to
  476. * @param value the value to set
  477. * @throws IllegalArgumentException if <code>array</code> is not a char,
  478. * int, long, float, or double array
  479. * @throws NullPointerException if <code>array</code> is null
  480. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  481. * bounds
  482. * @see #set(Object, int, Object)
  483. */
  484. public static void setChar(Object array, int index, char value)
  485. {
  486. if (array instanceof char[])
  487. ((char[]) array)[index] = value;
  488. else
  489. setInt(array, index, value);
  490. }
  491. /**
  492. * Sets an element of a short array.
  493. *
  494. * @param array the array to set a value of
  495. * @param index the array index to set the value to
  496. * @param value the value to set
  497. * @throws IllegalArgumentException if <code>array</code> is not a short,
  498. * int, long, float, or double array
  499. * @throws NullPointerException if <code>array</code> is null
  500. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  501. * bounds
  502. * @see #set(Object, int, Object)
  503. */
  504. public static void setShort(Object array, int index, short value)
  505. {
  506. if (array instanceof short[])
  507. ((short[]) array)[index] = value;
  508. else
  509. setInt(array, index, value);
  510. }
  511. /**
  512. * Sets an element of an int array.
  513. *
  514. * @param array the array to set a value of
  515. * @param index the array index to set the value to
  516. * @param value the value to set
  517. * @throws IllegalArgumentException if <code>array</code> is not an int,
  518. * long, float, or double array
  519. * @throws NullPointerException if <code>array</code> is null
  520. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  521. * bounds
  522. * @see #set(Object, int, Object)
  523. */
  524. public static void setInt(Object array, int index, int value)
  525. {
  526. if (array instanceof int[])
  527. ((int[]) array)[index] = value;
  528. else
  529. setLong(array, index, value);
  530. }
  531. /**
  532. * Sets an element of a long array.
  533. *
  534. * @param array the array to set a value of
  535. * @param index the array index to set the value to
  536. * @param value the value to set
  537. * @throws IllegalArgumentException if <code>array</code> is not a long,
  538. * float, or double array
  539. * @throws NullPointerException if <code>array</code> is null
  540. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  541. * bounds
  542. * @see #set(Object, int, Object)
  543. */
  544. public static void setLong(Object array, int index, long value)
  545. {
  546. if (array instanceof long[])
  547. ((long[]) array)[index] = value;
  548. else
  549. setFloat(array, index, value);
  550. }
  551. /**
  552. * Sets an element of a float array.
  553. *
  554. * @param array the array to set a value of
  555. * @param index the array index to set the value to
  556. * @param value the value to set
  557. * @throws IllegalArgumentException if <code>array</code> is not a float
  558. * or double array
  559. * @throws NullPointerException if <code>array</code> is null
  560. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  561. * bounds
  562. * @see #set(Object, int, Object)
  563. */
  564. public static void setFloat(Object array, int index, float value)
  565. {
  566. if (array instanceof float[])
  567. ((float[]) array)[index] = value;
  568. else
  569. setDouble(array, index, value);
  570. }
  571. /**
  572. * Sets an element of a double array.
  573. *
  574. * @param array the array to set a value of
  575. * @param index the array index to set the value to
  576. * @param value the value to set
  577. * @throws IllegalArgumentException if <code>array</code> is not a double
  578. * array
  579. * @throws NullPointerException if <code>array</code> is null
  580. * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
  581. * bounds
  582. * @see #set(Object, int, Object)
  583. */
  584. public static void setDouble(Object array, int index, double value)
  585. {
  586. if (array instanceof double[])
  587. ((double[]) array)[index] = value;
  588. else if (array == null)
  589. throw new NullPointerException();
  590. else
  591. throw new IllegalArgumentException();
  592. }
  593. /**
  594. * Dynamically and recursively create a multi-dimensioned array of objects.
  595. *
  596. * @param type guaranteed to be a valid object type
  597. * @param dimensions the dimensions of the array
  598. * @param index index of the current dimension to build
  599. * @return the new multi-dimensioned array
  600. * @throws NegativeArraySizeException if any entry of dimensions is negative
  601. * @throws OutOfMemoryError if memory allocation fails
  602. */
  603. // This would be faster if implemented natively, using the multianewarray
  604. // bytecode instead of this recursive call
  605. private static Object createMultiArray(Class type, int[] dimensions,
  606. int index)
  607. {
  608. if (index == dimensions.length - 1)
  609. return newInstance(type, dimensions[index]);
  610. Object toAdd = createMultiArray(type, dimensions, index + 1);
  611. Class thisType = toAdd.getClass();
  612. Object[] retval
  613. = (Object[]) VMArray.createObjectArray(thisType, dimensions[index]);
  614. if (dimensions[index] > 0)
  615. retval[0] = toAdd;
  616. int i = dimensions[index];
  617. while (--i > 0)
  618. retval[i] = createMultiArray(type, dimensions, index + 1);
  619. return retval;
  620. }
  621. }