Array.newInstance with multiple dimensions ArrayReflect
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 18 Jan 2013 18:35:17 +0100
branchArrayReflect
changeset 480dfebf4fbb711
parent 479 34931e381886
child 481 472209ab1112
Array.newInstance with multiple dimensions
emul/src/main/java/java/lang/reflect/Array.java
vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java
     1.1 --- a/emul/src/main/java/java/lang/reflect/Array.java	Fri Jan 18 17:04:16 2013 +0100
     1.2 +++ b/emul/src/main/java/java/lang/reflect/Array.java	Fri Jan 18 18:35:17 2013 +0100
     1.3 @@ -143,7 +143,12 @@
     1.4       */
     1.5      public static Object newInstance(Class<?> componentType, int... dimensions)
     1.6          throws IllegalArgumentException, NegativeArraySizeException {
     1.7 -        return multiNewArray(componentType, dimensions);
     1.8 +        StringBuilder sig = new StringBuilder();
     1.9 +        for (int i = 1; i < dimensions.length; i++) {
    1.10 +            sig.append('[');
    1.11 +        }
    1.12 +        sig.append(findSignature(componentType));
    1.13 +        return multiNewArray(sig.toString(), dimensions, 0);
    1.14      }
    1.15  
    1.16      /**
    1.17 @@ -521,9 +526,18 @@
    1.18      )
    1.19      private static native Object newArray(boolean primitive, String sig, int length);
    1.20  
    1.21 -    private static native Object multiNewArray(Class componentType,
    1.22 -        int[] dimensions)
    1.23 -        throws IllegalArgumentException, NegativeArraySizeException;
    1.24 +    private static Object multiNewArray(String sig, int[] dims, int index)
    1.25 +    throws IllegalArgumentException, NegativeArraySizeException {
    1.26 +        if (dims.length == index + 1) {
    1.27 +            return newArray(sig.length() == 2, sig, dims[index]);
    1.28 +        }
    1.29 +        Object[] arr = (Object[]) newArray(false, sig, dims[index]);
    1.30 +        String compsig = sig.substring(1);
    1.31 +        for (int i = 0; i < arr.length; i++) {
    1.32 +            arr[i] = multiNewArray(compsig, dims, index + 1);
    1.33 +        }
    1.34 +        return arr;
    1.35 +    }
    1.36  
    1.37  
    1.38      
     2.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java	Fri Jan 18 17:04:16 2013 +0100
     2.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java	Fri Jan 18 18:35:17 2013 +0100
     2.3 @@ -55,6 +55,15 @@
     2.4          return Array.newInstance(int.class, -5);
     2.5      }
     2.6      
     2.7 +    @Compare public int multiIntArray() {
     2.8 +        int[][][] arr = (int[][][]) Array.newInstance(int.class, 3, 3, 3);
     2.9 +        return arr[0][1][2] + 5 + arr[2][2][0];
    2.10 +    }
    2.11 +
    2.12 +    @Compare public String multiIntArrayCompType() {
    2.13 +        return Array.newInstance(int.class, 3, 3, 3).getClass().getName();
    2.14 +    }
    2.15 +    
    2.16      
    2.17      @Factory
    2.18      public static Object[] create() {