PrivateInvokeTest.java revision 5688:050116960e99
119370Spst/*
219370Spst * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
398944Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130803Smarcel *
598944Sobrien * This code is free software; you can redistribute it and/or modify it
619370Spst * under the terms of the GNU General Public License version 2 only, as
798944Sobrien * published by the Free Software Foundation.  Oracle designates this
819370Spst * particular file as subject to the "Classpath" exception as provided
998944Sobrien * by Oracle in the LICENSE file that accompanied this code.
1098944Sobrien *
1198944Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1298944Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1319370Spst * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1498944Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1598944Sobrien * accompanied this code).
1698944Sobrien *
1798944Sobrien * You should have received a copy of the GNU General Public License version
1819370Spst * 2 along with this work; if not, write to the Free Software Foundation,
1998944Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2098944Sobrien *
2198944Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2298944Sobrien * or visit www.oracle.com if you need additional information or have any
2398944Sobrien * questions.
2419370Spst */
2519370Spst
2619370Spst/* @test
2719370Spst * @summary white-box testing of method handle sub-primitives
2819370Spst * @run junit test.java.lang.invoke.PrivateInvokeTest
2919370Spst */
3019370Spst
3119370Spstpackage test.java.lang.invoke;
3219370Spst
3319370Spstimport java.lang.invoke.*;
3419370Spstimport static java.lang.invoke.MethodHandles.*;
3519370Spstimport static java.lang.invoke.MethodType.*;
3619370Spstimport java.lang.reflect.*;
3719370Spstimport java.util.ArrayList;
3819370Spstimport java.util.Arrays;
3998944Sobrienimport java.util.logging.Level;
4098944Sobrienimport java.util.logging.Logger;
4198944Sobrienimport org.junit.*;
4298944Sobrienimport static org.junit.Assert.*;
43130803Smarcel
44130803Smarcelpublic class PrivateInvokeTest {
45130803Smarcel    // Utility functions
4619370Spst    private static final Lookup LOOKUP = lookup();
47130803Smarcel    private static final Class<?> THIS_CLASS = PrivateInvokeTest.class;
48130803Smarcel    private static final int
49130803Smarcel            REF_NONE                    = 0,  // null value
50130803Smarcel            REF_getField                = 1,
5119370Spst            REF_getStatic               = 2,
5219370Spst            REF_putField                = 3,
5319370Spst            REF_putStatic               = 4,
5419370Spst            REF_invokeVirtual           = 5,
5598944Sobrien            REF_invokeStatic            = 6,
5698944Sobrien            REF_invokeSpecial           = 7,
5798944Sobrien            REF_newInvokeSpecial        = 8,
5898944Sobrien            REF_invokeInterface         = 9,
5998944Sobrien            REF_LIMIT                  = 10,
6019370Spst            REF_MH_invokeBasic         = REF_NONE;;
6119370Spst    private static final String[] REF_KIND_NAMES = {
6219370Spst        "MH::invokeBasic",
6319370Spst        "REF_getField", "REF_getStatic", "REF_putField", "REF_putStatic",
6419370Spst        "REF_invokeVirtual", "REF_invokeStatic", "REF_invokeSpecial",
6519370Spst        "REF_newInvokeSpecial", "REF_invokeInterface"
6619370Spst    };
6719370Spst    private int verbose;
6819370Spst    //{ verbose = 99; }  // for debugging
6919370Spst    {
7019370Spst        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbose");
7119370Spst        if (vstr == null)
7219370Spst            vstr = System.getProperty(THIS_CLASS.getName()+".verbose");
7346283Sdfr        if (vstr == null)
7446283Sdfr            vstr = System.getProperty("test.verbose");
7546283Sdfr        if (vstr != null)  verbose = Integer.parseInt(vstr);
7646283Sdfr    }
7719370Spst    private static int referenceKind(Method m) {
7819370Spst        if (Modifier.isStatic(m.getModifiers()))
7919370Spst            return REF_invokeStatic;
8019370Spst        else if (m.getDeclaringClass().isInterface())
8119370Spst            return REF_invokeInterface;
8219370Spst        else if (Modifier.isFinal(m.getModifiers()) ||
8319370Spst            Modifier.isFinal(m.getDeclaringClass().getModifiers()))
8498944Sobrien            return REF_invokeSpecial;
8519370Spst        else
8619370Spst            return REF_invokeVirtual;
8719370Spst    }
8819370Spst    private static MethodType basicType(MethodType mtype) {
8919370Spst        MethodType btype = mtype.erase();
9019370Spst        if (btype.hasPrimitives()) {
9119370Spst            for (int i = -1; i < mtype.parameterCount(); i++) {
9219370Spst                Class<?> type = (i < 0 ? mtype.returnType() : mtype.parameterType(i));
9319370Spst                if (type == boolean.class ||
9419370Spst                    type == byte.class ||
9519370Spst                    type == char.class ||
9619370Spst                    type == short.class) {
9719370Spst                    type = int.class;
9819370Spst                    if (i < 0)
9919370Spst                        btype = btype.changeReturnType(type);
10019370Spst                    else
10119370Spst                        btype = btype.changeParameterType(i, type);
10219370Spst                }
10319370Spst            }
10419370Spst        }
10519370Spst        return btype;
10619370Spst    }
10719370Spst    private static Method getMethod(Class<?> defc, String name, Class<?>... ptypes) {
10819370Spst        try {
10998944Sobrien            return defc.getDeclaredMethod(name, ptypes);
11098944Sobrien        } catch (NoSuchMethodException ex) {
11198944Sobrien        }
11298944Sobrien        try {
11398944Sobrien            return defc.getMethod(name, ptypes);
11498944Sobrien        } catch (NoSuchMethodException ex) {
11598944Sobrien            throw new IllegalArgumentException(ex);
11698944Sobrien        }
11798944Sobrien    }
11898944Sobrien    private static MethodHandle unreflect(Method m) {
11998944Sobrien        try {
12098944Sobrien            MethodHandle mh = LOOKUP.unreflect(m);
12198944Sobrien            if (Modifier.isTransient(m.getModifiers()))
12298944Sobrien                mh = mh.asFixedArity();  // remove varargs wrapper
12319370Spst            return mh;
12419370Spst        } catch (IllegalAccessException ex) {
12519370Spst            throw new IllegalArgumentException(ex);
12619370Spst        }
12719370Spst    }
12819370Spst    private static final Lookup DIRECT_INVOKER_LOOKUP;
12919370Spst    private static final Class<?> MEMBER_NAME_CLASS;
13019370Spst    private static final MethodHandle MH_INTERNAL_MEMBER_NAME;
13146283Sdfr    private static final MethodHandle MH_DEBUG_STRING;
13219370Spst    static {
13398944Sobrien        try {
13419370Spst            // This is white box testing.  Use reflection to grab private implementation bits.
13598944Sobrien            String magicName = "IMPL_LOOKUP";
13619370Spst            Field magicLookup = MethodHandles.Lookup.class.getDeclaredField(magicName);
13746283Sdfr            // This unit test will fail if a security manager is installed.
13846283Sdfr            magicLookup.setAccessible(true);
13998944Sobrien            // Forbidden fruit...
14019370Spst            DIRECT_INVOKER_LOOKUP = (Lookup) magicLookup.get(null);
14198944Sobrien            MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName", false, MethodHandle.class.getClassLoader());
14219370Spst            MH_INTERNAL_MEMBER_NAME = DIRECT_INVOKER_LOOKUP
14398944Sobrien                    .findVirtual(MethodHandle.class, "internalMemberName", methodType(MEMBER_NAME_CLASS))
14419370Spst                    .asType(methodType(Object.class, MethodHandle.class));
14598944Sobrien            MH_DEBUG_STRING = DIRECT_INVOKER_LOOKUP
14619370Spst                    .findVirtual(MethodHandle.class, "debugString", methodType(String.class));
14798944Sobrien        } catch (ReflectiveOperationException ex) {
14819370Spst            throw new InternalError(ex);
14998944Sobrien        }
15019370Spst    }
15198944Sobrien    private Object internalMemberName(MethodHandle mh) {
15219370Spst        try {
15398944Sobrien            return MH_INTERNAL_MEMBER_NAME.invokeExact(mh);
15419370Spst        } catch (Throwable ex) {
15598944Sobrien            throw new InternalError(ex);
15619370Spst        }
15798944Sobrien    }
15819370Spst    private String debugString(MethodHandle mh) {
15998944Sobrien        try {
16019370Spst            return (String) MH_DEBUG_STRING.invokeExact(mh);
16198944Sobrien        } catch (Throwable ex) {
16219370Spst            throw new InternalError(ex);
16398944Sobrien        }
16419370Spst    }
16598944Sobrien    private static MethodHandle directInvoker(int refKind, MethodType mtype) {
16619370Spst        return directInvoker(REF_KIND_NAMES[refKind], mtype);
16798944Sobrien    }
16819370Spst    private static MethodHandle directInvoker(String name, MethodType mtype) {
16998944Sobrien        boolean isStatic;
17019370Spst        mtype = mtype.erase();
17198944Sobrien        if (name.startsWith("MH::")) {
17219370Spst            isStatic = false;
17398944Sobrien            name = strip("MH::", name);
17498944Sobrien        } else if (name.startsWith("REF_")) {
17519370Spst            isStatic = true;
17698944Sobrien            name = strip("REF_", name);
17719370Spst            if (name.startsWith("invoke"))
17898944Sobrien                name = "linkTo"+strip("invoke", name);
17919370Spst            mtype = mtype.appendParameterTypes(MEMBER_NAME_CLASS);
18098944Sobrien        } else {
18198944Sobrien            throw new AssertionError("name="+name);
18246283Sdfr        }
18319370Spst        //System.out.println("directInvoker = "+name+mtype);
18419370Spst        try {
18519370Spst            if (isStatic)
18619370Spst                return DIRECT_INVOKER_LOOKUP
18719370Spst                        .findStatic(MethodHandle.class, name, mtype);
18819370Spst            else
18919370Spst                return DIRECT_INVOKER_LOOKUP
19019370Spst                        .findVirtual(MethodHandle.class, name, mtype);
19119370Spst        } catch (ReflectiveOperationException ex) {
19219370Spst            throw new IllegalArgumentException(ex);
19319370Spst        }
19498944Sobrien    }
19519370Spst    private Object invokeWithArguments(Method m, Object... args) {
19619370Spst        Object recv = null;
197130803Smarcel        if (!Modifier.isStatic(m.getModifiers())) {
19819370Spst            recv = args[0];
19919370Spst            args = pop(1, args);
20019370Spst        }
20119370Spst        try {
20219370Spst            return m.invoke(recv, args);
20319370Spst        } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException ex) {
20419370Spst            throw new IllegalArgumentException(ex);
20598944Sobrien        }
20698944Sobrien    }
20719370Spst    private Object invokeWithArguments(MethodHandle mh, Object... args) {
20819370Spst        try {
20919370Spst            return mh.invokeWithArguments(args);
21019370Spst        } catch (Throwable ex) {
21119370Spst            throw new IllegalArgumentException(ex);
21219370Spst        }
21319370Spst    }
21419370Spst    private int counter;
21519370Spst    private Object makeArgument(Class<?> type) {
21619370Spst        final String cname = type.getSimpleName();
21719370Spst        final int n = ++counter;
21819370Spst        final int nn = (n << 10) + 13;
21919370Spst        if (type.isAssignableFrom(String.class)) {
22098944Sobrien            return "<"+cname+"#"+nn+">";
22198944Sobrien        }
22219370Spst        if (type == THIS_CLASS)  return this.withCounter(nn);
22319370Spst        if (type == Integer.class   || type == int.class)     return nn;
22419370Spst        if (type == Character.class || type == char.class)    return (char)(n % 100+' ');
22519370Spst        if (type == Byte.class      || type == byte.class)    return (byte)-(n % 100);
22619370Spst        if (type == Long.class      || type == long.class)    return (long)nn;
22719370Spst        throw new IllegalArgumentException("don't know how to make argument of type: "+type);
22819370Spst    }
22919370Spst    private Object[] makeArguments(Class<?>... ptypes) {
23019370Spst        Object[] args = new Object[ptypes.length];
23119370Spst        for (int i = 0; i < args.length; i++)
23219370Spst            args[i] = makeArgument(ptypes[i]);
23319370Spst        return args;
23419370Spst    }
23519370Spst    private Object[] makeArguments(MethodType mtype) {
23619370Spst        return makeArguments(mtype.parameterArray());
23719370Spst    }
23819370Spst    private Object[] pop(int n, Object[] args) {
23919370Spst        if (n >= 0)
24019370Spst            return Arrays.copyOfRange(args, n, args.length);
24119370Spst        else
24219370Spst            return Arrays.copyOfRange(args, 0, args.length+n);
24319370Spst    }
24419370Spst    private Object[] pushAtFront(Object arg1, Object[] args) {
24519370Spst        Object[] res = new Object[1+args.length];
24619370Spst        res[0] = arg1;
24719370Spst        System.arraycopy(args, 0, res, 1, args.length);
24819370Spst        return res;
24919370Spst    }
25019370Spst    private Object[] pushAtBack(Object[] args, Object argN) {
25119370Spst        Object[] res = new Object[1+args.length];
25298944Sobrien        System.arraycopy(args, 0, res, 0, args.length);
25319370Spst        res[args.length] = argN;
25419370Spst        return res;
25519370Spst    }
25619370Spst    private static String strip(String prefix, String s) {
25719370Spst        assert(s.startsWith(prefix));
25819370Spst        return s.substring(prefix.length());
25919370Spst    }
26019370Spst
26119370Spst    private final int[] refKindTestCounts = new int[REF_KIND_NAMES.length];
26219370Spst    @After
26319370Spst    public void printCounts() {
26419370Spst        ArrayList<String> zeroes = new ArrayList<>();
26519370Spst        for (int i = 0; i < refKindTestCounts.length; i++) {
26619370Spst            final int count = refKindTestCounts[i];
26719370Spst            final String name = REF_KIND_NAMES[i];
26819370Spst            if (count == 0) {
26919370Spst                if (name != null)  zeroes.add(name);
27019370Spst                continue;
27119370Spst            }
27219370Spst            if (verbose >= 0)
27319370Spst                System.out.println("test count for "+name+" : "+count);
27419370Spst            else if (name != null)
27598944Sobrien                zeroes.add(name);
27619370Spst        }
27719370Spst        if (verbose >= 0)
27819370Spst            System.out.println("test counts zero for "+zeroes);
27919370Spst    }
28019370Spst
28119370Spst    // Test subjects
282130803Smarcel    public static String makeString(Object x) { return "makeString("+x+")"; }
28398944Sobrien    public static String dupString(String x) { return "("+x+"+"+x+")"; }
28419370Spst    public static String intString(int x) { return "intString("+x+")"; }
28519370Spst    public static String byteString(byte x) { return "byteString("+x+")"; }
28619370Spst    public static String longString(String x, long y, String z) { return "longString("+x+y+z+")"; }
28719370Spst
28819370Spst    public final String toString() {
28946283Sdfr        return "<"+getClass().getSimpleName()+"#"+counter+">";
29046283Sdfr    }
29146283Sdfr    public final String hello() { return "hello from "+this; }
29246283Sdfr    private PrivateInvokeTest withCounter(int counter) {
29319370Spst        PrivateInvokeTest res = new PrivateInvokeTest();
29419370Spst        res.counter = counter;
29519370Spst        return res;
29619370Spst    }
29746283Sdfr
29819370Spst    public static void main(String... av) throws Throwable {
29998944Sobrien        new PrivateInvokeTest().run();
30046283Sdfr    }
30119370Spst    public void run() throws Throwable {
30219370Spst        testFirst();
30319370Spst        testInvokeDirect();
30419370Spst    }
30598944Sobrien
30698944Sobrien    @Test
30798944Sobrien    public void testFirst() throws Throwable {
30898944Sobrien        if (true)  return;  // nothing here
30946283Sdfr        try {
31019370Spst            System.out.println("start of testFirst");
31119370Spst        } finally {
31219370Spst            System.out.println("end of testFirst");
313130803Smarcel        }
31446283Sdfr    }
31519370Spst
31619370Spst    @Test
31719370Spst    public void testInvokeDirect() {
31819370Spst        testInvokeDirect(getMethod(THIS_CLASS, "hello"));
31919370Spst        testInvokeDirect(getMethod(Object.class, "toString"));
32019370Spst        testInvokeDirect(getMethod(Comparable.class, "compareTo", Object.class));
32119370Spst        testInvokeDirect(getMethod(THIS_CLASS, "makeString", Object.class));
322130803Smarcel        testInvokeDirect(getMethod(THIS_CLASS, "dupString", String.class));
323130803Smarcel        testInvokeDirect(getMethod(THIS_CLASS, "intString", int.class));
32498944Sobrien        testInvokeDirect(getMethod(THIS_CLASS, "byteString", byte.class));
32598944Sobrien        testInvokeDirect(getMethod(THIS_CLASS, "longString", String.class, long.class, String.class));
32698944Sobrien    }
32798944Sobrien
32898944Sobrien    void testInvokeDirect(Method m) {
32998944Sobrien        final int refKind = referenceKind(m);
33019370Spst        testInvokeDirect(m, refKind);
33198944Sobrien        testInvokeDirect(m, REF_MH_invokeBasic);
33298944Sobrien    }
33398944Sobrien    void testInvokeDirect(Method m, int refKind) {
33419370Spst        if (verbose >= 1)
33598944Sobrien            System.out.println("testInvoke m="+m+" : "+REF_KIND_NAMES[refKind]);
33619370Spst        final MethodHandle mh = unreflect(m);
33719370Spst        Object[] args = makeArguments(mh.type());
33819370Spst        Object res1 = invokeWithArguments(m, args);
33919370Spst        // res1 comes from java.lang.reflect.Method::invoke
34019370Spst        if (verbose >= 1)
34119370Spst            System.out.println("m"+Arrays.asList(args)+" => "+res1);
34219370Spst        // res2 comes from java.lang.invoke.MethodHandle::invoke
34319370Spst        Object res2 = invokeWithArguments(mh, args);
34419370Spst        assertEquals(res1, res2);
34519370Spst        MethodType mtype = mh.type();
34619370Spst        testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
347130803Smarcel        MethodType etype = mtype.erase();
34898944Sobrien        if (etype != mtype) {
34919370Spst            // Try a detuned invoker.
350130803Smarcel            testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
35119370Spst        }
35219370Spst        MethodType btype = basicType(mtype);
353130803Smarcel        if (btype != mtype && btype != etype) {
354130803Smarcel            // Try a detuned invoker.
355130803Smarcel            testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
35619370Spst        }
357130803Smarcel        if (false) {
35819370Spst            // this can crash the JVM
359130803Smarcel            testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
360130803Smarcel        }
36119370Spst        refKindTestCounts[refKind] += 1;
362130803Smarcel    }
363130803Smarcel
364130803Smarcel    void testInvokeVia(String kind, int refKind, MethodHandle invoker, MethodHandle mh, Object res1, Object... args) {
365130803Smarcel        Object[] args1;
366130803Smarcel        if (refKind == REF_MH_invokeBasic)
367130803Smarcel            args1 = pushAtFront(mh, args);
368130803Smarcel        else
369130803Smarcel            args1 = pushAtBack(args, internalMemberName(mh));
370130803Smarcel        if (verbose >= 2) {
371130803Smarcel            System.out.println(kind+" invoker="+invoker+" mh="+debugString(mh)+" args="+Arrays.asList(args1));
372130803Smarcel        }
373130803Smarcel        Object res3 = invokeWithArguments(invoker, args1);
374130803Smarcel        assertEquals(res1, res3);
375130803Smarcel    }
376130803Smarcel}
377130803Smarcel