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 classGetNameForMultiIntArrayInner() {
117 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
118 int[][][][][][] subarr = arr[0];
119 int[][][][][] subsubarr = subarr[0];
120 return subsubarr.getClass().getName();
122 @Compare public String classGetNameForMultiStringArray() {
123 return (new String[3][4][5][6][7][8][9]).getClass().getName();
126 @Compare public String classForByte() throws Exception {
127 return Class.forName("[Z").getName();
130 @Compare public String classForUnknownArray() {
132 return Class.forName("[W").getName();
133 } catch (Exception ex) {
134 return ex.getClass().getName();
138 @Compare public String classForUnknownDeepArray() {
140 return Class.forName("[[[[[W").getName();
141 } catch (Exception ex) {
142 return ex.getClass().getName();
146 @Compare public String componentGetNameForObjectArray() {
147 return (new Object[3]).getClass().getComponentType().getName();
149 @Compare public boolean sameComponentGetNameForObjectArray() {
150 return (new Object[3]).getClass().getComponentType() == Object.class;
152 @Compare public String componentGetNameForSimpleIntArray() {
153 return (new int[3]).getClass().getComponentType().getName();
155 @Compare public String componentGetNameForMultiIntArray() {
156 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
158 @Compare public String componentGetNameForMultiStringArray() {
159 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
160 StringBuilder sb = new StringBuilder();
162 sb.append(c.getName()).append("\n");
163 c = c.getComponentType();
168 return sb.toString();
171 @Compare public boolean isArray() {
172 return new Object[0].getClass().isArray();
175 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
176 private static String[] sort(String[] arr, int len) {
177 List<String> list = Arrays.asList(arr).subList(0, len);
178 Collections.sort(list);
179 return list.toArray(new String[0]);
182 @JavaScriptBody(args = {}, body = "return new Object();")
183 private static Object newObject() {
188 public static Object[] create() {
189 return VMTest.create(ReflectionTest.class);