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 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, int mask) {
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 final Method m = INSTANCE.create(clazz, name, data, sig);
89 if ((m.getModifiers() & mask) == 0) {
92 namesAndData[cnt++] = m;
94 Method[] arr = new Method[cnt];
95 for (int i = 0; i < cnt; i++) {
96 arr[i] = (Method) namesAndData[i];
101 public static int signatureElements(String sig) {
102 Enumeration<Class> en = signatureParser(sig);
104 while (en.hasMoreElements()) {
111 public static Enumeration<Class> signatureParser(final String sig) {
112 class E implements Enumeration<Class> {
115 public boolean hasMoreElements() {
116 return pos < sig.length();
119 public Class nextElement() {
120 switch (sig.charAt(pos++)) {
139 int up = sig.indexOf("_2");
140 String type = sig.substring(1, up);
142 return Class.forName(type);
143 } catch (ClassNotFoundException ex) {
147 throw new UnsupportedOperationException(sig + " at " + pos);