rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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 boolean RunnableRunReturnsVoid() throws NoSuchMethodException {
79 return Runnable.class.getMethod("run").getReturnType() == Void.TYPE;
82 @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
83 return Runnable.class.getDeclaredMethod("run").getName();
86 @Compare public String intValue() throws Exception {
87 return Integer.class.getConstructor(int.class).newInstance(10).toString();
90 @Compare public String getMethodWithArray() throws Exception {
91 return Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class).getName();
94 @Compare public String namesOfMethods() {
95 StringBuilder sb = new StringBuilder();
96 String[] arr = new String[20];
98 for (Method m : StaticUse.class.getMethods()) {
99 arr[i++] = m.getName();
101 for (String s : sort(arr, i)) {
102 sb.append(s).append("\n");
104 return sb.toString();
108 public String namesOfDeclaredMethods() {
109 StringBuilder sb = new StringBuilder();
110 String[] arr = new String[20];
111 Method[] methods = StaticUse.class.getDeclaredMethods();
112 if (methods.length != 2) {
113 throw new IllegalStateException("Expecting just two methods, was: " + methods.length);
116 for (Method m : methods) {
117 arr[i++] = m.getName();
119 for (String s : sort(arr, i)) {
120 sb.append(s).append("\n");
122 return sb.toString();
125 @Compare public String paramsOfConstructors() {
126 StringBuilder sb = new StringBuilder();
127 String[] arr = new String[20];
129 for (Constructor<?> m : StaticUse.class.getConstructors()) {
130 arr[i++] = m.getName();
132 for (String s : sort(arr, i)) {
133 sb.append(s).append("\n");
135 return sb.toString();
138 @Compare public String namesOfDeclaringClassesOfMethods() {
139 StringBuilder sb = new StringBuilder();
140 String[] arr = new String[20];
142 for (Method m : StaticUse.class.getMethods()) {
143 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
145 for (String s : sort(arr, i)) {
146 sb.append(s).append("\n");
148 return sb.toString();
151 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
152 StaticUse.class.getMethod("instanceMethod").invoke(null);
153 return "should not happen";
156 @Compare public String classCastException() {
158 Integer i = (Integer)StaticUseSub.getNonNull();
159 return "" + i.intValue();
160 } catch (ClassCastException ex) {
161 return ex.getClass().getName();
165 @Compare public String methodThatThrowsException() throws Exception {
166 StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
167 return "should not happen";
170 @Compare public Object voidReturnType() throws Exception {
171 return StaticUse.class.getMethod("instanceMethod").getReturnType();
174 @Retention(RetentionPolicy.RUNTIME)
178 @Compare public String annoClass() throws Exception {
179 Retention r = Ann.class.getAnnotation(Retention.class);
180 assert r != null : "Annotation is present";
181 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
182 return r.annotationType().getName();
185 @Compare public boolean isAnnotation() {
186 return Ann.class.isAnnotation();
188 @Compare public boolean isNotAnnotation() {
189 return String.class.isAnnotation();
191 @Compare public boolean isNotAnnotationEnum() {
192 return E.class.isAnnotation();
195 @Compare public boolean isEnum() {
196 return E.A.getClass().isEnum();
199 @Compare public boolean isNotEnum() {
200 return "".getClass().isEnum();
203 @Compare public String newInstanceFails() {
205 return "success: " + StaticUseSub.class.newInstance();
206 } catch (IllegalAccessException ex) {
208 } catch (InstantiationException ex) {
213 @Compare public String paramTypes() throws Exception {
214 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
215 final Class[] pt = plus.getParameterTypes();
216 return pt[0].getName();
218 @Compare public String paramTypesNotFound() throws Exception {
219 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
221 @Compare public int methodWithArgs() throws Exception {
222 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
223 return (Integer)plus.invoke(null, 2, 3);
226 @Compare public String classGetNameForByte() {
227 return byte.class.getName();
229 @Compare public String classGetNameForBaseObject() {
230 return newObject().getClass().getName();
232 @Compare public String classGetNameForJavaObject() {
233 return new Object().getClass().getName();
235 @Compare public String classGetNameForObjectArray() {
236 return (new Object[3]).getClass().getName();
238 @Compare public String classGetNameForSimpleIntArray() {
239 return (new int[3]).getClass().getName();
241 @Compare public boolean sameClassGetNameForSimpleCharArray() {
242 return (new char[3]).getClass() == (new char[34]).getClass();
244 @Compare public String classGetNameForMultiIntArray() {
245 return (new int[3][4][5][6][7][8][9]).getClass().getName();
247 @Compare public String classGetNameForMultiIntArrayInner() {
248 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
249 int[][][][][][] subarr = arr[0];
250 int[][][][][] subsubarr = subarr[0];
251 return subsubarr.getClass().getName();
253 @Compare public String classGetNameForMultiStringArray() {
254 return (new String[3][4][5][6][7][8][9]).getClass().getName();
257 @Compare public String classForByte() throws Exception {
258 return Class.forName("[Z").getName();
261 @Compare public String classForUnknownArray() {
263 return Class.forName("[W").getName();
264 } catch (Exception ex) {
265 return ex.getClass().getName();
269 @Compare public String classForUnknownDeepArray() {
271 return Class.forName("[[[[[W").getName();
272 } catch (Exception ex) {
273 return ex.getClass().getName();
277 @Compare public int callAbst() throws Exception {
278 class Impl extends Abst {
284 Abst impl = new Impl();
285 return (int) Abst.class.getMethod("abst").invoke(impl);
288 @Compare public String componentGetNameForObjectArray() {
289 return (new Object[3]).getClass().getComponentType().getName();
291 @Compare public boolean sameComponentGetNameForObjectArray() {
292 return (new Object[3]).getClass().getComponentType() == Object.class;
294 @Compare public String componentGetNameForSimpleIntArray() {
295 return (new int[3]).getClass().getComponentType().getName();
297 @Compare public String componentGetNameForMultiIntArray() {
298 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
300 @Compare public String componentGetNameForMultiStringArray() {
301 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
302 StringBuilder sb = new StringBuilder();
304 sb.append(c.getName()).append("\n");
305 c = c.getComponentType();
310 return sb.toString();
313 @Compare public boolean isArray() {
314 return new Object[0].getClass().isArray();
317 @Compare public String copyDataViaReflection() throws Exception {
319 d.setName("Hello world!");
321 Method getter = d.getClass().getMethod("getName");
322 Object val = getter.invoke(d);
324 Data clone = new Data();
325 Method setter = d.getClass().getMethod("setName", String.class);
326 setter.invoke(clone, val);
328 return clone.getName();
331 @Compare public String copyStaticDataViaReflection() throws Exception {
332 Data.setStatic("Hello world!");
334 Method getter = Data.class.getMethod("getStatic");
335 Object val = getter.invoke(null);
337 Method setter = Data.class.getMethod("setStatic", String.class);
338 setter.invoke(null, val);
340 return Data.getStatic();
343 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
344 private static String[] sort(String[] arr, int len) {
345 List<String> list = Arrays.asList(arr).subList(0, len);
346 Collections.sort(list);
347 return list.toArray(new String[0]);
350 @JavaScriptBody(args = {}, body = "return new Object();")
351 private static Object newObject() {
356 public static Object[] create() {
357 return VMTest.create(ReflectionTest.class);
360 public static abstract class Abst {
361 public abstract int abst();
364 public static final class Data {
366 private static String nameStatic;
368 public String getName() {
372 public void setName(String name) {
376 public static String getStatic() {
380 public static void setStatic(String n) {