151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1996, 2006, 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 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Array} class provides static methods to dynamically create and 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access Java arrays. 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>{@code Array} permits widening conversions to occur during a get or set 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operation, but throws an {@code IllegalArgumentException} if a narrowing 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion would occur. 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Nakul Saraiya 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiclass Array { 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Class Array is not instantiable. 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Array() {} 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new array with the specified component type and 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length. 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Invoking this method is equivalent to creating an array 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as follows: 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * int[] x = {length}; 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Array.newInstance(componentType, x); 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param componentType the {@code Class} object representing the 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type of the new array 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param length the length of the new array 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the new array 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} parameter is null 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if componentType is {@link Void#TYPE} 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NegativeArraySizeException if the specified {@code length} 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is negative 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Object newInstance(Class<?> componentType, int length) 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NegativeArraySizeException { 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return newArray(componentType, length); 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new array 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the specified component type and dimensions. 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If {@code componentType} 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a non-array class or interface, the new array 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has {@code dimensions.length} dimensions and 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} as its component type. If 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} represents an array class, the 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number of dimensions of the new array is equal to the sum 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of {@code dimensions.length} and the number of 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * dimensions of {@code componentType}. In this case, the 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type of the new array is the component type of 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType}. 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The number of dimensions of the new array must not 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exceed the number of array dimensions supported by the 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation (typically 255). 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param componentType the {@code Class} object representing the component 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type of the new array 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param dimensions an array of {@code int} representing the dimensions of 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the new array 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the new array 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code componentType} argument is null 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the specified {@code dimensions} 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is a zero-dimensional array, or if the number of 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * requested dimensions exceeds the limit on the number of array dimensions 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * supported by the implementation (typically 255), or if componentType 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is {@link Void#TYPE}. 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NegativeArraySizeException if any of the components in 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified {@code dimensions} argument is negative. 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Object newInstance(Class<?> componentType, int... dimensions) 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws IllegalArgumentException, NegativeArraySizeException { 1098ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (dimensions.length <= 0 || dimensions.length > 255) { 1108ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new IllegalArgumentException("Bad number of dimensions: " + dimensions.length); 1118ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 1128ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (componentType == void.class) { 1138ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new IllegalArgumentException("Can't allocate an array of void"); 1148ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 1158ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak if (componentType == null) { 1168ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak throw new NullPointerException("componentType == null"); 1178ee7abf4ac99dbf807471de86e3e7dc01cac9bf8Przemyslaw Szczepaniak } 11805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return createMultiArray(componentType, dimensions); 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the length of the specified array object, as an {@code int}. 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the length of the array 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the object argument is not 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static int getLength(Object array) { 13005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 13105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((Object[]) array).length; 13205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof boolean[]) { 13305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array).length; 13405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 13505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array).length; 13605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 13705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array).length; 13805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 13905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((double[]) array).length; 14005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 14105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array).length; 14205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 14305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array).length; 14405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 14505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array).length; 14605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 14705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array).length; 14805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 14905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 15005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object. The value is automatically wrapped in an object 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it has a primitive type. 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the (possibly wrapped) value of the indexed component in 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 16905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 17005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((Object[]) array)[index]; 17105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 17205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 17305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; 17405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 17505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 17605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Byte.valueOf(((byte[]) array)[index]); 17705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 17805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 17905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Character.valueOf(((char[]) array)[index]); 18005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 18205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Short.valueOf(((short[]) array)[index]); 18305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 18505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Integer.valueOf(((int[]) array)[index]); 18605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 18705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 18805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return Long.valueOf(((long[]) array)[index]); 18905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 19105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new Float(((float[]) array)[index]); 19205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 19405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new Double(((double[]) array)[index]); 19505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array == null) { 19705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new NullPointerException("array == null"); 19805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 19905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 20005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code boolean}. 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 21805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 21905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 22005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((boolean[]) array)[index]; 22105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 22205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 22305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code byte}. 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 24205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 24305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 24405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 24505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 24605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code char}. 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 26505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 26605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 26705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 26805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 26905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code short}. 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 28805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 28905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 29005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 29105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 29205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 29305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 29405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as an {@code int}. 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 31205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 31305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 31405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 31505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 31605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 31705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 31805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 31905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 32005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 32105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 32205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 32305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code long}. 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 34105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 34205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 34305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 34405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 34505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 34605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 34705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 34805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 34905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 35005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 35105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 35205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 35305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 35405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code float}. 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 37205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 37305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 37405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array)[index]; 37505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 37605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 37705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 37805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 37905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 38005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 38105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 38205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 38305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 38405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 38505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 38605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 38705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of the indexed component in the specified 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array object, as a {@code double}. 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value of the indexed component in the specified array 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object is null 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object is not 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array, or if the indexed element cannot be converted to the 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type by an identity or widening conversion 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to the 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length of the specified array 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#get 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 40505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 40605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 40705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((double[]) array)[index]; 40805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof byte[]) { 40905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((byte[]) array)[index]; 41005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof char[]) { 41105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((char[]) array)[index]; 41205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 41305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((float[]) array)[index]; 41405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 41505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((int[]) array)[index]; 41605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 41705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((long[]) array)[index]; 41805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 41905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return ((short[]) array)[index]; 42005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 42105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 42205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified new value. The new value is first 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * automatically unwrapped if the array has a primitive component 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value the new value of the indexed component 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the array component type is primitive and 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an unwrapping conversion fails 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 44105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 44205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (!array.getClass().isArray()) { 44305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 44405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 44505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski 44605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof Object[]) { 44705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value != null && !array.getClass().getComponentType().isInstance(value)) { 44805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw incompatibleType(array); 44905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 45005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((Object[]) array)[index] = value; 45105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 45205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value == null) { 45305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Primitive array can't take null values."); 45405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 45505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (value instanceof Boolean) { 45605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setBoolean(array, index, ((Boolean) value).booleanValue()); 45705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Byte) { 45805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setByte(array, index, ((Byte) value).byteValue()); 45905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Character) { 46005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setChar(array, index, ((Character) value).charValue()); 46105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Short) { 46205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setShort(array, index, ((Short) value).shortValue()); 46305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Integer) { 46405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setInt(array, index, ((Integer) value).intValue()); 46505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Long) { 46605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setLong(array, index, ((Long) value).longValue()); 46705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Float) { 46805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setFloat(array, index, ((Float) value).floatValue()); 46905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (value instanceof Double) { 47005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski setDouble(array, index, ((Double) value).doubleValue()); 47105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code boolean} value. 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 4809e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4929e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/z/value 49305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setBoolean(Object array, int index, boolean value) { 49405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof boolean[]) { 49505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((boolean[]) array)[index] = value; 49605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 49705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 49805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 49905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code byte} value. 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5069e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5189e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/b/value 51905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setByte(Object array, int index, byte value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 52005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof byte[]) { 52105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((byte[]) array)[index] = value; 52205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 52305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 52405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 52505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 52605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 52705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 52805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 52905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 53005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof short[]) { 53105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((short[]) array)[index] = value; 53205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 53305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 53405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 53505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code char} value. 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5429e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5549e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/c/value 55505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setChar(Object array, int index, char value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 55605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof char[]) { 55705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((char[]) array)[index] = value; 55805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 55905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 56005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 56105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 56205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 56305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 56405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 56505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 56605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 56705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 56805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 56905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code short} value. 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 5769e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5889e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/s/value 58905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setShort(Object array, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 59005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof short[]) { 59105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((short[]) array)[index] = value; 59205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 59305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 59405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 59505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 59605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof int[]) { 59705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 59805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 59905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 60005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 60105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 60205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 60305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code int} value. 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6109e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6229e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/i/value 62305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 62405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof int[]) { 62505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((int[]) array)[index] = value; 62605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 62705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 62805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 62905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 63005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof long[]) { 63105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 63205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 63305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 63405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 63505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code long} value. 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6429e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6549e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/l/value 65505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setLong(Object array, int index, long value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 65605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof long[]) { 65705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((long[]) array)[index] = value; 65805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 65905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 66005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof float[]) { 66105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 66205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 66305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 66405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 66505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code float} value. 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 6729e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6849e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/f/value 68505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setFloat(Object array, int index, float value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 68605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof float[]) { 68705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((float[]) array)[index] = value; 68805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (array instanceof double[]) { 68905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 69005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 69105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 69205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 69305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the value of the indexed component of the specified array 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object to the specified {@code double} value. 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param array the array 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param index the index into the array 7009e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param value the new value of the indexed component 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException If the specified object argument 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the specified object argument 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not an array, or if the specified value cannot be converted 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the underlying array's component type by an identity or a 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive widening conversion 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ArrayIndexOutOfBoundsException If the specified {@code index} 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument is negative, or if it is greater than or equal to 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the specified array 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Array#set 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7129e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath // Android changed param name s/d/value 71305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski public static void setDouble(Object array, int index, double value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { 71405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array instanceof double[]) { 71505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski ((double[]) array)[index] = value; 71605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 71705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw badArray(array); 71805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 71905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 72205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Create a multi-dimensional array of objects with the specified type. 72305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski */ 72405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static native Object createMultiArray(Class<?> componentType, int[] dimensions) throws NegativeArraySizeException; 72505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski 72605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski /** 72705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Returns a new array of the specified component type and length. 72805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Equivalent to {@code new componentType[size]}. 72905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * 73005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * @throws NullPointerException 73105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * if the component type is null 73205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * @throws NegativeArraySizeException 73305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * if {@code size < 0} 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7359fbdd1b27de1a3d10054681082fde07845fb8529Narayan Kamath private static Object newArray(Class<?> componentType, int size) throws NegativeArraySizeException { 73605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (!componentType.isPrimitive()) { 73705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return createObjectArray(componentType, size); 73805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == char.class) { 73905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new char[size]; 74005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == int.class) { 74105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new int[size]; 74205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == byte.class) { 74305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new byte[size]; 74405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == boolean.class) { 74505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new boolean[size]; 74605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == short.class) { 74705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new short[size]; 74805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == long.class) { 74905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new long[size]; 75005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == float.class) { 75105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new float[size]; 75205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == double.class) { 75305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski return new double[size]; 75405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (componentType == void.class) { 75505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Can't allocate an array of void"); 75605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 75705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new AssertionError(); 75805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski /* 76105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski * Create a one-dimensional array of objects with the specified type. 76205f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski */ 76305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static native Object createObjectArray(Class<?> componentType, int length) throws NegativeArraySizeException; 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static IllegalArgumentException notAnArray(Object o) { 76605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Not an array: " + o.getClass()); 76705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static IllegalArgumentException incompatibleType(Object o) { 77005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new IllegalArgumentException("Array has incompatible type: " + o.getClass()); 77105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77305f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski private static RuntimeException badArray(Object array) { 77405f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski if (array == null) { 77505f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw new NullPointerException("array == null"); 77605f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else if (!array.getClass().isArray()) { 77705f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw notAnArray(array); 77805f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } else { 77905f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski throw incompatibleType(array); 78005f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 78105f3b6342e5375c8994e1d54c3f588e09ef21854Piotr Jastrzebski } 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 783