1.1 --- a/emul/src/main/java/java/lang/reflect/Array.java Fri Jan 18 12:07:11 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 }