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.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();
71 @ClassesMarker(self = Self.class, number = 42, nicknames = {})
72 public static class Self {
75 @ClassesMarker(number = 42, nicknames = {})
76 public static class DefaultSelf {
79 public static int self() {
80 ClassesMarker cm = Self.class.getAnnotation(ClassesMarker.class);
81 if (cm.self() == Self.class) {
88 public static int defaultSelf() {
89 ClassesMarker cm = DefaultSelf.class.getAnnotation(ClassesMarker.class);
90 if (cm.self() == Object.class) {
93 throw new IllegalStateException("" + cm.self());
97 public static String simpleName() {
98 return IOException.class.getSimpleName();
100 public static String canonicalName() {
101 return IOException.class.getCanonicalName();
104 public static String objectName() throws NoSuchMethodException {
105 return IOException.class.getMethod("wait").getDeclaringClass().getName();
108 public static boolean newInstance() throws Exception {
109 IOException ioe = IOException.class.newInstance();
110 if (ioe instanceof IOException) {
111 return ioe.getClass() == IOException.class;
113 throw new IllegalStateException("Not a subtype: " + ioe);
115 public static String newInstanceNoPubConstructor() throws Exception {
117 Float f = Float.class.newInstance();
118 return "wrong, can't instantiate: " + f;
119 } catch (Exception ex) {
120 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
123 public static int getMarker() {
124 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
127 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
128 assert cm instanceof Object : "Is object " + cm;
129 assert cm instanceof Annotation : "Is annotation " + cm;
130 assert !((Object)cm instanceof Class) : "Is not Class " + cm;
131 return cm == null ? -1 : cm.number();
133 public static int getMarkerDefault() {
134 try { throw new IllegalStateException(); } catch (Exception e) {}
135 ClassesMarker cm = CD.class.getAnnotation(ClassesMarker.class);
136 return cm == null ? -1 : cm.number();
138 public static String getMarkerNicknames() {
139 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
144 final Object[] arr = cm.nicknames();
145 assert arr instanceof Object[] : "Instance of Object array: " + arr;
146 assert arr instanceof String[] : "Instance of String array: " + arr;
147 assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
149 StringBuilder sb = new StringBuilder();
150 for (String s : cm.nicknames()) {
151 sb.append(s).append("\n");
153 return sb.toString().toString();
156 static String listObject(boolean string, String prefix) {
157 final Class<?> c = string ? String.class : Object.class;
158 StringBuilder sb = new StringBuilder();
159 for (Method m : c.getMethods()) {
160 final String n = m.getName();
161 if (n.startsWith(prefix)) {
162 sb.append(n).append("\n");
165 return sb.toString().toString();
167 @Retention(RetentionPolicy.CLASS)
171 public static String getRetention() throws Exception {
172 Retention r = Ann.class.getAnnotation(Retention.class);
173 assert r != null : "Annotation is present";
174 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
175 return r.annotationType().getName();
177 public static String getMarkerE() {
178 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
182 return cm.count().name();
185 @ClassesMarker(nicknames = {})
188 public static String getMarkerED() {
189 ClassesMarker cm = CD.class.getAnnotation(ClassesMarker.class);
193 return cm.count().name();
195 public static String getNamer(boolean direct) {
197 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
198 return cm == null ? null : cm.name();
200 for (Annotation a : Classes.class.getAnnotations()) {
201 if (a instanceof ClassesNamer) {
202 return ((ClassesNamer)a).name();
207 public static int getInnerNamer() {
208 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
209 assert cm != null : "ClassesNamer is present";
210 return cm.anno().value();
212 public static int getInnerNamers() {
213 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
214 assert cm != null : "ClassesNamer is present";
216 for (ClassesMarker.Anno anno : cm.subs()) {
222 public static String intType() {
223 return Integer.TYPE.getName();
226 public static int primitive() {
229 public static boolean primitiveB() {
233 public static String primitiveType(String method) throws Exception {
234 return reflectiveMethodCall(false, method).getClass().getName();
237 @JavaScriptBody(args = "msg", body = "throw msg;")
238 private static native void thrw(String msg);
240 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
242 StringBuilder sb = new StringBuilder();
244 final Class<? extends Annotation> v = ClassesMarker.class;
245 for (Method m : Classes.class.getMethods()) {
246 sb.append("\n").append(m.getName());
248 if (m.getName().equals(mn)) {
253 if (m.getAnnotation(v) != null) {
260 find = Classes.class.getMethod(mn);
264 throw new NullPointerException(sb.toString());
266 return find.invoke(null);
269 public static int reflectiveSum(int a, int b) throws Exception {
270 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
271 return (int) m.invoke(null, a, b);
274 private abstract class Application {
275 public abstract int getID();
278 private class MyApplication extends Application {
285 public static boolean isClassAssignable() {
286 return Application.class.isAssignableFrom(MyApplication.class);
289 public static String valueEnum(String v) {
290 return ClassesMarker.E.valueOf(v).toString();
293 public static String typeOfFn() {
294 return fn().getClass().getName();
297 @JavaScriptBody(args = { }, body = "return function() { alert('x'); };")
298 private native static Object fn();
300 public static boolean instanceOfSuperInterface() {
301 Object obj = new SuperSerial() {
303 return obj instanceof Serializable;
306 public static String superInterface() {
307 return dumpInterfaces(SuperSerial.class);
310 private static String dumpInterfaces(final Class<?> aClass) {
311 final Class<?>[] arr = aClass.getInterfaces();
312 StringBuilder sb = new StringBuilder();
313 for (Class<?> c : arr) {
314 sb.append(c.getName()).append("\n");
316 return sb.toString();
319 public static String superInterfaceInst() {
320 return dumpInterfaces(new SuperSerial() {}.getClass());
323 private static interface SuperSerial extends Serializable {