rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Mar 2016 15:26:19 +0100
changeset 1902 c3dee54ecc15
parent 1900 7865a00c30ef
permissions -rw-r--r--
Don't redefine already exported classes
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 boolean RunnableRunReturnsVoid() throws NoSuchMethodException {
    79         return Runnable.class.getMethod("run").getReturnType() == Void.TYPE;
    80     }
    81 
    82     @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
    83         return Runnable.class.getDeclaredMethod("run").getName();
    84     }
    85     
    86     @Compare public String intValue() throws Exception {
    87         return Integer.class.getConstructor(int.class).newInstance(10).toString();
    88     }
    89     
    90     @Compare public String getMethodWithArray() throws Exception {
    91         return Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class).getName();
    92     }
    93     
    94     @Compare public String namesOfMethods() {
    95         StringBuilder sb = new StringBuilder();
    96         String[] arr = new String[20];
    97         int i = 0;
    98         for (Method m : StaticUse.class.getMethods()) {
    99             arr[i++] = m.getName();
   100         }
   101         for (String s : sort(arr, i)) {
   102             sb.append(s).append("\n");
   103         }
   104         return sb.toString();
   105     }
   106 
   107     @Compare
   108     public String namesOfDeclaredMethods() {
   109         StringBuilder sb = new StringBuilder();
   110         String[] arr = new String[20];
   111         Method[] methods = StaticUse.class.getDeclaredMethods();
   112         if (methods.length != 2) {
   113             throw new IllegalStateException("Expecting just two methods, was: " + methods.length);
   114         }
   115         int i = 0;
   116         for (Method m : methods) {
   117             arr[i++] = m.getName();
   118         }
   119         for (String s : sort(arr, i)) {
   120             sb.append(s).append("\n");
   121         }
   122         return sb.toString();
   123     }
   124 
   125     @Compare public String paramsOfConstructors() {
   126         StringBuilder sb = new StringBuilder();
   127         String[] arr = new String[20];
   128         int i = 0;
   129         for (Constructor<?> m : StaticUse.class.getConstructors()) {
   130             arr[i++] = m.getName();
   131         }
   132         for (String s : sort(arr, i)) {
   133             sb.append(s).append("\n");
   134         }
   135         return sb.toString();
   136     }
   137 
   138     @Compare public String namesOfDeclaringClassesOfMethods() {
   139         StringBuilder sb = new StringBuilder();
   140         String[] arr = new String[20];
   141         int i = 0;
   142         for (Method m : StaticUse.class.getMethods()) {
   143             arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
   144         }
   145         for (String s : sort(arr, i)) {
   146             sb.append(s).append("\n");
   147         }
   148         return sb.toString();
   149     }
   150     
   151     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
   152         StaticUse.class.getMethod("instanceMethod").invoke(null);
   153         return "should not happen";
   154     }
   155     
   156     @Compare public String classCastException() {
   157         try {
   158             Integer i = (Integer)StaticUseSub.getNonNull();
   159             return "" + i.intValue();
   160         } catch (ClassCastException ex) {
   161             return ex.getClass().getName();
   162         }
   163     }
   164 
   165     @Compare public String methodThatThrowsException() throws Exception {
   166         StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
   167         return "should not happen";
   168     }
   169 
   170     @Compare public Object voidReturnType() throws Exception {
   171         return StaticUse.class.getMethod("instanceMethod").getReturnType();
   172     }
   173     
   174     @Retention(RetentionPolicy.RUNTIME)
   175     @interface Ann {
   176     }
   177     
   178     @Compare public String annoClass() throws Exception {
   179         Retention r = Ann.class.getAnnotation(Retention.class);
   180         assert r != null : "Annotation is present";
   181         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   182         return r.annotationType().getName();
   183     }
   184     
   185     @Compare public boolean isAnnotation() {
   186         return Ann.class.isAnnotation();
   187     }
   188     @Compare public boolean isNotAnnotation() {
   189         return String.class.isAnnotation();
   190     }
   191     @Compare public boolean isNotAnnotationEnum() {
   192         return E.class.isAnnotation();
   193     }
   194     enum E { A, B };
   195     @Compare public boolean isEnum() {
   196         return E.A.getClass().isEnum();
   197     }
   198 
   199     @Compare public boolean isNotEnum() {
   200         return "".getClass().isEnum();
   201     }
   202     
   203     @Compare public String newInstanceFails() {
   204         try {
   205             return "success: " + StaticUseSub.class.newInstance();
   206         } catch (IllegalAccessException ex) {
   207             return "failure";
   208         } catch (InstantiationException ex) {
   209             return "failure";
   210         }
   211     }
   212     
   213     @Compare public String paramTypes() throws Exception {
   214         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   215         final Class[] pt = plus.getParameterTypes();
   216         return pt[0].getName();
   217     }
   218     @Compare public String paramTypesNotFound() throws Exception {
   219         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   220     }
   221     @Compare public int methodWithArgs() throws Exception {
   222         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   223         return (Integer)plus.invoke(null, 2, 3);
   224     }
   225     
   226     @Compare public String classGetNameForByte() {
   227          return byte.class.getName();
   228     }
   229     @Compare public String classGetNameForBaseObject() {
   230         return newObject().getClass().getName();
   231     }
   232     @Compare public String classGetNameForJavaObject() {
   233         return new Object().getClass().getName();
   234     }
   235     @Compare public String classGetNameForObjectArray() {
   236         return (new Object[3]).getClass().getName();
   237     }
   238     @Compare public String classGetNameForSimpleIntArray() {
   239         return (new int[3]).getClass().getName();
   240     }
   241     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   242         return (new char[3]).getClass() == (new char[34]).getClass();
   243     }
   244     @Compare public String classGetNameForMultiIntArray() {
   245         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   246     }
   247     @Compare public String classGetNameForMultiIntArrayInner() {
   248         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   249         int[][][][][][] subarr = arr[0];
   250         int[][][][][] subsubarr = subarr[0];
   251         return subsubarr.getClass().getName();
   252     }
   253     @Compare public String classGetNameForMultiStringArray() {
   254         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   255     }
   256     
   257     @Compare public String classForByte() throws Exception {
   258         return Class.forName("[Z").getName();
   259     }
   260 
   261     @Compare public String classForUnknownArray() {
   262         try {
   263             return Class.forName("[W").getName();
   264         } catch (Exception ex) {
   265             return ex.getClass().getName();
   266         }
   267     }
   268     
   269     @Compare public String classForUnknownDeepArray() {
   270         try {
   271             return Class.forName("[[[[[W").getName();
   272         } catch (Exception ex) {
   273             return ex.getClass().getName();
   274         }
   275     }
   276     
   277     @Compare public int callAbst() throws Exception {
   278         class Impl extends Abst {
   279             @Override
   280             public int abst() {
   281                 return 10;
   282             }
   283         }
   284         Abst impl = new Impl();
   285         return (int) Abst.class.getMethod("abst").invoke(impl);
   286     }
   287     
   288     @Compare public String componentGetNameForObjectArray() {
   289         return (new Object[3]).getClass().getComponentType().getName();
   290     }
   291     @Compare public boolean sameComponentGetNameForObjectArray() {
   292         return (new Object[3]).getClass().getComponentType() == Object.class;
   293     }
   294     @Compare public String componentGetNameForSimpleIntArray() {
   295         return (new int[3]).getClass().getComponentType().getName();
   296     }
   297     @Compare public String componentGetNameForMultiIntArray() {
   298         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   299     }
   300     @Compare public String componentGetNameForMultiStringArray() {
   301         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   302         StringBuilder sb = new StringBuilder();
   303         for (;;) {
   304             sb.append(c.getName()).append("\n");
   305             c = c.getComponentType();
   306             if (c == null) {
   307                 break;
   308             }
   309         }
   310         return sb.toString();
   311     }
   312     
   313     @Compare public boolean isArray() {
   314         return new Object[0].getClass().isArray();
   315     }
   316 
   317     @Compare public String copyDataViaReflection() throws Exception {
   318         Data d = new Data();
   319         d.setName("Hello world!");
   320 
   321         Method getter = d.getClass().getMethod("getName");
   322         Object val = getter.invoke(d);
   323 
   324         Data clone = new Data();
   325         Method setter = d.getClass().getMethod("setName", String.class);
   326         setter.invoke(clone, val);
   327 
   328         return clone.getName();
   329     }
   330 
   331     @Compare public String copyStaticDataViaReflection() throws Exception {
   332         Data.setStatic("Hello world!");
   333 
   334         Method getter = Data.class.getMethod("getStatic");
   335         Object val = getter.invoke(null);
   336 
   337         Method setter = Data.class.getMethod("setStatic", String.class);
   338         setter.invoke(null, val);
   339 
   340         return Data.getStatic();
   341     }
   342     
   343     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   344     private static String[] sort(String[] arr, int len) {
   345         List<String> list = Arrays.asList(arr).subList(0, len);
   346         Collections.sort(list);
   347         return list.toArray(new String[0]);
   348     }
   349     
   350     @JavaScriptBody(args = {}, body = "return new Object();")
   351     private static Object newObject() {
   352         return new Object();
   353     }
   354     
   355     @Factory
   356     public static Object[] create() {
   357         return VMTest.create(ReflectionTest.class);
   358     }
   359     
   360     public static abstract class Abst {
   361         public abstract int abst();
   362     }
   363 
   364     public static final class Data {
   365         private String name;
   366         private static String nameStatic;
   367 
   368         public String getName() {
   369             return name;
   370         }
   371 
   372         public void setName(String name) {
   373             this.name = name;
   374         }
   375 
   376         public static String getStatic() {
   377             return nameStatic;
   378         }
   379 
   380         public static void setStatic(String n) {
   381             nameStatic = n;
   382         }
   383     }
   384 }