2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.tck;
20 import java.lang.reflect.Method;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
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;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public class ReflectionTest {
36 @Compare public boolean nonNullThis() {
40 @Compare public String intType() {
41 return Integer.TYPE.toString();
44 @Compare public String voidType() throws Exception {
45 return void.class.toString();
48 @Compare public String longClass() {
49 return long.class.toString();
52 @Compare public String namesOfMethods() {
53 StringBuilder sb = new StringBuilder();
54 String[] arr = new String[20];
56 for (Method m : StaticUse.class.getMethods()) {
57 arr[i++] = m.getName();
59 for (String s : sort(arr, i)) {
60 sb.append(s).append("\n");
65 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
66 StaticUse.class.getMethod("instanceMethod").invoke(null);
67 return "should not happen";
70 @Compare public Object voidReturnType() throws Exception {
71 return StaticUse.class.getMethod("instanceMethod").getReturnType();
74 @Compare public String newInstanceFails() throws InstantiationException {
76 return "success: " + StaticUse.class.newInstance();
77 } catch (IllegalAccessException ex) {
78 return ex.getClass().getName();
82 @Compare public String paramTypes() throws Exception {
83 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
84 final Class[] pt = plus.getParameterTypes();
85 return pt[0].getName();
87 @Compare public String paramTypesNotFound() throws Exception {
88 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
90 @Compare public int methodWithArgs() throws Exception {
91 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
92 return (Integer)plus.invoke(null, 2, 3);
95 @Compare public String classGetNameForByte() {
96 return byte.class.getName();
98 @Compare public String classGetNameForBaseObject() {
99 return newObject().getClass().getName();
101 @Compare public String classGetNameForJavaObject() {
102 return new Object().getClass().getName();
104 @Compare public String classGetNameForObjectArray() {
105 return (new Object[3]).getClass().getName();
107 @Compare public String classGetNameForSimpleIntArray() {
108 return (new int[3]).getClass().getName();
110 @Compare public boolean sameClassGetNameForSimpleCharArray() {
111 return (new char[3]).getClass() == (new char[34]).getClass();
113 @Compare public String classGetNameForMultiIntArray() {
114 return (new int[3][4][5][6][7][8][9]).getClass().getName();
116 @Compare public String classGetNameForMultiStringArray() {
117 return (new String[3][4][5][6][7][8][9]).getClass().getName();
120 @Compare public String classForByte() throws Exception {
121 return Class.forName("[Z").getName();
124 @Compare public String classForUnknownArray() {
126 return Class.forName("[W").getName();
127 } catch (Exception ex) {
128 return ex.getClass().getName();
132 @Compare public String classForUnknownDeepArray() {
134 return Class.forName("[[[[[W").getName();
135 } catch (Exception ex) {
136 return ex.getClass().getName();
140 @Compare public String componentGetNameForObjectArray() {
141 return (new Object[3]).getClass().getComponentType().getName();
143 @Compare public boolean sameComponentGetNameForObjectArray() {
144 return (new Object[3]).getClass().getComponentType() == Object.class;
146 @Compare public String componentGetNameForSimpleIntArray() {
147 return (new int[3]).getClass().getComponentType().getName();
149 @Compare public String componentGetNameForMultiIntArray() {
150 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
152 @Compare public String componentGetNameForMultiStringArray() {
153 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
154 StringBuilder sb = new StringBuilder();
156 sb.append(c.getName()).append("\n");
157 c = c.getComponentType();
162 return sb.toString();
165 @Compare public boolean isArray() {
166 return new Object[0].getClass().isArray();
169 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
170 private static String[] sort(String[] arr, int len) {
171 List<String> list = Arrays.asList(arr).subList(0, len);
172 Collections.sort(list);
173 return list.toArray(new String[0]);
176 @JavaScriptBody(args = {}, body = "return new Object();")
177 private static Object newObject() {
182 public static Object[] create() {
183 return VMTest.create(ReflectionTest.class);