vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 15 Jan 2013 09:46:21 +0100
brancharrays
changeset 453 5aca91d00356
parent 452 086c60adf08e
child 586 b670af2aa0f7
permissions -rw-r--r--
Proper type of subarrays of multi dimensional array
     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.bck2brwsr.tck;
    19 
    20 import java.lang.reflect.Method;
    21 import java.util.Arrays;
    22 import java.util.Collections;
    23 import java.util.List;
    24 import java.util.logging.Level;
    25 import java.util.logging.Logger;
    26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    27 import org.apidesign.bck2brwsr.vmtest.Compare;
    28 import org.apidesign.bck2brwsr.vmtest.VMTest;
    29 import org.testng.annotations.Factory;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class ReflectionTest {
    36     @Compare public boolean nonNullThis() {
    37         return this == null;
    38     }
    39     
    40     @Compare public String intType() {
    41         return Integer.TYPE.toString();
    42     }
    43 
    44     @Compare public String voidType() throws Exception {
    45         return void.class.toString();
    46     }
    47 
    48     @Compare public String longClass() {
    49         return long.class.toString();
    50     }
    51     
    52     @Compare public String namesOfMethods() {
    53         StringBuilder sb = new StringBuilder();
    54         String[] arr = new String[20];
    55         int i = 0;
    56         for (Method m : StaticUse.class.getMethods()) {
    57             arr[i++] = m.getName();
    58         }
    59         for (String s : sort(arr, i)) {
    60             sb.append(s).append("\n");
    61         }
    62         return sb.toString();
    63     }
    64     
    65     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
    66         StaticUse.class.getMethod("instanceMethod").invoke(null);
    67         return "should not happen";
    68     }
    69 
    70     @Compare public Object voidReturnType() throws Exception {
    71         return StaticUse.class.getMethod("instanceMethod").getReturnType();
    72     }
    73     
    74     @Compare public String newInstanceFails() throws InstantiationException {
    75         try {
    76             return "success: " + StaticUse.class.newInstance();
    77         } catch (IllegalAccessException ex) {
    78             return ex.getClass().getName();
    79         }
    80     }
    81     
    82     @Compare public String paramTypes() throws Exception {
    83         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
    84         final Class[] pt = plus.getParameterTypes();
    85         return pt[0].getName();
    86     }
    87     @Compare public String paramTypesNotFound() throws Exception {
    88         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
    89     }
    90     @Compare public int methodWithArgs() throws Exception {
    91         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
    92         return (Integer)plus.invoke(null, 2, 3);
    93     }
    94     
    95     @Compare public String classGetNameForByte() {
    96          return byte.class.getName();
    97     }
    98     @Compare public String classGetNameForBaseObject() {
    99         return newObject().getClass().getName();
   100     }
   101     @Compare public String classGetNameForJavaObject() {
   102         return new Object().getClass().getName();
   103     }
   104     @Compare public String classGetNameForObjectArray() {
   105         return (new Object[3]).getClass().getName();
   106     }
   107     @Compare public String classGetNameForSimpleIntArray() {
   108         return (new int[3]).getClass().getName();
   109     }
   110     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   111         return (new char[3]).getClass() == (new char[34]).getClass();
   112     }
   113     @Compare public String classGetNameForMultiIntArray() {
   114         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   115     }
   116     @Compare public String classGetNameForMultiIntArrayInner() {
   117         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   118         int[][][][][][] subarr = arr[0];
   119         int[][][][][] subsubarr = subarr[0];
   120         return subsubarr.getClass().getName();
   121     }
   122     @Compare public String classGetNameForMultiStringArray() {
   123         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   124     }
   125     
   126     @Compare public String classForByte() throws Exception {
   127         return Class.forName("[Z").getName();
   128     }
   129 
   130     @Compare public String classForUnknownArray() {
   131         try {
   132             return Class.forName("[W").getName();
   133         } catch (Exception ex) {
   134             return ex.getClass().getName();
   135         }
   136     }
   137     
   138     @Compare public String classForUnknownDeepArray() {
   139         try {
   140             return Class.forName("[[[[[W").getName();
   141         } catch (Exception ex) {
   142             return ex.getClass().getName();
   143         }
   144     }
   145     
   146     @Compare public String componentGetNameForObjectArray() {
   147         return (new Object[3]).getClass().getComponentType().getName();
   148     }
   149     @Compare public boolean sameComponentGetNameForObjectArray() {
   150         return (new Object[3]).getClass().getComponentType() == Object.class;
   151     }
   152     @Compare public String componentGetNameForSimpleIntArray() {
   153         return (new int[3]).getClass().getComponentType().getName();
   154     }
   155     @Compare public String componentGetNameForMultiIntArray() {
   156         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   157     }
   158     @Compare public String componentGetNameForMultiStringArray() {
   159         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   160         StringBuilder sb = new StringBuilder();
   161         for (;;) {
   162             sb.append(c.getName()).append("\n");
   163             c = c.getComponentType();
   164             if (c == null) {
   165                 break;
   166             }
   167         }
   168         return sb.toString();
   169     }
   170     
   171     @Compare public boolean isArray() {
   172         return new Object[0].getClass().isArray();
   173     }
   174     
   175     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   176     private static String[] sort(String[] arr, int len) {
   177         List<String> list = Arrays.asList(arr).subList(0, len);
   178         Collections.sort(list);
   179         return list.toArray(new String[0]);
   180     }
   181     
   182     @JavaScriptBody(args = {}, body = "return new Object();")
   183     private static Object newObject() {
   184         return new Object();
   185     }
   186     
   187     @Factory
   188     public static Object[] create() {
   189         return VMTest.create(ReflectionTest.class);
   190     }
   191     
   192 }