GetImplementorTest.java revision 11477:a709f2ee79e5
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 jdk.vm.ci/jdk.vm.ci.hotspot
32 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
33 * @build compiler.jvmci.compilerToVM.GetImplementorTest
34 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
35 *                   compiler.jvmci.compilerToVM.GetImplementorTest
36 */
37
38package compiler.jvmci.compilerToVM;
39
40import compiler.jvmci.common.testcases.AbstractClass;
41import compiler.jvmci.common.testcases.AbstractClassExtender;
42import compiler.jvmci.common.testcases.DoNotImplementInterface;
43import compiler.jvmci.common.testcases.DoNotExtendClass;
44import compiler.jvmci.common.testcases.MultipleImplementer1;
45import compiler.jvmci.common.testcases.MultipleImplementer2;
46import compiler.jvmci.common.testcases.MultipleImplementersInterface;
47import compiler.jvmci.common.testcases.SingleImplementer;
48import compiler.jvmci.common.testcases.SingleImplementerInterface;
49import compiler.jvmci.common.testcases.SingleSubclass;
50import compiler.jvmci.common.testcases.SingleSubclassedClass;
51import java.util.HashSet;
52import java.util.Set;
53import java.util.stream.Stream;
54import jdk.vm.ci.hotspot.CompilerToVMHelper;
55import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
56import jdk.test.lib.Asserts;
57import jdk.test.lib.Utils;
58
59public class GetImplementorTest {
60    public static void main(String args[]) {
61        GetImplementorTest test = new GetImplementorTest();
62        for (TestCase tcase : createTestCases()) {
63            test.runTest(tcase);
64        }
65    }
66
67    private static Set<TestCase> createTestCases() {
68        Set<TestCase> result = new HashSet<>();
69        Stream.of(
70                    SingleSubclass.class,
71                    AbstractClassExtender.class,
72                    MultipleImplementer2.class,
73                    MultipleImplementer1.class,
74                    MultipleImplementersInterface.class,
75                    DoNotImplementInterface.class,
76                    DoNotExtendClass.class,
77                    AbstractClass.class,
78                    SingleSubclassedClass.class)
79                .forEach(Utils::ensureClassIsLoaded);
80        // an interface with single class implementing it
81        result.add(new TestCase(SingleImplementerInterface.class,
82                SingleImplementer.class));
83        /* an interface with multiple implementers. According to getImplementor
84           javadoc, an itself should be returned in case of more than one
85           implementor
86         */
87        result.add(new TestCase(MultipleImplementersInterface.class,
88                MultipleImplementersInterface.class));
89        // an interface with no implementors
90        result.add(new TestCase(DoNotImplementInterface.class, null));
91        // an abstract class with extender class
92        result.add(new TestCase(AbstractClass.class, null));
93        // a simple class, which is not extended
94        result.add(new TestCase(DoNotExtendClass.class, null));
95        // a usual class, which is extended
96        result.add(new TestCase(SingleSubclassedClass.class, null));
97        return result;
98    }
99
100    private void runTest(TestCase tcase) {
101        System.out.println(tcase);
102        HotSpotResolvedObjectType resolvedIface = CompilerToVMHelper
103                .lookupType(Utils.toJVMTypeSignature(tcase.anInterface),
104                        getClass(), /* resolve = */ true);
105        HotSpotResolvedObjectType resolvedImplementer = CompilerToVMHelper
106                .getImplementor(resolvedIface);
107        HotSpotResolvedObjectType resolvedExpected = null;
108        if (tcase.expectedImplementer != null) {
109            resolvedExpected = CompilerToVMHelper.lookupType(Utils
110                    .toJVMTypeSignature(tcase.expectedImplementer),
111                    getClass(), /* resolve = */ true);
112        }
113        Asserts.assertEQ(resolvedImplementer, resolvedExpected,
114                "Unexpected implementer for " + tcase.anInterface.getName());
115    }
116
117    private static class TestCase {
118        public final Class<?> anInterface;
119        public final Class<?> expectedImplementer;
120
121        public TestCase(Class<?> iface, Class<?> expectedImplementer) {
122            this.anInterface = iface;
123            this.expectedImplementer = expectedImplementer;
124        }
125
126        @Override
127        public String toString() {
128            return String.format("CASE: interface=%s, expected=%s",
129                    anInterface.getName(),
130                    expectedImplementer == null
131                            ? null
132                            : expectedImplementer.getName());
133        }
134    }
135}
136