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