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 org.apidesign.bck2brwsr.core.JavaScriptBody;
30 /** Utilities to work on methods.
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public abstract class MethodImpl {
35 public static MethodImpl INSTANCE;
38 Class.forName(Method.class.getName());
39 } catch (ClassNotFoundException ex) {
40 throw new IllegalStateException(ex);
44 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
48 // bck2brwsr implementation
51 @JavaScriptBody(args = {"clazz", "prefix"},
53 + "var c = clazz.cnstr.prototype;"
54 + "var arr = new Array();\n"
56 + " if (m.indexOf(prefix) === 0) {\n"
58 + " arr.push(c[m]);\n"
62 private static native Object[] findMethodData(
63 Class<?> clazz, String prefix);
65 // XXX should not be public
66 public static Method findMethod(
67 Class<?> clazz, String name, Class<?>... parameterTypes) {
68 Object[] data = findMethodData(clazz, name + "__");
69 if (data.length == 0) {
72 String sig = ((String) data[0]).substring(name.length() + 2);
73 return INSTANCE.create(clazz, name, data[1], sig);
76 public static Method[] findMethods(Class<?> clazz) {
77 Object[] namesAndData = findMethodData(clazz, "");
79 for (int i = 0; i < namesAndData.length; i += 2) {
80 String sig = (String) namesAndData[i];
81 Object data = namesAndData[i + 1];
82 int middle = sig.indexOf("__");
86 String name = sig.substring(0, middle);
87 sig = sig.substring(middle + 2);
88 namesAndData[cnt++] = INSTANCE.create(clazz, name, data, sig);
90 Method[] arr = new Method[cnt];
91 for (int i = 0; i < cnt; i++) {
92 arr[i] = (Method) namesAndData[i];