1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang.reflect; 28 29 import dalvik.annotation.optimization.FastNative; 30 31 /** 32 * The {@code Array} class provides static methods to dynamically create and 33 * access Java arrays. 34 * 35 * <p>{@code Array} permits widening conversions to occur during a get or set 36 * operation, but throws an {@code IllegalArgumentException} if a narrowing 37 * conversion would occur. 38 * 39 * @author Nakul Saraiya 40 */ 41 public final 42 class Array { 43 44 /** 45 * Constructor. Class Array is not instantiable. 46 */ 47 private Array() {} 48 49 /** 50 * Creates a new array with the specified component type and 51 * length. 52 * Invoking this method is equivalent to creating an array 53 * as follows: 54 * <blockquote> 55 * <pre> 56 * int[] x = {length}; 57 * Array.newInstance(componentType, x); 58 * </pre> 59 * </blockquote> 60 * 61 * <p>The number of dimensions of the new array must not 62 * exceed 255. 63 * 64 * @param componentType the {@code Class} object representing the 65 * component type of the new array 66 * @param length the length of the new array 67 * @return the new array 68 * @exception NullPointerException if the specified 69 * {@code componentType} parameter is null 70 * @exception IllegalArgumentException if componentType is {@link 71 * Void#TYPE} or if the number of dimensions of the requested array 72 * instance exceed 255. 73 * @exception NegativeArraySizeException if the specified {@code length} 74 * is negative 75 */ 76 public static Object newInstance(Class<?> componentType, int length) 77 throws NegativeArraySizeException { 78 return newArray(componentType, length); 79 } 80 81 /** 82 * Creates a new array 83 * with the specified component type and dimensions. 84 * If {@code componentType} 85 * represents a non-array class or interface, the new array 86 * has {@code dimensions.length} dimensions and 87 * {@code componentType} as its component type. If 88 * {@code componentType} represents an array class, the 89 * number of dimensions of the new array is equal to the sum 90 * of {@code dimensions.length} and the number of 91 * dimensions of {@code componentType}. In this case, the 92 * component type of the new array is the component type of 93 * {@code componentType}. 94 * 95 * <p>The number of dimensions of the new array must not 96 * exceed 255. 97 * 98 * @param componentType the {@code Class} object representing the component 99 * type of the new array 100 * @param dimensions an array of {@code int} representing the dimensions of 101 * the new array 102 * @return the new array 103 * @exception NullPointerException if the specified 104 * {@code componentType} argument is null 105 * @exception IllegalArgumentException if the specified {@code dimensions} 106 * argument is a zero-dimensional array, if componentType is {@link 107 * Void#TYPE}, or if the number of dimensions of the requested array 108 * instance exceed 255. 109 * @exception NegativeArraySizeException if any of the components in 110 * the specified {@code dimensions} argument is negative. 111 */ 112 public static Object newInstance(Class<?> componentType, int... dimensions) 113 throws IllegalArgumentException, NegativeArraySizeException { 114 // Android-changed: New implementation of newInstance(Class, int...) 115 if (dimensions.length <= 0 || dimensions.length > 255) { 116 throw new IllegalArgumentException("Bad number of dimensions: " + dimensions.length); 117 } 118 if (componentType == void.class) { 119 throw new IllegalArgumentException("Can't allocate an array of void"); 120 } 121 if (componentType == null) { 122 throw new NullPointerException("componentType == null"); 123 } 124 return createMultiArray(componentType, dimensions); 125 } 126 127 /** 128 * Returns the length of the specified array object, as an {@code int}. 129 * 130 * @param array the array 131 * @return the length of the array 132 * @exception IllegalArgumentException if the object argument is not 133 * an array 134 */ 135 // Android-changed: Non-native implementation of getLength(Object) 136 // Android-changed: Removal of explicit throws IllegalArgumentException from method signature. 137 public static int getLength(Object array) 138 /* throws IllegalArgumentException */ { 139 if (array instanceof Object[]) { 140 return ((Object[]) array).length; 141 } else if (array instanceof boolean[]) { 142 return ((boolean[]) array).length; 143 } else if (array instanceof byte[]) { 144 return ((byte[]) array).length; 145 } else if (array instanceof char[]) { 146 return ((char[]) array).length; 147 } else if (array instanceof double[]) { 148 return ((double[]) array).length; 149 } else if (array instanceof float[]) { 150 return ((float[]) array).length; 151 } else if (array instanceof int[]) { 152 return ((int[]) array).length; 153 } else if (array instanceof long[]) { 154 return ((long[]) array).length; 155 } else if (array instanceof short[]) { 156 return ((short[]) array).length; 157 } 158 throw badArray(array); 159 } 160 161 /** 162 * Returns the value of the indexed component in the specified 163 * array object. The value is automatically wrapped in an object 164 * if it has a primitive type. 165 * 166 * @param array the array 167 * @param index the index 168 * @return the (possibly wrapped) value of the indexed component in 169 * the specified array 170 * @exception NullPointerException If the specified object is null 171 * @exception IllegalArgumentException If the specified object is not 172 * an array 173 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 174 * argument is negative, or if it is greater than or equal to the 175 * length of the specified array 176 */ 177 // Android-changed: Non-native implementation of get(Object, int) 178 public static Object get(Object array, int index) 179 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 180 if (array instanceof Object[]) { 181 return ((Object[]) array)[index]; 182 } 183 if (array instanceof boolean[]) { 184 return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; 185 } 186 if (array instanceof byte[]) { 187 return Byte.valueOf(((byte[]) array)[index]); 188 } 189 if (array instanceof char[]) { 190 return Character.valueOf(((char[]) array)[index]); 191 } 192 if (array instanceof short[]) { 193 return Short.valueOf(((short[]) array)[index]); 194 } 195 if (array instanceof int[]) { 196 return Integer.valueOf(((int[]) array)[index]); 197 } 198 if (array instanceof long[]) { 199 return Long.valueOf(((long[]) array)[index]); 200 } 201 if (array instanceof float[]) { 202 return new Float(((float[]) array)[index]); 203 } 204 if (array instanceof double[]) { 205 return new Double(((double[]) array)[index]); 206 } 207 if (array == null) { 208 throw new NullPointerException("array == null"); 209 } 210 throw notAnArray(array); 211 } 212 213 /** 214 * Returns the value of the indexed component in the specified 215 * array object, as a {@code boolean}. 216 * 217 * @param array the array 218 * @param index the index 219 * @return the value of the indexed component in the specified array 220 * @exception NullPointerException If the specified object is null 221 * @exception IllegalArgumentException If the specified object is not 222 * an array, or if the indexed element cannot be converted to the 223 * return type by an identity or widening conversion 224 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 225 * argument is negative, or if it is greater than or equal to the 226 * length of the specified array 227 * @see Array#get 228 */ 229 // Android-changed: Non-native implementation of getBoolean(Object, int) 230 public static boolean getBoolean(Object array, int index) 231 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 232 if (array instanceof boolean[]) { 233 return ((boolean[]) array)[index]; 234 } 235 throw badArray(array); 236 } 237 238 /** 239 * Returns the value of the indexed component in the specified 240 * array object, as a {@code byte}. 241 * 242 * @param array the array 243 * @param index the index 244 * @return the value of the indexed component in the specified array 245 * @exception NullPointerException If the specified object is null 246 * @exception IllegalArgumentException If the specified object is not 247 * an array, or if the indexed element cannot be converted to the 248 * return type by an identity or widening conversion 249 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 250 * argument is negative, or if it is greater than or equal to the 251 * length of the specified array 252 * @see Array#get 253 */ 254 // Android-changed: Non-native implementation of getByte(Object, int) 255 public static byte getByte(Object array, int index) 256 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 257 if (array instanceof byte[]) { 258 return ((byte[]) array)[index]; 259 } 260 throw badArray(array); 261 } 262 263 /** 264 * Returns the value of the indexed component in the specified 265 * array object, as a {@code char}. 266 * 267 * @param array the array 268 * @param index the index 269 * @return the value of the indexed component in the specified array 270 * @exception NullPointerException If the specified object is null 271 * @exception IllegalArgumentException If the specified object is not 272 * an array, or if the indexed element cannot be converted to the 273 * return type by an identity or widening conversion 274 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 275 * argument is negative, or if it is greater than or equal to the 276 * length of the specified array 277 * @see Array#get 278 */ 279 // Android-changed: Non-native implementation of getChar(Object, int) 280 public static char getChar(Object array, int index) 281 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 282 if (array instanceof char[]) { 283 return ((char[]) array)[index]; 284 } 285 throw badArray(array); 286 } 287 288 /** 289 * Returns the value of the indexed component in the specified 290 * array object, as a {@code short}. 291 * 292 * @param array the array 293 * @param index the index 294 * @return the value of the indexed component in the specified array 295 * @exception NullPointerException If the specified object is null 296 * @exception IllegalArgumentException If the specified object is not 297 * an array, or if the indexed element cannot be converted to the 298 * return type by an identity or widening conversion 299 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 300 * argument is negative, or if it is greater than or equal to the 301 * length of the specified array 302 * @see Array#get 303 */ 304 // Android-changed: Non-native implementation of getShort(Object, int) 305 public static short getShort(Object array, int index) 306 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 307 if (array instanceof short[]) { 308 return ((short[]) array)[index]; 309 } else if (array instanceof byte[]) { 310 return ((byte[]) array)[index]; 311 } 312 throw badArray(array); 313 } 314 315 /** 316 * Returns the value of the indexed component in the specified 317 * array object, as an {@code int}. 318 * 319 * @param array the array 320 * @param index the index 321 * @return the value of the indexed component in the specified array 322 * @exception NullPointerException If the specified object is null 323 * @exception IllegalArgumentException If the specified object is not 324 * an array, or if the indexed element cannot be converted to the 325 * return type by an identity or widening conversion 326 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 327 * argument is negative, or if it is greater than or equal to the 328 * length of the specified array 329 * @see Array#get 330 */ 331 // Android-changed: Non-native implementation of getInt(Object, int) 332 public static int getInt(Object array, int index) 333 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 334 if (array instanceof int[]) { 335 return ((int[]) array)[index]; 336 } else if (array instanceof byte[]) { 337 return ((byte[]) array)[index]; 338 } else if (array instanceof char[]) { 339 return ((char[]) array)[index]; 340 } else if (array instanceof short[]) { 341 return ((short[]) array)[index]; 342 } 343 throw badArray(array); 344 } 345 346 /** 347 * Returns the value of the indexed component in the specified 348 * array object, as a {@code long}. 349 * 350 * @param array the array 351 * @param index the index 352 * @return the value of the indexed component in the specified array 353 * @exception NullPointerException If the specified object is null 354 * @exception IllegalArgumentException If the specified object is not 355 * an array, or if the indexed element cannot be converted to the 356 * return type by an identity or widening conversion 357 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 358 * argument is negative, or if it is greater than or equal to the 359 * length of the specified array 360 * @see Array#get 361 */ 362 // Android-changed: Non-native implementation of getLong(Object, int) 363 public static long getLong(Object array, int index) 364 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 365 if (array instanceof long[]) { 366 return ((long[]) array)[index]; 367 } else if (array instanceof byte[]) { 368 return ((byte[]) array)[index]; 369 } else if (array instanceof char[]) { 370 return ((char[]) array)[index]; 371 } else if (array instanceof int[]) { 372 return ((int[]) array)[index]; 373 } else if (array instanceof short[]) { 374 return ((short[]) array)[index]; 375 } 376 throw badArray(array); 377 } 378 379 /** 380 * Returns the value of the indexed component in the specified 381 * array object, as a {@code float}. 382 * 383 * @param array the array 384 * @param index the index 385 * @return the value of the indexed component in the specified array 386 * @exception NullPointerException If the specified object is null 387 * @exception IllegalArgumentException If the specified object is not 388 * an array, or if the indexed element cannot be converted to the 389 * return type by an identity or widening conversion 390 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 391 * argument is negative, or if it is greater than or equal to the 392 * length of the specified array 393 * @see Array#get 394 */ 395 // Android-changed: Non-native implementation of getFloat(Object, int) 396 public static float getFloat(Object array, int index) 397 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 398 if (array instanceof float[]) { 399 return ((float[]) array)[index]; 400 } else if (array instanceof byte[]) { 401 return ((byte[]) array)[index]; 402 } else if (array instanceof char[]) { 403 return ((char[]) array)[index]; 404 } else if (array instanceof int[]) { 405 return ((int[]) array)[index]; 406 } else if (array instanceof long[]) { 407 return ((long[]) array)[index]; 408 } else if (array instanceof short[]) { 409 return ((short[]) array)[index]; 410 } 411 throw badArray(array); 412 } 413 414 /** 415 * Returns the value of the indexed component in the specified 416 * array object, as a {@code double}. 417 * 418 * @param array the array 419 * @param index the index 420 * @return the value of the indexed component in the specified array 421 * @exception NullPointerException If the specified object is null 422 * @exception IllegalArgumentException If the specified object is not 423 * an array, or if the indexed element cannot be converted to the 424 * return type by an identity or widening conversion 425 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 426 * argument is negative, or if it is greater than or equal to the 427 * length of the specified array 428 * @see Array#get 429 */ 430 // Android-changed: Non-native implementation of getDouble(Object, int) 431 public static double getDouble(Object array, int index) 432 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 433 if (array instanceof double[]) { 434 return ((double[]) array)[index]; 435 } else if (array instanceof byte[]) { 436 return ((byte[]) array)[index]; 437 } else if (array instanceof char[]) { 438 return ((char[]) array)[index]; 439 } else if (array instanceof float[]) { 440 return ((float[]) array)[index]; 441 } else if (array instanceof int[]) { 442 return ((int[]) array)[index]; 443 } else if (array instanceof long[]) { 444 return ((long[]) array)[index]; 445 } else if (array instanceof short[]) { 446 return ((short[]) array)[index]; 447 } 448 throw badArray(array); 449 } 450 451 /** 452 * Sets the value of the indexed component of the specified array 453 * object to the specified new value. The new value is first 454 * automatically unwrapped if the array has a primitive component 455 * type. 456 * @param array the array 457 * @param index the index into the array 458 * @param value the new value of the indexed component 459 * @exception NullPointerException If the specified object argument 460 * is null 461 * @exception IllegalArgumentException If the specified object argument 462 * is not an array, or if the array component type is primitive and 463 * an unwrapping conversion fails 464 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 465 * argument is negative, or if it is greater than or equal to 466 * the length of the specified array 467 */ 468 // Android-changed: Non-native implementation of set(Object, int, Object) 469 public static void set(Object array, int index, Object value) 470 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 471 if (!array.getClass().isArray()) { 472 throw notAnArray(array); 473 } 474 475 if (array instanceof Object[]) { 476 if (value != null && !array.getClass().getComponentType().isInstance(value)) { 477 throw incompatibleType(array); 478 } 479 ((Object[]) array)[index] = value; 480 } else { 481 if (value == null) { 482 throw new IllegalArgumentException("Primitive array can't take null values."); 483 } 484 if (value instanceof Boolean) { 485 setBoolean(array, index, ((Boolean) value).booleanValue()); 486 } else if (value instanceof Byte) { 487 setByte(array, index, ((Byte) value).byteValue()); 488 } else if (value instanceof Character) { 489 setChar(array, index, ((Character) value).charValue()); 490 } else if (value instanceof Short) { 491 setShort(array, index, ((Short) value).shortValue()); 492 } else if (value instanceof Integer) { 493 setInt(array, index, ((Integer) value).intValue()); 494 } else if (value instanceof Long) { 495 setLong(array, index, ((Long) value).longValue()); 496 } else if (value instanceof Float) { 497 setFloat(array, index, ((Float) value).floatValue()); 498 } else if (value instanceof Double) { 499 setDouble(array, index, ((Double) value).doubleValue()); 500 } 501 } 502 } 503 504 /** 505 * Sets the value of the indexed component of the specified array 506 * object to the specified {@code boolean} value. 507 * @param array the array 508 * @param index the index into the array 509 * @param z the new value of the indexed component 510 * @exception NullPointerException If the specified object argument 511 * is null 512 * @exception IllegalArgumentException If the specified object argument 513 * is not an array, or if the specified value cannot be converted 514 * to the underlying array's component type by an identity or a 515 * primitive widening conversion 516 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 517 * argument is negative, or if it is greater than or equal to 518 * the length of the specified array 519 * @see Array#set 520 */ 521 // Android-changed: Non-native implementation of setBoolean(Object, int, boolean) 522 // Android-changed: Removal of explicit runtime exceptions throws clause 523 public static void setBoolean(Object array, int index, boolean z) 524 /* throws IllegalArgumentException, ArrayIndexOutOfBoundsException */ { 525 if (array instanceof boolean[]) { 526 ((boolean[]) array)[index] = z; 527 } else { 528 throw badArray(array); 529 } 530 } 531 532 /** 533 * Sets the value of the indexed component of the specified array 534 * object to the specified {@code byte} value. 535 * @param array the array 536 * @param index the index into the array 537 * @param b the new value of the indexed component 538 * @exception NullPointerException If the specified object argument 539 * is null 540 * @exception IllegalArgumentException If the specified object argument 541 * is not an array, or if the specified value cannot be converted 542 * to the underlying array's component type by an identity or a 543 * primitive widening conversion 544 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 545 * argument is negative, or if it is greater than or equal to 546 * the length of the specified array 547 * @see Array#set 548 */ 549 // Android-changed: Non-native implementation of setByte(Object, int, byte) 550 public static void setByte(Object array, int index, byte b) 551 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 552 if (array instanceof byte[]) { 553 ((byte[]) array)[index] = b; 554 } else if (array instanceof double[]) { 555 ((double[]) array)[index] = b; 556 } else if (array instanceof float[]) { 557 ((float[]) array)[index] = b; 558 } else if (array instanceof int[]) { 559 ((int[]) array)[index] = b; 560 } else if (array instanceof long[]) { 561 ((long[]) array)[index] = b; 562 } else if (array instanceof short[]) { 563 ((short[]) array)[index] = b; 564 } else { 565 throw badArray(array); 566 } 567 } 568 569 /** 570 * Sets the value of the indexed component of the specified array 571 * object to the specified {@code char} value. 572 * @param array the array 573 * @param index the index into the array 574 * @param c the new value of the indexed component 575 * @exception NullPointerException If the specified object argument 576 * is null 577 * @exception IllegalArgumentException If the specified object argument 578 * is not an array, or if the specified value cannot be converted 579 * to the underlying array's component type by an identity or a 580 * primitive widening conversion 581 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 582 * argument is negative, or if it is greater than or equal to 583 * the length of the specified array 584 * @see Array#set 585 */ 586 // Android-changed: Non-native implementation of setChar(Object, int, char) 587 public static void setChar(Object array, int index, char c) 588 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 589 if (array instanceof char[]) { 590 ((char[]) array)[index] = c; 591 } else if (array instanceof double[]) { 592 ((double[]) array)[index] = c; 593 } else if (array instanceof float[]) { 594 ((float[]) array)[index] = c; 595 } else if (array instanceof int[]) { 596 ((int[]) array)[index] = c; 597 } else if (array instanceof long[]) { 598 ((long[]) array)[index] = c; 599 } else { 600 throw badArray(array); 601 } 602 } 603 604 /** 605 * Sets the value of the indexed component of the specified array 606 * object to the specified {@code short} value. 607 * @param array the array 608 * @param index the index into the array 609 * @param s the new value of the indexed component 610 * @exception NullPointerException If the specified object argument 611 * is null 612 * @exception IllegalArgumentException If the specified object argument 613 * is not an array, or if the specified value cannot be converted 614 * to the underlying array's component type by an identity or a 615 * primitive widening conversion 616 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 617 * argument is negative, or if it is greater than or equal to 618 * the length of the specified array 619 * @see Array#set 620 */ 621 // Android-changed: Non-native implementation of setShort(Object, int, short) 622 public static void setShort(Object array, int index, short s) 623 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 624 if (array instanceof short[]) { 625 ((short[]) array)[index] = s; 626 } else if (array instanceof double[]) { 627 ((double[]) array)[index] = s; 628 } else if (array instanceof float[]) { 629 ((float[]) array)[index] = s; 630 } else if (array instanceof int[]) { 631 ((int[]) array)[index] = s; 632 } else if (array instanceof long[]) { 633 ((long[]) array)[index] = s; 634 } else { 635 throw badArray(array); 636 } 637 } 638 639 /** 640 * Sets the value of the indexed component of the specified array 641 * object to the specified {@code int} value. 642 * @param array the array 643 * @param index the index into the array 644 * @param i the new value of the indexed component 645 * @exception NullPointerException If the specified object argument 646 * is null 647 * @exception IllegalArgumentException If the specified object argument 648 * is not an array, or if the specified value cannot be converted 649 * to the underlying array's component type by an identity or a 650 * primitive widening conversion 651 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 652 * argument is negative, or if it is greater than or equal to 653 * the length of the specified array 654 * @see Array#set 655 */ 656 // Android-changed: Non-native implementation of setInt(Object, int, int) 657 public static void setInt(Object array, int index, int i) 658 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 659 if (array instanceof int[]) { 660 ((int[]) array)[index] = i; 661 } else if (array instanceof double[]) { 662 ((double[]) array)[index] = i; 663 } else if (array instanceof float[]) { 664 ((float[]) array)[index] = i; 665 } else if (array instanceof long[]) { 666 ((long[]) array)[index] = i; 667 } else { 668 throw badArray(array); 669 } 670 } 671 672 /** 673 * Sets the value of the indexed component of the specified array 674 * object to the specified {@code long} value. 675 * @param array the array 676 * @param index the index into the array 677 * @param l the new value of the indexed component 678 * @exception NullPointerException If the specified object argument 679 * is null 680 * @exception IllegalArgumentException If the specified object argument 681 * is not an array, or if the specified value cannot be converted 682 * to the underlying array's component type by an identity or a 683 * primitive widening conversion 684 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 685 * argument is negative, or if it is greater than or equal to 686 * the length of the specified array 687 * @see Array#set 688 */ 689 // Android-changed: Non-native implementation of setBoolean(Object, int, long) 690 public static void setLong(Object array, int index, long l) 691 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 692 if (array instanceof long[]) { 693 ((long[]) array)[index] = l; 694 } else if (array instanceof double[]) { 695 ((double[]) array)[index] = l; 696 } else if (array instanceof float[]) { 697 ((float[]) array)[index] = l; 698 } else { 699 throw badArray(array); 700 } 701 } 702 703 /** 704 * Sets the value of the indexed component of the specified array 705 * object to the specified {@code float} value. 706 * @param array the array 707 * @param index the index into the array 708 * @param f the new value of the indexed component 709 * @exception NullPointerException If the specified object argument 710 * is null 711 * @exception IllegalArgumentException If the specified object argument 712 * is not an array, or if the specified value cannot be converted 713 * to the underlying array's component type by an identity or a 714 * primitive widening conversion 715 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 716 * argument is negative, or if it is greater than or equal to 717 * the length of the specified array 718 * @see Array#set 719 */ 720 public static void setFloat(Object array, int index, float f) 721 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 722 if (array instanceof float[]) { 723 ((float[]) array)[index] = f; 724 } else if (array instanceof double[]) { 725 ((double[]) array)[index] = f; 726 } else { 727 throw badArray(array); 728 } 729 } 730 731 /** 732 * Sets the value of the indexed component of the specified array 733 * object to the specified {@code double} value. 734 * @param array the array 735 * @param index the index into the array 736 * @param d the new value of the indexed component 737 * @exception NullPointerException If the specified object argument 738 * is null 739 * @exception IllegalArgumentException If the specified object argument 740 * is not an array, or if the specified value cannot be converted 741 * to the underlying array's component type by an identity or a 742 * primitive widening conversion 743 * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 744 * argument is negative, or if it is greater than or equal to 745 * the length of the specified array 746 * @see Array#set 747 */ 748 // Android-changed: Non-native implementation of setDouble(Object, int, double) 749 public static void setDouble(Object array, int index, double d) 750 throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 751 if (array instanceof double[]) { 752 ((double[]) array)[index] = d; 753 } else { 754 throw badArray(array); 755 } 756 } 757 758 /* 759 * Private 760 */ 761 762 // Android-added: Added javadocs for newArray(Class, int) 763 /** 764 * Returns a new array of the specified component type and length. 765 * Equivalent to {@code new componentType[size]}. 766 * 767 * @throws NullPointerException 768 * if the component type is null 769 * @throws NegativeArraySizeException 770 * if {@code size < 0} 771 */ 772 // Android-changed: Non-native implementation of newArray(Class, int) 773 private static Object newArray(Class<?> componentType, int length) 774 throws NegativeArraySizeException { 775 if (!componentType.isPrimitive()) { 776 return createObjectArray(componentType, length); 777 } else if (componentType == char.class) { 778 return new char[length]; 779 } else if (componentType == int.class) { 780 return new int[length]; 781 } else if (componentType == byte.class) { 782 return new byte[length]; 783 } else if (componentType == boolean.class) { 784 return new boolean[length]; 785 } else if (componentType == short.class) { 786 return new short[length]; 787 } else if (componentType == long.class) { 788 return new long[length]; 789 } else if (componentType == float.class) { 790 return new float[length]; 791 } else if (componentType == double.class) { 792 return new double[length]; 793 } else if (componentType == void.class) { 794 throw new IllegalArgumentException("Can't allocate an array of void"); 795 } 796 throw new AssertionError(); 797 } 798 799 // Android-removed: multiNewArray(Class, int[]) method. createMultiArray used instead. 800 /* 801 private static native Object multiNewArray(Class<?> componentType, 802 int[] dimensions) 803 throws IllegalArgumentException, NegativeArraySizeException; 804 */ 805 806 // Android-added: createMultiArray(Class, int[]) method. Used instead of multiNewArray 807 /* 808 * Create a multi-dimensional array of objects with the specified type. 809 */ 810 @FastNative 811 private static native Object createMultiArray(Class<?> componentType, int[] dimensions) 812 throws NegativeArraySizeException; 813 814 // BEGIN Android-added: Helper methods to support custom method implementations. 815 /* 816 * Create a one-dimensional array of objects with the specified type. 817 */ 818 @FastNative 819 private static native Object createObjectArray(Class<?> componentType, int length) 820 throws NegativeArraySizeException; 821 822 private static IllegalArgumentException notAnArray(Object o) { 823 throw new IllegalArgumentException("Not an array: " + o.getClass()); 824 } 825 826 private static IllegalArgumentException incompatibleType(Object o) { 827 throw new IllegalArgumentException("Array has incompatible type: " + o.getClass()); 828 } 829 830 private static RuntimeException badArray(Object array) { 831 if (array == null) { 832 throw new NullPointerException("array == null"); 833 } else if (!array.getClass().isArray()) { 834 throw notAnArray(array); 835 } else { 836 throw incompatibleType(array); 837 } 838 } 839 // END Android-added: Helper methods to support custom method implementations. 840 } 841