emul/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Dec 2012 12:35:32 +0100
changeset 392 44a5802816be
parent 391 8cddb5d3e18f
child 418 c0bbf144c2c6
permissions -rw-r--r--
Class.getMethods returns only public methods
jaroslav@391
     1
/*
jaroslav@391
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@391
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@391
     4
 *
jaroslav@391
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@391
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@391
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@391
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@391
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@391
    10
 *
jaroslav@391
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@391
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@391
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@391
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@391
    15
 * accompanied this code).
jaroslav@391
    16
 *
jaroslav@391
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@391
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@391
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@391
    20
 *
jaroslav@391
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@391
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@391
    23
 * questions.
jaroslav@391
    24
 */
jaroslav@391
    25
package org.apidesign.bck2brwsr.emul;
jaroslav@391
    26
jaroslav@391
    27
import java.lang.reflect.Method;
jaroslav@391
    28
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@391
    29
jaroslav@391
    30
/** Utilities to work on methods.
jaroslav@391
    31
 *
jaroslav@391
    32
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@391
    33
 */
jaroslav@391
    34
public abstract class MethodImpl {
jaroslav@391
    35
    public static MethodImpl INSTANCE;
jaroslav@391
    36
    static {
jaroslav@391
    37
        try {
jaroslav@391
    38
            Class.forName(Method.class.getName());
jaroslav@391
    39
        } catch (ClassNotFoundException ex) {
jaroslav@391
    40
            throw new IllegalStateException(ex);
jaroslav@391
    41
        }
jaroslav@391
    42
    }
jaroslav@391
    43
    
jaroslav@391
    44
    protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
jaroslav@391
    45
    
jaroslav@391
    46
    
jaroslav@391
    47
    //
jaroslav@391
    48
    // bck2brwsr implementation
jaroslav@391
    49
    //
jaroslav@391
    50
jaroslav@391
    51
    @JavaScriptBody(args = {"clazz", "prefix"},
jaroslav@391
    52
        body = ""
jaroslav@391
    53
        + "var c = clazz.cnstr.prototype;"
jaroslav@391
    54
        + "var arr = new Array();\n"
jaroslav@391
    55
        + "for (m in c) {\n"
jaroslav@391
    56
        + "  if (m.indexOf(prefix) === 0) {\n"
jaroslav@391
    57
        + "     arr.push(m);\n"
jaroslav@391
    58
        + "     arr.push(c[m]);\n"
jaroslav@391
    59
        + "  }"
jaroslav@391
    60
        + "}\n"
jaroslav@391
    61
        + "return arr;")
jaroslav@391
    62
    private static native Object[] findMethodData(
jaroslav@391
    63
        Class<?> clazz, String prefix);
jaroslav@391
    64
jaroslav@391
    65
    public static Method findMethod(
jaroslav@391
    66
        Class<?> clazz, String name, Class<?>... parameterTypes) {
jaroslav@391
    67
        Object[] data = findMethodData(clazz, name + "__");
jaroslav@391
    68
        if (data.length == 0) {
jaroslav@391
    69
            return null;
jaroslav@391
    70
        }
jaroslav@391
    71
        String sig = ((String) data[0]).substring(name.length() + 2);
jaroslav@391
    72
        return INSTANCE.create(clazz, name, data[1], sig);
jaroslav@391
    73
    }
jaroslav@391
    74
jaroslav@392
    75
    public static Method[] findMethods(Class<?> clazz, int mask) {
jaroslav@391
    76
        Object[] namesAndData = findMethodData(clazz, "");
jaroslav@391
    77
        int cnt = 0;
jaroslav@391
    78
        for (int i = 0; i < namesAndData.length; i += 2) {
jaroslav@391
    79
            String sig = (String) namesAndData[i];
jaroslav@391
    80
            Object data = namesAndData[i + 1];
jaroslav@391
    81
            int middle = sig.indexOf("__");
jaroslav@391
    82
            if (middle == -1) {
jaroslav@391
    83
                continue;
jaroslav@391
    84
            }
jaroslav@391
    85
            String name = sig.substring(0, middle);
jaroslav@391
    86
            sig = sig.substring(middle + 2);
jaroslav@392
    87
            final Method m = INSTANCE.create(clazz, name, data, sig);
jaroslav@392
    88
            if ((m.getModifiers() & mask) == 0) {
jaroslav@392
    89
                continue;
jaroslav@392
    90
            }
jaroslav@392
    91
            namesAndData[cnt++] = m;
jaroslav@391
    92
        }
jaroslav@391
    93
        Method[] arr = new Method[cnt];
jaroslav@391
    94
        for (int i = 0; i < cnt; i++) {
jaroslav@391
    95
            arr[i] = (Method) namesAndData[i];
jaroslav@391
    96
        }
jaroslav@391
    97
        return arr;
jaroslav@391
    98
    }
jaroslav@391
    99
    
jaroslav@391
   100
}