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.Constructor;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
29 import org.apidesign.bck2brwsr.vmtest.Compare;
30 import org.apidesign.bck2brwsr.vmtest.VMTest;
31 import org.testng.annotations.Factory;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public class ReflectionTest {
38 @Compare public boolean nonNullThis() {
42 @Compare public String intType() {
43 return Integer.TYPE.toString();
46 @Compare public String voidType() throws Exception {
47 return void.class.toString();
50 @Compare public String longClass() {
51 return long.class.toString();
54 @Compare public boolean isRunnableInterface() {
55 return Runnable.class.isInterface();
58 @Compare public boolean isAssignableToPrimitiveType() {
59 return boolean.class.isAssignableFrom(Runnable.class);
62 @Compare public boolean isAssignableFromPrimitiveType() {
63 return Runnable.class.isAssignableFrom(boolean.class);
66 @Compare public boolean isAssignableLongFromInt() {
67 return long.class.isAssignableFrom(int.class);
70 @Compare public boolean isAssignableIntFromLong() {
71 return int.class.isAssignableFrom(long.class);
74 @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
75 return Runnable.class.getMethod("run").getName();
78 @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
79 return Runnable.class.getDeclaredMethod("run").getName();
82 @Compare public String intValue() throws Exception {
83 return Integer.class.getConstructor(int.class).newInstance(10).toString();
86 @Compare public String getMethodWithArray() throws Exception {
87 return Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class).getName();
90 @Compare public String namesOfMethods() {
91 StringBuilder sb = new StringBuilder();
92 String[] arr = new String[20];
94 for (Method m : StaticUse.class.getMethods()) {
95 arr[i++] = m.getName();
97 for (String s : sort(arr, i)) {
98 sb.append(s).append("\n");
100 return sb.toString();
103 @Compare public String paramsOfConstructors() {
104 StringBuilder sb = new StringBuilder();
105 String[] arr = new String[20];
107 for (Constructor<?> m : StaticUse.class.getConstructors()) {
108 arr[i++] = m.getName();
110 for (String s : sort(arr, i)) {
111 sb.append(s).append("\n");
113 return sb.toString();
116 @Compare public String namesOfDeclaringClassesOfMethods() {
117 StringBuilder sb = new StringBuilder();
118 String[] arr = new String[20];
120 for (Method m : StaticUse.class.getMethods()) {
121 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
123 for (String s : sort(arr, i)) {
124 sb.append(s).append("\n");
126 return sb.toString();
129 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
130 StaticUse.class.getMethod("instanceMethod").invoke(null);
131 return "should not happen";
134 @Compare public String classCastException() {
136 Integer i = (Integer)StaticUseSub.getNonNull();
137 return "" + i.intValue();
138 } catch (ClassCastException ex) {
139 return ex.getClass().getName();
143 @Compare public String methodThatThrowsException() throws Exception {
144 StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
145 return "should not happen";
148 @Compare public Object voidReturnType() throws Exception {
149 return StaticUse.class.getMethod("instanceMethod").getReturnType();
152 @Retention(RetentionPolicy.RUNTIME)
156 @Compare public String annoClass() throws Exception {
157 Retention r = Ann.class.getAnnotation(Retention.class);
158 assert r != null : "Annotation is present";
159 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
160 return r.annotationType().getName();
163 @Compare public boolean isAnnotation() {
164 return Ann.class.isAnnotation();
166 @Compare public boolean isNotAnnotation() {
167 return String.class.isAnnotation();
169 @Compare public boolean isNotAnnotationEnum() {
170 return E.class.isAnnotation();
173 @Compare public boolean isEnum() {
174 return E.A.getClass().isEnum();
177 @Compare public boolean isNotEnum() {
178 return "".getClass().isEnum();
181 @Compare public String newInstanceFails() {
183 return "success: " + StaticUseSub.class.newInstance();
184 } catch (IllegalAccessException ex) {
186 } catch (InstantiationException ex) {
191 @Compare public String paramTypes() throws Exception {
192 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
193 final Class[] pt = plus.getParameterTypes();
194 return pt[0].getName();
196 @Compare public String paramTypesNotFound() throws Exception {
197 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
199 @Compare public int methodWithArgs() throws Exception {
200 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
201 return (Integer)plus.invoke(null, 2, 3);
204 @Compare public String classGetNameForByte() {
205 return byte.class.getName();
207 @Compare public String classGetNameForBaseObject() {
208 return newObject().getClass().getName();
210 @Compare public String classGetNameForJavaObject() {
211 return new Object().getClass().getName();
213 @Compare public String classGetNameForObjectArray() {
214 return (new Object[3]).getClass().getName();
216 @Compare public String classGetNameForSimpleIntArray() {
217 return (new int[3]).getClass().getName();
219 @Compare public boolean sameClassGetNameForSimpleCharArray() {
220 return (new char[3]).getClass() == (new char[34]).getClass();
222 @Compare public String classGetNameForMultiIntArray() {
223 return (new int[3][4][5][6][7][8][9]).getClass().getName();
225 @Compare public String classGetNameForMultiIntArrayInner() {
226 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
227 int[][][][][][] subarr = arr[0];
228 int[][][][][] subsubarr = subarr[0];
229 return subsubarr.getClass().getName();
231 @Compare public String classGetNameForMultiStringArray() {
232 return (new String[3][4][5][6][7][8][9]).getClass().getName();
235 @Compare public String classForByte() throws Exception {
236 return Class.forName("[Z").getName();
239 @Compare public String classForUnknownArray() {
241 return Class.forName("[W").getName();
242 } catch (Exception ex) {
243 return ex.getClass().getName();
247 @Compare public String classForUnknownDeepArray() {
249 return Class.forName("[[[[[W").getName();
250 } catch (Exception ex) {
251 return ex.getClass().getName();
255 @Compare public int callAbst() throws Exception {
256 class Impl extends Abst {
262 Abst impl = new Impl();
263 return (int) Abst.class.getMethod("abst").invoke(impl);
266 @Compare public String componentGetNameForObjectArray() {
267 return (new Object[3]).getClass().getComponentType().getName();
269 @Compare public boolean sameComponentGetNameForObjectArray() {
270 return (new Object[3]).getClass().getComponentType() == Object.class;
272 @Compare public String componentGetNameForSimpleIntArray() {
273 return (new int[3]).getClass().getComponentType().getName();
275 @Compare public String componentGetNameForMultiIntArray() {
276 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
278 @Compare public String componentGetNameForMultiStringArray() {
279 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
280 StringBuilder sb = new StringBuilder();
282 sb.append(c.getName()).append("\n");
283 c = c.getComponentType();
288 return sb.toString();
291 @Compare public boolean isArray() {
292 return new Object[0].getClass().isArray();
295 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
296 private static String[] sort(String[] arr, int len) {
297 List<String> list = Arrays.asList(arr).subList(0, len);
298 Collections.sort(list);
299 return list.toArray(new String[0]);
302 @JavaScriptBody(args = {}, body = "return new Object();")
303 private static Object newObject() {
308 public static Object[] create() {
309 return VMTest.create(ReflectionTest.class);
312 public static abstract class Abst {
313 public abstract int abst();