1f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes/* 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership. 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License. You may obtain a copy of the License at 8f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 10f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/* 18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * you may not use this file except in compliance with the License. 22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * You may obtain a copy of the License at 23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.lang.reflect; 34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 36f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Provides static methods to create and access arrays dynamically. 37adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic final class Array { 39f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes private Array() { 40f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 41f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 42f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes private static IllegalArgumentException notAnArray(Object o) { 43f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new IllegalArgumentException("Not an array: " + o.getClass()); 44f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 45f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes 46f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes private static IllegalArgumentException incompatibleType(Object o) { 47f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new IllegalArgumentException("Array has incompatible type: " + o.getClass()); 48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 49f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 50ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes private static RuntimeException badArray(Object array) { 51ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes if (array == null) { 52ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes throw new NullPointerException("array == null"); 53ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes } else if (!array.getClass().isArray()) { 54ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes throw notAnArray(array); 55ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes } else { 56ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes throw incompatibleType(array); 57ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes } 58ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes } 59ad369fb678d4f6148fcb635449f8852906b2a6a7Elliott Hughes 60adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 61f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the element of the array at the specified index. Equivalent to {@code array[index]}. 62f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * If the array component is a primitive type, the result is automatically boxed. 63f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 64f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 65adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array 67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 69adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 70f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 71f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof Object[]) { 72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((Object[]) array)[index]; 73f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 74f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof boolean[]) { 75adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; 76f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 77f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof byte[]) { 78a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return Byte.valueOf(((byte[]) array)[index]); 79f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 80f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof char[]) { 81a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return Character.valueOf(((char[]) array)[index]); 82f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 83f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof short[]) { 84a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return Short.valueOf(((short[]) array)[index]); 85f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 86f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof int[]) { 87a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return Integer.valueOf(((int[]) array)[index]); 88f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 89f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof long[]) { 90a0a4196cb15480959f053d0ebe6b412bd23c8170Elliott Hughes return Long.valueOf(((long[]) array)[index]); 91f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 92f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof float[]) { 93adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new Float(((float[]) array)[index]); 94f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 95f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof double[]) { 96adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new Double(((double[]) array)[index]); 97f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 98f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array == null) { 99f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new NullPointerException("array == null"); 100f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 101f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw notAnArray(array); 102f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 103f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 105f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the boolean at the given index in the given boolean array. 106f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 107f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 114f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof boolean[]) { 116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((boolean[]) array)[index]; 117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 118f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 119adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 122f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the byte at the given index in the given byte array. 123f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 124f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 127adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 128adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 131f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof byte[]) { 133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((byte[]) array)[index]; 134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 135f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 139f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the char at the given index in the given char array. 140f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 141f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 142adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 148f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 149adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof char[]) { 150adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((char[]) array)[index]; 151adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 152f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 153adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 156f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the double at the given index in the given array. 157f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Applies to byte, char, float, double, int, long, and short arrays. 158f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 159f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 166f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof double[]) { 168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((double[]) array)[index]; 169f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 170f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((byte[]) array)[index]; 171f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof char[]) { 172f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((char[]) array)[index]; 173f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 174f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((float[]) array)[index]; 175f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 176f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((int[]) array)[index]; 177f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 178f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((long[]) array)[index]; 179f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 180f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((short[]) array)[index]; 181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 182f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 186f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the float at the given index in the given array. 187f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Applies to byte, char, float, int, long, and short arrays. 188f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 189f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 190adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 191adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 192adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 193adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 194adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 195adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 196f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 197adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof float[]) { 198adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((float[]) array)[index]; 199f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 200f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((byte[]) array)[index]; 201f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof char[]) { 202f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((char[]) array)[index]; 203f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 204f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((int[]) array)[index]; 205f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 206f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((long[]) array)[index]; 207f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 208f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((short[]) array)[index]; 209adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 210f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 214f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the int at the given index in the given array. 215f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Applies to byte, char, int, and short arrays. 216f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 217f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 218adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 223adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 224f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 225adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof int[]) { 226adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((int[]) array)[index]; 227f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 228f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((byte[]) array)[index]; 229f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof char[]) { 230f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((char[]) array)[index]; 231f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 232f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((short[]) array)[index]; 233adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 234f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 235adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 236adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 237adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 238f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the length of the array. Equivalent to {@code array.length}. 239f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 240f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 241adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 242adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array 243adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 244adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public static int getLength(Object array) { 245f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof Object[]) { 246adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((Object[]) array).length; 247f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof boolean[]) { 248adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((boolean[]) array).length; 249f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 250adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((byte[]) array).length; 251f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof char[]) { 252adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((char[]) array).length; 253f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 254f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((double[]) array).length; 255f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 256f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((float[]) array).length; 257f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 258adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((int[]) array).length; 259f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 260adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((long[]) array).length; 261f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 262f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((short[]) array).length; 263f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 264f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 265f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 266adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 267adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 268f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the long at the given index in the given array. 269f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Applies to byte, char, int, long, and short arrays. 270f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 271f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 272adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 273adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 274adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 278f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 279adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof long[]) { 280adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((long[]) array)[index]; 281f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 282f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((byte[]) array)[index]; 283f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof char[]) { 284f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((char[]) array)[index]; 285f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 286f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((int[]) array)[index]; 287f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 288f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((short[]) array)[index]; 289adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 290f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 291adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 292adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 293adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 294f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns the short at the given index in the given array. 295f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Applies to byte and short arrays. 296f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 297f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 298adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 299adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the element at the 300adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * index position can not be converted to the return type 301adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 302adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 303adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 304f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 305f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (array instanceof short[]) { 306adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return ((short[]) array)[index]; 307f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof byte[]) { 308f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes return ((byte[]) array)[index]; 309f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 310f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 311adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 312adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 314adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Returns a new multidimensional array of the specified component type and 315f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * dimensions. Equivalent to {@code new componentType[d0][d1]...[dn]} for a 316f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * dimensions array of { d0, d1, ... , dn }. 317f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 318f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 319adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws NegativeArraySizeException 320adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if any of the dimensions are negative 321adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 322adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the array of dimensions is of size zero, or exceeds the 323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limit of the number of dimension for an array (currently 255) 324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 325f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static Object newInstance(Class<?> componentType, int... dimensions) throws NegativeArraySizeException, IllegalArgumentException { 326f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (dimensions.length <= 0 || dimensions.length > 255) { 327f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new IllegalArgumentException("Bad number of dimensions: " + dimensions.length); 328f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 329f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (componentType == void.class) { 330f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new IllegalArgumentException("Can't allocate an array of void"); 331f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 332f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (componentType == null) { 333f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new NullPointerException("componentType == null"); 334f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 335adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return createMultiArray(componentType, dimensions); 336adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 337adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 338adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /* 339adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Create a multi-dimensional array of objects with the specified type. 340adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 341f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes private static native Object createMultiArray(Class<?> componentType, int[] dimensions) throws NegativeArraySizeException; 342adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 343adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 344f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Returns a new array of the specified component type and length. 345f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Equivalent to {@code new componentType[size]}. 346f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 347adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws NullPointerException 348adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the component type is null 349adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws NegativeArraySizeException 350adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code size < 0} 351adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 352f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static Object newInstance(Class<?> componentType, int size) throws NegativeArraySizeException { 353e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes if (!componentType.isPrimitive()) { 354adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return createObjectArray(componentType, size); 355f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == boolean.class) { 356adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new boolean[size]; 357f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == byte.class) { 358adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new byte[size]; 359f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == char.class) { 360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new char[size]; 361f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == short.class) { 362adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new short[size]; 363f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == int.class) { 364adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new int[size]; 365f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == long.class) { 366adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new long[size]; 367f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == float.class) { 368adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new float[size]; 369f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == double.class) { 370adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project return new double[size]; 371f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (componentType == void.class) { 372f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw new IllegalArgumentException("Can't allocate an array of void"); 373e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes } 374e26ba79900d471d02d656f686926918ef7dc751fElliott Hughes throw new AssertionError(); 375adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 376adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 377adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /* 378adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Create a one-dimensional array of objects with the specified type. 379adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 380f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes private static native Object createObjectArray(Class<?> componentType, int length) throws NegativeArraySizeException; 381f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 382adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 383f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets the element of the array at the specified index to the value. 384f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Equivalent to {@code array[index] = value}. If the array 3858da55422ed93013260c1536080b14661bfecfefbElliott Hughes * component is a primitive type, the value is automatically unboxed. 386f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 387f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 388adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 389adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code array} is not an array or the value cannot be 390adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 391adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 392adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 393adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 394f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (!array.getClass().isArray()) { 396f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw notAnArray(array); 397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 398f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 399adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof Object[]) { 400f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (value != null && !array.getClass().getComponentType().isInstance(value)) { 401f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw incompatibleType(array); 402adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 403adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((Object[]) array)[index] = value; 404adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 405adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (value == null) { 406adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project throw new IllegalArgumentException("Primitive array can't take null values."); 407adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 408f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes if (value instanceof Boolean) { 409adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setBoolean(array, index, ((Boolean) value).booleanValue()); 410f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Byte) { 411adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setByte(array, index, ((Byte) value).byteValue()); 412f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Character) { 413adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setChar(array, index, ((Character) value).charValue()); 414f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Short) { 415adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setShort(array, index, ((Short) value).shortValue()); 416f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Integer) { 417adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setInt(array, index, ((Integer) value).intValue()); 418f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Long) { 419adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setLong(array, index, ((Long) value).longValue()); 420f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Float) { 421adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setFloat(array, index, ((Float) value).floatValue()); 422f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (value instanceof Double) { 423adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project setDouble(array, index, ((Double) value).doubleValue()); 424f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } 425adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 426adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 427adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 428adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 429f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to boolean arrays. 430f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 431f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 432adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 433adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 434adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 435adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 436adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 437adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 438adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project public static void setBoolean(Object array, int index, boolean value) { 439adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof boolean[]) { 440adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((boolean[]) array)[index] = value; 441adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 442f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 447f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to byte, double, float, int, long, and short arrays. 448f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 449f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 450adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 452adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 453adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 454adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 455adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 456f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setByte(Object array, int index, byte value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 457adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof byte[]) { 458adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((byte[]) array)[index] = value; 459f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 460f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 461f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 462f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((float[]) array)[index] = value; 463f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 464f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((int[]) array)[index] = value; 465f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 466f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((long[]) array)[index] = value; 467f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof short[]) { 468f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((short[]) array)[index] = value; 469adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 470f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 472adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 473adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 474adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 475f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to char, double, float, int, and long arrays. 476f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 477f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 478adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 479adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 480adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 481adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 482adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 483adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 484f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setChar(Object array, int index, char value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 485adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof char[]) { 486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((char[]) array)[index] = value; 487f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 488f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 489f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 490f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((float[]) array)[index] = value; 491f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 492f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((int[]) array)[index] = value; 493f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 494f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((long[]) array)[index] = value; 495adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 496f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 497adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 498adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 499adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 500adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 501f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to double arrays. 502f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 503f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 504adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 505adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 506adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 507adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 508adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 510f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setDouble(Object array, int index, double value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 511adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof double[]) { 512adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((double[]) array)[index] = value; 513adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 514f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 515adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 516adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 517adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 518adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 519f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to double and float arrays. 520f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 521f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 522adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 523adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 524adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 525adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 526adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 527adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 528f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setFloat(Object array, int index, float value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 529adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof float[]) { 530adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((float[]) array)[index] = value; 531f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 532f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 533adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 534f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 535adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 537adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 538adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 539f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to double, float, int, and long arrays. 540f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 541f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 542adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 543adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 544adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 545adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 546adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 547adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 548f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 549adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof int[]) { 550adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((int[]) array)[index] = value; 551f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 552f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 553f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 554f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((float[]) array)[index] = value; 555f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 556f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((long[]) array)[index] = value; 557adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 558f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 559adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 560adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 561adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 562adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 563f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to double, float, and long arrays. 564f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 565f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 566adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 567adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 568adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 569adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 570adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 571adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 572f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setLong(Object array, int index, long value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 573adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof long[]) { 574adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((long[]) array)[index] = value; 575f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 576f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 577f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 578f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((float[]) array)[index] = value; 579adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 580f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 581adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 582adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 583adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 584adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 585f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * Sets {@code array[index] = value}. Applies to double, float, int, long, and short arrays. 586f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 587f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes * @throws NullPointerException if {@code array == null} 588adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws IllegalArgumentException 589adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if the {@code array} is not an array or the value cannot be 590adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * converted to the array type by a widening conversion 591adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @throws ArrayIndexOutOfBoundsException 592adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if {@code index < 0 || index >= array.length} 593adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 594f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes public static void setShort(Object array, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 595adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project if (array instanceof short[]) { 596adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project ((short[]) array)[index] = value; 597f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof double[]) { 598f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((double[]) array)[index] = value; 599f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof float[]) { 600f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((float[]) array)[index] = value; 601f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof int[]) { 602f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((int[]) array)[index] = value; 603f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes } else if (array instanceof long[]) { 604f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes ((long[]) array)[index] = value; 605adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } else { 606f449df0090c25793242f3ecc1653b6b0c331be92Elliott Hughes throw badArray(array); 607adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 608adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 609adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 610