1.1 --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java Sun Feb 03 08:01:48 2013 +0100
1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java Sun Feb 03 11:01:08 2013 +0100
1.3 @@ -24,6 +24,7 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.emul.reflect;
1.6
1.7 +import java.lang.reflect.Array;
1.8 import java.lang.reflect.Method;
1.9 import java.util.Enumeration;
1.10 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.11 @@ -41,7 +42,7 @@
1.12 throw new IllegalStateException(ex);
1.13 }
1.14 }
1.15 -
1.16 +
1.17 protected abstract Method create(Class<?> declaringClass, String name, Object data, String sig);
1.18
1.19
1.20 @@ -107,6 +108,62 @@
1.21 }
1.22 return arr;
1.23 }
1.24 + static String toSignature(Method m) {
1.25 + StringBuilder sb = new StringBuilder();
1.26 + sb.append(m.getName()).append("__");
1.27 + appendType(sb, m.getReturnType());
1.28 + Class<?>[] arr = m.getParameterTypes();
1.29 + for (int i = 0; i < arr.length; i++) {
1.30 + appendType(sb, arr[i]);
1.31 + }
1.32 + return sb.toString();
1.33 + }
1.34 +
1.35 + private static void appendType(StringBuilder sb, Class<?> type) {
1.36 + if (type == Integer.TYPE) {
1.37 + sb.append('I');
1.38 + return;
1.39 + }
1.40 + if (type == Long.TYPE) {
1.41 + sb.append('J');
1.42 + return;
1.43 + }
1.44 + if (type == Double.TYPE) {
1.45 + sb.append('D');
1.46 + return;
1.47 + }
1.48 + if (type == Float.TYPE) {
1.49 + sb.append('F');
1.50 + return;
1.51 + }
1.52 + if (type == Byte.TYPE) {
1.53 + sb.append('B');
1.54 + return;
1.55 + }
1.56 + if (type == Boolean.TYPE) {
1.57 + sb.append('Z');
1.58 + return;
1.59 + }
1.60 + if (type == Short.TYPE) {
1.61 + sb.append('S');
1.62 + return;
1.63 + }
1.64 + if (type == Void.TYPE) {
1.65 + sb.append('V');
1.66 + return;
1.67 + }
1.68 + if (type == Character.TYPE) {
1.69 + sb.append('C');
1.70 + return;
1.71 + }
1.72 + if (type.isArray()) {
1.73 + sb.append("_3");
1.74 + appendType(sb, type.getComponentType());
1.75 + return;
1.76 + }
1.77 + sb.append('L').append(type.getName().replace('.', '_'));
1.78 + sb.append("_2");
1.79 + }
1.80
1.81 public static int signatureElements(String sig) {
1.82 Enumeration<Class> en = signatureParser(sig);
1.83 @@ -148,13 +205,19 @@
1.84 return Character.TYPE;
1.85 case 'L':
1.86 try {
1.87 - int up = sig.indexOf("_2");
1.88 - String type = sig.substring(1, up);
1.89 + int up = sig.indexOf("_2", pos);
1.90 + String type = sig.substring(pos, up);
1.91 pos = up + 2;
1.92 - return Class.forName(type);
1.93 + return Class.forName(type.replace('_', '.'));
1.94 } catch (ClassNotFoundException ex) {
1.95 - // should not happen
1.96 + throw new IllegalStateException(ex);
1.97 }
1.98 + case '_': {
1.99 + char nch = sig.charAt(pos++);
1.100 + assert nch == '3' : "Can't find '3' at " + sig.substring(pos - 1);
1.101 + final Class compType = nextElement();
1.102 + return Array.newInstance(compType, 0).getClass();
1.103 + }
1.104 }
1.105 throw new UnsupportedOperationException(sig + " at " + pos);
1.106 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/emul/mini/src/test/java/org/apidesign/bck2brwsr/emul/reflect/MethodImplTest.java Sun Feb 03 11:01:08 2013 +0100
2.3 @@ -0,0 +1,56 @@
2.4 +/*
2.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2.7 + *
2.8 + * This code is free software; you can redistribute it and/or modify it
2.9 + * under the terms of the GNU General Public License version 2 only, as
2.10 + * published by the Free Software Foundation. Oracle designates this
2.11 + * particular file as subject to the "Classpath" exception as provided
2.12 + * by Oracle in the LICENSE file that accompanied this code.
2.13 + *
2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2.17 + * version 2 for more details (a copy is included in the LICENSE file that
2.18 + * accompanied this code).
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License version
2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2.23 + *
2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2.25 + * or visit www.oracle.com if you need additional information or have any
2.26 + * questions.
2.27 + */
2.28 +package org.apidesign.bck2brwsr.emul.reflect;
2.29 +
2.30 +import java.lang.reflect.Method;
2.31 +import java.util.Enumeration;
2.32 +import org.testng.annotations.Test;
2.33 +
2.34 +/**
2.35 + *
2.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.37 + */
2.38 +public class MethodImplTest {
2.39 +
2.40 + public MethodImplTest() {
2.41 + }
2.42 +
2.43 + public static String[] arr(String... arr) {
2.44 + return arr;
2.45 + }
2.46 +
2.47 + @Test
2.48 + public void testSignatureForMethodWithAnArray() throws NoSuchMethodException {
2.49 + Method m = MethodImplTest.class.getMethod("arr", String[].class);
2.50 + String sig = MethodImpl.toSignature(m);
2.51 + int sep = sig.indexOf("__");
2.52 + assert sep > 0 : "Separator found " + sig;
2.53 +
2.54 + Enumeration<Class> en = MethodImpl.signatureParser(sig.substring(sep + 2));
2.55 +
2.56 + assert en.nextElement() == m.getReturnType() : "Return type is the same";
2.57 + assert en.nextElement() == m.getParameterTypes()[0] : "1st param type is the same";
2.58 + }
2.59 +}
2.60 \ No newline at end of file