Encapsulation. Moving shared code into TestVM instance.
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(333))
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 assert cm instanceof Object : "Is object " + cm;
101 assert cm instanceof Annotation : "Is annotation " + cm;
102 assert !((Object)cm instanceof Class) : "Is not Class " + cm;
103 return cm == null ? -1 : cm.number();
105 public static String getMarkerNicknames() {
106 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
111 final Object[] arr = cm.nicknames();
112 assert arr instanceof Object[] : "Instance of Object array: " + arr;
113 assert arr instanceof String[] : "Instance of String array: " + arr;
114 assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
116 StringBuilder sb = new StringBuilder();
117 for (String s : cm.nicknames()) {
118 sb.append(s).append("\n");
120 return sb.toString().toString();
122 @Retention(RetentionPolicy.CLASS)
126 public static String getRetention() throws Exception {
127 Retention r = Ann.class.getAnnotation(Retention.class);
128 assert r != null : "Annotation is present";
129 assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
130 return r.annotationType().getName();
132 public static String getMarkerE() {
133 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
137 return cm.count().name();
139 public static String getNamer(boolean direct) {
141 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
142 return cm == null ? null : cm.name();
144 for (Annotation a : Classes.class.getAnnotations()) {
145 if (a instanceof ClassesNamer) {
146 return ((ClassesNamer)a).name();
151 public static int getInnerNamer() {
152 ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
153 assert cm != null : "ClassesNamer is present";
154 return cm.anno().value();
156 public static int getInnerNamers() {
157 ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
158 assert cm != null : "ClassesNamer is present";
160 for (ClassesMarker.Anno anno : cm.subs()) {
166 public static String intType() {
167 return Integer.TYPE.getName();
170 public static int primitive() {
173 public static boolean primitiveB() {
177 public static String primitiveType(String method) throws Exception {
178 return reflectiveMethodCall(false, method).getClass().getName();
181 @JavaScriptBody(args = "msg", body = "throw msg;")
182 private static native void thrw(String msg);
184 public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
186 StringBuilder sb = new StringBuilder();
188 final Class<? extends Annotation> v = ClassesMarker.class;
189 for (Method m : Classes.class.getMethods()) {
190 sb.append("\n").append(m.getName());
192 if (m.getName().equals(mn)) {
197 if (m.getAnnotation(v) != null) {
204 find = Classes.class.getMethod(mn);
208 throw new NullPointerException(sb.toString());
210 return find.invoke(null);
213 public static int reflectiveSum(int a, int b) throws Exception {
214 Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
215 return (int) m.invoke(null, a, b);