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.reflect.Method;
23 import java.net.MalformedURLException;
24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
30 @ClassesMarker(number = 10, nicknames = { "Ten", "Deset" })
31 @ClassesNamer(name = "my text")
32 public class Classes {
33 public static String nameOfIO() {
34 return nameFor(IOException.class);
37 private static String nameFor(Class<?> c) {
41 private static final Class<?> PRELOAD = Runnable.class;
42 private static final Class<?> PRELOAD2 = ClassesMarker.E.class;
44 public static boolean isInterface(String s) throws ClassNotFoundException {
45 return Class.forName(s).isInterface();
48 public static boolean equalsClassesOfExceptions() {
49 return MalformedURLException.class.getSuperclass() == IOException.class;
51 public static boolean differenceInClasses() {
52 Class<?> c1 = MalformedURLException.class;
53 Class<?> c2 = IOException.class;
57 public static String classForInstance() {
58 return new IOException().getClass().getName().toString();
61 @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
62 public static String name() {
63 return IOException.class.getName().toString();
65 public static String simpleName() {
66 return IOException.class.getSimpleName();
68 public static String canonicalName() {
69 return IOException.class.getCanonicalName();
71 public static boolean newInstance() throws Exception {
72 IOException ioe = IOException.class.newInstance();
73 if (ioe instanceof IOException) {
74 return ioe.getClass() == IOException.class;
76 throw new IllegalStateException("Not a subtype: " + ioe);
78 public static String newInstanceNoPubConstructor() throws Exception {
80 Float f = Float.class.newInstance();
81 return "wrong, can't instantiate: " + f;
82 } catch (Exception ex) {
83 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
86 public static int getMarker() {
87 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
90 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
91 return cm == null ? -1 : cm.number();
93 public static String getMarkerNicknames() {
94 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
98 StringBuilder sb = new StringBuilder();
99 for (String s : cm.nicknames()) {
100 sb.append(s).append("\n");
102 return sb.toString().toString();
104 public static String getNamer(boolean direct) {
106 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
107 return cm == null ? null : cm.name();
109 for (Annotation a : Classes.class.getAnnotations()) {
110 if (a instanceof ClassesNamer) {
111 return ((ClassesNamer)a).name();
117 public static String intType() {
118 return Integer.TYPE.getName();
121 public static int primitive() {
124 public static boolean primitiveB() {
128 public static String primitiveType(String method) throws Exception {
129 return reflectiveMethodCall(false, method).getClass().getName();
132 @JavaScriptBody(args = "msg", body = "throw msg;")
133 private static native void thrw(String msg);
135 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
137 StringBuilder sb = new StringBuilder();
139 final Class<? extends Annotation> v = ClassesMarker.class;
140 for (Method m : Classes.class.getMethods()) {
141 sb.append("\n").append(m.getName());
143 if (m.getName().equals(mn)) {
148 if (m.getAnnotation(v) != null) {
155 find = Classes.class.getMethod(mn);
159 throw new NullPointerException(sb.toString());
161 return find.invoke(null);
164 public static int reflectiveSum(int a, int b) throws Exception {
165 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
166 return (int) m.invoke(null, a, b);