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.bck2brwsr.emul.reflect;
20 import java.lang.reflect.Array;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
23 import java.util.Enumeration;
24 import org.apidesign.bck2brwsr.core.Exported;
25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 /** Utilities to work on methods.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 public abstract class MethodImpl {
32 public static MethodImpl INSTANCE;
35 Class.forName(Method.class.getName());
36 } catch (ClassNotFoundException ex) {
37 throw new IllegalStateException(ex);
41 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
42 protected abstract Constructor create(Class<?> declaringClass, Object data, String sig);
46 // bck2brwsr implementation
49 @JavaScriptBody(args = {"clazz", "prefix", "cnstr"},
51 + "var c = clazz.cnstr;\n"
52 + "if (!cnstr) c = c.prototype;"
53 + "var arr = new Array();\n"
55 + " if (m.indexOf(prefix) === 0) {\n"
56 + " if (!c[m].cls) continue;\n"
58 + " arr.push(c[m]);\n"
59 + " arr.push(c[m].cls.$class);\n"
63 private static native Object[] findMethodData(
64 Class<?> clazz, String prefix, boolean cnstr);
66 public static Constructor findConstructor(
67 Class<?> clazz, Class<?>... parameterTypes) {
68 Object[] data = findMethodData(clazz, "cons__", true);
69 BIG: for (int i = 0; i < data.length; i += 3) {
70 String sig = ((String) data[i]).substring(6);
71 Class<?> cls = (Class<?>) data[i + 2];
72 Constructor tmp = INSTANCE.create(cls, data[i + 1], sig);
73 Class<?>[] tmpParms = tmp.getParameterTypes();
74 if (parameterTypes.length != tmpParms.length) {
77 for (int j = 0; j < tmpParms.length; j++) {
78 if (!parameterTypes[j].equals(tmpParms[j])) {
87 public static Constructor[] findConstructors(Class<?> clazz, int mask) {
88 Object[] namesAndData = findMethodData(clazz, "", true);
90 for (int i = 0; i < namesAndData.length; i += 3) {
91 String sig = (String) namesAndData[i];
92 Object data = namesAndData[i + 1];
93 if (!sig.startsWith("cons__")) {
96 sig = sig.substring(6);
97 Class<?> cls = (Class<?>) namesAndData[i + 2];
98 final Constructor m = INSTANCE.create(cls, data, sig);
99 if ((m.getModifiers() & mask) == 0) {
102 namesAndData[cnt++] = m;
104 Constructor[] arr = new Constructor[cnt];
105 for (int i = 0; i < cnt; i++) {
106 arr[i] = (Constructor) namesAndData[i];
110 public static Method findMethod(
111 Class<?> clazz, String name, Class<?>... parameterTypes) {
112 Object[] data = findMethodData(clazz, name + "__", false);
113 BIG: for (int i = 0; i < data.length; i += 3) {
114 String sig = ((String) data[i]).substring(name.length() + 2);
115 Class<?> cls = (Class<?>) data[i + 2];
116 Method tmp = INSTANCE.create(cls, name, data[i + 1], sig);
117 Class<?>[] tmpParms = tmp.getParameterTypes();
118 if (parameterTypes.length != tmpParms.length) {
121 for (int j = 0; j < tmpParms.length; j++) {
122 if (!parameterTypes[j].equals(tmpParms[j])) {
131 public static Method[] findMethods(Class<?> clazz, int mask) {
132 Object[] namesAndData = findMethodData(clazz, "", false);
134 for (int i = 0; i < namesAndData.length; i += 3) {
135 String sig = (String) namesAndData[i];
136 Object data = namesAndData[i + 1];
137 int middle = sig.indexOf("__");
141 if (sig.startsWith("$") && sig.endsWith("$")) {
142 // produced by Closure compiler in debug mode
143 // needs to be ignored
146 String name = sig.substring(0, middle);
147 sig = sig.substring(middle + 2);
148 Class<?> cls = (Class<?>) namesAndData[i + 2];
149 final Method m = INSTANCE.create(cls, name, data, sig);
150 if ((m.getModifiers() & mask) == 0) {
153 namesAndData[cnt++] = m;
155 Method[] arr = new Method[cnt];
156 for (int i = 0; i < cnt; i++) {
157 arr[i] = (Method) namesAndData[i];
162 @Exported static String toSignature(Method m) {
163 StringBuilder sb = new StringBuilder();
164 sb.append(m.getName()).append("__");
165 appendType(sb, m.getReturnType());
166 Class<?>[] arr = m.getParameterTypes();
167 for (int i = 0; i < arr.length; i++) {
168 appendType(sb, arr[i]);
170 return sb.toString();
173 private static void appendType(StringBuilder sb, Class<?> type) {
174 if (type == Integer.TYPE) {
178 if (type == Long.TYPE) {
182 if (type == Double.TYPE) {
186 if (type == Float.TYPE) {
190 if (type == Byte.TYPE) {
194 if (type == Boolean.TYPE) {
198 if (type == Short.TYPE) {
202 if (type == Void.TYPE) {
206 if (type == Character.TYPE) {
210 if (type.isArray()) {
212 appendType(sb, type.getComponentType());
215 sb.append('L').append(type.getName().replace('.', '_'));
219 public static int signatureElements(String sig) {
220 Enumeration<Class> en = signatureParser(sig);
222 while (en.hasMoreElements()) {
229 public static Enumeration<Class> signatureParser(final String sig) {
230 class E implements Enumeration<Class> {
236 while (sig.charAt(len - 1) == '$') {
241 public boolean hasMoreElements() {
245 public Class nextElement() {
246 switch (sig.charAt(pos++)) {
264 return Character.TYPE;
267 int up = sig.indexOf("_2", pos);
268 String type = sig.substring(pos, up);
270 return Class.forName(type.replace('_', '.'));
271 } catch (ClassNotFoundException ex) {
272 throw new IllegalStateException(ex);
275 char nch = sig.charAt(pos++);
276 assert nch == '3' : "Can't find '3' at " + sig.substring(pos - 1);
277 final Class compType = nextElement();
278 return Array.newInstance(compType, 0).getClass();
281 throw new UnsupportedOperationException(sig + " at " + pos);