rt/vm/src/test/java/org/apidesign/vm4brwsr/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 06 May 2014 08:36:54 +0200
branchclosure
changeset 1532 10d26626c426
parent 807 e93506e603ad
child 1534 ca538fb33f48
permissions -rw-r--r--
1st step in eliminating Class.forName when working with array types. Class.getComponentType on directly allocated arrays does not need that.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /**
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public class Array {
    27     byte[] bytes = { 1 };
    28     short[] shorts = { 2, 3 };
    29     int[] ints = { 4, 5, 6 };
    30     float[] floats = { 7, 8, 9, 10 };
    31     double[][] doubles = { {11}, {12}, {13}, {14}, {15} };
    32     char[] chars = { 'a', 'b' };
    33     
    34     private Array() {
    35     }
    36     
    37     byte bytes() {
    38         return bytes[0];
    39     }
    40     short shorts() {
    41         return shorts[1];
    42     }
    43     
    44     int[] ints() {
    45         return ints;
    46     }
    47     
    48     float floats() {
    49         return floats[3];
    50     }
    51     
    52     double doubles() {
    53         return doubles[4][0];
    54     }
    55     
    56     static double[][] dbls = new double[1][2];
    57     public static double twoDoubles() {
    58         return dbls[0][0] + dbls[0][0];
    59     }
    60 
    61     static int[][] tints = new int[1][2];
    62     public static int twoInts() {
    63         return tints[0][0] + tints[0][0];
    64     }
    65     
    66     private static final Array[] ARR = { new Array(), new Array(), new Array() };
    67     
    68     private static Array[][] arr() {
    69         Array[][] matrix = new Array[3][3];
    70         for (int i = 0; i < ARR.length; i++) {
    71             matrix[i][i] = ARR[i];
    72         }
    73         return matrix;
    74     }
    75     private static <T> T[] filter(T[] in) {
    76         return in;
    77     }
    78     
    79     public static double sum() {
    80         double sum = 0.0;
    81         for (Array[] row : arr()) {
    82             int indx = -1;
    83             for (Array a : row) {
    84                 indx++;
    85                 if (a == null) {
    86                     continue;
    87                 }
    88                 sum += a.bytes();
    89                 sum += a.shorts();
    90                 sum += a.ints()[2];
    91                 sum += a.floats();
    92                 sum += filter(row)[indx].doubles();
    93             }
    94         }
    95         return sum;
    96     }
    97     private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
    98     public static int simple(boolean clone) {
    99         int[] ar;
   100         if (clone) {
   101             ar = arr.clone();
   102         } else {
   103             ar = arr;
   104         }
   105         
   106         int sum = 0;
   107         for (int a : ar) {
   108             sum += a;
   109         }
   110         return sum;
   111     }
   112     
   113     public static String objectArrayClass() {
   114         return Object[].class.getName();
   115     }
   116     
   117     public static boolean instanceOfArray(Object obj) {
   118         return obj instanceof Object[];
   119     }
   120     
   121     public static int sum(int size) {
   122         int[] arr = new int[size];
   123         return arr[0] + arr[1];
   124     }
   125     
   126     static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
   127         while (count-- > 0) {
   128             dst[dstBegin++] = value[srcBegin++];
   129         }
   130     }
   131 
   132     public static char copyArray() {
   133         char[] arr = { '0' };
   134         arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
   135         return arr[0];
   136     }
   137     
   138     @JavaScriptBody(args = {  }, body = 
   139         "if (!vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2) throw 'forName not defined';\n"
   140       + "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2 = function(s) {\n"
   141       + "  throw 'Do not call me: ' + s;\n"
   142       + "};\n")
   143     private static void disableClassForName() {
   144     }
   145     
   146     public static String nameOfClonedComponent() {
   147         disableClassForName();
   148         Object[] intArr = new Integer[10];
   149         intArr = intArr.clone();
   150         return intArr.getClass().getComponentType().getName();
   151     }
   152     
   153     public static int multiLen() {
   154         return new int[1][0].length;
   155     }
   156 }