1. /*
  2. * @(#)Array.java 1.8 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. /**
  9. * The <code>Array</code> class provides static methods to dynamically create and
  10. * access Java arrays.
  11. *
  12. * <p><code>Array</code> permits widening conversions to occur during a get or set
  13. * operation, but throws an <code>IllegalArgumentException</code> if a narrowing
  14. * conversion would occur.
  15. *
  16. * @author Nakul Saraiya
  17. */
  18. public final
  19. class Array {
  20. /**
  21. * Constructor. Class Array is not instantiable.
  22. */
  23. private Array() {}
  24. /**
  25. * Creates a new array with the specified component type and
  26. * length.
  27. * Invoking this method is equivalent to creating an array
  28. * as follows:
  29. * <blockquote>
  30. * <pre>
  31. * int[] x = {length];
  32. * Array.newInstance[componentType, x];
  33. * </pre>
  34. * </blockquote>
  35. *
  36. * @param componentType the <code>Class</code> object representing the
  37. * component type of the new array
  38. * @param length the length of the new array
  39. * @return the new array
  40. * @exception NullPointerException if the specified
  41. * <code>componentType</code> parameter is null
  42. * @exception NegativeArraySizeException if the specified <code>length</code>
  43. * is negative
  44. */
  45. public static Object newInstance(Class componentType, int length)
  46. throws NegativeArraySizeException {
  47. return newArray(componentType, length);
  48. }
  49. /**
  50. * Creates a new array
  51. * with the specified component type and dimensions.
  52. * If <code>componentType</code>
  53. * represents a non-array class or interface, the new array
  54. * has <code>dimensions.length</code> dimensions and 
  55. * <code>componentType </code> as its component type. If
  56. * <code>componentType</code> represents an array class, the
  57. * number of dimensions of the new array is equal to the sum
  58. * of <code>dimensions.length</code> and the number of
  59. * dimensions of <code>componentType</code>. In this case, the
  60. * component type of the new array is the component type of
  61. * <code>componentType</code>.
  62. *
  63. * <p>The number of dimensions of the new array must not
  64. * exceed the number of array dimensions supported by the
  65. * implementation (typically 255).
  66. *
  67. * @param componentType the <code>Class</code> object representing the component
  68. * type of the new array
  69. * @param dimensions an array of <code>int</code> types representing the dimensions of
  70. * the new array
  71. * @return the new array
  72. * @exception NullPointerException if the specified
  73. * <code>componentType</code> argument is null
  74. * @exception IllegalArgumentException if the specified <code>dimensions</code>
  75. * argument is a zero-dimensional array, or if the number of
  76. * requested dimensions exceeds the limit on the number of array dimensions
  77. * supported by the implementation (typically 255).
  78. * @exception NegativeArraySizeException if any of the components in
  79. * the specified <code>dimensions</code> argument is negative.
  80. */
  81. public static Object newInstance(Class componentType, int[] dimensions)
  82. throws IllegalArgumentException, NegativeArraySizeException {
  83. return multiNewArray(componentType, dimensions);
  84. }
  85. /**
  86. * Returns the length of the specified array object, as an <code>int</code>.
  87. *
  88. * @param array the array
  89. * @return the length of the array
  90. * @exception IllegalArgumentException if the object argument is not
  91. * an array
  92. */
  93. public static native int getLength(Object array)
  94. throws IllegalArgumentException;
  95. /**
  96. * Returns the value of the indexed component in the specified
  97. * array object. The value is automatically wrapped in an object
  98. * if it has a primitive type.
  99. *
  100. * @param array the array
  101. * @param index the index
  102. * @return the (possibly wrapped) value of the indexed component in
  103. * the specified array
  104. * @exception NullPointerException If the specified object is null
  105. * @exception IllegalArgumentException If the specified object is not
  106. * an array
  107. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  108. * argument is negative, or if it is greater than or equal to the
  109. * length of the specified array
  110. */
  111. public static native Object get(Object array, int index)
  112. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  113. /**
  114. * Returns the value of the indexed component in the specified
  115. * array object, as a <code>boolean</code>.
  116. *
  117. * @param array the array
  118. * @param index the index
  119. * @return the value of the indexed component in the specified array
  120. * @exception NullPointerException If the specified object is null
  121. * @exception IllegalArgumentException If the specified object is not
  122. * an array, or if the indexed element cannot be converted to the
  123. * return type by an identity or widening conversion
  124. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  125. * argument is negative, or if it is greater than or equal to the
  126. * length of the specified array
  127. * @see Array#get
  128. */
  129. public static native boolean getBoolean(Object array, int index)
  130. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  131. /**
  132. * Returns the value of the indexed component in the specified
  133. * array object, as a <code>byte</code>.
  134. *
  135. * @param array the array
  136. * @param index the index
  137. * @return the value of the indexed component in the specified array
  138. * @exception NullPointerException If the specified object is null
  139. * @exception IllegalArgumentException If the specified object is not
  140. * an array, or if the indexed element cannot be converted to the
  141. * return type by an identity or widening conversion
  142. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  143. * argument is negative, or if it is greater than or equal to the
  144. * length of the specified array
  145. * @see Array#get
  146. */
  147. public static native byte getByte(Object array, int index)
  148. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  149. /**
  150. * Returns the value of the indexed component in the specified
  151. * array object, as a <code>char</code>.
  152. *
  153. * @param array the array
  154. * @param index the index
  155. * @return the value of the indexed component in the specified array
  156. * @exception NullPointerException If the specified object is null
  157. * @exception IllegalArgumentException If the specified object is not
  158. * an array, or if the indexed element cannot be converted to the
  159. * return type by an identity or widening conversion
  160. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  161. * argument is negative, or if it is greater than or equal to the
  162. * length of the specified array
  163. * @see Array#get
  164. */
  165. public static native char getChar(Object array, int index)
  166. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  167. /**
  168. * Returns the value of the indexed component in the specified
  169. * array object, as a <code>short</code>.
  170. *
  171. * @param array the array
  172. * @param index the index
  173. * @return the value of the indexed component in the specified array
  174. * @exception NullPointerException If the specified object is null
  175. * @exception IllegalArgumentException If the specified object is not
  176. * an array, or if the indexed element cannot be converted to the
  177. * return type by an identity or widening conversion
  178. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  179. * argument is negative, or if it is greater than or equal to the
  180. * length of the specified array
  181. * @see Array#get
  182. */
  183. public static native short getShort(Object array, int index)
  184. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  185. /**
  186. * Returns the value of the indexed component in the specified
  187. * array object, as an <code>int</code>.
  188. *
  189. * @param array the array
  190. * @param index the index
  191. * @return the value of the indexed component in the specified array
  192. * @exception NullPointerException If the specified object is null
  193. * @exception IllegalArgumentException If the specified object is not
  194. * an array, or if the indexed element cannot be converted to the
  195. * return type by an identity or widening conversion
  196. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  197. * argument is negative, or if it is greater than or equal to the
  198. * length of the specified array
  199. * @see Array#get
  200. */
  201. public static native int getInt(Object array, int index)
  202. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  203. /**
  204. * Returns the value of the indexed component in the specified
  205. * array object, as a <code>long</code>.
  206. *
  207. * @param array the array
  208. * @param index the index
  209. * @return the value of the indexed component in the specified array
  210. * @exception NullPointerException If the specified object is null
  211. * @exception IllegalArgumentException If the specified object is not
  212. * an array, or if the indexed element cannot be converted to the
  213. * return type by an identity or widening conversion
  214. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  215. * argument is negative, or if it is greater than or equal to the
  216. * length of the specified array
  217. * @see Array#get
  218. */
  219. public static native long getLong(Object array, int index)
  220. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  221. /**
  222. * Returns the value of the indexed component in the specified
  223. * array object, as a <code>float</code>.
  224. *
  225. * @param array the array
  226. * @param index the index
  227. * @return the value of the indexed component in the specified array
  228. * @exception NullPointerException If the specified object is null
  229. * @exception IllegalArgumentException If the specified object is not
  230. * an array, or if the indexed element cannot be converted to the
  231. * return type by an identity or widening conversion
  232. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  233. * argument is negative, or if it is greater than or equal to the
  234. * length of the specified array
  235. * @see Array#get
  236. */
  237. public static native float getFloat(Object array, int index)
  238. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  239. /**
  240. * Returns the value of the indexed component in the specified
  241. * array object, as a <code>double</code>.
  242. *
  243. * @param array the array
  244. * @param index the index
  245. * @return the value of the indexed component in the specified array
  246. * @exception NullPointerException If the specified object is null
  247. * @exception IllegalArgumentException If the specified object is not
  248. * an array, or if the indexed element cannot be converted to the
  249. * return type by an identity or widening conversion
  250. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  251. * argument is negative, or if it is greater than or equal to the
  252. * length of the specified array
  253. * @see Array#get
  254. */
  255. public static native double getDouble(Object array, int index)
  256. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  257. /**
  258. * Sets the value of the indexed component of the specified array
  259. * object to the specified new value. The new value is first
  260. * automatically unwrapped if the array has a primitive component
  261. * type.
  262. * @param array the array
  263. * @param index the index into the array
  264. * @param value the new value of the indexed component
  265. * @exception NullPointerException If the specified object argument
  266. * is null, or if the array component type is primitive and the specified
  267. * value is null
  268. * @exception IllegalArgumentException If the specified object argument
  269. * is not an array, or if the array component type is primitive and
  270. * the specified value cannot be converted to the primitive type by
  271. * a combination of unwrapping and identity or widening conversions
  272. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  273. * argument is negative, or if it is greater than or equal to
  274. * the length of the specified array
  275. */
  276. public static native void set(Object array, int index, Object value)
  277. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  278. /**
  279. * Sets the value of the indexed component of the specified array
  280. * object to the specified <code>boolean</code> value.
  281. * @param array the array
  282. * @param index the index into the array
  283. * @param value the new value of the indexed component
  284. * @exception NullPointerException If the specified object argument
  285. * is null
  286. * @exception IllegalArgumentException If the specified object argument
  287. * is not an array, or if the the specified value cannot be converted
  288. * to the underlying array's component type by an identity or a
  289. * primitive widening widening conversion
  290. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  291. * argument is negative, or if it is greater than or equal to
  292. * the length of the specified array
  293. * @see Array#set
  294. */
  295. public static native void setBoolean(Object array, int index, boolean z)
  296. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  297. /**
  298. * Sets the value of the indexed component of the specified array
  299. * object to the specified <code>boolean</code> value.
  300. * @param array the array
  301. * @param index the index into the array
  302. * @param value the new value of the indexed component
  303. * @exception NullPointerException If the specified object argument
  304. * is null
  305. * @exception IllegalArgumentException If the specified object argument
  306. * is not an array, or if the the specified value cannot be converted
  307. * to the underlying array's component type by an identity or a
  308. * primitive widening widening conversion
  309. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  310. * argument is negative, or if it is greater than or equal to
  311. * the length of the specified array
  312. * @see Array#set
  313. */
  314. public static native void setByte(Object array, int index, byte b)
  315. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  316. /**
  317. * Sets the value of the indexed component of the specified array
  318. * object to the specified <code>byte</code> value.
  319. * @param array the array
  320. * @param index the index into the array
  321. * @param value the new value of the indexed component
  322. * @exception NullPointerException If the specified object argument
  323. * is null
  324. * @exception IllegalArgumentException If the specified object argument
  325. * is not an array, or if the the specified value cannot be converted
  326. * to the underlying array's component type by an identity or a
  327. * primitive widening widening conversion
  328. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  329. * argument is negative, or if it is greater than or equal to
  330. * the length of the specified array
  331. * @see Array#set
  332. */
  333. public static native void setChar(Object array, int index, char c)
  334. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  335. /**
  336. * Sets the value of the indexed component of the specified array
  337. * object to the specified <code>short</code> value.
  338. * @param array the array
  339. * @param index the index into the array
  340. * @param value the new value of the indexed component
  341. * @exception NullPointerException If the specified object argument
  342. * is null
  343. * @exception IllegalArgumentException If the specified object argument
  344. * is not an array, or if the the specified value cannot be converted
  345. * to the underlying array's component type by an identity or a
  346. * primitive widening widening conversion
  347. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  348. * argument is negative, or if it is greater than or equal to
  349. * the length of the specified array
  350. * @see Array#set
  351. */
  352. public static native void setShort(Object array, int index, short s)
  353. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  354. /**
  355. * Sets the value of the indexed component of the specified array
  356. * object to the specified <code>int</code> value.
  357. * @param array the array
  358. * @param index the index into the array
  359. * @param value the new value of the indexed component
  360. * @exception NullPointerException If the specified object argument
  361. * is null
  362. * @exception IllegalArgumentException If the specified object argument
  363. * is not an array, or if the the specified value cannot be converted
  364. * to the underlying array's component type by an identity or a
  365. * primitive widening widening conversion
  366. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  367. * argument is negative, or if it is greater than or equal to
  368. * the length of the specified array
  369. * @see Array#set
  370. */
  371. public static native void setInt(Object array, int index, int i)
  372. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  373. /**
  374. * Sets the value of the indexed component of the specified array
  375. * object to the specified <code>long</code> value.
  376. * @param array the array
  377. * @param index the index into the array
  378. * @param value the new value of the indexed component
  379. * @exception NullPointerException If the specified object argument
  380. * is null
  381. * @exception IllegalArgumentException If the specified object argument
  382. * is not an array, or if the the specified value cannot be converted
  383. * to the underlying array's component type by an identity or a
  384. * primitive widening widening conversion
  385. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  386. * argument is negative, or if it is greater than or equal to
  387. * the length of the specified array
  388. * @see Array#set
  389. */
  390. public static native void setLong(Object array, int index, long l)
  391. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  392. /**
  393. * Sets the value of the indexed component of the specified array
  394. * object to the specified <code>float</code> value.
  395. * @param array the array
  396. * @param index the index into the array
  397. * @param value the new value of the indexed component
  398. * @exception NullPointerException If the specified object argument
  399. * is null
  400. * @exception IllegalArgumentException If the specified object argument
  401. * is not an array, or if the the specified value cannot be converted
  402. * to the underlying array's component type by an identity or a
  403. * primitive widening widening conversion
  404. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  405. * argument is negative, or if it is greater than or equal to
  406. * the length of the specified array
  407. * @see Array#set
  408. */
  409. public static native void setFloat(Object array, int index, float f)
  410. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  411. /**
  412. * Sets the value of the indexed component of the specified array
  413. * object to the specified <code>double</code> value.
  414. * @param array the array
  415. * @param index the index into the array
  416. * @param value the new value of the indexed component
  417. * @exception NullPointerException If the specified object argument
  418. * is null
  419. * @exception IllegalArgumentException If the specified object argument
  420. * is not an array, or if the the specified value cannot be converted
  421. * to the underlying array's component type by an identity or a
  422. * primitive widening widening conversion
  423. * @exception ArrayIndexOutOfBoundsException If the specified <code>index</code>
  424. * argument is negative, or if it is greater than or equal to
  425. * the length of the specified array
  426. * @see Array#set
  427. */
  428. public static native void setDouble(Object array, int index, double d)
  429. throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
  430. /*
  431. * Private
  432. */
  433. private static native Object newArray(Class componentType, int length)
  434. throws NegativeArraySizeException;
  435. private static native Object multiNewArray(Class componentType,
  436. int[] dimensions)
  437. throws IllegalArgumentException, NegativeArraySizeException;
  438. }