jaroslav@717
|
1 |
/**
|
jaroslav@717
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@717
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@717
|
4 |
*
|
jaroslav@717
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@717
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@717
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@717
|
8 |
*
|
jaroslav@717
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@717
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@717
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@717
|
12 |
* GNU General Public License for more details.
|
jaroslav@717
|
13 |
*
|
jaroslav@717
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@717
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@717
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@717
|
17 |
*/
|
jaroslav@717
|
18 |
package org.apidesign.bck2brwsr.ide.editor;
|
jaroslav@717
|
19 |
|
jaroslav@717
|
20 |
import java.lang.reflect.Method;
|
jaroslav@717
|
21 |
import java.util.HashMap;
|
jaroslav@717
|
22 |
import java.util.Map;
|
jaroslav@717
|
23 |
|
jaroslav@717
|
24 |
/**
|
jaroslav@717
|
25 |
* JNI Helper.
|
jaroslav@717
|
26 |
* To facilitate lookup of methods by name and signature, instead of manually parsing signatures,
|
jaroslav@717
|
27 |
* constructs the map of all methods and uses Class.getName() to generate almost-correct signatures.
|
jaroslav@717
|
28 |
*/
|
jaroslav@717
|
29 |
public class JNIHelper {
|
jaroslav@717
|
30 |
|
jaroslav@717
|
31 |
static Method method(String clazz, String method, String signature) {
|
jaroslav@717
|
32 |
final Map<String, Method> methods = methodMap(JNIHelper.clazz(clazz));
|
jaroslav@717
|
33 |
return methods.get(methodKey(method, signature));
|
jaroslav@717
|
34 |
}
|
jaroslav@717
|
35 |
|
jaroslav@717
|
36 |
static Class<?> clazz(String clazz) {
|
jaroslav@717
|
37 |
try {
|
jaroslav@717
|
38 |
return Class.forName(clazz);
|
jaroslav@717
|
39 |
} catch (ClassNotFoundException e) {
|
jaroslav@717
|
40 |
throw new IllegalArgumentException(e);
|
jaroslav@717
|
41 |
}
|
jaroslav@717
|
42 |
}
|
jaroslav@717
|
43 |
|
jaroslav@717
|
44 |
static Map<String, Method> methodMap(final Class<?> clazz) {
|
jaroslav@717
|
45 |
final Map<String, Method> map = new HashMap<String, Method>();
|
jaroslav@717
|
46 |
final Method[] methods = clazz.getDeclaredMethods();
|
jaroslav@717
|
47 |
for (int i = 0; i < methods.length; i++) {
|
jaroslav@717
|
48 |
final Method method = methods[i];
|
jaroslav@717
|
49 |
map.put(methodKey(method.getName(), signature(method)), method);
|
jaroslav@717
|
50 |
}
|
jaroslav@717
|
51 |
return map;
|
jaroslav@717
|
52 |
}
|
jaroslav@717
|
53 |
|
jaroslav@717
|
54 |
static String methodKey(String method, String signature) {
|
jaroslav@717
|
55 |
return method + '@' + signature;
|
jaroslav@717
|
56 |
}
|
jaroslav@717
|
57 |
|
jaroslav@717
|
58 |
static String signature(final Method method) {
|
jaroslav@717
|
59 |
final Class<?>[] parameterTypes = method.getParameterTypes();
|
jaroslav@717
|
60 |
final StringBuilder b = new StringBuilder();
|
jaroslav@717
|
61 |
for (int j = 0; j < parameterTypes.length; j++) {
|
jaroslav@717
|
62 |
b.append(signature(parameterTypes[j]));
|
jaroslav@717
|
63 |
}
|
jaroslav@717
|
64 |
return b.toString();
|
jaroslav@717
|
65 |
}
|
jaroslav@717
|
66 |
|
jaroslav@717
|
67 |
static String signature(final Class<?> clazz) {
|
jaroslav@717
|
68 |
if (clazz == boolean.class) return "Z";
|
jaroslav@717
|
69 |
else if (clazz == byte.class) return "B";
|
jaroslav@717
|
70 |
else if (clazz == char.class) return "C";
|
jaroslav@717
|
71 |
else if (clazz == double.class) return "D";
|
jaroslav@717
|
72 |
else if (clazz == float.class) return "F";
|
jaroslav@717
|
73 |
else if (clazz == int.class) return "I";
|
jaroslav@717
|
74 |
else if (clazz == long.class) return "J";
|
jaroslav@717
|
75 |
else if (clazz == short.class) return "S";
|
jaroslav@717
|
76 |
else if (clazz == void.class) return "V";
|
jaroslav@717
|
77 |
else if (clazz.isArray()) return clazz.getName().replace('.','/');
|
jaroslav@717
|
78 |
else return "L" + clazz.getName().replace('.','/') + ";";
|
jaroslav@717
|
79 |
}
|
jaroslav@717
|
80 |
}
|