LongReferenceCastingTest.java revision 11707:ad7af1afda7a
1/*
2 * Copyright (c) 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
25/**
26 * @test
27 * @bug 8148752
28 * @summary Test correct casting of MH arguments during inlining.
29 *
30 * @run main compiler.jsr292.LongReferenceCastingTest
31 */
32
33package compiler.jsr292;
34
35import java.lang.invoke.MethodHandle;
36import java.lang.invoke.MethodHandles;
37import java.lang.invoke.MethodType;
38
39public class LongReferenceCastingTest {
40    static final String MY_STRING = "myString";
41    static final MethodHandle MH;
42
43    static {
44        try {
45            MethodHandles.Lookup lookup = MethodHandles.lookup();
46            MethodType mt = MethodType.methodType(String.class, long.class, Object.class, String.class);
47            MH = lookup.findVirtual(LongReferenceCastingTest.class, "myMethod", mt);
48        } catch (Exception e) {
49            throw new Error(e);
50        }
51    }
52
53    public String myMethod(long l, Object o, String s) {
54        // The long argument occupies two stack slots, causing C2 to treat it as
55        // two arguments and casting the fist one two long and the second one to Object.
56        // As a result, Object o is casted to String and the o.toString() call is
57        // inlined as String::toString(). We fail at runtime because 'o' is not a String.
58        return o.toString();
59    }
60
61    public String toString() {
62        return MY_STRING;
63    }
64
65    public static void main(String[] args) throws Exception {
66        LongReferenceCastingTest test = new LongReferenceCastingTest();
67        try {
68            for (int i = 0; i < 20_000; ++i) {
69                if (!test.invoke().equals(MY_STRING)) {
70                    throw new RuntimeException("Invalid string");
71                }
72            }
73        } catch (Throwable t) {
74            throw new RuntimeException("Test failed", t);
75        }
76    }
77
78    public String invoke() throws Throwable {
79        return (String) MH.invokeExact(this, 0L, (Object)this, MY_STRING);
80    }
81}
82