jaroslav@222
|
1 |
/**
|
jaroslav@222
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@222
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@222
|
4 |
*
|
jaroslav@222
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@222
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@222
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@222
|
8 |
*
|
jaroslav@222
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@222
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@222
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@222
|
12 |
* GNU General Public License for more details.
|
jaroslav@222
|
13 |
*
|
jaroslav@222
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@222
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@222
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@222
|
17 |
*/
|
jaroslav@222
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@222
|
19 |
|
jaroslav@222
|
20 |
import java.io.IOException;
|
jaroslav@237
|
21 |
import java.lang.annotation.Annotation;
|
jaroslav@261
|
22 |
import java.lang.reflect.Method;
|
jaroslav@222
|
23 |
import java.net.MalformedURLException;
|
jaroslav@264
|
24 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@222
|
25 |
|
jaroslav@222
|
26 |
/**
|
jaroslav@222
|
27 |
*
|
jaroslav@222
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@222
|
29 |
*/
|
jaroslav@235
|
30 |
@ClassesMarker(number = 10)
|
jaroslav@237
|
31 |
@ClassesNamer(name = "my text")
|
jaroslav@222
|
32 |
public class Classes {
|
jaroslav@303
|
33 |
public static String nameOfIO() {
|
jaroslav@303
|
34 |
return nameFor(IOException.class);
|
jaroslav@303
|
35 |
}
|
jaroslav@303
|
36 |
|
jaroslav@303
|
37 |
private static String nameFor(Class<?> c) {
|
jaroslav@303
|
38 |
return c.getName();
|
jaroslav@303
|
39 |
}
|
jaroslav@303
|
40 |
|
jaroslav@355
|
41 |
public static boolean isInterface(String s) throws ClassNotFoundException {
|
jaroslav@355
|
42 |
return Class.forName(s).isInterface();
|
jaroslav@355
|
43 |
}
|
jaroslav@355
|
44 |
|
jaroslav@222
|
45 |
public static boolean equalsClassesOfExceptions() {
|
jaroslav@222
|
46 |
return MalformedURLException.class.getSuperclass() == IOException.class;
|
jaroslav@222
|
47 |
}
|
jaroslav@222
|
48 |
public static boolean differenceInClasses() {
|
jaroslav@222
|
49 |
Class<?> c1 = MalformedURLException.class;
|
jaroslav@222
|
50 |
Class<?> c2 = IOException.class;
|
jaroslav@222
|
51 |
return c1 != c2;
|
jaroslav@222
|
52 |
}
|
jaroslav@222
|
53 |
|
jaroslav@225
|
54 |
public static String classForInstance() {
|
jaroslav@225
|
55 |
return new IOException().getClass().getName().toString();
|
jaroslav@225
|
56 |
}
|
jaroslav@225
|
57 |
|
jaroslav@261
|
58 |
@ClassesMarker(number = 1)
|
jaroslav@222
|
59 |
public static String name() {
|
jaroslav@223
|
60 |
return IOException.class.getName().toString();
|
jaroslav@222
|
61 |
}
|
jaroslav@222
|
62 |
public static String simpleName() {
|
jaroslav@222
|
63 |
return IOException.class.getSimpleName();
|
jaroslav@222
|
64 |
}
|
jaroslav@222
|
65 |
public static String canonicalName() {
|
jaroslav@222
|
66 |
return IOException.class.getCanonicalName();
|
jaroslav@222
|
67 |
}
|
jaroslav@231
|
68 |
public static boolean newInstance() throws Exception {
|
jaroslav@231
|
69 |
IOException ioe = IOException.class.newInstance();
|
jaroslav@231
|
70 |
if (ioe instanceof IOException) {
|
jaroslav@231
|
71 |
return ioe.getClass() == IOException.class;
|
jaroslav@231
|
72 |
}
|
jaroslav@231
|
73 |
throw new IllegalStateException("Not a subtype: " + ioe);
|
jaroslav@222
|
74 |
}
|
jaroslav@397
|
75 |
public static String newInstanceNoPubConstructor() throws Exception {
|
jaroslav@397
|
76 |
try {
|
jaroslav@397
|
77 |
Float f = Float.class.newInstance();
|
jaroslav@397
|
78 |
return "wrong, can't instantiate: " + f;
|
jaroslav@397
|
79 |
} catch (Exception ex) {
|
jaroslav@397
|
80 |
return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
|
jaroslav@397
|
81 |
}
|
jaroslav@397
|
82 |
}
|
jaroslav@235
|
83 |
public static int getMarker() {
|
jaroslav@235
|
84 |
if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
|
jaroslav@235
|
85 |
return -2;
|
jaroslav@235
|
86 |
}
|
jaroslav@235
|
87 |
ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
|
jaroslav@235
|
88 |
return cm == null ? -1 : cm.number();
|
jaroslav@235
|
89 |
}
|
jaroslav@237
|
90 |
public static String getNamer(boolean direct) {
|
jaroslav@237
|
91 |
if (direct) {
|
jaroslav@237
|
92 |
ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
|
jaroslav@237
|
93 |
return cm == null ? null : cm.name();
|
jaroslav@237
|
94 |
}
|
jaroslav@237
|
95 |
for (Annotation a : Classes.class.getAnnotations()) {
|
jaroslav@237
|
96 |
if (a instanceof ClassesNamer) {
|
jaroslav@237
|
97 |
return ((ClassesNamer)a).name();
|
jaroslav@237
|
98 |
}
|
jaroslav@237
|
99 |
}
|
jaroslav@237
|
100 |
return null;
|
jaroslav@237
|
101 |
}
|
jaroslav@261
|
102 |
|
jaroslav@353
|
103 |
public static String intType() {
|
jaroslav@353
|
104 |
return Integer.TYPE.getName();
|
jaroslav@353
|
105 |
}
|
jaroslav@353
|
106 |
|
jaroslav@354
|
107 |
public static int primitive() {
|
jaroslav@354
|
108 |
return 1;
|
jaroslav@354
|
109 |
}
|
jaroslav@355
|
110 |
public static boolean primitiveB() {
|
jaroslav@355
|
111 |
return true;
|
jaroslav@355
|
112 |
}
|
jaroslav@354
|
113 |
|
jaroslav@355
|
114 |
public static String primitiveType(String method) throws Exception {
|
jaroslav@355
|
115 |
return reflectiveMethodCall(false, method).getClass().getName();
|
jaroslav@354
|
116 |
}
|
jaroslav@354
|
117 |
|
jaroslav@264
|
118 |
@JavaScriptBody(args = "msg", body = "throw msg;")
|
jaroslav@264
|
119 |
private static native void thrw(String msg);
|
jaroslav@264
|
120 |
|
jaroslav@265
|
121 |
public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
|
jaroslav@264
|
122 |
Method find = null;
|
jaroslav@264
|
123 |
StringBuilder sb = new StringBuilder();
|
jaroslav@261
|
124 |
if (!direct) {
|
jaroslav@261
|
125 |
final Class<? extends Annotation> v = ClassesMarker.class;
|
jaroslav@264
|
126 |
for (Method m : Classes.class.getMethods()) {
|
jaroslav@264
|
127 |
sb.append("\n").append(m.getName());
|
jaroslav@265
|
128 |
if (mn != null) {
|
jaroslav@265
|
129 |
if (m.getName().equals(mn)) {
|
jaroslav@265
|
130 |
find = m;
|
jaroslav@265
|
131 |
break;
|
jaroslav@265
|
132 |
}
|
jaroslav@265
|
133 |
} else {
|
jaroslav@265
|
134 |
if (m.getAnnotation(v) != null) {
|
jaroslav@265
|
135 |
find = m;
|
jaroslav@265
|
136 |
break;
|
jaroslav@265
|
137 |
}
|
jaroslav@261
|
138 |
}
|
jaroslav@261
|
139 |
}
|
jaroslav@264
|
140 |
} else {
|
jaroslav@265
|
141 |
find = Classes.class.getMethod(mn);
|
jaroslav@261
|
142 |
}
|
jaroslav@264
|
143 |
if (find == null) {
|
jaroslav@264
|
144 |
thrw(sb.toString());
|
jaroslav@264
|
145 |
throw new NullPointerException(sb.toString());
|
jaroslav@264
|
146 |
}
|
jaroslav@264
|
147 |
return find.invoke(null);
|
jaroslav@261
|
148 |
}
|
jaroslav@418
|
149 |
|
jaroslav@418
|
150 |
public static int reflectiveSum(int a, int b) throws Exception {
|
jaroslav@418
|
151 |
Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
|
jaroslav@418
|
152 |
return (int) m.invoke(null, a, b);
|
jaroslav@418
|
153 |
}
|
jaroslav@222
|
154 |
}
|