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)
33 @ClassesNamer(name = "my text")
34 public class Classes {
35 public static String nameOfIO() {
36 return nameFor(IOException.class);
39 private static String nameFor(Class<?> c) {
43 private static final Class<?> PRELOAD = Runnable.class;
45 public static boolean isInterface(String s) throws ClassNotFoundException {
46 return Class.forName(s).isInterface();
49 public static boolean equalsClassesOfExceptions() {
50 return MalformedURLException.class.getSuperclass() == IOException.class;
52 public static boolean differenceInClasses() {
53 Class<?> c1 = MalformedURLException.class;
54 Class<?> c2 = IOException.class;
58 public static String classForInstance() {
59 return new IOException().getClass().getName().toString();
62 @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
63 public static String name() {
64 return IOException.class.getName().toString();
66 public static String simpleName() {
67 return IOException.class.getSimpleName();
69 public static String canonicalName() {
70 return IOException.class.getCanonicalName();
73 public static String objectName() throws NoSuchMethodException {
74 return IOException.class.getMethod("wait").getDeclaringClass().getName();
77 public static boolean newInstance() throws Exception {
78 IOException ioe = IOException.class.newInstance();
79 if (ioe instanceof IOException) {
80 return ioe.getClass() == IOException.class;
82 throw new IllegalStateException("Not a subtype: " + ioe);
84 public static String newInstanceNoPubConstructor() throws Exception {
86 Float f = Float.class.newInstance();
87 return "wrong, can't instantiate: " + f;
88 } catch (Exception ex) {
89 return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
92 public static int getMarker() {
93 if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
96 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
97 return cm == null ? -1 : cm.number();
99 public static String getMarkerNicknames() {
100 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
104 StringBuilder sb = new StringBuilder();
105 for (String s : cm.nicknames()) {
106 sb.append(s).append("\n");
108 return sb.toString().toString();
110 @Retention(RetentionPolicy.CLASS)
114 public static String getRetention() throws Exception {
115 Retention r = Ann.class.getAnnotation(Retention.class);
116 assert r != null : "Annotation is present";
117 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
118 return r.annotationType().getName();
120 public static String getMarkerE() {
121 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
125 return cm.count().name();
127 public static String getNamer(boolean direct) {
129 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
130 return cm == null ? null : cm.name();
132 for (Annotation a : Classes.class.getAnnotations()) {
133 if (a instanceof ClassesNamer) {
134 return ((ClassesNamer)a).name();
140 public static String intType() {
141 return Integer.TYPE.getName();
144 public static int primitive() {
147 public static boolean primitiveB() {
151 public static String primitiveType(String method) throws Exception {
152 return reflectiveMethodCall(false, method).getClass().getName();
155 @JavaScriptBody(args = "msg", body = "throw msg;")
156 private static native void thrw(String msg);
158 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
160 StringBuilder sb = new StringBuilder();
162 final Class<? extends Annotation> v = ClassesMarker.class;
163 for (Method m : Classes.class.getMethods()) {
164 sb.append("\n").append(m.getName());
166 if (m.getName().equals(mn)) {
171 if (m.getAnnotation(v) != null) {
178 find = Classes.class.getMethod(mn);
182 throw new NullPointerException(sb.toString());
184 return find.invoke(null);
187 public static int reflectiveSum(int a, int b) throws Exception {
188 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
189 return (int) m.invoke(null, a, b);