rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Nov 2014 19:32:00 +0100
changeset 1723 3a1f262311cf
parent 1565 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java@8977a022e424
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
     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() {
   182         try {
   183             return "success: " + StaticUseSub.class.newInstance();
   184         } catch (IllegalAccessException ex) {
   185             return "failure";
   186         } catch (InstantiationException ex) {
   187             return "failure";
   188         }
   189     }
   190     
   191     @Compare public String paramTypes() throws Exception {
   192         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   193         final Class[] pt = plus.getParameterTypes();
   194         return pt[0].getName();
   195     }
   196     @Compare public String paramTypesNotFound() throws Exception {
   197         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   198     }
   199     @Compare public int methodWithArgs() throws Exception {
   200         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   201         return (Integer)plus.invoke(null, 2, 3);
   202     }
   203     
   204     @Compare public String classGetNameForByte() {
   205          return byte.class.getName();
   206     }
   207     @Compare public String classGetNameForBaseObject() {
   208         return newObject().getClass().getName();
   209     }
   210     @Compare public String classGetNameForJavaObject() {
   211         return new Object().getClass().getName();
   212     }
   213     @Compare public String classGetNameForObjectArray() {
   214         return (new Object[3]).getClass().getName();
   215     }
   216     @Compare public String classGetNameForSimpleIntArray() {
   217         return (new int[3]).getClass().getName();
   218     }
   219     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   220         return (new char[3]).getClass() == (new char[34]).getClass();
   221     }
   222     @Compare public String classGetNameForMultiIntArray() {
   223         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   224     }
   225     @Compare public String classGetNameForMultiIntArrayInner() {
   226         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   227         int[][][][][][] subarr = arr[0];
   228         int[][][][][] subsubarr = subarr[0];
   229         return subsubarr.getClass().getName();
   230     }
   231     @Compare public String classGetNameForMultiStringArray() {
   232         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   233     }
   234     
   235     @Compare public String classForByte() throws Exception {
   236         return Class.forName("[Z").getName();
   237     }
   238 
   239     @Compare public String classForUnknownArray() {
   240         try {
   241             return Class.forName("[W").getName();
   242         } catch (Exception ex) {
   243             return ex.getClass().getName();
   244         }
   245     }
   246     
   247     @Compare public String classForUnknownDeepArray() {
   248         try {
   249             return Class.forName("[[[[[W").getName();
   250         } catch (Exception ex) {
   251             return ex.getClass().getName();
   252         }
   253     }
   254     
   255     @Compare public int callAbst() throws Exception {
   256         class Impl extends Abst {
   257             @Override
   258             public int abst() {
   259                 return 10;
   260             }
   261         }
   262         Abst impl = new Impl();
   263         return (int) Abst.class.getMethod("abst").invoke(impl);
   264     }
   265     
   266     @Compare public String componentGetNameForObjectArray() {
   267         return (new Object[3]).getClass().getComponentType().getName();
   268     }
   269     @Compare public boolean sameComponentGetNameForObjectArray() {
   270         return (new Object[3]).getClass().getComponentType() == Object.class;
   271     }
   272     @Compare public String componentGetNameForSimpleIntArray() {
   273         return (new int[3]).getClass().getComponentType().getName();
   274     }
   275     @Compare public String componentGetNameForMultiIntArray() {
   276         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   277     }
   278     @Compare public String componentGetNameForMultiStringArray() {
   279         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   280         StringBuilder sb = new StringBuilder();
   281         for (;;) {
   282             sb.append(c.getName()).append("\n");
   283             c = c.getComponentType();
   284             if (c == null) {
   285                 break;
   286             }
   287         }
   288         return sb.toString();
   289     }
   290     
   291     @Compare public boolean isArray() {
   292         return new Object[0].getClass().isArray();
   293     }
   294     
   295     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   296     private static String[] sort(String[] arr, int len) {
   297         List<String> list = Arrays.asList(arr).subList(0, len);
   298         Collections.sort(list);
   299         return list.toArray(new String[0]);
   300     }
   301     
   302     @JavaScriptBody(args = {}, body = "return new Object();")
   303     private static Object newObject() {
   304         return new Object();
   305     }
   306     
   307     @Factory
   308     public static Object[] create() {
   309         return VMTest.create(ReflectionTest.class);
   310     }
   311     
   312     public static abstract class Abst {
   313         public abstract int abst();
   314     }
   315 }