GetClassInitializerTest.java revision 12365:d1a00bd8dcca
138032Speter/*
238032Speter * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
338032Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
438032Speter *
538032Speter * This code is free software; you can redistribute it and/or modify it
638032Speter * under the terms of the GNU General Public License version 2 only, as
738032Speter * published by the Free Software Foundation.
8285229Sgshapiro *
9285229Sgshapiro * This code is distributed in the hope that it will be useful, but WITHOUT
10285229Sgshapiro * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11285229Sgshapiro * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12285229Sgshapiro * version 2 for more details (a copy is included in the LICENSE file that
13285229Sgshapiro * accompanied this code).
14285229Sgshapiro *
15285229Sgshapiro * You should have received a copy of the GNU General Public License version
16285229Sgshapiro * 2 along with this work; if not, write to the Free Software Foundation,
17285229Sgshapiro * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18285229Sgshapiro *
19285229Sgshapiro * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20285229Sgshapiro * or visit www.oracle.com if you need additional information or have any
21285229Sgshapiro * questions.
22285229Sgshapiro */
23285229Sgshapiro
24285229Sgshapiro/*
25285229Sgshapiro * @test
26285229Sgshapiro * @bug 8136421
27285229Sgshapiro * @requires vm.jvmci
28285229Sgshapiro * @library / /test/lib
29285229Sgshapiro * @library ../common/patches
30285229Sgshapiro * @modules java.base/jdk.internal.misc
31285229Sgshapiro * @modules jdk.vm.ci/jdk.vm.ci.hotspot
32285229Sgshapiro * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
33285229Sgshapiro * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
34285229Sgshapiro *                   compiler.jvmci.compilerToVM.GetClassInitializerTest
35285229Sgshapiro */
36285229Sgshapiro
37285229Sgshapiropackage compiler.jvmci.compilerToVM;
38285229Sgshapiro
39285229Sgshapiroimport compiler.jvmci.common.testcases.AbstractClass;
40285229Sgshapiroimport compiler.jvmci.common.testcases.AbstractClassExtender;
41285229Sgshapiroimport compiler.jvmci.common.testcases.DoNotExtendClass;
42285229Sgshapiroimport compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender;
43285229Sgshapiroimport compiler.jvmci.common.testcases.SingleImplementer;
44285229Sgshapiroimport compiler.jvmci.common.testcases.SingleImplementerInterface;
45285229Sgshapiroimport jdk.test.lib.Asserts;
46285229Sgshapiroimport jdk.test.lib.Utils;
47285229Sgshapiroimport jdk.vm.ci.hotspot.CompilerToVMHelper;
48285229Sgshapiroimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
49285229Sgshapiroimport jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
50285229Sgshapiro
51285229Sgshapiroimport java.util.HashSet;
52285229Sgshapiroimport java.util.Set;
53285229Sgshapiro
54285229Sgshapiropublic class GetClassInitializerTest {
55285229Sgshapiro
56285229Sgshapiro    public static void main(String args[]) {
57285229Sgshapiro        GetClassInitializerTest test = new GetClassInitializerTest();
58285229Sgshapiro        for (TestCase tcase : createTestCases()) {
59285229Sgshapiro            test.runTest(tcase);
60285229Sgshapiro        }
61285229Sgshapiro    }
62285229Sgshapiro
63285229Sgshapiro    private static Set<TestCase> createTestCases() {
64285229Sgshapiro        Set<TestCase> result = new HashSet<>();
65285229Sgshapiro        // a simple class with initializer
66285229Sgshapiro        result.add(new TestCase(SingleImplementer.class, true));
67285229Sgshapiro        // an interface with initializer
68285229Sgshapiro        result.add(new TestCase(SingleImplementerInterface.class, true));
69285229Sgshapiro        // an abstract class with initializer
70285229Sgshapiro        result.add(new TestCase(AbstractClass.class, true));
71285229Sgshapiro        // a class without initializer, extending class with initializer
72285229Sgshapiro        result.add(new TestCase(AbstractClassExtender.class, false));
73285229Sgshapiro        // an interface without initializer
74285229Sgshapiro        result.add(new TestCase(MultipleImplementersInterfaceExtender.class, false));
75285229Sgshapiro        // a class without initializer
76285229Sgshapiro        result.add(new TestCase(DoNotExtendClass.class, false));
77285229Sgshapiro        return result;
78285229Sgshapiro    }
79285229Sgshapiro
80285229Sgshapiro    private void runTest(TestCase tcase) {
81285229Sgshapiro        System.out.println(tcase);
82285229Sgshapiro        String className = tcase.holder.getName();
83285229Sgshapiro        HotSpotResolvedObjectType resolvedClazz = CompilerToVMHelper
84285229Sgshapiro                .lookupType(Utils.toJVMTypeSignature(tcase.holder),
85285229Sgshapiro                        getClass(), /* resolve = */ true);
86285229Sgshapiro        HotSpotResolvedJavaMethod initializer = CompilerToVMHelper
87285229Sgshapiro                .getClassInitializer(resolvedClazz);
88285229Sgshapiro        if (tcase.isPositive) {
89285229Sgshapiro            Asserts.assertNotNull(initializer, "Couldn't get initializer for "
90285229Sgshapiro                    + className);
91285229Sgshapiro            Asserts.assertEQ(initializer.getName(), "<clinit>",
92285229Sgshapiro                    "Unexpected initializer name for " + className);
93285229Sgshapiro        } else {
94285229Sgshapiro            Asserts.assertNull(initializer, "Unexpected: found initializer for "
95285229Sgshapiro                    + className);
96285229Sgshapiro        }
97285229Sgshapiro    }
98285229Sgshapiro
99285229Sgshapiro    private static class TestCase {
100285229Sgshapiro        public final Class<?> holder;
101285229Sgshapiro        public final boolean isPositive;
102285229Sgshapiro
103285229Sgshapiro        public TestCase(Class<?> clazz, boolean isPositive) {
104285229Sgshapiro            this.holder = clazz;
105285229Sgshapiro            this.isPositive = isPositive;
106285229Sgshapiro        }
107285229Sgshapiro
108285229Sgshapiro        @Override
109285229Sgshapiro        public String toString() {
110285229Sgshapiro            return "CASE: clazz=" + holder.getName()
111285229Sgshapiro                    + ", isPositive=" + isPositive;
112285229Sgshapiro        }
113285229Sgshapiro    }
114285229Sgshapiro}
115285229Sgshapiro