151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 3b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang.reflect; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkinimport dalvik.annotation.optimization.FastNative; 30fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Array} class provides static methods to dynamically create and 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access Java arrays. 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>{@code Array} permits widening conversions to occur during a get or set 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operation, but throws an {@code IllegalArgumentException} if a narrowing 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion would occur. 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Nakul Saraiya 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiclass Array { 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Class Array is not instantiable. 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Array() {} 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new array with the specified component type and 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length. 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Invoking this method is equivalent to creating an array 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as follows: 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * int[] x = {length}; 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Array.newInstance(componentType, x); 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 60b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * <p>The number of dimensions of the new array must not 61b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * exceed 255. 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param componentType the {@code Class} object representing the 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type of the new array 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param length the length of the new array 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the new array 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} parameter is null 69b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * @exception IllegalArgumentException if componentType is {@link 70b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * Void#TYPE} or if the number of dimensions of the requested array 71b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * instance exceed 255. 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NegativeArraySizeException if the specified {@code length} 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is negative 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Object newInstance(Class<?> componentType, int length) 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NegativeArraySizeException { 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return newArray(componentType, length); 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new array 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the specified component type and dimensions. 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If {@code componentType} 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a non-array class or interface, the new array 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has {@code dimensions.length} dimensions and 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} as its component type. If 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} represents an array class, the 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number of dimensions of the new array is equal to the sum 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of {@code dimensions.length} and the number of 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * dimensions of {@code componentType}. In this case, the 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type of the new array is the component type of 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType}. 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The number of dimensions of the new array must not 95b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * exceed 255. 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param componentType the {@code Class} object representing the component 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type of the new array 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param dimensions an array of {@code int} representing the dimensions of 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the new array 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the new array 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} argument is null 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the specified {@code dimensions} 105b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * argument is a zero-dimensional array, if componentType is {@link 106b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * Void#TYPE}, or if the number of dimensions of the requested array 107b464cd3f7909638eaae0cfd30935e8fb22297153Przemyslaw Szczepaniak * instance exceed 255. 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NegativeArraySizeException if any of the components in 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified {@code dimensions} argument is negative. 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Object newInstance(Class<?> componentType, int... dimensions) 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws IllegalArgumentException, NegativeArraySizeException { 1138ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (dimensions.length <= 0 || dimensions.length > 255) { 1148ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new IllegalArgumentException("Bad number of dimensions: " + dimensions.length); 1158ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 1168ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (componentType == void.class) { 1178ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new IllegalArgumentException("Can't allocate an array of void"); 1188ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 1198ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (componentType == null) { 1208ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new NullPointerException("componentType == null"); 1218ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 12205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return createMultiArray(componentType, dimensions); 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the length of the specified array object, as an {@code int}. 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the length of the array 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the object argument is not 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static int getLength(Object array) { 13405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 13505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((Object[]) array).length; 13605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof boolean[]) { 13705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array).length; 13805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 13905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array).length; 14005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 14105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array).length; 14205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 14305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((double[]) array).length; 14405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 14505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array).length; 14605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 14705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array).length; 14805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 14905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array).length; 15005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 15105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array).length; 15205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 15305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 15405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object. The value is automatically wrapped in an object 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it has a primitive type. 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the (possibly wrapped) value of the indexed component in 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 17205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 17305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 17405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((Object[]) array)[index]; 17505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 17605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 17705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; 17805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 17905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 18005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Byte.valueOf(((byte[]) array)[index]); 18105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 18305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Character.valueOf(((char[]) array)[index]); 18405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 18605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Short.valueOf(((short[]) array)[index]); 18705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 18905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Integer.valueOf(((int[]) array)[index]); 19005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 19205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Long.valueOf(((long[]) array)[index]); 19305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 19505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new Float(((float[]) array)[index]); 19605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 19805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new Double(((double[]) array)[index]); 19905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 20005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array == null) { 20105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new NullPointerException("array == null"); 20205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 20305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 20405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code boolean}. 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 22205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 22305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 22405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array)[index]; 22505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 22605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 22705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code byte}. 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 24605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 24705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 24805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 24905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 25005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code char}. 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 26905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 27005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 27105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 27205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 27305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code short}. 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 29105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 29205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 29305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 29405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 29505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 29605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 29705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 29805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as an {@code int}. 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 31605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 31705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 31805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 31905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 32005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 32105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 32205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 32305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 32405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 32505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 32605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 32705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code long}. 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 34505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 34605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 34705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 34805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 34905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 35005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 35105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 35205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 35305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 35405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 35505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 35605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 35705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 35805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code float}. 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 37605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 37705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 37805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array)[index]; 37905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 38005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 38105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 38205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 38305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 38405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 38505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 38605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 38705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 38805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 38905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 39005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 39105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code double}. 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 40905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 41005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 41105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((double[]) array)[index]; 41205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 41305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 41405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 41505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 41605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 41705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array)[index]; 41805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 41905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 42005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 42105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 42205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 42305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 42405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 42505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 42605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified new value. The new value is first 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * automatically unwrapped if the array has a primitive component 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value the new value of the indexed component 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the array component type is primitive and 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an unwrapping conversion fails 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 44505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 44605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (!array.getClass().isArray()) { 44705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 44805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 44905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski 45005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 45105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value != null && !array.getClass().getComponentType().isInstance(value)) { 45205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw incompatibleType(array); 45305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 45405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((Object[]) array)[index] = value; 45505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 45605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value == null) { 45705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Primitive array can't take null values."); 45805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 45905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value instanceof Boolean) { 46005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setBoolean(array, index, ((Boolean) value).booleanValue()); 46105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Byte) { 46205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setByte(array, index, ((Byte) value).byteValue()); 46305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Character) { 46405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setChar(array, index, ((Character) value).charValue()); 46505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Short) { 46605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setShort(array, index, ((Short) value).shortValue()); 46705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Integer) { 46805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setInt(array, index, ((Integer) value).intValue()); 46905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Long) { 47005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setLong(array, index, ((Long) value).longValue()); 47105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Float) { 47205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setFloat(array, index, ((Float) value).floatValue()); 47305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Double) { 47405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setDouble(array, index, ((Double) value).doubleValue()); 47505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code boolean} value. 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 4849e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4966975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/z/value 49705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setBoolean(Object array, int index, boolean value) { 49805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 49905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((boolean[]) array)[index] = value; 50005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 50105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 50205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 50305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code byte} value. 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5109e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5226975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/b/value 52305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setByte(Object array, int index, byte value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 52405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 52505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((byte[]) array)[index] = value; 52605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 52705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 52805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 52905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 53005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 53105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 53205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 53305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 53405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 53505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((short[]) array)[index] = value; 53605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 53705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 53805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 53905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code char} value. 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5469e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5586975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/c/value 55905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setChar(Object array, int index, char value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 56005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 56105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((char[]) array)[index] = value; 56205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 56305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 56405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 56505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 56605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 56705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 56805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 56905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 57005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 57105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 57205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 57305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code short} value. 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5809e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5926975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/s/value 59305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setShort(Object array, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 59405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 59505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((short[]) array)[index] = value; 59605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 59705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 59805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 59905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 60005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 60105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 60205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 60305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 60405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 60505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 60605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 60705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code int} value. 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6149e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6266975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/i/value 62705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 62805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 62905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 63005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 63105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 63205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 63305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 63405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 63505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 63605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 63705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 63805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 63905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code long} value. 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6469e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6586975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/l/value 65905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setLong(Object array, int index, long value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 66005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 66105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 66205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 66305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 66405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 66505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 66605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 66705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 66805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 66905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code float} value. 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6769e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6886975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/f/value 68905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setFloat(Object array, int index, float value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 69005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 69105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 69205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 69305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 69405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 69505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 69605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 69705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code double} value. 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 7049e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7166975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed param name s/d/value 71705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setDouble(Object array, int index, double value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 71805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 71905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 72005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 72105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 72205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 72305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 72605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Create a multi-dimensional array of objects with the specified type. 72705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski */ 728fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 72905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static native Object createMultiArray(Class<?> componentType, int[] dimensions) throws NegativeArraySizeException; 73005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski 73105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski /** 73205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Returns a new array of the specified component type and length. 73305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Equivalent to {@code new componentType[size]}. 73405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * 73505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * @throws NullPointerException 73605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * if the component type is null 73705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * @throws NegativeArraySizeException 73805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * if {@code size < 0} 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7409fbdd1b27de1a3d10054681082fde07845fb8529Narayan Kamath private static Object newArray(Class<?> componentType, int size) throws NegativeArraySizeException { 74105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (!componentType.isPrimitive()) { 74205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return createObjectArray(componentType, size); 74305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == char.class) { 74405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new char[size]; 74505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == int.class) { 74605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new int[size]; 74705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == byte.class) { 74805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new byte[size]; 74905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == boolean.class) { 75005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new boolean[size]; 75105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == short.class) { 75205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new short[size]; 75305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == long.class) { 75405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new long[size]; 75505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == float.class) { 75605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new float[size]; 75705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == double.class) { 75805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new double[size]; 75905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == void.class) { 76005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Can't allocate an array of void"); 76105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 76205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new AssertionError(); 76305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski /* 76605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Create a one-dimensional array of objects with the specified type. 76705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski */ 768fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 76905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static native Object createObjectArray(Class<?> componentType, int length) throws NegativeArraySizeException; 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static IllegalArgumentException notAnArray(Object o) { 77205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Not an array: " + o.getClass()); 77305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static IllegalArgumentException incompatibleType(Object o) { 77605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Array has incompatible type: " + o.getClass()); 77705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static RuntimeException badArray(Object array) { 78005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array == null) { 78105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new NullPointerException("array == null"); 78205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (!array.getClass().isArray()) { 78305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 78405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 78505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw incompatibleType(array); 78605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 78705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 789