1/*
2 * Copyright (c) 2013, 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 * @test
26 * @bug 8015436
27 * @summary the IK _initial_method_idnum value must be adjusted if overpass methods are added
28 * @library /test/lib /
29 * @modules java.base/jdk.internal.misc
30 * @build compiler.runtime.cr8015436.Test8015436
31 *
32 * @run driver compiler.runtime.cr8015436.Driver8015436
33 */
34
35/*
36 * The test checks that a MemberName for the defaultMethod() is cached in
37 * the class MemberNameTable without a crash in the VM fastdebug mode.
38 * The original issue was that the InstanceKlass _initial_method_idnum was
39 * not adjusted properly when the overpass methods are added to the class.
40 * The expected/correct behavior: The test does not crash nor throw any exceptions.
41 * All the invocations of the defaultMethod() must be completed successfully.
42 */
43
44package compiler.runtime.cr8015436;
45
46import java.lang.invoke.MethodHandle;
47import java.lang.invoke.MethodHandles;
48import java.lang.invoke.MethodType;
49
50public class Test8015436 implements InterfaceWithDefaultMethod {
51    public static final String SOME_MTD_INVOKED = "someMethod() invoked";
52    public static final String DEFAULT_MTD_INVOKED_DIRECTLY = "defaultMethod() invoked directly";
53    public static final String DEFAULT_MTD_INVOKED_MH = "defaultMethod() invoked via a MethodHandle";
54
55    @Override
56    public void someMethod() {
57        System.out.println(SOME_MTD_INVOKED);
58    }
59
60    public static void main(String[] args) throws Throwable {
61        Test8015436 testObj = new Test8015436();
62        testObj.someMethod();
63        testObj.defaultMethod(DEFAULT_MTD_INVOKED_DIRECTLY);
64
65        MethodHandles.Lookup lookup = MethodHandles.lookup();
66        MethodType   mt = MethodType.methodType(void.class, String.class);
67        MethodHandle mh = lookup.findVirtual(Test8015436.class, "defaultMethod", mt);
68        mh.invokeExact(testObj, DEFAULT_MTD_INVOKED_MH);
69    }
70}
71
72interface InterfaceWithDefaultMethod {
73    public void someMethod();
74
75    default public void defaultMethod(String str){
76        System.out.println(str);
77    }
78}
79/*
80 * A successful execution gives the output:
81 *   someMethod() invoked
82 *   defaultMethod() invoked directly
83 *   defaultMethod() invoked via a MethodHandle
84 */
85