Moving non-public access method into org.apidesign.bck2brwsr.emul package
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Dec 2012 08:48:08 +0100
changeset 3918cddb5d3e18f
parent 390 40fabaeca68d
child 392 44a5802816be
Moving non-public access method into org.apidesign.bck2brwsr.emul package
emul/src/main/java/java/lang/Class.java
emul/src/main/java/java/lang/reflect/Method.java
emul/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java
     1.1 --- a/emul/src/main/java/java/lang/Class.java	Fri Dec 28 07:59:43 2012 +0100
     1.2 +++ b/emul/src/main/java/java/lang/Class.java	Fri Dec 28 08:48:08 2012 +0100
     1.3 @@ -32,6 +32,7 @@
     1.4  import java.lang.reflect.Method;
     1.5  import java.lang.reflect.TypeVariable;
     1.6  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.7 +import org.apidesign.bck2brwsr.emul.MethodImpl;
     1.8  
     1.9  /**
    1.10   * Instances of the class {@code Class} represent classes and
    1.11 @@ -640,7 +641,7 @@
    1.12       * @since JDK1.1
    1.13       */
    1.14      public Method[] getMethods() throws SecurityException {
    1.15 -        return Method.findMethods(this);
    1.16 +        return MethodImpl.findMethods(this);
    1.17      }
    1.18  
    1.19      /**
    1.20 @@ -771,7 +772,7 @@
    1.21       */
    1.22      public Method getMethod(String name, Class<?>... parameterTypes)
    1.23          throws SecurityException {
    1.24 -        Method m = Method.findMethod(this, name, parameterTypes);
    1.25 +        Method m = MethodImpl.findMethod(this, name, parameterTypes);
    1.26          if (m == null) {
    1.27              throw new SecurityException(); // XXX: NoSuchMethodException
    1.28          }
     2.1 --- a/emul/src/main/java/java/lang/reflect/Method.java	Fri Dec 28 07:59:43 2012 +0100
     2.2 +++ b/emul/src/main/java/java/lang/reflect/Method.java	Fri Dec 28 08:48:08 2012 +0100
     2.3 @@ -28,6 +28,7 @@
     2.4  import java.lang.annotation.Annotation;
     2.5  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.6  import org.apidesign.bck2brwsr.emul.AnnotationImpl;
     2.7 +import org.apidesign.bck2brwsr.emul.MethodImpl;
     2.8  
     2.9  /**
    2.10   * A {@code Method} provides information about, and access to, a single method
    2.11 @@ -653,57 +654,12 @@
    2.12      public Annotation[][] getParameterAnnotations() {
    2.13          throw new UnsupportedOperationException();
    2.14      }
    2.15 -    
    2.16 -    //
    2.17 -    // bck2brwsr implementation
    2.18 -    //
    2.19  
    2.20 -    @JavaScriptBody(args = { "clazz", "prefix" },
    2.21 -        body = ""
    2.22 -        + "var c = clazz.cnstr.prototype;"
    2.23 -        + "var arr = new Array();\n"
    2.24 -        + "for (m in c) {\n"
    2.25 -        + "  if (m.indexOf(prefix) === 0) {\n"
    2.26 -        + "     arr.push(m);\n"
    2.27 -        + "     arr.push(c[m]);\n"
    2.28 -        + "  }"
    2.29 -        + "}\n"
    2.30 -        + "return arr;"
    2.31 -    )
    2.32 -    private static native Object[] findMethodData(
    2.33 -        Class<?> clazz, String prefix
    2.34 -    );
    2.35 -
    2.36 -    // XXX should not be public
    2.37 -    public static Method findMethod(
    2.38 -        Class<?> clazz, String name, Class<?>... parameterTypes
    2.39 -    ) {
    2.40 -        Object[] data = findMethodData(clazz, name + "__");
    2.41 -        if (data.length == 0) {
    2.42 -            return null;
    2.43 -        }
    2.44 -        String sig = ((String)data[0]).substring(name.length() + 2);
    2.45 -        return new Method(clazz, name, data[1], sig);
    2.46 -    }
    2.47 -    
    2.48 -    public static Method[] findMethods(Class<?> clazz) {
    2.49 -        Object[] namesAndData = findMethodData(clazz, "");
    2.50 -        int cnt = 0;
    2.51 -        for (int i = 0; i < namesAndData.length; i += 2) {
    2.52 -            String sig = (String) namesAndData[i];
    2.53 -            Object data = namesAndData[i + 1];
    2.54 -            int middle = sig.indexOf("__");
    2.55 -            if (middle == -1) {
    2.56 -                continue;
    2.57 +    static {
    2.58 +        MethodImpl.INSTANCE = new MethodImpl() {
    2.59 +            protected Method create(Class<?> declaringClass, String name, Object data, String sig) {
    2.60 +                return new Method(declaringClass, name, data, sig);
    2.61              }
    2.62 -            String name = sig.substring(0, middle);
    2.63 -            sig = sig.substring(middle + 2);
    2.64 -            namesAndData[cnt++] = new Method(clazz, name, data, sig);
    2.65 -        }
    2.66 -        Method[] arr = new Method[cnt];
    2.67 -        for (int i = 0; i < cnt; i++) {
    2.68 -            arr[i] = (Method)namesAndData[i];
    2.69 -        }
    2.70 -        return arr;
    2.71 +        };
    2.72      }
    2.73  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/emul/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java	Fri Dec 28 08:48:08 2012 +0100
     3.3 @@ -0,0 +1,97 @@
     3.4 +/*
     3.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.  Oracle designates this
    3.11 + * particular file as subject to the "Classpath" exception as provided
    3.12 + * by Oracle in the LICENSE file that accompanied this code.
    3.13 + *
    3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.17 + * version 2 for more details (a copy is included in the LICENSE file that
    3.18 + * accompanied this code).
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License version
    3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.23 + *
    3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.25 + * or visit www.oracle.com if you need additional information or have any
    3.26 + * questions.
    3.27 + */
    3.28 +package org.apidesign.bck2brwsr.emul;
    3.29 +
    3.30 +import java.lang.reflect.Method;
    3.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    3.32 +
    3.33 +/** Utilities to work on methods.
    3.34 + *
    3.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.36 + */
    3.37 +public abstract class MethodImpl {
    3.38 +    public static MethodImpl INSTANCE;
    3.39 +    static {
    3.40 +        try {
    3.41 +            Class.forName(Method.class.getName());
    3.42 +        } catch (ClassNotFoundException ex) {
    3.43 +            throw new IllegalStateException(ex);
    3.44 +        }
    3.45 +    }
    3.46 +    
    3.47 +    protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
    3.48 +    
    3.49 +    
    3.50 +    //
    3.51 +    // bck2brwsr implementation
    3.52 +    //
    3.53 +
    3.54 +    @JavaScriptBody(args = {"clazz", "prefix"},
    3.55 +        body = ""
    3.56 +        + "var c = clazz.cnstr.prototype;"
    3.57 +        + "var arr = new Array();\n"
    3.58 +        + "for (m in c) {\n"
    3.59 +        + "  if (m.indexOf(prefix) === 0) {\n"
    3.60 +        + "     arr.push(m);\n"
    3.61 +        + "     arr.push(c[m]);\n"
    3.62 +        + "  }"
    3.63 +        + "}\n"
    3.64 +        + "return arr;")
    3.65 +    private static native Object[] findMethodData(
    3.66 +        Class<?> clazz, String prefix);
    3.67 +
    3.68 +    // XXX should not be public
    3.69 +    public static Method findMethod(
    3.70 +        Class<?> clazz, String name, Class<?>... parameterTypes) {
    3.71 +        Object[] data = findMethodData(clazz, name + "__");
    3.72 +        if (data.length == 0) {
    3.73 +            return null;
    3.74 +        }
    3.75 +        String sig = ((String) data[0]).substring(name.length() + 2);
    3.76 +        return INSTANCE.create(clazz, name, data[1], sig);
    3.77 +    }
    3.78 +
    3.79 +    public static Method[] findMethods(Class<?> clazz) {
    3.80 +        Object[] namesAndData = findMethodData(clazz, "");
    3.81 +        int cnt = 0;
    3.82 +        for (int i = 0; i < namesAndData.length; i += 2) {
    3.83 +            String sig = (String) namesAndData[i];
    3.84 +            Object data = namesAndData[i + 1];
    3.85 +            int middle = sig.indexOf("__");
    3.86 +            if (middle == -1) {
    3.87 +                continue;
    3.88 +            }
    3.89 +            String name = sig.substring(0, middle);
    3.90 +            sig = sig.substring(middle + 2);
    3.91 +            namesAndData[cnt++] = INSTANCE.create(clazz, name, data, sig);
    3.92 +        }
    3.93 +        Method[] arr = new Method[cnt];
    3.94 +        for (int i = 0; i < cnt; i++) {
    3.95 +            arr[i] = (Method) namesAndData[i];
    3.96 +        }
    3.97 +        return arr;
    3.98 +    }
    3.99 +    
   3.100 +}