emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 20:39:23 +0100
branchemul
changeset 554 05224402145d
parent 430 emul/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java@b4940ef87438
permissions -rw-r--r--
First attempt to separate 'mini' profile from the rest of JDK APIs
     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 java.util.Enumeration;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 
    31 /** Utilities to work on methods.
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public abstract class MethodImpl {
    36     public static MethodImpl INSTANCE;
    37     static {
    38         try {
    39             Class.forName(Method.class.getName());
    40         } catch (ClassNotFoundException ex) {
    41             throw new IllegalStateException(ex);
    42         }
    43     }
    44     
    45     protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
    46     
    47     
    48     //
    49     // bck2brwsr implementation
    50     //
    51 
    52     @JavaScriptBody(args = {"clazz", "prefix"},
    53         body = ""
    54         + "var c = clazz.cnstr.prototype;"
    55         + "var arr = new Array();\n"
    56         + "for (m in c) {\n"
    57         + "  if (m.indexOf(prefix) === 0) {\n"
    58         + "     arr.push(m);\n"
    59         + "     arr.push(c[m]);\n"
    60         + "  }"
    61         + "}\n"
    62         + "return arr;")
    63     private static native Object[] findMethodData(
    64         Class<?> clazz, String prefix);
    65 
    66     public static Method findMethod(
    67         Class<?> clazz, String name, Class<?>... parameterTypes) {
    68         Object[] data = findMethodData(clazz, name + "__");
    69         BIG: for (int i = 0; i < data.length; i += 2) {
    70             String sig = ((String) data[0]).substring(name.length() + 2);
    71             Method tmp = INSTANCE.create(clazz, name, data[1], sig);
    72             Class<?>[] tmpParms = tmp.getParameterTypes();
    73             if (parameterTypes.length != tmpParms.length) {
    74                 continue;
    75             }
    76             for (int j = 0; j < tmpParms.length; j++) {
    77                 if (!parameterTypes[j].equals(tmpParms[j])) {
    78                     continue BIG;
    79                 }
    80             }
    81             return tmp;
    82         }
    83         return null;
    84     }
    85 
    86     public static Method[] findMethods(Class<?> clazz, int mask) {
    87         Object[] namesAndData = findMethodData(clazz, "");
    88         int cnt = 0;
    89         for (int i = 0; i < namesAndData.length; i += 2) {
    90             String sig = (String) namesAndData[i];
    91             Object data = namesAndData[i + 1];
    92             int middle = sig.indexOf("__");
    93             if (middle == -1) {
    94                 continue;
    95             }
    96             String name = sig.substring(0, middle);
    97             sig = sig.substring(middle + 2);
    98             final Method m = INSTANCE.create(clazz, name, data, sig);
    99             if ((m.getModifiers() & mask) == 0) {
   100                 continue;
   101             }
   102             namesAndData[cnt++] = m;
   103         }
   104         Method[] arr = new Method[cnt];
   105         for (int i = 0; i < cnt; i++) {
   106             arr[i] = (Method) namesAndData[i];
   107         }
   108         return arr;
   109     }
   110 
   111     public static int signatureElements(String sig) {
   112         Enumeration<Class> en = signatureParser(sig);
   113         int cnt = 0;
   114         while (en.hasMoreElements()) {
   115             en.nextElement();
   116             cnt++;
   117         }
   118         return cnt;
   119     }
   120     
   121     public static Enumeration<Class> signatureParser(final String sig) {
   122         class E implements Enumeration<Class> {
   123             int pos;
   124             
   125             public boolean hasMoreElements() {
   126                 return pos < sig.length();
   127             }
   128 
   129             public Class nextElement() {
   130                 switch (sig.charAt(pos++)) {
   131                     case 'I':
   132                         return Integer.TYPE;
   133                     case 'J':
   134                         return Long.TYPE;
   135                     case 'D':
   136                         return Double.TYPE;
   137                     case 'F':
   138                         return Float.TYPE;
   139                     case 'B':
   140                         return Byte.TYPE;
   141                     case 'Z':
   142                         return Boolean.TYPE;
   143                     case 'S':
   144                         return Short.TYPE;
   145                     case 'V':
   146                         return Void.TYPE;
   147                     case 'C':
   148                         return Character.TYPE;
   149                     case 'L':
   150                         try {
   151                             int up = sig.indexOf("_2");
   152                             String type = sig.substring(1, up);
   153                             pos = up + 2;
   154                             return Class.forName(type);
   155                         } catch (ClassNotFoundException ex) {
   156                             // should not happen
   157                         }
   158                 }
   159                 throw new UnsupportedOperationException(sig + " at " + pos);
   160             }
   161         }
   162         return new E();
   163     }
   164 }