Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetClassInitializerTest.java
41153 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 813642126* @requires vm.jvmci27* @library / /test/lib28* @library ../common/patches29* @modules java.base/jdk.internal.misc30* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot31* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper32* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI33* -XX:-UseJVMCICompiler34* compiler.jvmci.compilerToVM.GetClassInitializerTest35*/3637package compiler.jvmci.compilerToVM;3839import compiler.jvmci.common.testcases.AbstractClass;40import compiler.jvmci.common.testcases.AbstractClassExtender;41import compiler.jvmci.common.testcases.DoNotExtendClass;42import compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender;43import compiler.jvmci.common.testcases.SingleImplementer;44import compiler.jvmci.common.testcases.SingleImplementerInterface;45import jdk.test.lib.Asserts;46import jdk.test.lib.Utils;47import jdk.vm.ci.hotspot.CompilerToVMHelper;48import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;49import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;5051import java.util.HashSet;52import java.util.Set;5354public class GetClassInitializerTest {5556public static void main(String args[]) {57GetClassInitializerTest test = new GetClassInitializerTest();58for (TestCase tcase : createTestCases()) {59test.runTest(tcase);60}61}6263private static Set<TestCase> createTestCases() {64Set<TestCase> result = new HashSet<>();65// a simple class with initializer66result.add(new TestCase(SingleImplementer.class, true));67// an interface with initializer68result.add(new TestCase(SingleImplementerInterface.class, true));69// an abstract class with initializer70result.add(new TestCase(AbstractClass.class, true));71// a class without initializer, extending class with initializer72result.add(new TestCase(AbstractClassExtender.class, false));73// an interface without initializer74result.add(new TestCase(MultipleImplementersInterfaceExtender.class, false));75// a class without initializer76result.add(new TestCase(DoNotExtendClass.class, false));77return result;78}7980private void runTest(TestCase tcase) {81System.out.println(tcase);82String className = tcase.holder.getName();83HotSpotResolvedObjectType resolvedClazz = CompilerToVMHelper84.lookupTypeHelper(Utils.toJVMTypeSignature(tcase.holder),85getClass(), /* resolve = */ true);86HotSpotResolvedJavaMethod initializer = CompilerToVMHelper87.getClassInitializer(resolvedClazz);88if (tcase.isPositive) {89Asserts.assertNotNull(initializer, "Couldn't get initializer for "90+ className);91Asserts.assertEQ(initializer.getName(), "<clinit>",92"Unexpected initializer name for " + className);93} else {94Asserts.assertNull(initializer, "Unexpected: found initializer for "95+ className);96}97}9899private static class TestCase {100public final Class<?> holder;101public final boolean isPositive;102103public TestCase(Class<?> clazz, boolean isPositive) {104this.holder = clazz;105this.isPositive = isPositive;106}107108@Override109public String toString() {110return "CASE: clazz=" + holder.getName()111+ ", isPositive=" + isPositive;112}113}114}115116117