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 isRunnableDeclaresRunMethod() throws NoSuchMethodException {
77 return Runnable.class.getDeclaredMethod("run").getName();
80 @Compare public String namesOfMethods() {
81 StringBuilder sb = new StringBuilder();
82 String[] arr = new String[20];
84 for (Method m : StaticUse.class.getMethods()) {
85 arr[i++] = m.getName();
87 for (String s : sort(arr, i)) {
88 sb.append(s).append("\n");
93 @Compare public String namesOfDeclaringClassesOfMethods() {
94 StringBuilder sb = new StringBuilder();
95 String[] arr = new String[20];
97 for (Method m : StaticUse.class.getMethods()) {
98 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
100 for (String s : sort(arr, i)) {
101 sb.append(s).append("\n");
103 return sb.toString();
106 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
107 StaticUse.class.getMethod("instanceMethod").invoke(null);
108 return "should not happen";
111 @Compare public String classCastException() {
113 Integer i = (Integer)StaticUseSub.getNonNull();
114 return "" + i.intValue();
115 } catch (ClassCastException ex) {
116 return ex.getClass().getName();
120 @Compare public String methodThatThrowsException() throws Exception {
121 StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
122 return "should not happen";
125 @Compare public Object voidReturnType() throws Exception {
126 return StaticUse.class.getMethod("instanceMethod").getReturnType();
129 @Retention(RetentionPolicy.RUNTIME)
133 @Compare public String annoClass() throws Exception {
134 Retention r = Ann.class.getAnnotation(Retention.class);
135 assert r != null : "Annotation is present";
136 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
137 return r.annotationType().getName();
140 @Compare public boolean isAnnotation() {
141 return Ann.class.isAnnotation();
143 @Compare public boolean isNotAnnotation() {
144 return String.class.isAnnotation();
146 @Compare public boolean isNotAnnotationEnum() {
147 return E.class.isAnnotation();
150 @Compare public boolean isEnum() {
151 return E.A.getClass().isEnum();
154 @Compare public boolean isNotEnum() {
155 return "".getClass().isEnum();
158 @Compare public String newInstanceFails() throws InstantiationException {
160 return "success: " + StaticUseSub.class.newInstance();
161 } catch (IllegalAccessException ex) {
162 return ex.getClass().getName();
166 @Compare public String paramTypes() throws Exception {
167 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
168 final Class[] pt = plus.getParameterTypes();
169 return pt[0].getName();
171 @Compare public String paramTypesNotFound() throws Exception {
172 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
174 @Compare public int methodWithArgs() throws Exception {
175 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
176 return (Integer)plus.invoke(null, 2, 3);
179 @Compare public String classGetNameForByte() {
180 return byte.class.getName();
182 @Compare public String classGetNameForBaseObject() {
183 return newObject().getClass().getName();
185 @Compare public String classGetNameForJavaObject() {
186 return new Object().getClass().getName();
188 @Compare public String classGetNameForObjectArray() {
189 return (new Object[3]).getClass().getName();
191 @Compare public String classGetNameForSimpleIntArray() {
192 return (new int[3]).getClass().getName();
194 @Compare public boolean sameClassGetNameForSimpleCharArray() {
195 return (new char[3]).getClass() == (new char[34]).getClass();
197 @Compare public String classGetNameForMultiIntArray() {
198 return (new int[3][4][5][6][7][8][9]).getClass().getName();
200 @Compare public String classGetNameForMultiIntArrayInner() {
201 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
202 int[][][][][][] subarr = arr[0];
203 int[][][][][] subsubarr = subarr[0];
204 return subsubarr.getClass().getName();
206 @Compare public String classGetNameForMultiStringArray() {
207 return (new String[3][4][5][6][7][8][9]).getClass().getName();
210 @Compare public String classForByte() throws Exception {
211 return Class.forName("[Z").getName();
214 @Compare public String classForUnknownArray() {
216 return Class.forName("[W").getName();
217 } catch (Exception ex) {
218 return ex.getClass().getName();
222 @Compare public String classForUnknownDeepArray() {
224 return Class.forName("[[[[[W").getName();
225 } catch (Exception ex) {
226 return ex.getClass().getName();
230 @Compare public String componentGetNameForObjectArray() {
231 return (new Object[3]).getClass().getComponentType().getName();
233 @Compare public boolean sameComponentGetNameForObjectArray() {
234 return (new Object[3]).getClass().getComponentType() == Object.class;
236 @Compare public String componentGetNameForSimpleIntArray() {
237 return (new int[3]).getClass().getComponentType().getName();
239 @Compare public String componentGetNameForMultiIntArray() {
240 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
242 @Compare public String componentGetNameForMultiStringArray() {
243 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
244 StringBuilder sb = new StringBuilder();
246 sb.append(c.getName()).append("\n");
247 c = c.getComponentType();
252 return sb.toString();
255 @Compare public boolean isArray() {
256 return new Object[0].getClass().isArray();
259 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
260 private static String[] sort(String[] arr, int len) {
261 List<String> list = Arrays.asList(arr).subList(0, len);
262 Collections.sort(list);
263 return list.toArray(new String[0]);
266 @JavaScriptBody(args = {}, body = "return new Object();")
267 private static Object newObject() {
272 public static Object[] create() {
273 return VMTest.create(ReflectionTest.class);