Need to make sure an object implements all transitive interfaces, not just those specified directly
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.vm4brwsr;
20 import java.io.IOException;
21 import java.io.Serializable;
22 import java.lang.annotation.Annotation;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 import java.lang.reflect.Method;
26 import java.net.MalformedURLException;
27 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 @ClassesMarker(number = 10, nicknames = { "Ten", "Deset" }, count = ClassesMarker.E.TWO, subs = {
34 @ClassesMarker.Anno(Integer.SIZE),
35 @ClassesMarker.Anno(Integer.MIN_VALUE)
37 @ClassesNamer(name = "my text", anno = @ClassesMarker.Anno(333))
38 public class Classes {
39 public static String nameOfIO() {
40 return nameFor(IOException.class);
43 private static String nameFor(Class<?> c) {
47 private static final Class<?> PRELOAD = Runnable.class;
49 public static boolean isInterface(String s) throws ClassNotFoundException {
50 return Class.forName(s).isInterface();
53 public static boolean equalsClassesOfExceptions() {
54 return MalformedURLException.class.getSuperclass() == IOException.class;
56 public static boolean differenceInClasses() {
57 Class<?> c1 = MalformedURLException.class;
58 Class<?> c2 = IOException.class;
62 public static String classForInstance() {
63 return new IOException().getClass().getName().toString();
66 @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
67 public static String name() {
68 return IOException.class.getName().toString();
70 public static String simpleName() {
71 return IOException.class.getSimpleName();
73 public static String canonicalName() {
74 return IOException.class.getCanonicalName();
77 public static String objectName() throws NoSuchMethodException {
78 return IOException.class.getMethod("wait").getDeclaringClass().getName();
81 public static boolean newInstance() throws Exception {
82 IOException ioe = IOException.class.newInstance();
83 if (ioe instanceof IOException) {
84 return ioe.getClass() == IOException.class;
86 throw new IllegalStateException("Not a subtype: " + ioe);
88 public static String newInstanceNoPubConstructor() throws Exception {
90 Float f = Float.class.newInstance();
91 return "wrong, can't instantiate: " + f;
92 } catch (Exception ex) {
93 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
96 public static int getMarker() {
97 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
100 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
101 assert cm instanceof Object : "Is object " + cm;
102 assert cm instanceof Annotation : "Is annotation " + cm;
103 assert !((Object)cm instanceof Class) : "Is not Class " + cm;
104 return cm == null ? -1 : cm.number();
106 public static String getMarkerNicknames() {
107 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
112 final Object[] arr = cm.nicknames();
113 assert arr instanceof Object[] : "Instance of Object array: " + arr;
114 assert arr instanceof String[] : "Instance of String array: " + arr;
115 assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
117 StringBuilder sb = new StringBuilder();
118 for (String s : cm.nicknames()) {
119 sb.append(s).append("\n");
121 return sb.toString().toString();
123 @Retention(RetentionPolicy.CLASS)
127 public static String getRetention() throws Exception {
128 Retention r = Ann.class.getAnnotation(Retention.class);
129 assert r != null : "Annotation is present";
130 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
131 return r.annotationType().getName();
133 public static String getMarkerE() {
134 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
138 return cm.count().name();
140 public static String getNamer(boolean direct) {
142 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
143 return cm == null ? null : cm.name();
145 for (Annotation a : Classes.class.getAnnotations()) {
146 if (a instanceof ClassesNamer) {
147 return ((ClassesNamer)a).name();
152 public static int getInnerNamer() {
153 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
154 assert cm != null : "ClassesNamer is present";
155 return cm.anno().value();
157 public static int getInnerNamers() {
158 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
159 assert cm != null : "ClassesNamer is present";
161 for (ClassesMarker.Anno anno : cm.subs()) {
167 public static String intType() {
168 return Integer.TYPE.getName();
171 public static int primitive() {
174 public static boolean primitiveB() {
178 public static String primitiveType(String method) throws Exception {
179 return reflectiveMethodCall(false, method).getClass().getName();
182 @JavaScriptBody(args = "msg", body = "throw msg;")
183 private static native void thrw(String msg);
185 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
187 StringBuilder sb = new StringBuilder();
189 final Class<? extends Annotation> v = ClassesMarker.class;
190 for (Method m : Classes.class.getMethods()) {
191 sb.append("\n").append(m.getName());
193 if (m.getName().equals(mn)) {
198 if (m.getAnnotation(v) != null) {
205 find = Classes.class.getMethod(mn);
209 throw new NullPointerException(sb.toString());
211 return find.invoke(null);
214 public static int reflectiveSum(int a, int b) throws Exception {
215 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
216 return (int) m.invoke(null, a, b);
219 private abstract class Application {
220 public abstract int getID();
223 private class MyApplication extends Application {
230 public static boolean isClassAssignable() {
231 return Application.class.isAssignableFrom(MyApplication.class);
234 public static String valueEnum(String v) {
235 return ClassesMarker.E.valueOf(v).toString();
238 public static String typeOfFn() {
239 return fn().getClass().getName();
242 @JavaScriptBody(args = { }, body = "return function() { alert('x'); };")
243 private native static Object fn();
245 public static boolean instanceOfSuperInterface() {
246 Object obj = new SuperSerial() {
248 return obj instanceof Serializable;
251 private static interface SuperSerial extends Serializable {