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