jaroslav@717: /** jaroslav@717: * Back 2 Browser Bytecode Translator jaroslav@717: * Copyright (C) 2012 Jaroslav Tulach jaroslav@717: * jaroslav@717: * This program is free software: you can redistribute it and/or modify jaroslav@717: * it under the terms of the GNU General Public License as published by jaroslav@717: * the Free Software Foundation, version 2 of the License. jaroslav@717: * jaroslav@717: * This program is distributed in the hope that it will be useful, jaroslav@717: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@717: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@717: * GNU General Public License for more details. jaroslav@717: * jaroslav@717: * You should have received a copy of the GNU General Public License jaroslav@717: * along with this program. Look for COPYING file in the top folder. jaroslav@717: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@717: */ jaroslav@717: package org.apidesign.bck2brwsr.ide.editor; jaroslav@717: jaroslav@717: import java.lang.reflect.Method; jaroslav@717: import java.util.HashMap; jaroslav@717: import java.util.Map; jaroslav@717: jaroslav@717: /** jaroslav@717: * JNI Helper. jaroslav@717: * To facilitate lookup of methods by name and signature, instead of manually parsing signatures, jaroslav@717: * constructs the map of all methods and uses Class.getName() to generate almost-correct signatures. jaroslav@717: */ jaroslav@717: public class JNIHelper { jaroslav@717: jaroslav@717: static Method method(String clazz, String method, String signature) { jaroslav@717: final Map methods = methodMap(JNIHelper.clazz(clazz)); jaroslav@717: return methods.get(methodKey(method, signature)); jaroslav@717: } jaroslav@717: jaroslav@717: static Class clazz(String clazz) { jaroslav@717: try { jaroslav@717: return Class.forName(clazz); jaroslav@717: } catch (ClassNotFoundException e) { jaroslav@717: throw new IllegalArgumentException(e); jaroslav@717: } jaroslav@717: } jaroslav@717: jaroslav@717: static Map methodMap(final Class clazz) { jaroslav@717: final Map map = new HashMap(); jaroslav@717: final Method[] methods = clazz.getDeclaredMethods(); jaroslav@717: for (int i = 0; i < methods.length; i++) { jaroslav@717: final Method method = methods[i]; jaroslav@717: map.put(methodKey(method.getName(), signature(method)), method); jaroslav@717: } jaroslav@717: return map; jaroslav@717: } jaroslav@717: jaroslav@717: static String methodKey(String method, String signature) { jaroslav@717: return method + '@' + signature; jaroslav@717: } jaroslav@717: jaroslav@717: static String signature(final Method method) { jaroslav@717: final Class[] parameterTypes = method.getParameterTypes(); jaroslav@717: final StringBuilder b = new StringBuilder(); jaroslav@717: for (int j = 0; j < parameterTypes.length; j++) { jaroslav@717: b.append(signature(parameterTypes[j])); jaroslav@717: } jaroslav@717: return b.toString(); jaroslav@717: } jaroslav@717: jaroslav@717: static String signature(final Class clazz) { jaroslav@717: if (clazz == boolean.class) return "Z"; jaroslav@717: else if (clazz == byte.class) return "B"; jaroslav@717: else if (clazz == char.class) return "C"; jaroslav@717: else if (clazz == double.class) return "D"; jaroslav@717: else if (clazz == float.class) return "F"; jaroslav@717: else if (clazz == int.class) return "I"; jaroslav@717: else if (clazz == long.class) return "J"; jaroslav@717: else if (clazz == short.class) return "S"; jaroslav@717: else if (clazz == void.class) return "V"; jaroslav@717: else if (clazz.isArray()) return clazz.getName().replace('.','/'); jaroslav@717: else return "L" + clazz.getName().replace('.','/') + ";"; jaroslav@717: } jaroslav@717: }