vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 03 Feb 2013 23:18:47 +0100
branchreflection
changeset 655 044c72732424
parent 649 4b16b7e23cab
child 661 66e080030577
permissions -rw-r--r--
Requiring reference to enums as soon as they are used in annotations
     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 String isRunnableHasRunMethod() throws NoSuchMethodException {
    57         return Runnable.class.getMethod("run").getName();
    58     }
    59     
    60     @Compare public String namesOfMethods() {
    61         StringBuilder sb = new StringBuilder();
    62         String[] arr = new String[20];
    63         int i = 0;
    64         for (Method m : StaticUse.class.getMethods()) {
    65             arr[i++] = m.getName();
    66         }
    67         for (String s : sort(arr, i)) {
    68             sb.append(s).append("\n");
    69         }
    70         return sb.toString();
    71     }
    72 
    73     @Compare public String namesOfDeclaringClassesOfMethods() {
    74         StringBuilder sb = new StringBuilder();
    75         String[] arr = new String[20];
    76         int i = 0;
    77         for (Method m : StaticUse.class.getMethods()) {
    78             arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
    79         }
    80         for (String s : sort(arr, i)) {
    81             sb.append(s).append("\n");
    82         }
    83         return sb.toString();
    84     }
    85     
    86     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
    87         StaticUse.class.getMethod("instanceMethod").invoke(null);
    88         return "should not happen";
    89     }
    90 
    91     @Compare public Object voidReturnType() throws Exception {
    92         return StaticUse.class.getMethod("instanceMethod").getReturnType();
    93     }
    94     
    95     @Retention(RetentionPolicy.RUNTIME)
    96     @interface Ann {
    97     }
    98     
    99     @Compare public String annoClass() throws Exception {
   100         Retention r = Ann.class.getAnnotation(Retention.class);
   101         assert r != null : "Annotation is present";
   102         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   103         return r.annotationType().getName();
   104     }
   105     
   106     enum E { A, B };
   107     @Compare public boolean isEnum() {
   108         return E.A.getClass().isEnum();
   109     }
   110 
   111     @Compare public boolean isNotEnum() {
   112         return "".getClass().isEnum();
   113     }
   114     
   115     @Compare public String newInstanceFails() throws InstantiationException {
   116         try {
   117             return "success: " + StaticUse.class.newInstance();
   118         } catch (IllegalAccessException ex) {
   119             return ex.getClass().getName();
   120         }
   121     }
   122     
   123     @Compare public String paramTypes() throws Exception {
   124         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   125         final Class[] pt = plus.getParameterTypes();
   126         return pt[0].getName();
   127     }
   128     @Compare public String paramTypesNotFound() throws Exception {
   129         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   130     }
   131     @Compare public int methodWithArgs() throws Exception {
   132         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   133         return (Integer)plus.invoke(null, 2, 3);
   134     }
   135     
   136     @Compare public String classGetNameForByte() {
   137          return byte.class.getName();
   138     }
   139     @Compare public String classGetNameForBaseObject() {
   140         return newObject().getClass().getName();
   141     }
   142     @Compare public String classGetNameForJavaObject() {
   143         return new Object().getClass().getName();
   144     }
   145     @Compare public String classGetNameForObjectArray() {
   146         return (new Object[3]).getClass().getName();
   147     }
   148     @Compare public String classGetNameForSimpleIntArray() {
   149         return (new int[3]).getClass().getName();
   150     }
   151     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   152         return (new char[3]).getClass() == (new char[34]).getClass();
   153     }
   154     @Compare public String classGetNameForMultiIntArray() {
   155         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   156     }
   157     @Compare public String classGetNameForMultiIntArrayInner() {
   158         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   159         int[][][][][][] subarr = arr[0];
   160         int[][][][][] subsubarr = subarr[0];
   161         return subsubarr.getClass().getName();
   162     }
   163     @Compare public String classGetNameForMultiStringArray() {
   164         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   165     }
   166     
   167     @Compare public String classForByte() throws Exception {
   168         return Class.forName("[Z").getName();
   169     }
   170 
   171     @Compare public String classForUnknownArray() {
   172         try {
   173             return Class.forName("[W").getName();
   174         } catch (Exception ex) {
   175             return ex.getClass().getName();
   176         }
   177     }
   178     
   179     @Compare public String classForUnknownDeepArray() {
   180         try {
   181             return Class.forName("[[[[[W").getName();
   182         } catch (Exception ex) {
   183             return ex.getClass().getName();
   184         }
   185     }
   186     
   187     @Compare public String componentGetNameForObjectArray() {
   188         return (new Object[3]).getClass().getComponentType().getName();
   189     }
   190     @Compare public boolean sameComponentGetNameForObjectArray() {
   191         return (new Object[3]).getClass().getComponentType() == Object.class;
   192     }
   193     @Compare public String componentGetNameForSimpleIntArray() {
   194         return (new int[3]).getClass().getComponentType().getName();
   195     }
   196     @Compare public String componentGetNameForMultiIntArray() {
   197         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   198     }
   199     @Compare public String componentGetNameForMultiStringArray() {
   200         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   201         StringBuilder sb = new StringBuilder();
   202         for (;;) {
   203             sb.append(c.getName()).append("\n");
   204             c = c.getComponentType();
   205             if (c == null) {
   206                 break;
   207             }
   208         }
   209         return sb.toString();
   210     }
   211     
   212     @Compare public boolean isArray() {
   213         return new Object[0].getClass().isArray();
   214     }
   215     
   216     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   217     private static String[] sort(String[] arr, int len) {
   218         List<String> list = Arrays.asList(arr).subList(0, len);
   219         Collections.sort(list);
   220         return list.toArray(new String[0]);
   221     }
   222     
   223     @JavaScriptBody(args = {}, body = "return new Object();")
   224     private static Object newObject() {
   225         return new Object();
   226     }
   227     
   228     @Factory
   229     public static Object[] create() {
   230         return VMTest.create(ReflectionTest.class);
   231     }
   232     
   233 }