Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetVtableIndexForInterfaceTest.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 java.base/jdk.internal.org.objectweb.asm31* java.base/jdk.internal.org.objectweb.asm.tree32* jdk.internal.vm.ci/jdk.vm.ci.hotspot33* jdk.internal.vm.ci/jdk.vm.ci.code34* jdk.internal.vm.ci/jdk.vm.ci.meta35* jdk.internal.vm.ci/jdk.vm.ci.runtime36*37* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper38* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI39* -XX:-UseJVMCICompiler40* compiler.jvmci.compilerToVM.GetVtableIndexForInterfaceTest41*/4243package compiler.jvmci.compilerToVM;4445import compiler.jvmci.common.CTVMUtilities;46import compiler.jvmci.common.testcases.AbstractClass;47import compiler.jvmci.common.testcases.AnotherSingleImplementer;48import compiler.jvmci.common.testcases.AnotherSingleImplementerInterface;49import compiler.jvmci.common.testcases.DoNotExtendClass;50import compiler.jvmci.common.testcases.MultipleAbstractImplementer;51import compiler.jvmci.common.testcases.MultipleImplementersInterface;52import compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender;53import compiler.jvmci.common.testcases.SingleImplementer;54import compiler.jvmci.common.testcases.SingleImplementerInterface;55import compiler.jvmci.common.testcases.SingleSubclass;56import compiler.jvmci.common.testcases.SingleSubclassedClass;57import jdk.test.lib.Asserts;58import jdk.test.lib.Utils;59import jdk.vm.ci.hotspot.CompilerToVMHelper;60import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;61import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;6263import java.lang.reflect.Method;64import java.util.HashSet;65import java.util.Set;66import java.util.stream.Stream;6768public class GetVtableIndexForInterfaceTest {69private static final int INVALID_VTABLE_INDEX = -4; // see method.hpp: VtableIndexFlag7071public static void main(String args[]) {72GetVtableIndexForInterfaceTest test73= new GetVtableIndexForInterfaceTest();74try {75for (TestCase tcase : createTestCases()) {76test.runTest(tcase);77}78} catch (NoSuchMethodException e) {79throw new Error("TEST BUG: can't find requested method", e);80}81}8283private static Set<TestCase> createTestCases() {84Set<TestCase> result = new HashSet<>();85Stream.of(86AbstractClass.class,87SingleImplementer.class,88SingleImplementerInterface.class,89MultipleImplementersInterface.class,90MultipleImplementersInterfaceExtender.class,91SingleSubclass.class,92SingleSubclassedClass.class,93DoNotExtendClass.class,94MultipleAbstractImplementer.class95)96.forEach(Utils::ensureClassIsLoaded);97// non iface method98result.add(new TestCase(SingleImplementer.class,99SingleImplementer.class, "nonInterfaceMethod",100false, InternalError.class));101// iface method w/o default implementation102result.add(new TestCase(SingleImplementer.class,103SingleImplementerInterface.class, "interfaceMethod", false));104/* another iface which provides default implementation for the105original iface*/106result.add(new TestCase(MultipleImplementersInterfaceExtender.class,107MultipleImplementersInterface.class, "testMethod", false,108InternalError.class));109// iface method w/ default implementation110result.add(new TestCase(SingleImplementer.class,111SingleImplementerInterface.class, "defaultMethod", true));112// non iface class113result.add(new TestCase(SingleSubclass.class,114SingleSubclassedClass.class, "inheritedMethod", false,115InternalError.class));116// class not implementing iface117result.add(new TestCase(DoNotExtendClass.class,118SingleImplementerInterface.class, "defaultMethod", false,119InternalError.class));120// abstract class which doesn't implement iface121result.add(new TestCase(AbstractClass.class,122SingleImplementerInterface.class, "defaultMethod", false,123InternalError.class));124// abstract class which implements iface125result.add(new TestCase(MultipleAbstractImplementer.class,126MultipleImplementersInterface.class, "defaultMethod", true));127// class not initialized128result.add(new TestCase(AnotherSingleImplementer.class,129AnotherSingleImplementerInterface.class, "defaultMethod", false,130InternalError.class));131return result;132}133134private void runTest(TestCase tcase) throws NoSuchMethodException {135System.out.println(tcase);136Method method = tcase.holder.getDeclaredMethod(tcase.methodName);137HotSpotResolvedObjectType metaspaceKlass = CompilerToVMHelper138.lookupTypeHelper(Utils.toJVMTypeSignature(tcase.receiver),139getClass(), /* resolve = */ true);140HotSpotResolvedJavaMethod metaspaceMethod = CTVMUtilities141.getResolvedMethod(tcase.holder, method);142int index = 0;143try {144index = CompilerToVMHelper145.getVtableIndexForInterfaceMethod(metaspaceKlass,146metaspaceMethod);147} catch (Throwable t) {148if (tcase.isPositive || tcase.expectedException == null) {149throw new Error("Caught unexpected exception " + t);150}151if (!tcase.expectedException.equals(t.getClass())) {152throw new Error(String.format("Caught %s while expected %s",153t.getClass().getName(),154tcase.expectedException.getName()));155}156return;157}158if (tcase.expectedException != null) {159throw new AssertionError("Expected exception wasn't caught: "160+ tcase.expectedException.getName());161}162if (tcase.isPositive) {163Asserts.assertNE(index, INVALID_VTABLE_INDEX,164"Unexpected: got invalid index");165} else {166Asserts.assertEQ(index, INVALID_VTABLE_INDEX,167"Unexpected: got valid index ");168}169}170171private static class TestCase {172public final Class<?> receiver;173public final Class<?> holder;174public final String methodName;175public final boolean isPositive;176public final Class<? extends Throwable> expectedException;177178public TestCase(Class<?> receiver, Class<?> holder, String methodName,179boolean isPositive,180Class<? extends Throwable> expectedException) {181this.receiver = receiver;182this.holder = holder;183this.methodName = methodName;184this.isPositive = isPositive;185this.expectedException = expectedException;186}187188public TestCase(Class<?> receiver, Class<?> holder, String methodName,189boolean isPositive) {190this(receiver, holder, methodName, isPositive, null);191}192193@Override194public String toString() {195return String.format("CASE: receiver=%s, holder=%s, method=%s,"196+ " isPositive=%s%n", receiver.getName(), holder.getName(),197methodName, isPositive);198}199}200}201202203