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