PrivateInvokeTest.java revision 5943:61af38b8d4ff
1/*
2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* @test
27 * @summary white-box testing of method handle sub-primitives
28 * @run junit test.java.lang.invoke.PrivateInvokeTest
29 */
30
31package test.java.lang.invoke;
32
33import java.lang.invoke.*;
34import static java.lang.invoke.MethodHandles.*;
35import static java.lang.invoke.MethodType.*;
36import java.lang.reflect.*;
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.logging.Level;
40import java.util.logging.Logger;
41import org.junit.*;
42import static org.junit.Assert.*;
43
44public class PrivateInvokeTest {
45    // Utility functions
46    private static final Lookup LOOKUP = lookup();
47    private static final Class<?> THIS_CLASS = PrivateInvokeTest.class;
48    private static final int
49            REF_NONE                    = 0,  // null value
50            REF_getField                = 1,
51            REF_getStatic               = 2,
52            REF_putField                = 3,
53            REF_putStatic               = 4,
54            REF_invokeVirtual           = 5,
55            REF_invokeStatic            = 6,
56            REF_invokeSpecial           = 7,
57            REF_newInvokeSpecial        = 8,
58            REF_invokeInterface         = 9,
59            REF_LIMIT                  = 10,
60            REF_MH_invokeBasic         = REF_NONE;;
61    private static final String[] REF_KIND_NAMES = {
62        "MH::invokeBasic",
63        "REF_getField", "REF_getStatic", "REF_putField", "REF_putStatic",
64        "REF_invokeVirtual", "REF_invokeStatic", "REF_invokeSpecial",
65        "REF_newInvokeSpecial", "REF_invokeInterface"
66    };
67    private int verbose;
68    //{ verbose = 99; }  // for debugging
69    {
70        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbose");
71        if (vstr == null)
72            vstr = System.getProperty(THIS_CLASS.getName()+".verbose");
73        if (vstr == null)
74            vstr = System.getProperty("test.verbose");
75        if (vstr != null)  verbose = Integer.parseInt(vstr);
76    }
77    private static int referenceKind(Method m) {
78        if (Modifier.isStatic(m.getModifiers()))
79            return REF_invokeStatic;
80        else if (m.getDeclaringClass().isInterface())
81            return REF_invokeInterface;
82        else if (Modifier.isFinal(m.getModifiers()) ||
83            Modifier.isFinal(m.getDeclaringClass().getModifiers()))
84            return REF_invokeSpecial;
85        else
86            return REF_invokeVirtual;
87    }
88    private static MethodType basicType(MethodType mtype) {
89        MethodType btype = mtype.erase();
90        if (btype.hasPrimitives()) {
91            for (int i = -1; i < mtype.parameterCount(); i++) {
92                Class<?> type = (i < 0 ? mtype.returnType() : mtype.parameterType(i));
93                if (type == boolean.class ||
94                    type == byte.class ||
95                    type == char.class ||
96                    type == short.class) {
97                    type = int.class;
98                    if (i < 0)
99                        btype = btype.changeReturnType(type);
100                    else
101                        btype = btype.changeParameterType(i, type);
102                }
103            }
104        }
105        return btype;
106    }
107    private static Method getMethod(Class<?> defc, String name, Class<?>... ptypes) {
108        try {
109            return defc.getDeclaredMethod(name, ptypes);
110        } catch (NoSuchMethodException ex) {
111        }
112        try {
113            return defc.getMethod(name, ptypes);
114        } catch (NoSuchMethodException ex) {
115            throw new IllegalArgumentException(ex);
116        }
117    }
118    private static MethodHandle unreflect(Method m) {
119        try {
120            MethodHandle mh = LOOKUP.unreflect(m);
121            if (Modifier.isTransient(m.getModifiers()))
122                mh = mh.asFixedArity();  // remove varargs wrapper
123            return mh;
124        } catch (IllegalAccessException ex) {
125            throw new IllegalArgumentException(ex);
126        }
127    }
128    private static final Lookup DIRECT_INVOKER_LOOKUP;
129    private static final Class<?> MEMBER_NAME_CLASS;
130    private static final MethodHandle MH_INTERNAL_MEMBER_NAME;
131    private static final MethodHandle MH_DEBUG_STRING;
132    static {
133        try {
134            // This is white box testing.  Use reflection to grab private implementation bits.
135            String magicName = "IMPL_LOOKUP";
136            Field magicLookup = MethodHandles.Lookup.class.getDeclaredField(magicName);
137            // This unit test will fail if a security manager is installed.
138            magicLookup.setAccessible(true);
139            // Forbidden fruit...
140            DIRECT_INVOKER_LOOKUP = (Lookup) magicLookup.get(null);
141            MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName", false, MethodHandle.class.getClassLoader());
142            MH_INTERNAL_MEMBER_NAME = DIRECT_INVOKER_LOOKUP
143                    .findVirtual(MethodHandle.class, "internalMemberName", methodType(MEMBER_NAME_CLASS))
144                    .asType(methodType(Object.class, MethodHandle.class));
145            MH_DEBUG_STRING = DIRECT_INVOKER_LOOKUP
146                    .findVirtual(MethodHandle.class, "debugString", methodType(String.class));
147        } catch (ReflectiveOperationException ex) {
148            throw new Error(ex);
149        }
150    }
151    private Object internalMemberName(MethodHandle mh) {
152        try {
153            return MH_INTERNAL_MEMBER_NAME.invokeExact(mh);
154        } catch (Throwable ex) {
155            throw new Error(ex);
156        }
157    }
158    private String debugString(MethodHandle mh) {
159        try {
160            return (String) MH_DEBUG_STRING.invokeExact(mh);
161        } catch (Throwable ex) {
162            throw new Error(ex);
163        }
164    }
165    private static MethodHandle directInvoker(int refKind, MethodType mtype) {
166        return directInvoker(REF_KIND_NAMES[refKind], mtype);
167    }
168    private static MethodHandle directInvoker(String name, MethodType mtype) {
169        boolean isStatic;
170        mtype = mtype.erase();
171        if (name.startsWith("MH::")) {
172            isStatic = false;
173            name = strip("MH::", name);
174        } else if (name.startsWith("REF_")) {
175            isStatic = true;
176            name = strip("REF_", name);
177            if (name.startsWith("invoke"))
178                name = "linkTo"+strip("invoke", name);
179            mtype = mtype.appendParameterTypes(MEMBER_NAME_CLASS);
180        } else {
181            throw new AssertionError("name="+name);
182        }
183        //System.out.println("directInvoker = "+name+mtype);
184        try {
185            if (isStatic)
186                return DIRECT_INVOKER_LOOKUP
187                        .findStatic(MethodHandle.class, name, mtype);
188            else
189                return DIRECT_INVOKER_LOOKUP
190                        .findVirtual(MethodHandle.class, name, mtype);
191        } catch (ReflectiveOperationException ex) {
192            throw new IllegalArgumentException(ex);
193        }
194    }
195    private Object invokeWithArguments(Method m, Object... args) {
196        Object recv = null;
197        if (!Modifier.isStatic(m.getModifiers())) {
198            recv = args[0];
199            args = pop(1, args);
200        }
201        try {
202            return m.invoke(recv, args);
203        } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException ex) {
204            throw new IllegalArgumentException(ex);
205        }
206    }
207    private Object invokeWithArguments(MethodHandle mh, Object... args) {
208        try {
209            return mh.invokeWithArguments(args);
210        } catch (Throwable ex) {
211            throw new IllegalArgumentException(ex);
212        }
213    }
214    private int counter;
215    private Object makeArgument(Class<?> type) {
216        final String cname = type.getSimpleName();
217        final int n = ++counter;
218        final int nn = (n << 10) + 13;
219        if (type.isAssignableFrom(String.class)) {
220            return "<"+cname+"#"+nn+">";
221        }
222        if (type == THIS_CLASS)  return this.withCounter(nn);
223        if (type == Integer.class   || type == int.class)     return nn;
224        if (type == Character.class || type == char.class)    return (char)(n % 100+' ');
225        if (type == Byte.class      || type == byte.class)    return (byte)-(n % 100);
226        if (type == Long.class      || type == long.class)    return (long)nn;
227        throw new IllegalArgumentException("don't know how to make argument of type: "+type);
228    }
229    private Object[] makeArguments(Class<?>... ptypes) {
230        Object[] args = new Object[ptypes.length];
231        for (int i = 0; i < args.length; i++)
232            args[i] = makeArgument(ptypes[i]);
233        return args;
234    }
235    private Object[] makeArguments(MethodType mtype) {
236        return makeArguments(mtype.parameterArray());
237    }
238    private Object[] pop(int n, Object[] args) {
239        if (n >= 0)
240            return Arrays.copyOfRange(args, n, args.length);
241        else
242            return Arrays.copyOfRange(args, 0, args.length+n);
243    }
244    private Object[] pushAtFront(Object arg1, Object[] args) {
245        Object[] res = new Object[1+args.length];
246        res[0] = arg1;
247        System.arraycopy(args, 0, res, 1, args.length);
248        return res;
249    }
250    private Object[] pushAtBack(Object[] args, Object argN) {
251        Object[] res = new Object[1+args.length];
252        System.arraycopy(args, 0, res, 0, args.length);
253        res[args.length] = argN;
254        return res;
255    }
256    private static String strip(String prefix, String s) {
257        assert(s.startsWith(prefix));
258        return s.substring(prefix.length());
259    }
260
261    private final int[] refKindTestCounts = new int[REF_KIND_NAMES.length];
262    @After
263    public void printCounts() {
264        ArrayList<String> zeroes = new ArrayList<>();
265        for (int i = 0; i < refKindTestCounts.length; i++) {
266            final int count = refKindTestCounts[i];
267            final String name = REF_KIND_NAMES[i];
268            if (count == 0) {
269                if (name != null)  zeroes.add(name);
270                continue;
271            }
272            if (verbose >= 0)
273                System.out.println("test count for "+name+" : "+count);
274            else if (name != null)
275                zeroes.add(name);
276        }
277        if (verbose >= 0)
278            System.out.println("test counts zero for "+zeroes);
279    }
280
281    // Test subjects
282    public static String makeString(Object x) { return "makeString("+x+")"; }
283    public static String dupString(String x) { return "("+x+"+"+x+")"; }
284    public static String intString(int x) { return "intString("+x+")"; }
285    public static String byteString(byte x) { return "byteString("+x+")"; }
286    public static String longString(String x, long y, String z) { return "longString("+x+y+z+")"; }
287
288    public final String toString() {
289        return "<"+getClass().getSimpleName()+"#"+counter+">";
290    }
291    public final String hello() { return "hello from "+this; }
292    private PrivateInvokeTest withCounter(int counter) {
293        PrivateInvokeTest res = new PrivateInvokeTest();
294        res.counter = counter;
295        return res;
296    }
297
298    public static void main(String... av) throws Throwable {
299        new PrivateInvokeTest().run();
300    }
301    public void run() throws Throwable {
302        testFirst();
303        testInvokeDirect();
304    }
305
306    @Test
307    public void testFirst() throws Throwable {
308        if (true)  return;  // nothing here
309        try {
310            System.out.println("start of testFirst");
311        } finally {
312            System.out.println("end of testFirst");
313        }
314    }
315
316    @Test
317    public void testInvokeDirect() {
318        testInvokeDirect(getMethod(THIS_CLASS, "hello"));
319        testInvokeDirect(getMethod(Object.class, "toString"));
320        testInvokeDirect(getMethod(Comparable.class, "compareTo", Object.class));
321        testInvokeDirect(getMethod(THIS_CLASS, "makeString", Object.class));
322        testInvokeDirect(getMethod(THIS_CLASS, "dupString", String.class));
323        testInvokeDirect(getMethod(THIS_CLASS, "intString", int.class));
324        testInvokeDirect(getMethod(THIS_CLASS, "byteString", byte.class));
325        testInvokeDirect(getMethod(THIS_CLASS, "longString", String.class, long.class, String.class));
326    }
327
328    void testInvokeDirect(Method m) {
329        final int refKind = referenceKind(m);
330        testInvokeDirect(m, refKind);
331        testInvokeDirect(m, REF_MH_invokeBasic);
332    }
333    void testInvokeDirect(Method m, int refKind) {
334        if (verbose >= 1)
335            System.out.println("testInvoke m="+m+" : "+REF_KIND_NAMES[refKind]);
336        final MethodHandle mh = unreflect(m);
337        Object[] args = makeArguments(mh.type());
338        Object res1 = invokeWithArguments(m, args);
339        // res1 comes from java.lang.reflect.Method::invoke
340        if (verbose >= 1)
341            System.out.println("m"+Arrays.asList(args)+" => "+res1);
342        // res2 comes from java.lang.invoke.MethodHandle::invoke
343        Object res2 = invokeWithArguments(mh, args);
344        assertEquals(res1, res2);
345        MethodType mtype = mh.type();
346        testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
347        MethodType etype = mtype.erase();
348        if (etype != mtype) {
349            // Try a detuned invoker.
350            testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
351        }
352        MethodType btype = basicType(mtype);
353        if (btype != mtype && btype != etype) {
354            // Try a detuned invoker.
355            testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
356        }
357        if (false) {
358            // this can crash the JVM
359            testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
360        }
361        refKindTestCounts[refKind] += 1;
362    }
363
364    void testInvokeVia(String kind, int refKind, MethodHandle invoker, MethodHandle mh, Object res1, Object... args) {
365        Object[] args1;
366        if (refKind == REF_MH_invokeBasic)
367            args1 = pushAtFront(mh, args);
368        else
369            args1 = pushAtBack(args, internalMemberName(mh));
370        if (verbose >= 2) {
371            System.out.println(kind+" invoker="+invoker+" mh="+debugString(mh)+" args="+Arrays.asList(args1));
372        }
373        Object res3 = invokeWithArguments(invoker, args1);
374        assertEquals(res1, res3);
375    }
376}
377