jaroslav@391
|
1 |
/*
|
jaroslav@391
|
2 |
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@391
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@391
|
4 |
*
|
jaroslav@391
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@391
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@391
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@391
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@391
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@391
|
10 |
*
|
jaroslav@391
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@391
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@391
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@391
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@391
|
15 |
* accompanied this code).
|
jaroslav@391
|
16 |
*
|
jaroslav@391
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@391
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@391
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@391
|
20 |
*
|
jaroslav@391
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@391
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@391
|
23 |
* questions.
|
jaroslav@391
|
24 |
*/
|
jaroslav@391
|
25 |
package org.apidesign.bck2brwsr.emul;
|
jaroslav@391
|
26 |
|
jaroslav@391
|
27 |
import java.lang.reflect.Method;
|
jaroslav@418
|
28 |
import java.util.Enumeration;
|
jaroslav@391
|
29 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@391
|
30 |
|
jaroslav@391
|
31 |
/** Utilities to work on methods.
|
jaroslav@391
|
32 |
*
|
jaroslav@391
|
33 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@391
|
34 |
*/
|
jaroslav@391
|
35 |
public abstract class MethodImpl {
|
jaroslav@391
|
36 |
public static MethodImpl INSTANCE;
|
jaroslav@391
|
37 |
static {
|
jaroslav@391
|
38 |
try {
|
jaroslav@391
|
39 |
Class.forName(Method.class.getName());
|
jaroslav@391
|
40 |
} catch (ClassNotFoundException ex) {
|
jaroslav@391
|
41 |
throw new IllegalStateException(ex);
|
jaroslav@391
|
42 |
}
|
jaroslav@391
|
43 |
}
|
jaroslav@391
|
44 |
|
jaroslav@391
|
45 |
protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
|
jaroslav@391
|
46 |
|
jaroslav@391
|
47 |
|
jaroslav@391
|
48 |
//
|
jaroslav@391
|
49 |
// bck2brwsr implementation
|
jaroslav@391
|
50 |
//
|
jaroslav@391
|
51 |
|
jaroslav@391
|
52 |
@JavaScriptBody(args = {"clazz", "prefix"},
|
jaroslav@391
|
53 |
body = ""
|
jaroslav@391
|
54 |
+ "var c = clazz.cnstr.prototype;"
|
jaroslav@391
|
55 |
+ "var arr = new Array();\n"
|
jaroslav@391
|
56 |
+ "for (m in c) {\n"
|
jaroslav@391
|
57 |
+ " if (m.indexOf(prefix) === 0) {\n"
|
jaroslav@391
|
58 |
+ " arr.push(m);\n"
|
jaroslav@391
|
59 |
+ " arr.push(c[m]);\n"
|
jaroslav@391
|
60 |
+ " }"
|
jaroslav@391
|
61 |
+ "}\n"
|
jaroslav@391
|
62 |
+ "return arr;")
|
jaroslav@391
|
63 |
private static native Object[] findMethodData(
|
jaroslav@391
|
64 |
Class<?> clazz, String prefix);
|
jaroslav@391
|
65 |
|
jaroslav@391
|
66 |
public static Method findMethod(
|
jaroslav@391
|
67 |
Class<?> clazz, String name, Class<?>... parameterTypes) {
|
jaroslav@391
|
68 |
Object[] data = findMethodData(clazz, name + "__");
|
jaroslav@420
|
69 |
BIG: for (int i = 0; i < data.length; i += 2) {
|
jaroslav@420
|
70 |
String sig = ((String) data[0]).substring(name.length() + 2);
|
jaroslav@420
|
71 |
Method tmp = INSTANCE.create(clazz, name, data[1], sig);
|
jaroslav@420
|
72 |
Class<?>[] tmpParms = tmp.getParameterTypes();
|
jaroslav@420
|
73 |
if (parameterTypes.length != tmpParms.length) {
|
jaroslav@420
|
74 |
continue;
|
jaroslav@420
|
75 |
}
|
jaroslav@420
|
76 |
for (int j = 0; j < tmpParms.length; j++) {
|
jaroslav@420
|
77 |
if (!parameterTypes[j].equals(tmpParms[j])) {
|
jaroslav@420
|
78 |
continue BIG;
|
jaroslav@420
|
79 |
}
|
jaroslav@420
|
80 |
}
|
jaroslav@420
|
81 |
return tmp;
|
jaroslav@391
|
82 |
}
|
jaroslav@420
|
83 |
return null;
|
jaroslav@391
|
84 |
}
|
jaroslav@391
|
85 |
|
jaroslav@392
|
86 |
public static Method[] findMethods(Class<?> clazz, int mask) {
|
jaroslav@391
|
87 |
Object[] namesAndData = findMethodData(clazz, "");
|
jaroslav@391
|
88 |
int cnt = 0;
|
jaroslav@391
|
89 |
for (int i = 0; i < namesAndData.length; i += 2) {
|
jaroslav@391
|
90 |
String sig = (String) namesAndData[i];
|
jaroslav@391
|
91 |
Object data = namesAndData[i + 1];
|
jaroslav@391
|
92 |
int middle = sig.indexOf("__");
|
jaroslav@391
|
93 |
if (middle == -1) {
|
jaroslav@391
|
94 |
continue;
|
jaroslav@391
|
95 |
}
|
jaroslav@391
|
96 |
String name = sig.substring(0, middle);
|
jaroslav@391
|
97 |
sig = sig.substring(middle + 2);
|
jaroslav@392
|
98 |
final Method m = INSTANCE.create(clazz, name, data, sig);
|
jaroslav@392
|
99 |
if ((m.getModifiers() & mask) == 0) {
|
jaroslav@392
|
100 |
continue;
|
jaroslav@392
|
101 |
}
|
jaroslav@392
|
102 |
namesAndData[cnt++] = m;
|
jaroslav@391
|
103 |
}
|
jaroslav@391
|
104 |
Method[] arr = new Method[cnt];
|
jaroslav@391
|
105 |
for (int i = 0; i < cnt; i++) {
|
jaroslav@391
|
106 |
arr[i] = (Method) namesAndData[i];
|
jaroslav@391
|
107 |
}
|
jaroslav@391
|
108 |
return arr;
|
jaroslav@391
|
109 |
}
|
jaroslav@418
|
110 |
|
jaroslav@418
|
111 |
public static int signatureElements(String sig) {
|
jaroslav@418
|
112 |
Enumeration<Class> en = signatureParser(sig);
|
jaroslav@418
|
113 |
int cnt = 0;
|
jaroslav@418
|
114 |
while (en.hasMoreElements()) {
|
jaroslav@418
|
115 |
en.nextElement();
|
jaroslav@418
|
116 |
cnt++;
|
jaroslav@418
|
117 |
}
|
jaroslav@418
|
118 |
return cnt;
|
jaroslav@418
|
119 |
}
|
jaroslav@391
|
120 |
|
jaroslav@418
|
121 |
public static Enumeration<Class> signatureParser(final String sig) {
|
jaroslav@418
|
122 |
class E implements Enumeration<Class> {
|
jaroslav@418
|
123 |
int pos;
|
jaroslav@418
|
124 |
|
jaroslav@418
|
125 |
public boolean hasMoreElements() {
|
jaroslav@418
|
126 |
return pos < sig.length();
|
jaroslav@418
|
127 |
}
|
jaroslav@418
|
128 |
|
jaroslav@418
|
129 |
public Class nextElement() {
|
jaroslav@418
|
130 |
switch (sig.charAt(pos++)) {
|
jaroslav@418
|
131 |
case 'I':
|
jaroslav@418
|
132 |
return Integer.TYPE;
|
jaroslav@418
|
133 |
case 'J':
|
jaroslav@418
|
134 |
return Long.TYPE;
|
jaroslav@418
|
135 |
case 'D':
|
jaroslav@418
|
136 |
return Double.TYPE;
|
jaroslav@418
|
137 |
case 'F':
|
jaroslav@418
|
138 |
return Float.TYPE;
|
jaroslav@418
|
139 |
case 'B':
|
jaroslav@418
|
140 |
return Byte.TYPE;
|
jaroslav@418
|
141 |
case 'Z':
|
jaroslav@418
|
142 |
return Boolean.TYPE;
|
jaroslav@418
|
143 |
case 'S':
|
jaroslav@418
|
144 |
return Short.TYPE;
|
jaroslav@418
|
145 |
case 'V':
|
jaroslav@418
|
146 |
return Void.TYPE;
|
jaroslav@430
|
147 |
case 'C':
|
jaroslav@430
|
148 |
return Character.TYPE;
|
jaroslav@418
|
149 |
case 'L':
|
jaroslav@418
|
150 |
try {
|
jaroslav@418
|
151 |
int up = sig.indexOf("_2");
|
jaroslav@418
|
152 |
String type = sig.substring(1, up);
|
jaroslav@418
|
153 |
pos = up + 2;
|
jaroslav@418
|
154 |
return Class.forName(type);
|
jaroslav@418
|
155 |
} catch (ClassNotFoundException ex) {
|
jaroslav@418
|
156 |
// should not happen
|
jaroslav@418
|
157 |
}
|
jaroslav@418
|
158 |
}
|
jaroslav@418
|
159 |
throw new UnsupportedOperationException(sig + " at " + pos);
|
jaroslav@418
|
160 |
}
|
jaroslav@418
|
161 |
}
|
jaroslav@418
|
162 |
return new E();
|
jaroslav@418
|
163 |
}
|
jaroslav@391
|
164 |
}
|