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