emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java
branchcanvas
changeset 808 bd8b726902a3
parent 731 e8283f10a127
parent 807 e93506e603ad
child 809 5c61f5e4898e
     1.1 --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java	Thu Feb 14 12:06:16 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,157 +0,0 @@
     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.emul.reflect;
    1.22 -
    1.23 -import java.lang.reflect.Method;
    1.24 -import java.util.Enumeration;
    1.25 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.26 -
    1.27 -/** Utilities to work on methods.
    1.28 - *
    1.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.30 - */
    1.31 -public abstract class MethodImpl {
    1.32 -    public static MethodImpl INSTANCE;
    1.33 -    static {
    1.34 -        try {
    1.35 -            Class.forName(Method.class.getName());
    1.36 -        } catch (ClassNotFoundException ex) {
    1.37 -            throw new IllegalStateException(ex);
    1.38 -        }
    1.39 -    }
    1.40 -    
    1.41 -    protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
    1.42 -    
    1.43 -    
    1.44 -    //
    1.45 -    // bck2brwsr implementation
    1.46 -    //
    1.47 -
    1.48 -    @JavaScriptBody(args = {"clazz", "prefix"},
    1.49 -        body = ""
    1.50 -        + "var c = clazz.cnstr.prototype;"
    1.51 -        + "var arr = new Array();\n"
    1.52 -        + "for (m in c) {\n"
    1.53 -        + "  if (m.indexOf(prefix) === 0) {\n"
    1.54 -        + "     arr.push(m);\n"
    1.55 -        + "     arr.push(c[m]);\n"
    1.56 -        + "  }"
    1.57 -        + "}\n"
    1.58 -        + "return arr;")
    1.59 -    private static native Object[] findMethodData(
    1.60 -        Class<?> clazz, String prefix);
    1.61 -
    1.62 -    public static Method findMethod(
    1.63 -        Class<?> clazz, String name, Class<?>... parameterTypes) {
    1.64 -        Object[] data = findMethodData(clazz, name + "__");
    1.65 -        BIG: for (int i = 0; i < data.length; i += 2) {
    1.66 -            String sig = ((String) data[0]).substring(name.length() + 2);
    1.67 -            Method tmp = INSTANCE.create(clazz, name, data[1], sig);
    1.68 -            Class<?>[] tmpParms = tmp.getParameterTypes();
    1.69 -            if (parameterTypes.length != tmpParms.length) {
    1.70 -                continue;
    1.71 -            }
    1.72 -            for (int j = 0; j < tmpParms.length; j++) {
    1.73 -                if (!parameterTypes[j].equals(tmpParms[j])) {
    1.74 -                    continue BIG;
    1.75 -                }
    1.76 -            }
    1.77 -            return tmp;
    1.78 -        }
    1.79 -        return null;
    1.80 -    }
    1.81 -
    1.82 -    public static Method[] findMethods(Class<?> clazz, int mask) {
    1.83 -        Object[] namesAndData = findMethodData(clazz, "");
    1.84 -        int cnt = 0;
    1.85 -        for (int i = 0; i < namesAndData.length; i += 2) {
    1.86 -            String sig = (String) namesAndData[i];
    1.87 -            Object data = namesAndData[i + 1];
    1.88 -            int middle = sig.indexOf("__");
    1.89 -            if (middle == -1) {
    1.90 -                continue;
    1.91 -            }
    1.92 -            String name = sig.substring(0, middle);
    1.93 -            sig = sig.substring(middle + 2);
    1.94 -            final Method m = INSTANCE.create(clazz, name, data, sig);
    1.95 -            if ((m.getModifiers() & mask) == 0) {
    1.96 -                continue;
    1.97 -            }
    1.98 -            namesAndData[cnt++] = m;
    1.99 -        }
   1.100 -        Method[] arr = new Method[cnt];
   1.101 -        for (int i = 0; i < cnt; i++) {
   1.102 -            arr[i] = (Method) namesAndData[i];
   1.103 -        }
   1.104 -        return arr;
   1.105 -    }
   1.106 -
   1.107 -    public static int signatureElements(String sig) {
   1.108 -        Enumeration<Class> en = signatureParser(sig);
   1.109 -        int cnt = 0;
   1.110 -        while (en.hasMoreElements()) {
   1.111 -            en.nextElement();
   1.112 -            cnt++;
   1.113 -        }
   1.114 -        return cnt;
   1.115 -    }
   1.116 -    
   1.117 -    public static Enumeration<Class> signatureParser(final String sig) {
   1.118 -        class E implements Enumeration<Class> {
   1.119 -            int pos;
   1.120 -            
   1.121 -            public boolean hasMoreElements() {
   1.122 -                return pos < sig.length();
   1.123 -            }
   1.124 -
   1.125 -            public Class nextElement() {
   1.126 -                switch (sig.charAt(pos++)) {
   1.127 -                    case 'I':
   1.128 -                        return Integer.TYPE;
   1.129 -                    case 'J':
   1.130 -                        return Long.TYPE;
   1.131 -                    case 'D':
   1.132 -                        return Double.TYPE;
   1.133 -                    case 'F':
   1.134 -                        return Float.TYPE;
   1.135 -                    case 'B':
   1.136 -                        return Byte.TYPE;
   1.137 -                    case 'Z':
   1.138 -                        return Boolean.TYPE;
   1.139 -                    case 'S':
   1.140 -                        return Short.TYPE;
   1.141 -                    case 'V':
   1.142 -                        return Void.TYPE;
   1.143 -                    case 'C':
   1.144 -                        return Character.TYPE;
   1.145 -                    case 'L':
   1.146 -                        try {
   1.147 -                            int up = sig.indexOf("_2");
   1.148 -                            String type = sig.substring(1, up);
   1.149 -                            pos = up + 2;
   1.150 -                            return Class.forName(type);
   1.151 -                        } catch (ClassNotFoundException ex) {
   1.152 -                            // should not happen
   1.153 -                        }
   1.154 -                }
   1.155 -                throw new UnsupportedOperationException(sig + " at " + pos);
   1.156 -            }
   1.157 -        }
   1.158 -        return new E();
   1.159 -    }
   1.160 -}