rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Mar 2013 16:16:30 +0100
branchmodel
changeset 886 88540bb74300
parent 775 a13e33fd5c2e
child 938 0eec1b51c13c
permissions -rw-r--r--
Class.isAssignableFrom and primitive types works better now
     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 namesOfMethods() {
    77         StringBuilder sb = new StringBuilder();
    78         String[] arr = new String[20];
    79         int i = 0;
    80         for (Method m : StaticUse.class.getMethods()) {
    81             arr[i++] = m.getName();
    82         }
    83         for (String s : sort(arr, i)) {
    84             sb.append(s).append("\n");
    85         }
    86         return sb.toString();
    87     }
    88 
    89     @Compare public String namesOfDeclaringClassesOfMethods() {
    90         StringBuilder sb = new StringBuilder();
    91         String[] arr = new String[20];
    92         int i = 0;
    93         for (Method m : StaticUse.class.getMethods()) {
    94             arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
    95         }
    96         for (String s : sort(arr, i)) {
    97             sb.append(s).append("\n");
    98         }
    99         return sb.toString();
   100     }
   101     
   102     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
   103         StaticUse.class.getMethod("instanceMethod").invoke(null);
   104         return "should not happen";
   105     }
   106 
   107     @Compare public Object voidReturnType() throws Exception {
   108         return StaticUse.class.getMethod("instanceMethod").getReturnType();
   109     }
   110     
   111     @Retention(RetentionPolicy.RUNTIME)
   112     @interface Ann {
   113     }
   114     
   115     @Compare public String annoClass() throws Exception {
   116         Retention r = Ann.class.getAnnotation(Retention.class);
   117         assert r != null : "Annotation is present";
   118         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   119         return r.annotationType().getName();
   120     }
   121     
   122     @Compare public boolean isAnnotation() {
   123         return Ann.class.isAnnotation();
   124     }
   125     @Compare public boolean isNotAnnotation() {
   126         return String.class.isAnnotation();
   127     }
   128     @Compare public boolean isNotAnnotationEnum() {
   129         return E.class.isAnnotation();
   130     }
   131     enum E { A, B };
   132     @Compare public boolean isEnum() {
   133         return E.A.getClass().isEnum();
   134     }
   135 
   136     @Compare public boolean isNotEnum() {
   137         return "".getClass().isEnum();
   138     }
   139     
   140     @Compare public String newInstanceFails() throws InstantiationException {
   141         try {
   142             return "success: " + StaticUseSub.class.newInstance();
   143         } catch (IllegalAccessException ex) {
   144             return ex.getClass().getName();
   145         }
   146     }
   147     
   148     @Compare public String paramTypes() throws Exception {
   149         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   150         final Class[] pt = plus.getParameterTypes();
   151         return pt[0].getName();
   152     }
   153     @Compare public String paramTypesNotFound() throws Exception {
   154         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   155     }
   156     @Compare public int methodWithArgs() throws Exception {
   157         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   158         return (Integer)plus.invoke(null, 2, 3);
   159     }
   160     
   161     @Compare public String classGetNameForByte() {
   162          return byte.class.getName();
   163     }
   164     @Compare public String classGetNameForBaseObject() {
   165         return newObject().getClass().getName();
   166     }
   167     @Compare public String classGetNameForJavaObject() {
   168         return new Object().getClass().getName();
   169     }
   170     @Compare public String classGetNameForObjectArray() {
   171         return (new Object[3]).getClass().getName();
   172     }
   173     @Compare public String classGetNameForSimpleIntArray() {
   174         return (new int[3]).getClass().getName();
   175     }
   176     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   177         return (new char[3]).getClass() == (new char[34]).getClass();
   178     }
   179     @Compare public String classGetNameForMultiIntArray() {
   180         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   181     }
   182     @Compare public String classGetNameForMultiIntArrayInner() {
   183         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   184         int[][][][][][] subarr = arr[0];
   185         int[][][][][] subsubarr = subarr[0];
   186         return subsubarr.getClass().getName();
   187     }
   188     @Compare public String classGetNameForMultiStringArray() {
   189         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   190     }
   191     
   192     @Compare public String classForByte() throws Exception {
   193         return Class.forName("[Z").getName();
   194     }
   195 
   196     @Compare public String classForUnknownArray() {
   197         try {
   198             return Class.forName("[W").getName();
   199         } catch (Exception ex) {
   200             return ex.getClass().getName();
   201         }
   202     }
   203     
   204     @Compare public String classForUnknownDeepArray() {
   205         try {
   206             return Class.forName("[[[[[W").getName();
   207         } catch (Exception ex) {
   208             return ex.getClass().getName();
   209         }
   210     }
   211     
   212     @Compare public String componentGetNameForObjectArray() {
   213         return (new Object[3]).getClass().getComponentType().getName();
   214     }
   215     @Compare public boolean sameComponentGetNameForObjectArray() {
   216         return (new Object[3]).getClass().getComponentType() == Object.class;
   217     }
   218     @Compare public String componentGetNameForSimpleIntArray() {
   219         return (new int[3]).getClass().getComponentType().getName();
   220     }
   221     @Compare public String componentGetNameForMultiIntArray() {
   222         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   223     }
   224     @Compare public String componentGetNameForMultiStringArray() {
   225         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   226         StringBuilder sb = new StringBuilder();
   227         for (;;) {
   228             sb.append(c.getName()).append("\n");
   229             c = c.getComponentType();
   230             if (c == null) {
   231                 break;
   232             }
   233         }
   234         return sb.toString();
   235     }
   236     
   237     @Compare public boolean isArray() {
   238         return new Object[0].getClass().isArray();
   239     }
   240     
   241     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   242     private static String[] sort(String[] arr, int len) {
   243         List<String> list = Arrays.asList(arr).subList(0, len);
   244         Collections.sort(list);
   245         return list.toArray(new String[0]);
   246     }
   247     
   248     @JavaScriptBody(args = {}, body = "return new Object();")
   249     private static Object newObject() {
   250         return new Object();
   251     }
   252     
   253     @Factory
   254     public static Object[] create() {
   255         return VMTest.create(ReflectionTest.class);
   256     }
   257     
   258 }