PrivateInvokeTest.java revision 6073:cea72c2bf071
11590Srgrimes/*
21590Srgrimes * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41590Srgrimes *
51590Srgrimes * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
71590Srgrimes * published by the Free Software Foundation.  Oracle designates this
81590Srgrimes * particular file as subject to the "Classpath" exception as provided
91590Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101590Srgrimes *
111590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151590Srgrimes * accompanied this code).
161590Srgrimes *
171590Srgrimes * You should have received a copy of the GNU General Public License version
181590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201590Srgrimes *
211590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221590Srgrimes * or visit www.oracle.com if you need additional information or have any
231590Srgrimes * questions.
241590Srgrimes */
251590Srgrimes
261590Srgrimes/* @test
271590Srgrimes * @summary white-box testing of method handle sub-primitives
281590Srgrimes * @run junit test.java.lang.invoke.PrivateInvokeTest
291590Srgrimes */
301590Srgrimes
3174769Smikehpackage test.java.lang.invoke;
3288150Smikeh
3374769Smikehimport java.lang.invoke.*;
341590Srgrimesimport static java.lang.invoke.MethodHandles.*;
3599112Sobrienimport static java.lang.invoke.MethodType.*;
3699112Sobrienimport java.lang.reflect.*;
371590Srgrimesimport java.util.ArrayList;
381590Srgrimesimport java.util.Arrays;
391590Srgrimesimport java.util.logging.Level;
401590Srgrimesimport java.util.logging.Logger;
411590Srgrimesimport org.junit.*;
421590Srgrimesimport static org.junit.Assert.*;
431590Srgrimes
441590Srgrimespublic class PrivateInvokeTest {
451590Srgrimes    // Utility functions
461590Srgrimes    private static final Lookup LOOKUP = lookup();
471590Srgrimes    private static final Class<?> THIS_CLASS = PrivateInvokeTest.class;
481590Srgrimes    private static final int
491590Srgrimes            REF_NONE                    = 0,  // null value
501590Srgrimes            REF_getField                = 1,
511590Srgrimes            REF_getStatic               = 2,
52216564Scharnier            REF_putField                = 3,
531590Srgrimes            REF_putStatic               = 4,
541590Srgrimes            REF_invokeVirtual           = 5,
5577274Smikeh            REF_invokeStatic            = 6,
561590Srgrimes            REF_invokeSpecial           = 7,
571590Srgrimes            REF_newInvokeSpecial        = 8,
5874769Smikeh            REF_invokeInterface         = 9,
5977274Smikeh            REF_LIMIT                  = 10,
6074769Smikeh            REF_MH_invokeBasic         = REF_NONE;;
6177274Smikeh    private static final String[] REF_KIND_NAMES = {
6277274Smikeh        "MH::invokeBasic",
6377274Smikeh        "REF_getField", "REF_getStatic", "REF_putField", "REF_putStatic",
6477274Smikeh        "REF_invokeVirtual", "REF_invokeStatic", "REF_invokeSpecial",
6577274Smikeh        "REF_newInvokeSpecial", "REF_invokeInterface"
661590Srgrimes    };
6777274Smikeh    private int verbose;
681590Srgrimes    //{ verbose = 99; }  // for debugging
691590Srgrimes    {
701590Srgrimes        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbose");
711590Srgrimes        if (vstr == null)
721590Srgrimes            vstr = System.getProperty(THIS_CLASS.getName()+".verbose");
731590Srgrimes        if (vstr == null)
741590Srgrimes            vstr = System.getProperty("test.verbose");
75216564Scharnier        if (vstr != null)  verbose = Integer.parseInt(vstr);
761590Srgrimes    }
771590Srgrimes    private static int referenceKind(Method m) {
7877274Smikeh        if (Modifier.isStatic(m.getModifiers()))
791590Srgrimes            return REF_invokeStatic;
8077274Smikeh        else if (m.getDeclaringClass().isInterface())
8177274Smikeh            return REF_invokeInterface;
8277274Smikeh        else if (Modifier.isFinal(m.getModifiers()) ||
8377274Smikeh            Modifier.isFinal(m.getDeclaringClass().getModifiers()))
8477274Smikeh            return REF_invokeSpecial;
8577274Smikeh        else
861590Srgrimes            return REF_invokeVirtual;
871590Srgrimes    }
881590Srgrimes    private static MethodType basicType(MethodType mtype) {
891590Srgrimes        MethodType btype = mtype.erase();
901590Srgrimes        if (btype.hasPrimitives()) {
911590Srgrimes            for (int i = -1; i < mtype.parameterCount(); i++) {
921590Srgrimes                Class<?> type = (i < 0 ? mtype.returnType() : mtype.parameterType(i));
93216564Scharnier                if (type == boolean.class ||
941590Srgrimes                    type == byte.class ||
951590Srgrimes                    type == char.class ||
9674769Smikeh                    type == short.class) {
9777274Smikeh                    type = int.class;
9877274Smikeh                    if (i < 0)
991590Srgrimes                        btype = btype.changeReturnType(type);
1001590Srgrimes                    else
1011590Srgrimes                        btype = btype.changeParameterType(i, type);
10274769Smikeh                }
10377274Smikeh            }
1041590Srgrimes        }
1051590Srgrimes        return btype;
1061590Srgrimes    }
1071590Srgrimes    private static Method getMethod(Class<?> defc, String name, Class<?>... ptypes) {
10877274Smikeh        try {
1091590Srgrimes            return defc.getDeclaredMethod(name, ptypes);
1101590Srgrimes        } catch (NoSuchMethodException ex) {
11174769Smikeh        }
11274769Smikeh        try {
11374769Smikeh            return defc.getMethod(name, ptypes);
1141590Srgrimes        } catch (NoSuchMethodException ex) {
1151590Srgrimes            throw new IllegalArgumentException(ex);
1161590Srgrimes        }
1171590Srgrimes    }
1181590Srgrimes    private static MethodHandle unreflect(Method m) {
1191590Srgrimes        try {
1201590Srgrimes            MethodHandle mh = LOOKUP.unreflect(m);
1211590Srgrimes            if (Modifier.isTransient(m.getModifiers()))
1221590Srgrimes                mh = mh.asFixedArity();  // remove varargs wrapper
1231590Srgrimes            return mh;
1241590Srgrimes        } catch (IllegalAccessException ex) {
1251590Srgrimes            throw new IllegalArgumentException(ex);
1261590Srgrimes        }
1271590Srgrimes    }
1281590Srgrimes    private static final Lookup DIRECT_INVOKER_LOOKUP;
1291590Srgrimes    private static final Class<?> MEMBER_NAME_CLASS;
1301590Srgrimes    private static final MethodHandle MH_INTERNAL_MEMBER_NAME;
1311590Srgrimes    private static final MethodHandle MH_DEBUG_STRING;
1321590Srgrimes    static {
13377274Smikeh        try {
1341590Srgrimes            // This is white box testing.  Use reflection to grab private implementation bits.
13574769Smikeh            String magicName = "IMPL_LOOKUP";
13674769Smikeh            Field magicLookup = MethodHandles.Lookup.class.getDeclaredField(magicName);
13774769Smikeh            // This unit test will fail if a security manager is installed.
13874769Smikeh            magicLookup.setAccessible(true);
13977274Smikeh            // Forbidden fruit...
1401590Srgrimes            DIRECT_INVOKER_LOOKUP = (Lookup) magicLookup.get(null);
1411590Srgrimes            MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName", false, MethodHandle.class.getClassLoader());
1421590Srgrimes            MH_INTERNAL_MEMBER_NAME = DIRECT_INVOKER_LOOKUP
1431590Srgrimes                    .findVirtual(MethodHandle.class, "internalMemberName", methodType(MEMBER_NAME_CLASS))
1441590Srgrimes                    .asType(methodType(Object.class, MethodHandle.class));
1451590Srgrimes            MH_DEBUG_STRING = DIRECT_INVOKER_LOOKUP
1461590Srgrimes                    .findVirtual(MethodHandle.class, "debugString", methodType(String.class));
147216564Scharnier        } catch (ReflectiveOperationException ex) {
1481590Srgrimes            throw new Error(ex);
14977274Smikeh        }
15077274Smikeh    }
1511590Srgrimes    private Object internalMemberName(MethodHandle mh) {
1521590Srgrimes        try {
15374769Smikeh            return MH_INTERNAL_MEMBER_NAME.invokeExact(mh);
15477274Smikeh        } catch (Throwable ex) {
1551590Srgrimes            throw new Error(ex);
1561590Srgrimes        }
1571590Srgrimes    }
15877274Smikeh    private String debugString(MethodHandle mh) {
15977274Smikeh        try {
1601590Srgrimes            return (String) MH_DEBUG_STRING.invokeExact(mh);
1611590Srgrimes        } catch (Throwable ex) {
1621590Srgrimes            throw new Error(ex);
1631590Srgrimes        }
1641590Srgrimes    }
1651590Srgrimes    private static MethodHandle directInvoker(int refKind, MethodType mtype) {
166216564Scharnier        return directInvoker(REF_KIND_NAMES[refKind], mtype);
1671590Srgrimes    }
1681590Srgrimes    private static MethodHandle directInvoker(String name, MethodType mtype) {
1691590Srgrimes        boolean isStatic;
17077274Smikeh        mtype = mtype.erase();
17177274Smikeh        if (name.startsWith("MH::")) {
17277274Smikeh            isStatic = false;
1731590Srgrimes            name = strip("MH::", name);
17474769Smikeh        } else if (name.startsWith("REF_")) {
17577274Smikeh            isStatic = true;
17677274Smikeh            name = strip("REF_", name);
1771590Srgrimes            if (name.startsWith("invoke"))
17874769Smikeh                name = "linkTo"+strip("invoke", name);
17977274Smikeh            mtype = mtype.appendParameterTypes(MEMBER_NAME_CLASS);
1801590Srgrimes        } else {
18177274Smikeh            throw new AssertionError("name="+name);
1821590Srgrimes        }
1831590Srgrimes        //System.out.println("directInvoker = "+name+mtype);
1841590Srgrimes        try {
185216564Scharnier            if (isStatic)
1861590Srgrimes                return DIRECT_INVOKER_LOOKUP
18798805Smikeh                        .findStatic(MethodHandle.class, name, mtype);
18832189Sjoerg            else
1891590Srgrimes                return DIRECT_INVOKER_LOOKUP
19032189Sjoerg                        .findVirtual(MethodHandle.class, name, mtype);
1911590Srgrimes        } catch (ReflectiveOperationException ex) {
1921590Srgrimes            throw new IllegalArgumentException(ex);
1931590Srgrimes        }
1941590Srgrimes    }
1951590Srgrimes    private Object invokeWithArguments(Method m, Object... args) {
1961590Srgrimes        Object recv = null;
1971590Srgrimes        if (!Modifier.isStatic(m.getModifiers())) {
198216564Scharnier            recv = args[0];
1991590Srgrimes            args = pop(1, args);
2001590Srgrimes        }
2011590Srgrimes        try {
2021590Srgrimes            return m.invoke(recv, args);
2031590Srgrimes        } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException ex) {
2041590Srgrimes            throw new IllegalArgumentException(ex);
2051590Srgrimes        }
2061590Srgrimes    }
2071590Srgrimes    private Object invokeWithArguments(MethodHandle mh, Object... args) {
20877274Smikeh        try {
2091590Srgrimes            return mh.invokeWithArguments(args);
2101590Srgrimes        } catch (Throwable ex) {
2111590Srgrimes            throw new IllegalArgumentException(ex);
2121590Srgrimes        }
21377274Smikeh    }
2141590Srgrimes    private int counter;
21577274Smikeh    private Object makeArgument(Class<?> type) {
2161590Srgrimes        final String cname = type.getSimpleName();
21777274Smikeh        final int n = ++counter;
2181590Srgrimes        final int nn = (n << 10) + 13;
2191590Srgrimes        if (type.isAssignableFrom(String.class)) {
22077274Smikeh            return "<"+cname+"#"+nn+">";
2211590Srgrimes        }
2221590Srgrimes        if (type == THIS_CLASS)  return this.withCounter(nn);
2231590Srgrimes        if (type == Integer.class   || type == int.class)     return nn;
2241590Srgrimes        if (type == Character.class || type == char.class)    return (char)(n % 100+' ');
2251590Srgrimes        if (type == Byte.class      || type == byte.class)    return (byte)-(n % 100);
2261590Srgrimes        if (type == Long.class      || type == long.class)    return (long)nn;
2271590Srgrimes        throw new IllegalArgumentException("don't know how to make argument of type: "+type);
22877274Smikeh    }
2291590Srgrimes    private Object[] makeArguments(Class<?>... ptypes) {
23077274Smikeh        Object[] args = new Object[ptypes.length];
2311590Srgrimes        for (int i = 0; i < args.length; i++)
23277274Smikeh            args[i] = makeArgument(ptypes[i]);
23377274Smikeh        return args;
2341590Srgrimes    }
2351590Srgrimes    private Object[] makeArguments(MethodType mtype) {
2361590Srgrimes        return makeArguments(mtype.parameterArray());
2371590Srgrimes    }
23877274Smikeh    private Object[] pop(int n, Object[] args) {
2391590Srgrimes        if (n >= 0)
2401590Srgrimes            return Arrays.copyOfRange(args, n, args.length);
24177274Smikeh        else
2421590Srgrimes            return Arrays.copyOfRange(args, 0, args.length+n);
2431590Srgrimes    }
2441590Srgrimes    private Object[] pushAtFront(Object arg1, Object[] args) {
24577274Smikeh        Object[] res = new Object[1+args.length];
2461590Srgrimes        res[0] = arg1;
2471590Srgrimes        System.arraycopy(args, 0, res, 1, args.length);
2481590Srgrimes        return res;
24977274Smikeh    }
25077274Smikeh    private Object[] pushAtBack(Object[] args, Object argN) {
25177274Smikeh        Object[] res = new Object[1+args.length];
25278193Smikeh        System.arraycopy(args, 0, res, 0, args.length);
25332189Sjoerg        res[args.length] = argN;
2541590Srgrimes        return res;
25577274Smikeh    }
2561590Srgrimes    private static String strip(String prefix, String s) {
2571590Srgrimes        assert(s.startsWith(prefix));
2581590Srgrimes        return s.substring(prefix.length());
2591590Srgrimes    }
2601590Srgrimes
2611590Srgrimes    private final int[] refKindTestCounts = new int[REF_KIND_NAMES.length];
2621590Srgrimes    @After
263216564Scharnier    public void printCounts() {
2641590Srgrimes        ArrayList<String> zeroes = new ArrayList<>();
2651590Srgrimes        for (int i = 0; i < refKindTestCounts.length; i++) {
2661590Srgrimes            final int count = refKindTestCounts[i];
26777274Smikeh            final String name = REF_KIND_NAMES[i];
26877274Smikeh            if (count == 0) {
2691590Srgrimes                if (name != null)  zeroes.add(name);
2701590Srgrimes                continue;
2711590Srgrimes            }
27277274Smikeh            if (verbose >= 0)
2731590Srgrimes                System.out.println("test count for "+name+" : "+count);
27474769Smikeh            else if (name != null)
27577274Smikeh                zeroes.add(name);
2761590Srgrimes        }
2771590Srgrimes        if (verbose >= 0)
2781590Srgrimes            System.out.println("test counts zero for "+zeroes);
2791590Srgrimes    }
2801590Srgrimes
2811590Srgrimes    // Test subjects
2821590Srgrimes    public static String makeString(Object x) { return "makeString("+x+")"; }
283216564Scharnier    public static String dupString(String x) { return "("+x+"+"+x+")"; }
2841590Srgrimes    public static String intString(int x) { return "intString("+x+")"; }
28577274Smikeh    public static String byteString(byte x) { return "byteString("+x+")"; }
28677274Smikeh    public static String longString(String x, long y, String z) { return "longString("+x+y+z+")"; }
2871590Srgrimes
2881590Srgrimes    public final String toString() {
2891590Srgrimes        return "<"+getClass().getSimpleName()+"#"+counter+">";
29077274Smikeh    }
2911590Srgrimes    public final String hello() { return "hello from "+this; }
29229574Sphk    private PrivateInvokeTest withCounter(int counter) {
2931590Srgrimes        PrivateInvokeTest res = new PrivateInvokeTest();
2941590Srgrimes        res.counter = counter;
2951590Srgrimes        return res;
2961590Srgrimes    }
2971590Srgrimes
2981590Srgrimes    public static void main(String... av) throws Throwable {
29977274Smikeh        new PrivateInvokeTest().run();
3001590Srgrimes    }
3011590Srgrimes    public void run() throws Throwable {
3021590Srgrimes        testFirst();
3031590Srgrimes        testInvokeDirect();
3041590Srgrimes    }
3051590Srgrimes
306216564Scharnier    @Test
3071590Srgrimes    public void testFirst() throws Throwable {
30877274Smikeh        if (true)  return;  // nothing here
3091590Srgrimes        try {
31029574Sphk            System.out.println("start of testFirst");
3111590Srgrimes        } finally {
3121590Srgrimes            System.out.println("end of testFirst");
3131590Srgrimes        }
3141590Srgrimes    }
31577274Smikeh
3161590Srgrimes    @Test
3171590Srgrimes    public void testInvokeDirect() {
3181590Srgrimes        testInvokeDirect(getMethod(THIS_CLASS, "hello"));
3191590Srgrimes        testInvokeDirect(getMethod(Object.class, "toString"));
3201590Srgrimes        testInvokeDirect(getMethod(Comparable.class, "compareTo", Object.class));
3211590Srgrimes        testInvokeDirect(getMethod(THIS_CLASS, "makeString", Object.class));
322216564Scharnier        testInvokeDirect(getMethod(THIS_CLASS, "dupString", String.class));
3231590Srgrimes        testInvokeDirect(getMethod(THIS_CLASS, "intString", int.class));
32477274Smikeh        testInvokeDirect(getMethod(THIS_CLASS, "byteString", byte.class));
32577274Smikeh        testInvokeDirect(getMethod(THIS_CLASS, "longString", String.class, long.class, String.class));
3261590Srgrimes    }
32729574Sphk
3281590Srgrimes    void testInvokeDirect(Method m) {
3291590Srgrimes        final int refKind = referenceKind(m);
33037453Sbde        testInvokeDirect(m, refKind);
3311590Srgrimes        testInvokeDirect(m, REF_MH_invokeBasic);
33277274Smikeh    }
3331590Srgrimes    void testInvokeDirect(Method m, int refKind) {
3341590Srgrimes        if (verbose >= 1)
3351590Srgrimes            System.out.println("testInvoke m="+m+" : "+REF_KIND_NAMES[refKind]);
3361590Srgrimes        final MethodHandle mh = unreflect(m);
3371590Srgrimes        Object[] args = makeArguments(mh.type());
3381590Srgrimes        Object res1 = invokeWithArguments(m, args);
3391590Srgrimes        // res1 comes from java.lang.reflect.Method::invoke
340216564Scharnier        if (verbose >= 1)
3411590Srgrimes            System.out.println("m"+Arrays.asList(args)+" => "+res1);
3421590Srgrimes        // res2 comes from java.lang.invoke.MethodHandle::invoke
34377274Smikeh        Object res2 = invokeWithArguments(mh, args);
34488150Smikeh        assertEquals(res1, res2);
3451590Srgrimes        MethodType mtype = mh.type();
3461590Srgrimes        testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
3471590Srgrimes        MethodType etype = mtype.erase();
3481590Srgrimes        if (etype != mtype) {
3491590Srgrimes            // Try a detuned invoker.
3501590Srgrimes            testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
3511590Srgrimes        }
3521590Srgrimes        MethodType btype = basicType(mtype);
353216564Scharnier        if (btype != mtype && btype != etype) {
3541590Srgrimes            // Try a detuned invoker.
35577274Smikeh            testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
35677274Smikeh        }
3571590Srgrimes        if (false) {
3581590Srgrimes            // this can crash the JVM
3591590Srgrimes            testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
36077274Smikeh        }
3611590Srgrimes        refKindTestCounts[refKind] += 1;
36277274Smikeh    }
3631590Srgrimes
36477274Smikeh    void testInvokeVia(String kind, int refKind, MethodHandle invoker, MethodHandle mh, Object res1, Object... args) {
3651590Srgrimes        Object[] args1;
36677274Smikeh        if (refKind == REF_MH_invokeBasic)
3671590Srgrimes            args1 = pushAtFront(mh, args);
36877274Smikeh        else
3691590Srgrimes            args1 = pushAtBack(args, internalMemberName(mh));
37077274Smikeh        if (verbose >= 2) {
3711590Srgrimes            System.out.println(kind+" invoker="+invoker+" mh="+debugString(mh)+" args="+Arrays.asList(args1));
37277274Smikeh        }
3731590Srgrimes        Object res3 = invokeWithArguments(invoker, args1);
3741590Srgrimes        assertEquals(res1, res3);
37577274Smikeh    }
3761590Srgrimes}
3771590Srgrimes