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.Method;
22 import java.util.Enumeration;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
25 /** Utilities to work on methods.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public abstract class MethodImpl {
30 public static MethodImpl INSTANCE;
33 Class.forName(Method.class.getName());
34 } catch (ClassNotFoundException ex) {
35 throw new IllegalStateException(ex);
39 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
43 // bck2brwsr implementation
46 @JavaScriptBody(args = {"clazz", "prefix"},
48 + "var c = clazz.cnstr.prototype;"
49 + "var arr = new Array();\n"
51 + " if (m.indexOf(prefix) === 0) {\n"
52 + " if (!c[m].cls) continue;\n"
54 + " arr.push(c[m]);\n"
55 + " arr.push(c[m].cls.$class);\n"
59 private static native Object[] findMethodData(
60 Class<?> clazz, String prefix);
62 public static Method findMethod(
63 Class<?> clazz, String name, Class<?>... parameterTypes) {
64 Object[] data = findMethodData(clazz, name + "__");
65 BIG: for (int i = 0; i < data.length; i += 3) {
66 String sig = ((String) data[i]).substring(name.length() + 2);
67 Class<?> cls = (Class<?>) data[i + 2];
68 Method tmp = INSTANCE.create(cls, name, data[i + 1], sig);
69 Class<?>[] tmpParms = tmp.getParameterTypes();
70 if (parameterTypes.length != tmpParms.length) {
73 for (int j = 0; j < tmpParms.length; j++) {
74 if (!parameterTypes[j].equals(tmpParms[j])) {
83 public static Method[] findMethods(Class<?> clazz, int mask) {
84 Object[] namesAndData = findMethodData(clazz, "");
86 for (int i = 0; i < namesAndData.length; i += 3) {
87 String sig = (String) namesAndData[i];
88 Object data = namesAndData[i + 1];
89 int middle = sig.indexOf("__");
93 String name = sig.substring(0, middle);
94 sig = sig.substring(middle + 2);
95 Class<?> cls = (Class<?>) namesAndData[i + 2];
96 final Method m = INSTANCE.create(cls, name, data, sig);
97 if ((m.getModifiers() & mask) == 0) {
100 namesAndData[cnt++] = m;
102 Method[] arr = new Method[cnt];
103 for (int i = 0; i < cnt; i++) {
104 arr[i] = (Method) namesAndData[i];
108 static String toSignature(Method m) {
109 StringBuilder sb = new StringBuilder();
110 sb.append(m.getName()).append("__");
111 appendType(sb, m.getReturnType());
112 Class<?>[] arr = m.getParameterTypes();
113 for (int i = 0; i < arr.length; i++) {
114 appendType(sb, arr[i]);
116 return sb.toString();
119 private static void appendType(StringBuilder sb, Class<?> type) {
120 if (type == Integer.TYPE) {
124 if (type == Long.TYPE) {
128 if (type == Double.TYPE) {
132 if (type == Float.TYPE) {
136 if (type == Byte.TYPE) {
140 if (type == Boolean.TYPE) {
144 if (type == Short.TYPE) {
148 if (type == Void.TYPE) {
152 if (type == Character.TYPE) {
156 if (type.isArray()) {
158 appendType(sb, type.getComponentType());
161 sb.append('L').append(type.getName().replace('.', '_'));
165 public static int signatureElements(String sig) {
166 Enumeration<Class> en = signatureParser(sig);
168 while (en.hasMoreElements()) {
175 public static Enumeration<Class> signatureParser(final String sig) {
176 class E implements Enumeration<Class> {
179 public boolean hasMoreElements() {
180 return pos < sig.length();
183 public Class nextElement() {
184 switch (sig.charAt(pos++)) {
202 return Character.TYPE;
205 int up = sig.indexOf("_2", pos);
206 String type = sig.substring(pos, up);
208 return Class.forName(type.replace('_', '.'));
209 } catch (ClassNotFoundException ex) {
210 throw new IllegalStateException(ex);
213 char nch = sig.charAt(pos++);
214 assert nch == '3' : "Can't find '3' at " + sig.substring(pos - 1);
215 final Class compType = nextElement();
216 return Array.newInstance(compType, 0).getClass();
219 throw new UnsupportedOperationException(sig + " at " + pos);