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