2 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 package org.apidesign.bck2brwsr.emul;
27 import java.lang.reflect.Method;
28 import java.util.Enumeration;
29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 /** Utilities to work on methods.
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public abstract class MethodImpl {
36 public static MethodImpl INSTANCE;
39 Class.forName(Method.class.getName());
40 } catch (ClassNotFoundException ex) {
41 throw new IllegalStateException(ex);
45 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
49 // bck2brwsr implementation
52 @JavaScriptBody(args = {"clazz", "prefix"},
54 + "var c = clazz.cnstr.prototype;"
55 + "var arr = new Array();\n"
57 + " if (m.indexOf(prefix) === 0) {\n"
59 + " arr.push(c[m]);\n"
63 private static native Object[] findMethodData(
64 Class<?> clazz, String prefix);
66 public static Method findMethod(
67 Class<?> clazz, String name, Class<?>... parameterTypes) {
68 Object[] data = findMethodData(clazz, name + "__");
69 BIG: for (int i = 0; i < data.length; i += 2) {
70 String sig = ((String) data[0]).substring(name.length() + 2);
71 Method tmp = INSTANCE.create(clazz, name, data[1], sig);
72 Class<?>[] tmpParms = tmp.getParameterTypes();
73 if (parameterTypes.length != tmpParms.length) {
76 for (int j = 0; j < tmpParms.length; j++) {
77 if (!parameterTypes[j].equals(tmpParms[j])) {
86 public static Method[] findMethods(Class<?> clazz, int mask) {
87 Object[] namesAndData = findMethodData(clazz, "");
89 for (int i = 0; i < namesAndData.length; i += 2) {
90 String sig = (String) namesAndData[i];
91 Object data = namesAndData[i + 1];
92 int middle = sig.indexOf("__");
96 String name = sig.substring(0, middle);
97 sig = sig.substring(middle + 2);
98 final Method m = INSTANCE.create(clazz, name, data, sig);
99 if ((m.getModifiers() & mask) == 0) {
102 namesAndData[cnt++] = m;
104 Method[] arr = new Method[cnt];
105 for (int i = 0; i < cnt; i++) {
106 arr[i] = (Method) namesAndData[i];
111 public static int signatureElements(String sig) {
112 Enumeration<Class> en = signatureParser(sig);
114 while (en.hasMoreElements()) {
121 public static Enumeration<Class> signatureParser(final String sig) {
122 class E implements Enumeration<Class> {
125 public boolean hasMoreElements() {
126 return pos < sig.length();
129 public Class nextElement() {
130 switch (sig.charAt(pos++)) {
149 int up = sig.indexOf("_2");
150 String type = sig.substring(1, up);
152 return Class.forName(type);
153 } catch (ClassNotFoundException ex) {
157 throw new UnsupportedOperationException(sig + " at " + pos);