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.Method;
21 import java.util.Enumeration;
22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
24 /** Utilities to work on methods.
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 public abstract class MethodImpl {
29 public static MethodImpl INSTANCE;
32 Class.forName(Method.class.getName());
33 } catch (ClassNotFoundException ex) {
34 throw new IllegalStateException(ex);
38 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
42 // bck2brwsr implementation
45 @JavaScriptBody(args = {"clazz", "prefix"},
47 + "var c = clazz.cnstr.prototype;"
48 + "var arr = new Array();\n"
50 + " if (m.indexOf(prefix) === 0) {\n"
52 + " arr.push(c[m]);\n"
56 private static native Object[] findMethodData(
57 Class<?> clazz, String prefix);
59 public static Method findMethod(
60 Class<?> clazz, String name, Class<?>... parameterTypes) {
61 Object[] data = findMethodData(clazz, name + "__");
62 BIG: for (int i = 0; i < data.length; i += 2) {
63 String sig = ((String) data[0]).substring(name.length() + 2);
64 Method tmp = INSTANCE.create(clazz, name, data[1], sig);
65 Class<?>[] tmpParms = tmp.getParameterTypes();
66 if (parameterTypes.length != tmpParms.length) {
69 for (int j = 0; j < tmpParms.length; j++) {
70 if (!parameterTypes[j].equals(tmpParms[j])) {
79 public static Method[] findMethods(Class<?> clazz, int mask) {
80 Object[] namesAndData = findMethodData(clazz, "");
82 for (int i = 0; i < namesAndData.length; i += 2) {
83 String sig = (String) namesAndData[i];
84 Object data = namesAndData[i + 1];
85 int middle = sig.indexOf("__");
89 String name = sig.substring(0, middle);
90 sig = sig.substring(middle + 2);
91 final Method m = INSTANCE.create(clazz, name, data, sig);
92 if ((m.getModifiers() & mask) == 0) {
95 namesAndData[cnt++] = m;
97 Method[] arr = new Method[cnt];
98 for (int i = 0; i < cnt; i++) {
99 arr[i] = (Method) namesAndData[i];
104 public static int signatureElements(String sig) {
105 Enumeration<Class> en = signatureParser(sig);
107 while (en.hasMoreElements()) {
114 public static Enumeration<Class> signatureParser(final String sig) {
115 class E implements Enumeration<Class> {
118 public boolean hasMoreElements() {
119 return pos < sig.length();
122 public Class nextElement() {
123 switch (sig.charAt(pos++)) {
141 return Character.TYPE;
144 int up = sig.indexOf("_2");
145 String type = sig.substring(1, up);
147 return Class.forName(type);
148 } catch (ClassNotFoundException ex) {
152 throw new UnsupportedOperationException(sig + " at " + pos);