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.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;
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 boolean isRunnableInterface() {
53 return Runnable.class.isInterface();
56 @Compare public boolean isAssignableToPrimitiveType() {
57 return boolean.class.isAssignableFrom(Runnable.class);
60 @Compare public boolean isAssignableFromPrimitiveType() {
61 return Runnable.class.isAssignableFrom(boolean.class);
64 @Compare public boolean isAssignableLongFromInt() {
65 return long.class.isAssignableFrom(int.class);
68 @Compare public boolean isAssignableIntFromLong() {
69 return int.class.isAssignableFrom(long.class);
72 @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
73 return Runnable.class.getMethod("run").getName();
76 @Compare public String namesOfMethods() {
77 StringBuilder sb = new StringBuilder();
78 String[] arr = new String[20];
80 for (Method m : StaticUse.class.getMethods()) {
81 arr[i++] = m.getName();
83 for (String s : sort(arr, i)) {
84 sb.append(s).append("\n");
89 @Compare public String namesOfDeclaringClassesOfMethods() {
90 StringBuilder sb = new StringBuilder();
91 String[] arr = new String[20];
93 for (Method m : StaticUse.class.getMethods()) {
94 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
96 for (String s : sort(arr, i)) {
97 sb.append(s).append("\n");
102 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
103 StaticUse.class.getMethod("instanceMethod").invoke(null);
104 return "should not happen";
107 @Compare public String classCastException() {
109 Integer i = (Integer)StaticUseSub.getNonNull();
110 return "" + i.intValue();
111 } catch (ClassCastException ex) {
112 return ex.getClass().getName();
116 @Compare public String methodThatThrowsException() throws Exception {
117 StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
118 return "should not happen";
121 @Compare public Object voidReturnType() throws Exception {
122 return StaticUse.class.getMethod("instanceMethod").getReturnType();
125 @Retention(RetentionPolicy.RUNTIME)
129 @Compare public String annoClass() throws Exception {
130 Retention r = Ann.class.getAnnotation(Retention.class);
131 assert r != null : "Annotation is present";
132 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
133 return r.annotationType().getName();
136 @Compare public boolean isAnnotation() {
137 return Ann.class.isAnnotation();
139 @Compare public boolean isNotAnnotation() {
140 return String.class.isAnnotation();
142 @Compare public boolean isNotAnnotationEnum() {
143 return E.class.isAnnotation();
146 @Compare public boolean isEnum() {
147 return E.A.getClass().isEnum();
150 @Compare public boolean isNotEnum() {
151 return "".getClass().isEnum();
154 @Compare public String newInstanceFails() throws InstantiationException {
156 return "success: " + StaticUseSub.class.newInstance();
157 } catch (IllegalAccessException ex) {
158 return ex.getClass().getName();
162 @Compare public String paramTypes() throws Exception {
163 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
164 final Class[] pt = plus.getParameterTypes();
165 return pt[0].getName();
167 @Compare public String paramTypesNotFound() throws Exception {
168 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
170 @Compare public int methodWithArgs() throws Exception {
171 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
172 return (Integer)plus.invoke(null, 2, 3);
175 @Compare public String classGetNameForByte() {
176 return byte.class.getName();
178 @Compare public String classGetNameForBaseObject() {
179 return newObject().getClass().getName();
181 @Compare public String classGetNameForJavaObject() {
182 return new Object().getClass().getName();
184 @Compare public String classGetNameForObjectArray() {
185 return (new Object[3]).getClass().getName();
187 @Compare public String classGetNameForSimpleIntArray() {
188 return (new int[3]).getClass().getName();
190 @Compare public boolean sameClassGetNameForSimpleCharArray() {
191 return (new char[3]).getClass() == (new char[34]).getClass();
193 @Compare public String classGetNameForMultiIntArray() {
194 return (new int[3][4][5][6][7][8][9]).getClass().getName();
196 @Compare public String classGetNameForMultiIntArrayInner() {
197 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
198 int[][][][][][] subarr = arr[0];
199 int[][][][][] subsubarr = subarr[0];
200 return subsubarr.getClass().getName();
202 @Compare public String classGetNameForMultiStringArray() {
203 return (new String[3][4][5][6][7][8][9]).getClass().getName();
206 @Compare public String classForByte() throws Exception {
207 return Class.forName("[Z").getName();
210 @Compare public String classForUnknownArray() {
212 return Class.forName("[W").getName();
213 } catch (Exception ex) {
214 return ex.getClass().getName();
218 @Compare public String classForUnknownDeepArray() {
220 return Class.forName("[[[[[W").getName();
221 } catch (Exception ex) {
222 return ex.getClass().getName();
226 @Compare public String componentGetNameForObjectArray() {
227 return (new Object[3]).getClass().getComponentType().getName();
229 @Compare public boolean sameComponentGetNameForObjectArray() {
230 return (new Object[3]).getClass().getComponentType() == Object.class;
232 @Compare public String componentGetNameForSimpleIntArray() {
233 return (new int[3]).getClass().getComponentType().getName();
235 @Compare public String componentGetNameForMultiIntArray() {
236 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
238 @Compare public String componentGetNameForMultiStringArray() {
239 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
240 StringBuilder sb = new StringBuilder();
242 sb.append(c.getName()).append("\n");
243 c = c.getComponentType();
248 return sb.toString();
251 @Compare public boolean isArray() {
252 return new Object[0].getClass().isArray();
255 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
256 private static String[] sort(String[] arr, int len) {
257 List<String> list = Arrays.asList(arr).subList(0, len);
258 Collections.sort(list);
259 return list.toArray(new String[0]);
262 @JavaScriptBody(args = {}, body = "return new Object();")
263 private static Object newObject() {
268 public static Object[] create() {
269 return VMTest.create(ReflectionTest.class);