rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
changeset 1723 3a1f262311cf
parent 1565 8977a022e424
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java	Wed Nov 19 19:32:00 2014 +0100
     1.3 @@ -0,0 +1,315 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.tck;
    1.22 +
    1.23 +import java.lang.annotation.Retention;
    1.24 +import java.lang.annotation.RetentionPolicy;
    1.25 +import java.lang.reflect.Constructor;
    1.26 +import java.lang.reflect.Method;
    1.27 +import java.lang.reflect.Proxy;
    1.28 +import java.util.Arrays;
    1.29 +import java.util.Collections;
    1.30 +import java.util.List;
    1.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.32 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.33 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.34 +import org.testng.annotations.Factory;
    1.35 +
    1.36 +/**
    1.37 + *
    1.38 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.39 + */
    1.40 +public class ReflectionTest {
    1.41 +    @Compare public boolean nonNullThis() {
    1.42 +        return this == null;
    1.43 +    }
    1.44 +    
    1.45 +    @Compare public String intType() {
    1.46 +        return Integer.TYPE.toString();
    1.47 +    }
    1.48 +
    1.49 +    @Compare public String voidType() throws Exception {
    1.50 +        return void.class.toString();
    1.51 +    }
    1.52 +
    1.53 +    @Compare public String longClass() {
    1.54 +        return long.class.toString();
    1.55 +    }
    1.56 +    
    1.57 +    @Compare public boolean isRunnableInterface() {
    1.58 +        return Runnable.class.isInterface();
    1.59 +    }
    1.60 +
    1.61 +    @Compare public boolean isAssignableToPrimitiveType() {
    1.62 +        return boolean.class.isAssignableFrom(Runnable.class);
    1.63 +    }
    1.64 +
    1.65 +    @Compare public boolean isAssignableFromPrimitiveType() {
    1.66 +        return Runnable.class.isAssignableFrom(boolean.class);
    1.67 +    }
    1.68 +
    1.69 +    @Compare public boolean isAssignableLongFromInt() {
    1.70 +        return long.class.isAssignableFrom(int.class);
    1.71 +    }
    1.72 +
    1.73 +    @Compare public boolean isAssignableIntFromLong() {
    1.74 +        return int.class.isAssignableFrom(long.class);
    1.75 +    }
    1.76 +
    1.77 +    @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
    1.78 +        return Runnable.class.getMethod("run").getName();
    1.79 +    }
    1.80 +
    1.81 +    @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
    1.82 +        return Runnable.class.getDeclaredMethod("run").getName();
    1.83 +    }
    1.84 +    
    1.85 +    @Compare public String intValue() throws Exception {
    1.86 +        return Integer.class.getConstructor(int.class).newInstance(10).toString();
    1.87 +    }
    1.88 +    
    1.89 +    @Compare public String getMethodWithArray() throws Exception {
    1.90 +        return Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class).getName();
    1.91 +    }
    1.92 +    
    1.93 +    @Compare public String namesOfMethods() {
    1.94 +        StringBuilder sb = new StringBuilder();
    1.95 +        String[] arr = new String[20];
    1.96 +        int i = 0;
    1.97 +        for (Method m : StaticUse.class.getMethods()) {
    1.98 +            arr[i++] = m.getName();
    1.99 +        }
   1.100 +        for (String s : sort(arr, i)) {
   1.101 +            sb.append(s).append("\n");
   1.102 +        }
   1.103 +        return sb.toString();
   1.104 +    }
   1.105 +
   1.106 +    @Compare public String paramsOfConstructors() {
   1.107 +        StringBuilder sb = new StringBuilder();
   1.108 +        String[] arr = new String[20];
   1.109 +        int i = 0;
   1.110 +        for (Constructor<?> m : StaticUse.class.getConstructors()) {
   1.111 +            arr[i++] = m.getName();
   1.112 +        }
   1.113 +        for (String s : sort(arr, i)) {
   1.114 +            sb.append(s).append("\n");
   1.115 +        }
   1.116 +        return sb.toString();
   1.117 +    }
   1.118 +
   1.119 +    @Compare public String namesOfDeclaringClassesOfMethods() {
   1.120 +        StringBuilder sb = new StringBuilder();
   1.121 +        String[] arr = new String[20];
   1.122 +        int i = 0;
   1.123 +        for (Method m : StaticUse.class.getMethods()) {
   1.124 +            arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
   1.125 +        }
   1.126 +        for (String s : sort(arr, i)) {
   1.127 +            sb.append(s).append("\n");
   1.128 +        }
   1.129 +        return sb.toString();
   1.130 +    }
   1.131 +    
   1.132 +    @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
   1.133 +        StaticUse.class.getMethod("instanceMethod").invoke(null);
   1.134 +        return "should not happen";
   1.135 +    }
   1.136 +    
   1.137 +    @Compare public String classCastException() {
   1.138 +        try {
   1.139 +            Integer i = (Integer)StaticUseSub.getNonNull();
   1.140 +            return "" + i.intValue();
   1.141 +        } catch (ClassCastException ex) {
   1.142 +            return ex.getClass().getName();
   1.143 +        }
   1.144 +    }
   1.145 +
   1.146 +    @Compare public String methodThatThrowsException() throws Exception {
   1.147 +        StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
   1.148 +        return "should not happen";
   1.149 +    }
   1.150 +
   1.151 +    @Compare public Object voidReturnType() throws Exception {
   1.152 +        return StaticUse.class.getMethod("instanceMethod").getReturnType();
   1.153 +    }
   1.154 +    
   1.155 +    @Retention(RetentionPolicy.RUNTIME)
   1.156 +    @interface Ann {
   1.157 +    }
   1.158 +    
   1.159 +    @Compare public String annoClass() throws Exception {
   1.160 +        Retention r = Ann.class.getAnnotation(Retention.class);
   1.161 +        assert r != null : "Annotation is present";
   1.162 +        assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   1.163 +        return r.annotationType().getName();
   1.164 +    }
   1.165 +    
   1.166 +    @Compare public boolean isAnnotation() {
   1.167 +        return Ann.class.isAnnotation();
   1.168 +    }
   1.169 +    @Compare public boolean isNotAnnotation() {
   1.170 +        return String.class.isAnnotation();
   1.171 +    }
   1.172 +    @Compare public boolean isNotAnnotationEnum() {
   1.173 +        return E.class.isAnnotation();
   1.174 +    }
   1.175 +    enum E { A, B };
   1.176 +    @Compare public boolean isEnum() {
   1.177 +        return E.A.getClass().isEnum();
   1.178 +    }
   1.179 +
   1.180 +    @Compare public boolean isNotEnum() {
   1.181 +        return "".getClass().isEnum();
   1.182 +    }
   1.183 +    
   1.184 +    @Compare public String newInstanceFails() {
   1.185 +        try {
   1.186 +            return "success: " + StaticUseSub.class.newInstance();
   1.187 +        } catch (IllegalAccessException ex) {
   1.188 +            return "failure";
   1.189 +        } catch (InstantiationException ex) {
   1.190 +            return "failure";
   1.191 +        }
   1.192 +    }
   1.193 +    
   1.194 +    @Compare public String paramTypes() throws Exception {
   1.195 +        Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   1.196 +        final Class[] pt = plus.getParameterTypes();
   1.197 +        return pt[0].getName();
   1.198 +    }
   1.199 +    @Compare public String paramTypesNotFound() throws Exception {
   1.200 +        return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   1.201 +    }
   1.202 +    @Compare public int methodWithArgs() throws Exception {
   1.203 +        Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   1.204 +        return (Integer)plus.invoke(null, 2, 3);
   1.205 +    }
   1.206 +    
   1.207 +    @Compare public String classGetNameForByte() {
   1.208 +         return byte.class.getName();
   1.209 +    }
   1.210 +    @Compare public String classGetNameForBaseObject() {
   1.211 +        return newObject().getClass().getName();
   1.212 +    }
   1.213 +    @Compare public String classGetNameForJavaObject() {
   1.214 +        return new Object().getClass().getName();
   1.215 +    }
   1.216 +    @Compare public String classGetNameForObjectArray() {
   1.217 +        return (new Object[3]).getClass().getName();
   1.218 +    }
   1.219 +    @Compare public String classGetNameForSimpleIntArray() {
   1.220 +        return (new int[3]).getClass().getName();
   1.221 +    }
   1.222 +    @Compare public boolean sameClassGetNameForSimpleCharArray() {
   1.223 +        return (new char[3]).getClass() == (new char[34]).getClass();
   1.224 +    }
   1.225 +    @Compare public String classGetNameForMultiIntArray() {
   1.226 +        return (new int[3][4][5][6][7][8][9]).getClass().getName();
   1.227 +    }
   1.228 +    @Compare public String classGetNameForMultiIntArrayInner() {
   1.229 +        final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   1.230 +        int[][][][][][] subarr = arr[0];
   1.231 +        int[][][][][] subsubarr = subarr[0];
   1.232 +        return subsubarr.getClass().getName();
   1.233 +    }
   1.234 +    @Compare public String classGetNameForMultiStringArray() {
   1.235 +        return (new String[3][4][5][6][7][8][9]).getClass().getName();
   1.236 +    }
   1.237 +    
   1.238 +    @Compare public String classForByte() throws Exception {
   1.239 +        return Class.forName("[Z").getName();
   1.240 +    }
   1.241 +
   1.242 +    @Compare public String classForUnknownArray() {
   1.243 +        try {
   1.244 +            return Class.forName("[W").getName();
   1.245 +        } catch (Exception ex) {
   1.246 +            return ex.getClass().getName();
   1.247 +        }
   1.248 +    }
   1.249 +    
   1.250 +    @Compare public String classForUnknownDeepArray() {
   1.251 +        try {
   1.252 +            return Class.forName("[[[[[W").getName();
   1.253 +        } catch (Exception ex) {
   1.254 +            return ex.getClass().getName();
   1.255 +        }
   1.256 +    }
   1.257 +    
   1.258 +    @Compare public int callAbst() throws Exception {
   1.259 +        class Impl extends Abst {
   1.260 +            @Override
   1.261 +            public int abst() {
   1.262 +                return 10;
   1.263 +            }
   1.264 +        }
   1.265 +        Abst impl = new Impl();
   1.266 +        return (int) Abst.class.getMethod("abst").invoke(impl);
   1.267 +    }
   1.268 +    
   1.269 +    @Compare public String componentGetNameForObjectArray() {
   1.270 +        return (new Object[3]).getClass().getComponentType().getName();
   1.271 +    }
   1.272 +    @Compare public boolean sameComponentGetNameForObjectArray() {
   1.273 +        return (new Object[3]).getClass().getComponentType() == Object.class;
   1.274 +    }
   1.275 +    @Compare public String componentGetNameForSimpleIntArray() {
   1.276 +        return (new int[3]).getClass().getComponentType().getName();
   1.277 +    }
   1.278 +    @Compare public String componentGetNameForMultiIntArray() {
   1.279 +        return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   1.280 +    }
   1.281 +    @Compare public String componentGetNameForMultiStringArray() {
   1.282 +        Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   1.283 +        StringBuilder sb = new StringBuilder();
   1.284 +        for (;;) {
   1.285 +            sb.append(c.getName()).append("\n");
   1.286 +            c = c.getComponentType();
   1.287 +            if (c == null) {
   1.288 +                break;
   1.289 +            }
   1.290 +        }
   1.291 +        return sb.toString();
   1.292 +    }
   1.293 +    
   1.294 +    @Compare public boolean isArray() {
   1.295 +        return new Object[0].getClass().isArray();
   1.296 +    }
   1.297 +    
   1.298 +    @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   1.299 +    private static String[] sort(String[] arr, int len) {
   1.300 +        List<String> list = Arrays.asList(arr).subList(0, len);
   1.301 +        Collections.sort(list);
   1.302 +        return list.toArray(new String[0]);
   1.303 +    }
   1.304 +    
   1.305 +    @JavaScriptBody(args = {}, body = "return new Object();")
   1.306 +    private static Object newObject() {
   1.307 +        return new Object();
   1.308 +    }
   1.309 +    
   1.310 +    @Factory
   1.311 +    public static Object[] create() {
   1.312 +        return VMTest.create(ReflectionTest.class);
   1.313 +    }
   1.314 +    
   1.315 +    public static abstract class Abst {
   1.316 +        public abstract int abst();
   1.317 +    }
   1.318 +}