# HG changeset patch # User Jaroslav Tulach # Date 1359885668 -3600 # Node ID 2569d9dd4b2886be3e24a65a49f73d235b31fa7f # Parent 4b16b7e23cabd59d7472347ccc3f5a79158ba1fa Test for proper behavior of MethodImpl own methods diff -r 4b16b7e23cab -r 2569d9dd4b28 emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java Sun Feb 03 08:01:48 2013 +0100 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java Sun Feb 03 11:01:08 2013 +0100 @@ -24,6 +24,7 @@ */ package org.apidesign.bck2brwsr.emul.reflect; +import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Enumeration; import org.apidesign.bck2brwsr.core.JavaScriptBody; @@ -41,7 +42,7 @@ throw new IllegalStateException(ex); } } - + protected abstract Method create(Class declaringClass, String name, Object data, String sig); @@ -107,6 +108,62 @@ } return arr; } + static String toSignature(Method m) { + StringBuilder sb = new StringBuilder(); + sb.append(m.getName()).append("__"); + appendType(sb, m.getReturnType()); + Class[] arr = m.getParameterTypes(); + for (int i = 0; i < arr.length; i++) { + appendType(sb, arr[i]); + } + return sb.toString(); + } + + private static void appendType(StringBuilder sb, Class type) { + if (type == Integer.TYPE) { + sb.append('I'); + return; + } + if (type == Long.TYPE) { + sb.append('J'); + return; + } + if (type == Double.TYPE) { + sb.append('D'); + return; + } + if (type == Float.TYPE) { + sb.append('F'); + return; + } + if (type == Byte.TYPE) { + sb.append('B'); + return; + } + if (type == Boolean.TYPE) { + sb.append('Z'); + return; + } + if (type == Short.TYPE) { + sb.append('S'); + return; + } + if (type == Void.TYPE) { + sb.append('V'); + return; + } + if (type == Character.TYPE) { + sb.append('C'); + return; + } + if (type.isArray()) { + sb.append("_3"); + appendType(sb, type.getComponentType()); + return; + } + sb.append('L').append(type.getName().replace('.', '_')); + sb.append("_2"); + } public static int signatureElements(String sig) { Enumeration en = signatureParser(sig); @@ -148,13 +205,19 @@ return Character.TYPE; case 'L': try { - int up = sig.indexOf("_2"); - String type = sig.substring(1, up); + int up = sig.indexOf("_2", pos); + String type = sig.substring(pos, up); pos = up + 2; - return Class.forName(type); + return Class.forName(type.replace('_', '.')); } catch (ClassNotFoundException ex) { - // should not happen + throw new IllegalStateException(ex); } + case '_': { + char nch = sig.charAt(pos++); + assert nch == '3' : "Can't find '3' at " + sig.substring(pos - 1); + final Class compType = nextElement(); + return Array.newInstance(compType, 0).getClass(); + } } throw new UnsupportedOperationException(sig + " at " + pos); } diff -r 4b16b7e23cab -r 2569d9dd4b28 emul/mini/src/test/java/org/apidesign/bck2brwsr/emul/reflect/MethodImplTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/mini/src/test/java/org/apidesign/bck2brwsr/emul/reflect/MethodImplTest.java Sun Feb 03 11:01:08 2013 +0100 @@ -0,0 +1,56 @@ +/* + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.apidesign.bck2brwsr.emul.reflect; + +import java.lang.reflect.Method; +import java.util.Enumeration; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class MethodImplTest { + + public MethodImplTest() { + } + + public static String[] arr(String... arr) { + return arr; + } + + @Test + public void testSignatureForMethodWithAnArray() throws NoSuchMethodException { + Method m = MethodImplTest.class.getMethod("arr", String[].class); + String sig = MethodImpl.toSignature(m); + int sep = sig.indexOf("__"); + assert sep > 0 : "Separator found " + sig; + + Enumeration en = MethodImpl.signatureParser(sig.substring(sep + 2)); + + assert en.nextElement() == m.getReturnType() : "Return type is the same"; + assert en.nextElement() == m.getParameterTypes()[0] : "1st param type is the same"; + } +} \ No newline at end of file