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.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 import org.apidesign.bck2brwsr.core.JavaScriptBody;
28 import org.apidesign.bck2brwsr.vmtest.Compare;
29 import org.apidesign.bck2brwsr.vmtest.VMTest;
30 import org.testng.annotations.Factory;
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 public class ReflectionTest {
37 @Compare public boolean nonNullThis() {
41 @Compare public String intType() {
42 return Integer.TYPE.toString();
45 @Compare public String voidType() throws Exception {
46 return void.class.toString();
49 @Compare public String longClass() {
50 return long.class.toString();
53 @Compare public boolean isRunnableInterface() {
54 return Runnable.class.isInterface();
57 @Compare public boolean isAssignableToPrimitiveType() {
58 return boolean.class.isAssignableFrom(Runnable.class);
61 @Compare public boolean isAssignableFromPrimitiveType() {
62 return Runnable.class.isAssignableFrom(boolean.class);
65 @Compare public boolean isAssignableLongFromInt() {
66 return long.class.isAssignableFrom(int.class);
69 @Compare public boolean isAssignableIntFromLong() {
70 return int.class.isAssignableFrom(long.class);
73 @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
74 return Runnable.class.getMethod("run").getName();
77 @Compare public String isRunnableDeclaresRunMethod() throws NoSuchMethodException {
78 return Runnable.class.getDeclaredMethod("run").getName();
81 @Compare public String intValue() throws Exception {
82 return Integer.class.getConstructor(int.class).newInstance(10).toString();
85 @Compare public String namesOfMethods() {
86 StringBuilder sb = new StringBuilder();
87 String[] arr = new String[20];
89 for (Method m : StaticUse.class.getMethods()) {
90 arr[i++] = m.getName();
92 for (String s : sort(arr, i)) {
93 sb.append(s).append("\n");
98 @Compare public String paramsOfConstructors() {
99 StringBuilder sb = new StringBuilder();
100 String[] arr = new String[20];
102 for (Constructor<?> m : StaticUse.class.getConstructors()) {
103 arr[i++] = m.getName();
105 for (String s : sort(arr, i)) {
106 sb.append(s).append("\n");
108 return sb.toString();
111 @Compare public String namesOfDeclaringClassesOfMethods() {
112 StringBuilder sb = new StringBuilder();
113 String[] arr = new String[20];
115 for (Method m : StaticUse.class.getMethods()) {
116 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
118 for (String s : sort(arr, i)) {
119 sb.append(s).append("\n");
121 return sb.toString();
124 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
125 StaticUse.class.getMethod("instanceMethod").invoke(null);
126 return "should not happen";
129 @Compare public String classCastException() {
131 Integer i = (Integer)StaticUseSub.getNonNull();
132 return "" + i.intValue();
133 } catch (ClassCastException ex) {
134 return ex.getClass().getName();
138 @Compare public String methodThatThrowsException() throws Exception {
139 StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
140 return "should not happen";
143 @Compare public Object voidReturnType() throws Exception {
144 return StaticUse.class.getMethod("instanceMethod").getReturnType();
147 @Retention(RetentionPolicy.RUNTIME)
151 @Compare public String annoClass() throws Exception {
152 Retention r = Ann.class.getAnnotation(Retention.class);
153 assert r != null : "Annotation is present";
154 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
155 return r.annotationType().getName();
158 @Compare public boolean isAnnotation() {
159 return Ann.class.isAnnotation();
161 @Compare public boolean isNotAnnotation() {
162 return String.class.isAnnotation();
164 @Compare public boolean isNotAnnotationEnum() {
165 return E.class.isAnnotation();
168 @Compare public boolean isEnum() {
169 return E.A.getClass().isEnum();
172 @Compare public boolean isNotEnum() {
173 return "".getClass().isEnum();
176 @Compare public String newInstanceFails() throws InstantiationException {
178 return "success: " + StaticUseSub.class.newInstance();
179 } catch (IllegalAccessException ex) {
180 return ex.getClass().getName();
184 @Compare public String paramTypes() throws Exception {
185 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
186 final Class[] pt = plus.getParameterTypes();
187 return pt[0].getName();
189 @Compare public String paramTypesNotFound() throws Exception {
190 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
192 @Compare public int methodWithArgs() throws Exception {
193 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
194 return (Integer)plus.invoke(null, 2, 3);
197 @Compare public String classGetNameForByte() {
198 return byte.class.getName();
200 @Compare public String classGetNameForBaseObject() {
201 return newObject().getClass().getName();
203 @Compare public String classGetNameForJavaObject() {
204 return new Object().getClass().getName();
206 @Compare public String classGetNameForObjectArray() {
207 return (new Object[3]).getClass().getName();
209 @Compare public String classGetNameForSimpleIntArray() {
210 return (new int[3]).getClass().getName();
212 @Compare public boolean sameClassGetNameForSimpleCharArray() {
213 return (new char[3]).getClass() == (new char[34]).getClass();
215 @Compare public String classGetNameForMultiIntArray() {
216 return (new int[3][4][5][6][7][8][9]).getClass().getName();
218 @Compare public String classGetNameForMultiIntArrayInner() {
219 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
220 int[][][][][][] subarr = arr[0];
221 int[][][][][] subsubarr = subarr[0];
222 return subsubarr.getClass().getName();
224 @Compare public String classGetNameForMultiStringArray() {
225 return (new String[3][4][5][6][7][8][9]).getClass().getName();
228 @Compare public String classForByte() throws Exception {
229 return Class.forName("[Z").getName();
232 @Compare public String classForUnknownArray() {
234 return Class.forName("[W").getName();
235 } catch (Exception ex) {
236 return ex.getClass().getName();
240 @Compare public String classForUnknownDeepArray() {
242 return Class.forName("[[[[[W").getName();
243 } catch (Exception ex) {
244 return ex.getClass().getName();
248 @Compare public String componentGetNameForObjectArray() {
249 return (new Object[3]).getClass().getComponentType().getName();
251 @Compare public boolean sameComponentGetNameForObjectArray() {
252 return (new Object[3]).getClass().getComponentType() == Object.class;
254 @Compare public String componentGetNameForSimpleIntArray() {
255 return (new int[3]).getClass().getComponentType().getName();
257 @Compare public String componentGetNameForMultiIntArray() {
258 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
260 @Compare public String componentGetNameForMultiStringArray() {
261 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
262 StringBuilder sb = new StringBuilder();
264 sb.append(c.getName()).append("\n");
265 c = c.getComponentType();
270 return sb.toString();
273 @Compare public boolean isArray() {
274 return new Object[0].getClass().isArray();
277 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
278 private static String[] sort(String[] arr, int len) {
279 List<String> list = Arrays.asList(arr).subList(0, len);
280 Collections.sort(list);
281 return list.toArray(new String[0]);
284 @JavaScriptBody(args = {}, body = "return new Object();")
285 private static Object newObject() {
290 public static Object[] create() {
291 return VMTest.create(ReflectionTest.class);