1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JNIHelper.java Wed Feb 13 12:08:00 2013 +0100
1.3 @@ -0,0 +1,80 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.ide.editor;
1.22 +
1.23 +import java.lang.reflect.Method;
1.24 +import java.util.HashMap;
1.25 +import java.util.Map;
1.26 +
1.27 +/**
1.28 + * JNI Helper.
1.29 + * To facilitate lookup of methods by name and signature, instead of manually parsing signatures,
1.30 + * constructs the map of all methods and uses Class.getName() to generate almost-correct signatures.
1.31 + */
1.32 +public class JNIHelper {
1.33 +
1.34 + static Method method(String clazz, String method, String signature) {
1.35 + final Map<String, Method> methods = methodMap(JNIHelper.clazz(clazz));
1.36 + return methods.get(methodKey(method, signature));
1.37 + }
1.38 +
1.39 + static Class<?> clazz(String clazz) {
1.40 + try {
1.41 + return Class.forName(clazz);
1.42 + } catch (ClassNotFoundException e) {
1.43 + throw new IllegalArgumentException(e);
1.44 + }
1.45 + }
1.46 +
1.47 + static Map<String, Method> methodMap(final Class<?> clazz) {
1.48 + final Map<String, Method> map = new HashMap<String, Method>();
1.49 + final Method[] methods = clazz.getDeclaredMethods();
1.50 + for (int i = 0; i < methods.length; i++) {
1.51 + final Method method = methods[i];
1.52 + map.put(methodKey(method.getName(), signature(method)), method);
1.53 + }
1.54 + return map;
1.55 + }
1.56 +
1.57 + static String methodKey(String method, String signature) {
1.58 + return method + '@' + signature;
1.59 + }
1.60 +
1.61 + static String signature(final Method method) {
1.62 + final Class<?>[] parameterTypes = method.getParameterTypes();
1.63 + final StringBuilder b = new StringBuilder();
1.64 + for (int j = 0; j < parameterTypes.length; j++) {
1.65 + b.append(signature(parameterTypes[j]));
1.66 + }
1.67 + return b.toString();
1.68 + }
1.69 +
1.70 + static String signature(final Class<?> clazz) {
1.71 + if (clazz == boolean.class) return "Z";
1.72 + else if (clazz == byte.class) return "B";
1.73 + else if (clazz == char.class) return "C";
1.74 + else if (clazz == double.class) return "D";
1.75 + else if (clazz == float.class) return "F";
1.76 + else if (clazz == int.class) return "I";
1.77 + else if (clazz == long.class) return "J";
1.78 + else if (clazz == short.class) return "S";
1.79 + else if (clazz == void.class) return "V";
1.80 + else if (clazz.isArray()) return clazz.getName().replace('.','/');
1.81 + else return "L" + clazz.getName().replace('.','/') + ";";
1.82 + }
1.83 +}