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.RetentionPolicy;
23 import java.lang.reflect.Method;
24 import java.net.MalformedURLException;
25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 @ClassesMarker(number = 10, nicknames = { "Ten", "Deset" }, count = ClassesMarker.E.TWO)
32 @ClassesNamer(name = "my text")
33 public class Classes {
34 public static String nameOfIO() {
35 return nameFor(IOException.class);
38 private static String nameFor(Class<?> c) {
42 private static final Class<?> PRELOAD = Runnable.class;
43 private static final Class<?> PRELOAD2 = ClassesMarker.E.class;
44 private static final Class<?> PRELOAD3 = RetentionPolicy.class;
46 public static boolean isInterface(String s) throws ClassNotFoundException {
47 return Class.forName(s).isInterface();
50 public static boolean equalsClassesOfExceptions() {
51 return MalformedURLException.class.getSuperclass() == IOException.class;
53 public static boolean differenceInClasses() {
54 Class<?> c1 = MalformedURLException.class;
55 Class<?> c2 = IOException.class;
59 public static String classForInstance() {
60 return new IOException().getClass().getName().toString();
63 @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
64 public static String name() {
65 return IOException.class.getName().toString();
67 public static String simpleName() {
68 return IOException.class.getSimpleName();
70 public static String canonicalName() {
71 return IOException.class.getCanonicalName();
73 public static boolean newInstance() throws Exception {
74 IOException ioe = IOException.class.newInstance();
75 if (ioe instanceof IOException) {
76 return ioe.getClass() == IOException.class;
78 throw new IllegalStateException("Not a subtype: " + ioe);
80 public static String newInstanceNoPubConstructor() throws Exception {
82 Float f = Float.class.newInstance();
83 return "wrong, can't instantiate: " + f;
84 } catch (Exception ex) {
85 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
88 public static int getMarker() {
89 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
92 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
93 return cm == null ? -1 : cm.number();
95 public static String getMarkerNicknames() {
96 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
100 StringBuilder sb = new StringBuilder();
101 for (String s : cm.nicknames()) {
102 sb.append(s).append("\n");
104 return sb.toString().toString();
106 public static String getMarkerE() {
107 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
111 return cm.count().name();
113 public static String getNamer(boolean direct) {
115 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
116 return cm == null ? null : cm.name();
118 for (Annotation a : Classes.class.getAnnotations()) {
119 if (a instanceof ClassesNamer) {
120 return ((ClassesNamer)a).name();
126 public static String intType() {
127 return Integer.TYPE.getName();
130 public static int primitive() {
133 public static boolean primitiveB() {
137 public static String primitiveType(String method) throws Exception {
138 return reflectiveMethodCall(false, method).getClass().getName();
141 @JavaScriptBody(args = "msg", body = "throw msg;")
142 private static native void thrw(String msg);
144 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
146 StringBuilder sb = new StringBuilder();
148 final Class<? extends Annotation> v = ClassesMarker.class;
149 for (Method m : Classes.class.getMethods()) {
150 sb.append("\n").append(m.getName());
152 if (m.getName().equals(mn)) {
157 if (m.getAnnotation(v) != null) {
164 find = Classes.class.getMethod(mn);
168 throw new NullPointerException(sb.toString());
170 return find.invoke(null);
173 public static int reflectiveSum(int a, int b) throws Exception {
174 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
175 return (int) m.invoke(null, a, b);