GetClassInitializerTest.java revision 10420:c558850fac57
12SN/A/* 26SN/A * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 32SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 42SN/A * 52SN/A * This code is free software; you can redistribute it and/or modify it 62SN/A * under the terms of the GNU General Public License version 2 only, as 72SN/A * published by the Free Software Foundation. 82SN/A * 92SN/A * This code is distributed in the hope that it will be useful, but WITHOUT 102SN/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 112SN/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 122SN/A * version 2 for more details (a copy is included in the LICENSE file that 132SN/A * accompanied this code). 142SN/A * 152SN/A * You should have received a copy of the GNU General Public License version 162SN/A * 2 along with this work; if not, write to the Free Software Foundation, 172SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 182SN/A * 192SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 202SN/A * or visit www.oracle.com if you need additional information or have any 212SN/A * questions. 222SN/A */ 232SN/A 242SN/A/* 252SN/A * @test 262SN/A * @bug 8136421 272SN/A * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") 28210SN/A * @library / /testlibrary /test/lib 292SN/A * @library ../common/patches 302SN/A * @modules jdk.vm.ci/jdk.vm.ci.hotspot 312SN/A * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper 322SN/A * @build compiler.jvmci.compilerToVM.GetClassInitializerTest 332SN/A * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 34210SN/A * compiler.jvmci.compilerToVM.GetClassInitializerTest 35252SN/A */ 361068Sattila 371068Sattilapackage compiler.jvmci.compilerToVM; 382SN/A 39210SN/Aimport compiler.jvmci.common.testcases.AbstractClass; 402SN/Aimport compiler.jvmci.common.testcases.AbstractClassExtender; 412SN/Aimport compiler.jvmci.common.testcases.DoNotExtendClass; 42429SN/Aimport compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender; 432SN/Aimport compiler.jvmci.common.testcases.SingleImplementer; 44143SN/Aimport compiler.jvmci.common.testcases.SingleImplementerInterface; 45210SN/Aimport java.util.HashSet; 46210SN/Aimport java.util.Set; 471220Ssundarimport jdk.vm.ci.hotspot.CompilerToVMHelper; 481220Ssundarimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 491220Ssundarimport jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 501220Ssundarimport jdk.test.lib.Asserts; 511220Ssundarimport jdk.test.lib.Utils; 521220Ssundar 531220Ssundarpublic class GetClassInitializerTest { 541002Shannesw 551118Shannesw public static void main(String args[]) { 561002Shannesw GetClassInitializerTest test = new GetClassInitializerTest(); 571002Shannesw for (TestCase tcase : createTestCases()) { 581118Shannesw test.runTest(tcase); 59210SN/A } 60210SN/A } 61210SN/A 62210SN/A private static Set<TestCase> createTestCases() { 631118Shannesw Set<TestCase> result = new HashSet<>(); 642SN/A // a simple class with initializer 652SN/A result.add(new TestCase(SingleImplementer.class, true)); 662SN/A // an interface with initializer 672SN/A result.add(new TestCase(SingleImplementerInterface.class, true)); 68252SN/A // an abstract class with initializer 69252SN/A result.add(new TestCase(AbstractClass.class, true)); 70252SN/A // a class without initializer, extending class with initializer 71252SN/A result.add(new TestCase(AbstractClassExtender.class, false)); 72252SN/A // an interface without initializer 732SN/A result.add(new TestCase(MultipleImplementersInterfaceExtender.class, false)); 74429SN/A // a class without initializer 751118Shannesw result.add(new TestCase(DoNotExtendClass.class, false)); 76210SN/A return result; 77210SN/A } 78429SN/A 79210SN/A private void runTest(TestCase tcase) { 801220Ssundar System.out.println(tcase); 81210SN/A String className = tcase.holder.getName(); 82210SN/A HotSpotResolvedObjectType resolvedClazz = CompilerToVMHelper 83210SN/A .lookupType(Utils.toJVMTypeSignature(tcase.holder), 84143SN/A getClass(), /* resolve = */ true); 85143SN/A HotSpotResolvedJavaMethod initializer = CompilerToVMHelper 86143SN/A .getClassInitializer(resolvedClazz); 87143SN/A if (tcase.isPositive) { 88143SN/A Asserts.assertNotNull(initializer, "Couldn't get initializer for " 891220Ssundar + className); 901220Ssundar Asserts.assertEQ(initializer.getName(), "<clinit>", 911220Ssundar "Unexpected initializer name for " + className); 921220Ssundar } else { 931220Ssundar Asserts.assertNull(initializer, "Unexpected: found initializer for " 941220Ssundar + className); 95143SN/A } 96429SN/A } 971220Ssundar 981220Ssundar private static class TestCase { 991220Ssundar public final Class<?> holder; 1001220Ssundar public final boolean isPositive; 1011220Ssundar 1021220Ssundar public TestCase(Class<?> clazz, boolean isPositive) { 1031220Ssundar this.holder = clazz; 1041220Ssundar this.isPositive = isPositive; 1051220Ssundar } 1061220Ssundar 1071220Ssundar @Override 1081220Ssundar public String toString() { 1091220Ssundar return "CASE: clazz=" + holder.getName() 1101220Ssundar + ", isPositive=" + isPositive; 1111220Ssundar } 112252SN/A } 1131220Ssundar} 114143SN/A