emul/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Dec 2012 08:48:08 +0100
changeset 391 8cddb5d3e18f
child 392 44a5802816be
permissions -rw-r--r--
Moving non-public access method into org.apidesign.bck2brwsr.emul package
     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     // XXX should not be public
    66     public static Method findMethod(
    67         Class<?> clazz, String name, Class<?>... parameterTypes) {
    68         Object[] data = findMethodData(clazz, name + "__");
    69         if (data.length == 0) {
    70             return null;
    71         }
    72         String sig = ((String) data[0]).substring(name.length() + 2);
    73         return INSTANCE.create(clazz, name, data[1], sig);
    74     }
    75 
    76     public static Method[] findMethods(Class<?> clazz) {
    77         Object[] namesAndData = findMethodData(clazz, "");
    78         int cnt = 0;
    79         for (int i = 0; i < namesAndData.length; i += 2) {
    80             String sig = (String) namesAndData[i];
    81             Object data = namesAndData[i + 1];
    82             int middle = sig.indexOf("__");
    83             if (middle == -1) {
    84                 continue;
    85             }
    86             String name = sig.substring(0, middle);
    87             sig = sig.substring(middle + 2);
    88             namesAndData[cnt++] = INSTANCE.create(clazz, name, data, sig);
    89         }
    90         Method[] arr = new Method[cnt];
    91         for (int i = 0; i < cnt; i++) {
    92             arr[i] = (Method) namesAndData[i];
    93         }
    94         return arr;
    95     }
    96     
    97 }