ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JNIHelper.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 12:45:02 +0100
branchide
changeset 718 b93760cedf02
parent 717 58ce0cd13d26
permissions -rw-r--r--
Using Igor's tokenizer in Lahvac's hint
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.ide.editor;
    19 
    20 import java.lang.reflect.Method;
    21 import java.util.HashMap;
    22 import java.util.Map;
    23 
    24 /**
    25  * JNI Helper.
    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.
    28  */
    29 class JNIHelper {
    30 
    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));
    34     }
    35 
    36     static Class<?> clazz(String clazz) {
    37         try {
    38             return Class.forName(clazz);
    39         } catch (ClassNotFoundException e) {
    40             throw new IllegalArgumentException(e);
    41         }
    42     }
    43 
    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);
    50         }
    51         return map;
    52     }
    53 
    54     static String methodKey(String method, String signature) {
    55         return method + '@' + signature;
    56     }
    57 
    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]));
    63         }
    64         return b.toString();
    65     }
    66 
    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('.','/') + ";";
    79     }
    80 }