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