FindUniqueConcreteMethodTest.java revision 11796:bf837d6d95f1
1/*
2 * Copyright (c) 2015, 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 8136421
27 * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
28 * @library / /testlibrary /test/lib
29 * @library ../common/patches
30 * @modules java.base/jdk.internal.misc
31 * @modules java.base/jdk.internal.org.objectweb.asm
32 *          java.base/jdk.internal.org.objectweb.asm.tree
33 *          jdk.vm.ci/jdk.vm.ci.hotspot
34 *          jdk.vm.ci/jdk.vm.ci.code
35 *          jdk.vm.ci/jdk.vm.ci.meta
36 *          jdk.vm.ci/jdk.vm.ci.runtime
37 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
38 * @build compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
39 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
40 *                   compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
41 */
42
43package compiler.jvmci.compilerToVM;
44
45import compiler.jvmci.common.CTVMUtilities;
46import compiler.jvmci.common.testcases.DuplicateSimpleSingleImplementerInterface;
47import compiler.jvmci.common.testcases.SimpleSingleImplementerInterface;
48import compiler.jvmci.common.testcases.MultipleImplementer1;
49import compiler.jvmci.common.testcases.MultipleSuperImplementers;
50import compiler.jvmci.common.testcases.SingleImplementer;
51import compiler.jvmci.common.testcases.SingleImplementerInterface;
52import compiler.jvmci.common.testcases.SingleSubclass;
53import jdk.test.lib.Asserts;
54import jdk.test.lib.Utils;
55import jdk.vm.ci.hotspot.CompilerToVMHelper;
56import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
57import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
58
59import java.lang.reflect.Method;
60import java.util.HashSet;
61import java.util.Set;
62
63public class FindUniqueConcreteMethodTest {
64    public static void main(String args[]) {
65        FindUniqueConcreteMethodTest test = new FindUniqueConcreteMethodTest();
66        try {
67            for (TestCase tcase : createTestCases()) {
68                test.runTest(tcase);
69            }
70        } catch (NoSuchMethodException e) {
71            throw new Error("TEST BUG: can't find method", e);
72        }
73    }
74
75    private static Set<TestCase> createTestCases() {
76        Set<TestCase> result = new HashSet<>();
77        // a public method
78        result.add(new TestCase(true, SingleSubclass.class,
79                SingleSubclass.class, "usualMethod"));
80        // overriden method
81        result.add(new TestCase(true, SingleSubclass.class,
82                SingleSubclass.class, "overridenMethod"));
83        // private method
84        result.add(new TestCase(true, SingleSubclass.class,
85                SingleSubclass.class, "privateMethod"));
86        // protected method
87        result.add(new TestCase(true, SingleSubclass.class,
88                SingleSubclass.class, "protectedMethod"));
89        // default(package-private) method
90        result.add(new TestCase(true, SingleSubclass.class,
91                SingleSubclass.class, "defaultAccessMethod"));
92        // default interface method redefined in implementer
93        result.add(new TestCase(true, MultipleImplementer1.class,
94                MultipleImplementer1.class, "defaultMethod"));
95        // interface method
96        result.add(new TestCase(true, MultipleImplementer1.class,
97                MultipleImplementer1.class, "testMethod"));
98        // default interface method not redefined in implementer
99        result.add(new TestCase(true, SingleImplementer.class,
100                SingleImplementerInterface.class, "defaultMethod"));
101        // static method
102        result.add(new TestCase(false, SingleSubclass.class,
103                SingleSubclass.class, "staticMethod"));
104        // interface method
105        result.add(new TestCase(false, MultipleSuperImplementers.class,
106                                DuplicateSimpleSingleImplementerInterface.class, "interfaceMethod", false));
107        result.add(new TestCase(false, MultipleSuperImplementers.class,
108                                SimpleSingleImplementerInterface.class, "interfaceMethod", false));
109        return result;
110    }
111
112    private void runTest(TestCase tcase) throws NoSuchMethodException {
113        System.out.println(tcase);
114        Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
115        HotSpotResolvedJavaMethod testMethod = CTVMUtilities
116            .getResolvedMethod(tcase.methodFromReceiver ? tcase.receiver : tcase.holder, method);
117        HotSpotResolvedObjectType resolvedType = CompilerToVMHelper
118                .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(),
119                /* resolve = */ true);
120        HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper
121                .findUniqueConcreteMethod(resolvedType, testMethod);
122        Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null,
123                "Unexpected concrete method for " + tcase.methodName);
124    }
125
126    private static class TestCase {
127        public final Class<?> receiver;
128        public final Class<?> holder;
129        public final String methodName;
130        public final boolean isPositive;
131        public final boolean methodFromReceiver;
132
133        public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder,
134                        String methodName, boolean methodFromReceiver) {
135            this.receiver = clazz;
136            this.methodName = methodName;
137            this.isPositive = isPositive;
138            this.holder = holder;
139            this.methodFromReceiver = methodFromReceiver;
140        }
141
142        public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder, String methodName) {
143            this(isPositive, clazz, holder, methodName, true);
144        }
145
146        @Override
147        public String toString() {
148            return String.format("CASE: receiver=%s, holder=%s, method=%s, isPositive=%s, methodFromReceiver=%s",
149                                 receiver.getName(), holder.getName(), methodName, isPositive, methodFromReceiver);
150        }
151    }
152}
153