FindUniqueConcreteMethodTest.java revision 12772:fbb9c8026495
1/*
2 * Copyright (c) 2015, 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 * @test
26 * @bug 8136421
27 * @requires vm.jvmci
28 * @library / /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.internal.vm.ci/jdk.vm.ci.hotspot
34 *          jdk.internal.vm.ci/jdk.vm.ci.code
35 *          jdk.internal.vm.ci/jdk.vm.ci.meta
36 *          jdk.internal.vm.ci/jdk.vm.ci.runtime
37 * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
38 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
39 *                   -Djvmci.Compiler=null
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, "usualMethod"));
79        // overriden method
80        result.add(new TestCase(true, SingleSubclass.class, "overridenMethod"));
81        // private method
82        result.add(new TestCase(true, SingleSubclass.class, "privateMethod"));
83        // protected method
84        result.add(new TestCase(true, SingleSubclass.class, "protectedMethod"));
85        // default(package-private) method
86        result.add(new TestCase(true, SingleSubclass.class, "defaultAccessMethod"));
87        // default interface method redefined in implementer
88        result.add(new TestCase(true, MultipleImplementer1.class, "defaultMethod"));
89        // interface method
90        result.add(new TestCase(true, 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, "staticMethod"));
96        // interface method
97        result.add(new TestCase(false, MultipleSuperImplementers.class,
98                                DuplicateSimpleSingleImplementerInterface.class, "interfaceMethod"));
99        result.add(new TestCase(false, MultipleSuperImplementers.class,
100                                SimpleSingleImplementerInterface.class, "interfaceMethod"));
101        return result;
102    }
103
104    private void runTest(TestCase tcase) throws NoSuchMethodException {
105        System.out.println(tcase);
106        Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
107        HotSpotResolvedJavaMethod testMethod = CTVMUtilities.getResolvedMethod(method);
108
109        HotSpotResolvedObjectType resolvedType = CompilerToVMHelper
110                .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(),
111                /* resolve = */ true);
112        HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper
113                .findUniqueConcreteMethod(resolvedType, testMethod);
114        Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null,
115                "Unexpected concrete method for " + tcase.methodName);
116    }
117
118    private static class TestCase {
119        public final Class<?> receiver;
120        public final Class<?> holder;
121        public final String methodName;
122        public final boolean isPositive;
123
124        public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder,
125                        String methodName) {
126            this.receiver = clazz;
127            this.methodName = methodName;
128            this.isPositive = isPositive;
129            this.holder = holder;
130        }
131
132        public TestCase(boolean isPositive, Class<?> clazz, String methodName) {
133            this(isPositive, clazz, clazz, methodName);
134        }
135
136        @Override
137        public String toString() {
138            return String.format("CASE: receiver=%s, holder=%s, method=%s, isPositive=%s",
139                                 receiver.getName(), holder.getName(), methodName, isPositive);
140        }
141    }
142}
143