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.ElementType;
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)
34 @ClassesNamer(name = "my text")
35 public class Classes {
36 public static String nameOfIO() {
37 return nameFor(IOException.class);
40 private static String nameFor(Class<?> c) {
44 private static final Class<?> PRELOAD = Runnable.class;
45 private static final Class<?> PRELOAD2 = ClassesMarker.E.class;
46 private static final Class<?> PRELOAD3 = RetentionPolicy.class;
47 private static final Class<?> PRELOAD4 = ElementType.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 return cm == null ? -1 : cm.number();
103 public static String getMarkerNicknames() {
104 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
108 StringBuilder sb = new StringBuilder();
109 for (String s : cm.nicknames()) {
110 sb.append(s).append("\n");
112 return sb.toString().toString();
114 @Retention(RetentionPolicy.CLASS)
118 public static String getRetention() throws Exception {
119 Retention r = Ann.class.getAnnotation(Retention.class);
120 assert r != null : "Annotation is present";
121 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
122 return r.annotationType().getName();
124 public static String getMarkerE() {
125 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
129 return cm.count().name();
131 public static String getNamer(boolean direct) {
133 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
134 return cm == null ? null : cm.name();
136 for (Annotation a : Classes.class.getAnnotations()) {
137 if (a instanceof ClassesNamer) {
138 return ((ClassesNamer)a).name();
144 public static String intType() {
145 return Integer.TYPE.getName();
148 public static int primitive() {
151 public static boolean primitiveB() {
155 public static String primitiveType(String method) throws Exception {
156 return reflectiveMethodCall(false, method).getClass().getName();
159 @JavaScriptBody(args = "msg", body = "throw msg;")
160 private static native void thrw(String msg);
162 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
164 StringBuilder sb = new StringBuilder();
166 final Class<? extends Annotation> v = ClassesMarker.class;
167 for (Method m : Classes.class.getMethods()) {
168 sb.append("\n").append(m.getName());
170 if (m.getName().equals(mn)) {
175 if (m.getAnnotation(v) != null) {
182 find = Classes.class.getMethod(mn);
186 throw new NullPointerException(sb.toString());
188 return find.invoke(null);
191 public static int reflectiveSum(int a, int b) throws Exception {
192 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
193 return (int) m.invoke(null, a, b);