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