Basic support for reflection on Array ArrayReflect
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 18 Jan 2013 16:52:20 +0100
branchArrayReflect
changeset 47722e99afe5083
parent 476 c21c98f493bd
child 479 34931e381886
Basic support for reflection on Array
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 12:18:28 2013 +0100
     1.2 +++ b/emul/src/main/java/java/lang/reflect/Array.java	Fri Jan 18 16:52:20 2013 +0100
     1.3 @@ -25,6 +25,8 @@
     1.4  
     1.5  package java.lang.reflect;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8 +
     1.9  /**
    1.10   * The {@code Array} class provides static methods to dynamically create and
    1.11   * access Java arrays.
    1.12 @@ -66,10 +68,44 @@
    1.13       * is negative
    1.14       */
    1.15      public static Object newInstance(Class<?> componentType, int length)
    1.16 -        throws NegativeArraySizeException {
    1.17 -        return newArray(componentType, length);
    1.18 +    throws NegativeArraySizeException {
    1.19 +        if (length < 0) {
    1.20 +            throw new NegativeArraySizeException();
    1.21 +        }
    1.22 +        String sig = findSignature(componentType);
    1.23 +        return newArray(componentType.isPrimitive(), sig, length);
    1.24      }
    1.25 -
    1.26 +    
    1.27 +    private static String findSignature(Class<?> type) {
    1.28 +        if (type == Integer.TYPE) {
    1.29 +            return "[I";
    1.30 +        }
    1.31 +        if (type == Long.TYPE) {
    1.32 +            return "[J";
    1.33 +        }
    1.34 +        if (type == Double.TYPE) {
    1.35 +            return "[D";
    1.36 +        }
    1.37 +        if (type == Float.TYPE) {
    1.38 +            return "[F";
    1.39 +        }
    1.40 +        if (type == Byte.TYPE) {
    1.41 +            return "[B";
    1.42 +        }
    1.43 +        if (type == Boolean.TYPE) {
    1.44 +            return "[Z";
    1.45 +        }
    1.46 +        if (type == Short.TYPE) {
    1.47 +            return "[S";
    1.48 +        }
    1.49 +        if (type == Character.TYPE) {
    1.50 +            return "[C";
    1.51 +        }
    1.52 +        if (type.getName().equals("void")) {
    1.53 +            throw new IllegalStateException("Can't create array for " + type);
    1.54 +        }
    1.55 +        return "[L" + type.getName() + ";";
    1.56 +    }
    1.57      /**
    1.58       * Creates a new array
    1.59       * with the specified component type and dimensions.
    1.60 @@ -474,12 +510,19 @@
    1.61       * Private
    1.62       */
    1.63  
    1.64 -    private static native Object newArray(Class componentType, int length)
    1.65 -        throws NegativeArraySizeException;
    1.66 +    @JavaScriptBody(args = { "primitive", "sig", "length" }, body =
    1.67 +          "var arr = new Array(length);\n"
    1.68 +        + "var value = primitive ? 0 : null;\n"
    1.69 +        + "for(var i = 0; i < length; i++) arr[i] = value;\n"
    1.70 +        + "arr.jvmName = sig;\n"
    1.71 +        + "return arr;"
    1.72 +    )
    1.73 +    private static native Object newArray(boolean primitive, String sig, int length);
    1.74  
    1.75      private static native Object multiNewArray(Class componentType,
    1.76          int[] dimensions)
    1.77          throws IllegalArgumentException, NegativeArraySizeException;
    1.78  
    1.79  
    1.80 +    
    1.81  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java	Fri Jan 18 16:52:20 2013 +0100
     2.3 @@ -0,0 +1,63 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.tck;
    2.22 +
    2.23 +import java.lang.reflect.Array;
    2.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
    2.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    2.26 +import org.testng.annotations.Factory;
    2.27 +
    2.28 +/**
    2.29 + *
    2.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.31 + */
    2.32 +public class ReflectionArrayTest {
    2.33 +    @Compare public int lengthOfStringArray() {
    2.34 +        String[] arr = (String[]) Array.newInstance(String.class, 10);
    2.35 +        return arr.length;
    2.36 +    }
    2.37 +
    2.38 +    @Compare public String compTypeOfStringArray() {
    2.39 +        String[] arr = (String[]) Array.newInstance(String.class, 10);
    2.40 +        return arr.getClass().getComponentType().getName();
    2.41 +    }
    2.42 +
    2.43 +    @Compare public Object negativeArrayExcp() {
    2.44 +        return Array.newInstance(String.class, -5);
    2.45 +    }
    2.46 +    
    2.47 +    @Compare public int lengthOfIntArray() {
    2.48 +        int[] arr = (int[]) Array.newInstance(Integer.TYPE, 10);
    2.49 +        return arr.length;
    2.50 +    }
    2.51 +
    2.52 +    @Compare public String compTypeOfIntArray() {
    2.53 +        int[] arr = (int[]) Array.newInstance(int.class, 10);
    2.54 +        return arr.getClass().getComponentType().getName();
    2.55 +    }
    2.56 +
    2.57 +    @Compare public Object intNegativeArrayExcp() {
    2.58 +        return Array.newInstance(int.class, -5);
    2.59 +    }
    2.60 +    
    2.61 +    
    2.62 +    @Factory
    2.63 +    public static Object[] create() {
    2.64 +        return VMTest.create(ReflectionArrayTest.class);
    2.65 +    }
    2.66 +}