| 11 | package java.lang.reflect; |
| 12 | |
| 13 | public final class Array { |
| 14 | private Array() { } |
| 15 | |
| 16 | public static Object get(Object array, int index) { |
| 17 | String className = array.getClass().getName(); |
| 18 | if (! className.startsWith("[")) { |
| 19 | throw new IllegalArgumentException(); |
| 20 | } |
| 21 | |
| 22 | switch (className.charAt(1)) { |
| 23 | case 'B': |
| 24 | return Byte.valueOf(((byte[]) array)[index]); |
| 25 | case 'C': |
| 26 | return Character.valueOf(((char[]) array)[index]); |
| 27 | case 'D': |
| 28 | return Double.valueOf(((double[]) array)[index]); |
| 29 | case 'F': |
| 30 | return Float.valueOf(((float[]) array)[index]); |
| 31 | case 'I': |
| 32 | return Integer.valueOf(((int[]) array)[index]); |
| 33 | case 'J': |
| 34 | return Long.valueOf(((long[]) array)[index]); |
| 35 | case 'S': |
| 36 | return Short.valueOf(((short[]) array)[index]); |
| 37 | case 'Z': |
| 38 | return Boolean.valueOf(((boolean[]) array)[index]); |
| 39 | case 'L': |
| 40 | case '[': |
| 41 | return ((Object[]) array)[index]; |
| 42 | |
| 43 | default: |
| 44 | throw new Error(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public static void set(Object array, int index, Object value) { |
| 49 | String className = array.getClass().getName(); |
| 50 | if (! className.startsWith("[")) { |
| 51 | throw new IllegalArgumentException(); |
| 52 | } |
| 53 | |
| 54 | switch (className.charAt(1)) { |
| 55 | case 'B': |
| 56 | ((byte[]) array)[index] = (Byte) value; |
| 57 | break; |
| 58 | case 'C': |
| 59 | ((char[]) array)[index] = (Character) value; |
| 60 | break; |
| 61 | case 'D': |
| 62 | ((double[]) array)[index] = (Double) value; |
| 63 | break; |
| 64 | case 'F': |
| 65 | ((float[]) array)[index] = (Float) value; |
| 66 | break; |
| 67 | case 'I': |
| 68 | ((int[]) array)[index] = (Integer) value; |
| 69 | break; |
| 70 | case 'J': |
nothing calls this directly
no outgoing calls
no test coverage detected