rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Oct 2013 09:45:09 +0200
changeset 1376 8e600271bba1
parent 1321 7a78a84ab583
child 1470 6ab756741111
permissions -rw-r--r--
Making reflection with array parameters working
     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.annotation.Retention;
    21 import java.lang.annotation.RetentionPolicy;
    22 import java.lang.reflect.Constructor;
    23 import java.lang.reflect.Method;
    24 import java.lang.reflect.Proxy;
    25 import java.util.Arrays;
    26 import java.util.Collections;
    27 import java.util.List;
    28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    29 import org.apidesign.bck2brwsr.vmtest.Compare;
    30 import org.apidesign.bck2brwsr.vmtest.VMTest;
    31 import org.testng.annotations.Factory;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class ReflectionTest {
    38     @Compare public boolean nonNullThis() {
    39         return this == null;
    40     }
    41     
    42     @Compare public String intType() {
    43         return Integer.TYPE.toString();
    44     }
    45 
    46     @Compare public String voidType() throws Exception {
    47         return void.class.toString();
    48     }
    49 
    50     @Compare public String longClass() {
    51         return long.class.toString();
    52     }
    53     
    54     @Compare public boolean isRunnableInterface() {
    55         return Runnable.class.isInterface();
    56     }
    57 
    58     @Compare public boolean isAssignableToPrimitiveType() {
    59         return boolean.class.isAssignableFrom(Runnable.class);
    60     }
    61 
    62     @Compare public boolean isAssignableFromPrimitiveType() {
    63         return Runnable.class.isAssignableFrom(boolean.class);
    64     }
    65 
    66     @Compare public boolean isAssignableLongFromInt() {
    67         return long.class.isAssignableFrom(int.class);
    68     }
    69 
    70     @Compare public boolean isAssignableIntFromLong() {
    71         return int.class.isAssignableFrom(long.class);
    72     }
    73 
    74     @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
    75         return Runnable.class.getMethod("run").getName();
    76     }
    77 
    78     @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
    79         return Runnable.class.getDeclaredMethod("run").getName();
    80     }
    81     
    82     @Compare public String intValue() throws Exception {
    83         return Integer.class.getConstructor(int.class).newInstance(10).toString();
    84     }
    85     
    86     @Compare public String getMethodWithArray() throws Exception {
    87         return Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class).getName();
    88     }
    89     
    90     @Compare public String namesOfMethods() {
    91         StringBuilder sb = new StringBuilder();
    92         String[] arr = new String[20];
    93         int i = 0;
    94         for (Method m : StaticUse.class.getMethods()) {
    95             arr[i++] = m.getName();
    96         }
    97         for (String s : sort(arr, i)) {
    98             sb.append(s).append("\n");
    99         }
   100         return sb.toString();
   101     }
   102 
   103     @Compare public String paramsOfConstructors() {
   104         StringBuilder sb = new StringBuilder();
   105         String[] arr = new String[20];
   106         int i = 0;
   107         for (Constructor<?> m : StaticUse.class.getConstructors()) {
   108             arr[i++] = m.getName();
   109         }
   110         for (String s : sort(arr, i)) {
   111             sb.append(s).append("\n");
   112         }
   113         return sb.toString();
   114     }
   115 
   116     @Compare public String namesOfDeclaringClassesOfMethods() {
   117         StringBuilder sb = new StringBuilder();
   118         String[] arr = new String[20];
   119         int i = 0;
   120         for (Method m : StaticUse.class.getMethods()) {
   121             arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
   122         }
   123         for (String s : sort(arr, i)) {
   124             sb.append(s).append("\n");
   125         }
   126         return sb.toString();
   127     }
   128     
   129     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
   130         StaticUse.class.getMethod("instanceMethod").invoke(null);
   131         return "should not happen";
   132     }
   133     
   134     @Compare public String classCastException() {
   135         try {
   136             Integer i = (Integer)StaticUseSub.getNonNull();
   137             return "" + i.intValue();
   138         } catch (ClassCastException ex) {
   139             return ex.getClass().getName();
   140         }
   141     }
   142 
   143     @Compare public String methodThatThrowsException() throws Exception {
   144         StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
   145         return "should not happen";
   146     }
   147 
   148     @Compare public Object voidReturnType() throws Exception {
   149         return StaticUse.class.getMethod("instanceMethod").getReturnType();
   150     }
   151     
   152     @Retention(RetentionPolicy.RUNTIME)
   153     @interface Ann {
   154     }
   155     
   156     @Compare public String annoClass() throws Exception {
   157         Retention r = Ann.class.getAnnotation(Retention.class);
   158         assert r != null : "Annotation is present";
   159         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   160         return r.annotationType().getName();
   161     }
   162     
   163     @Compare public boolean isAnnotation() {
   164         return Ann.class.isAnnotation();
   165     }
   166     @Compare public boolean isNotAnnotation() {
   167         return String.class.isAnnotation();
   168     }
   169     @Compare public boolean isNotAnnotationEnum() {
   170         return E.class.isAnnotation();
   171     }
   172     enum E { A, B };
   173     @Compare public boolean isEnum() {
   174         return E.A.getClass().isEnum();
   175     }
   176 
   177     @Compare public boolean isNotEnum() {
   178         return "".getClass().isEnum();
   179     }
   180     
   181     @Compare public String newInstanceFails() throws InstantiationException {
   182         try {
   183             return "success: " + StaticUseSub.class.newInstance();
   184         } catch (IllegalAccessException ex) {
   185             return ex.getClass().getName();
   186         }
   187     }
   188     
   189     @Compare public String paramTypes() throws Exception {
   190         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   191         final Class[] pt = plus.getParameterTypes();
   192         return pt[0].getName();
   193     }
   194     @Compare public String paramTypesNotFound() throws Exception {
   195         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   196     }
   197     @Compare public int methodWithArgs() throws Exception {
   198         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   199         return (Integer)plus.invoke(null, 2, 3);
   200     }
   201     
   202     @Compare public String classGetNameForByte() {
   203          return byte.class.getName();
   204     }
   205     @Compare public String classGetNameForBaseObject() {
   206         return newObject().getClass().getName();
   207     }
   208     @Compare public String classGetNameForJavaObject() {
   209         return new Object().getClass().getName();
   210     }
   211     @Compare public String classGetNameForObjectArray() {
   212         return (new Object[3]).getClass().getName();
   213     }
   214     @Compare public String classGetNameForSimpleIntArray() {
   215         return (new int[3]).getClass().getName();
   216     }
   217     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   218         return (new char[3]).getClass() == (new char[34]).getClass();
   219     }
   220     @Compare public String classGetNameForMultiIntArray() {
   221         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   222     }
   223     @Compare public String classGetNameForMultiIntArrayInner() {
   224         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   225         int[][][][][][] subarr = arr[0];
   226         int[][][][][] subsubarr = subarr[0];
   227         return subsubarr.getClass().getName();
   228     }
   229     @Compare public String classGetNameForMultiStringArray() {
   230         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   231     }
   232     
   233     @Compare public String classForByte() throws Exception {
   234         return Class.forName("[Z").getName();
   235     }
   236 
   237     @Compare public String classForUnknownArray() {
   238         try {
   239             return Class.forName("[W").getName();
   240         } catch (Exception ex) {
   241             return ex.getClass().getName();
   242         }
   243     }
   244     
   245     @Compare public String classForUnknownDeepArray() {
   246         try {
   247             return Class.forName("[[[[[W").getName();
   248         } catch (Exception ex) {
   249             return ex.getClass().getName();
   250         }
   251     }
   252     
   253     @Compare public String componentGetNameForObjectArray() {
   254         return (new Object[3]).getClass().getComponentType().getName();
   255     }
   256     @Compare public boolean sameComponentGetNameForObjectArray() {
   257         return (new Object[3]).getClass().getComponentType() == Object.class;
   258     }
   259     @Compare public String componentGetNameForSimpleIntArray() {
   260         return (new int[3]).getClass().getComponentType().getName();
   261     }
   262     @Compare public String componentGetNameForMultiIntArray() {
   263         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   264     }
   265     @Compare public String componentGetNameForMultiStringArray() {
   266         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   267         StringBuilder sb = new StringBuilder();
   268         for (;;) {
   269             sb.append(c.getName()).append("\n");
   270             c = c.getComponentType();
   271             if (c == null) {
   272                 break;
   273             }
   274         }
   275         return sb.toString();
   276     }
   277     
   278     @Compare public boolean isArray() {
   279         return new Object[0].getClass().isArray();
   280     }
   281     
   282     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   283     private static String[] sort(String[] arr, int len) {
   284         List<String> list = Arrays.asList(arr).subList(0, len);
   285         Collections.sort(list);
   286         return list.toArray(new String[0]);
   287     }
   288     
   289     @JavaScriptBody(args = {}, body = "return new Object();")
   290     private static Object newObject() {
   291         return new Object();
   292     }
   293     
   294     @Factory
   295     public static Object[] create() {
   296         return VMTest.create(ReflectionTest.class);
   297     }
   298     
   299 }