GetImplementorTest.java revision 9111:a41fe5ffa839
1263382Smarcel/*
2263382Smarcel * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3263382Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4263382Smarcel *
5263382Smarcel * This code is free software; you can redistribute it and/or modify it
6263382Smarcel * under the terms of the GNU General Public License version 2 only, as
7263382Smarcel * published by the Free Software Foundation.
8263382Smarcel *
9263382Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10263382Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11263382Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12263382Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13263382Smarcel * accompanied this code).
14263382Smarcel *
15263382Smarcel * You should have received a copy of the GNU General Public License version
16263382Smarcel * 2 along with this work; if not, write to the Free Software Foundation,
17263382Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18263382Smarcel *
19263382Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20263382Smarcel * or visit www.oracle.com if you need additional information or have any
21263382Smarcel * questions.
22263382Smarcel */
23263382Smarcel
24263382Smarcel/*
25263382Smarcel * @test
26263382Smarcel * @bug 8136421
27263382Smarcel * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
28263382Smarcel * @library / /testlibrary /../../test/lib/
29263382Smarcel * @compile ../common/CompilerToVMHelper.java
30263382Smarcel * @build compiler.jvmci.compilerToVM.GetImplementorTest
31263382Smarcel * @run main ClassFileInstaller
32263442Smarcel *     jdk.vm.ci.hotspot.CompilerToVMHelper
33263442Smarcel * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockExperimentalVMOptions
34263653Smarcel *     -XX:+EnableJVMCI compiler.jvmci.compilerToVM.GetImplementorTest
35263653Smarcel */
36263382Smarcel
37263382Smarcelpackage compiler.jvmci.compilerToVM;
38263414Smarcel
39263382Smarcelimport compiler.jvmci.common.testcases.AbstractClass;
40263382Smarcelimport compiler.jvmci.common.testcases.AbstractClassExtender;
41263382Smarcelimport compiler.jvmci.common.testcases.DoNotImplementInterface;
42263382Smarcelimport compiler.jvmci.common.testcases.DoNotExtendClass;
43263382Smarcelimport compiler.jvmci.common.testcases.MultipleImplementer1;
44263382Smarcelimport compiler.jvmci.common.testcases.MultipleImplementer2;
45263382Smarcelimport compiler.jvmci.common.testcases.MultipleImplementersInterface;
46263461Smarcelimport compiler.jvmci.common.testcases.SingleImplementer;
47263653Smarcelimport compiler.jvmci.common.testcases.SingleImplementerInterface;
48263653Smarcelimport compiler.jvmci.common.testcases.SingleSubclass;
49263466Smarcelimport compiler.jvmci.common.testcases.SingleSubclassedClass;
50263382Smarcelimport java.util.HashSet;
51263382Smarcelimport java.util.Set;
52263382Smarcelimport java.util.stream.Stream;
53263382Smarcelimport jdk.vm.ci.hotspot.CompilerToVMHelper;
54263382Smarcelimport jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl;
55263653Smarcelimport jdk.test.lib.Asserts;
56263653Smarcelimport jdk.test.lib.Utils;
57263653Smarcel
58263653Smarcelpublic class GetImplementorTest {
59263382Smarcel    public static void main(String args[]) {
60        GetImplementorTest test = new GetImplementorTest();
61        for (TestCase tcase : createTestCases()) {
62            test.runTest(tcase);
63        }
64    }
65
66    private static Set<TestCase> createTestCases() {
67        Set<TestCase> result = new HashSet<>();
68        Stream.of(
69                    SingleSubclass.class,
70                    AbstractClassExtender.class,
71                    MultipleImplementer2.class,
72                    MultipleImplementer1.class,
73                    MultipleImplementersInterface.class,
74                    DoNotImplementInterface.class,
75                    DoNotExtendClass.class,
76                    AbstractClass.class,
77                    SingleSubclassedClass.class)
78                .forEach(Utils::ensureClassIsLoaded);
79        // an interface with single class implementing it
80        result.add(new TestCase(SingleImplementerInterface.class,
81                SingleImplementer.class));
82        /* an interface with multiple implementers. According to getImplementor
83           javadoc, an itself should be returned in case of more than one
84           implementor
85         */
86        result.add(new TestCase(MultipleImplementersInterface.class,
87                MultipleImplementersInterface.class));
88        // an interface with no implementors
89        result.add(new TestCase(DoNotImplementInterface.class, null));
90        // an abstract class with extender class
91        result.add(new TestCase(AbstractClass.class, null));
92        // a simple class, which is not extended
93        result.add(new TestCase(DoNotExtendClass.class, null));
94        // a usual class, which is extended
95        result.add(new TestCase(SingleSubclassedClass.class, null));
96        return result;
97    }
98
99    private void runTest(TestCase tcase) {
100        System.out.println(tcase);
101        HotSpotResolvedObjectTypeImpl resolvedIface = CompilerToVMHelper
102                .lookupType(Utils.toJVMTypeSignature(tcase.anInterface),
103                        getClass(), /* resolve = */ true);
104        HotSpotResolvedObjectTypeImpl resolvedImplementer = CompilerToVMHelper
105                .getImplementor(resolvedIface);
106        HotSpotResolvedObjectTypeImpl resolvedExpected = null;
107        if (tcase.expectedImplementer != null) {
108            resolvedExpected = CompilerToVMHelper.lookupType(Utils
109                    .toJVMTypeSignature(tcase.expectedImplementer),
110                    getClass(), /* resolve = */ true);
111        }
112        Asserts.assertEQ(resolvedImplementer, resolvedExpected,
113                "Unexpected implementer for " + tcase.anInterface.getName());
114    }
115
116    private static class TestCase {
117        public final Class<?> anInterface;
118        public final Class<?> expectedImplementer;
119
120        public TestCase(Class<?> iface, Class<?> expectedImplementer) {
121            this.anInterface = iface;
122            this.expectedImplementer = expectedImplementer;
123        }
124
125        @Override
126        public String toString() {
127            return String.format("CASE: interface=%s, expected=%s",
128                    anInterface.getName(),
129                    expectedImplementer == null
130                            ? null
131                            : expectedImplementer.getName());
132        }
133    }
134}
135