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.lang.annotation.Annotation;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.reflect.Method;
25 import java.net.MalformedURLException;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 @ClassesMarker(number = 10, nicknames = { "Ten", "Deset" }, count = ClassesMarker.E.TWO, subs = {
33 @ClassesMarker.Anno(Integer.SIZE),
34 @ClassesMarker.Anno(Integer.MIN_VALUE)
36 @ClassesNamer(name = "my text", anno = @ClassesMarker.Anno(Integer.MAX_VALUE))
37 public class Classes {
38 public static String nameOfIO() {
39 return nameFor(IOException.class);
42 private static String nameFor(Class<?> c) {
46 private static final Class<?> PRELOAD = Runnable.class;
48 public static boolean isInterface(String s) throws ClassNotFoundException {
49 return Class.forName(s).isInterface();
52 public static boolean equalsClassesOfExceptions() {
53 return MalformedURLException.class.getSuperclass() == IOException.class;
55 public static boolean differenceInClasses() {
56 Class<?> c1 = MalformedURLException.class;
57 Class<?> c2 = IOException.class;
61 public static String classForInstance() {
62 return new IOException().getClass().getName().toString();
65 @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
66 public static String name() {
67 return IOException.class.getName().toString();
69 public static String simpleName() {
70 return IOException.class.getSimpleName();
72 public static String canonicalName() {
73 return IOException.class.getCanonicalName();
76 public static String objectName() throws NoSuchMethodException {
77 return IOException.class.getMethod("wait").getDeclaringClass().getName();
80 public static boolean newInstance() throws Exception {
81 IOException ioe = IOException.class.newInstance();
82 if (ioe instanceof IOException) {
83 return ioe.getClass() == IOException.class;
85 throw new IllegalStateException("Not a subtype: " + ioe);
87 public static String newInstanceNoPubConstructor() throws Exception {
89 Float f = Float.class.newInstance();
90 return "wrong, can't instantiate: " + f;
91 } catch (Exception ex) {
92 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
95 public static int getMarker() {
96 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
99 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
100 return cm == null ? -1 : cm.number();
102 public static String getMarkerNicknames() {
103 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
108 final Object[] arr = cm.nicknames();
109 assert arr instanceof Object[] : "Instance of Object array: " + arr;
110 assert arr instanceof String[] : "Instance of String array: " + arr;
111 assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
113 StringBuilder sb = new StringBuilder();
114 for (String s : cm.nicknames()) {
115 sb.append(s).append("\n");
117 return sb.toString().toString();
119 @Retention(RetentionPolicy.CLASS)
123 public static String getRetention() throws Exception {
124 Retention r = Ann.class.getAnnotation(Retention.class);
125 assert r != null : "Annotation is present";
126 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
127 return r.annotationType().getName();
129 public static String getMarkerE() {
130 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
134 return cm.count().name();
136 public static String getNamer(boolean direct) {
138 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
139 return cm == null ? null : cm.name();
141 for (Annotation a : Classes.class.getAnnotations()) {
142 if (a instanceof ClassesNamer) {
143 return ((ClassesNamer)a).name();
149 public static String intType() {
150 return Integer.TYPE.getName();
153 public static int primitive() {
156 public static boolean primitiveB() {
160 public static String primitiveType(String method) throws Exception {
161 return reflectiveMethodCall(false, method).getClass().getName();
164 @JavaScriptBody(args = "msg", body = "throw msg;")
165 private static native void thrw(String msg);
167 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
169 StringBuilder sb = new StringBuilder();
171 final Class<? extends Annotation> v = ClassesMarker.class;
172 for (Method m : Classes.class.getMethods()) {
173 sb.append("\n").append(m.getName());
175 if (m.getName().equals(mn)) {
180 if (m.getAnnotation(v) != null) {
187 find = Classes.class.getMethod(mn);
191 throw new NullPointerException(sb.toString());
193 return find.invoke(null);
196 public static int reflectiveSum(int a, int b) throws Exception {
197 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
198 return (int) m.invoke(null, a, b);