Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/ResolveMethodTest.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.ResolveMethodTest41*/4243package compiler.jvmci.compilerToVM;4445import compiler.jvmci.common.CTVMUtilities;46import compiler.jvmci.common.testcases.AbstractClass;47import compiler.jvmci.common.testcases.AbstractClassExtender;48import compiler.jvmci.common.testcases.MultipleImplementer1;49import compiler.jvmci.common.testcases.MultipleImplementer2;50import compiler.jvmci.common.testcases.MultipleImplementersInterface;51import compiler.jvmci.common.testcases.SingleImplementer;52import compiler.jvmci.common.testcases.SingleImplementerInterface;53import compiler.jvmci.common.testcases.SingleSubclass;54import compiler.jvmci.common.testcases.SingleSubclassedClass;55import jdk.internal.misc.Unsafe;56import jdk.test.lib.Asserts;57import jdk.test.lib.Utils;58import jdk.vm.ci.hotspot.CompilerToVMHelper;59import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;60import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;6162import java.util.HashSet;63import java.util.Set;6465public class ResolveMethodTest {66private static final Unsafe UNSAFE = Unsafe.getUnsafe();6768public static void main(String args[]) {69ResolveMethodTest test = new ResolveMethodTest();70// positive cases71try {72for (TestCase tcase: createTestCases()) {73test.runTest(tcase);74}75} catch (NoSuchMethodException e) {76throw new Error("TEST BUG: can't find requested method", e);77}78}7980private static Set<TestCase> createTestCases() {81Set<TestCase> result = new HashSet<>();82// a usual class public method83result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,84"usualMethod", ResolveMethodTest.class, true));85// an array method86result.add(new TestCase(int[].class, Object.class, "toString",87ResolveMethodTest.class, true));88// a method from base class, which was overriden in tested one89result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,90"overridenMethod", ResolveMethodTest.class, true));91// a method from base class, which was not overriden in tested one92result.add(new TestCase(SingleSubclass.class,93SingleSubclassedClass.class, "inheritedMethod",94ResolveMethodTest.class, true));95/* a method from base class, which was overriden in tested one with96base class as holder */97result.add(new TestCase(SingleSubclass.class,98SingleSubclassedClass.class, "overridenMethod",99ResolveMethodTest.class, true));100// an interface method101result.add(new TestCase(SingleImplementer.class,102SingleImplementerInterface.class, "interfaceMethod",103ResolveMethodTest.class, true));104// an interface default method overriden in implementer105result.add(new TestCase(MultipleImplementer1.class,106MultipleImplementersInterface.class, "defaultMethod",107ResolveMethodTest.class, true));108// an interface default method not overriden in implementer109result.add(new TestCase(MultipleImplementer2.class,110MultipleImplementersInterface.class, "defaultMethod",111ResolveMethodTest.class, true));112// an abstract method113result.add(new TestCase(AbstractClassExtender.class, AbstractClass.class,114"abstractMethod", ResolveMethodTest.class, true));115// private method with right accessor116result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,117"privateMethod", SingleSubclass.class, true));118// package-private method with right accessor119result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,120"defaultAccessMethod", SingleSubclass.class, true));121122// negative cases123124// private method of another class125result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,126"privateMethod", ResolveMethodTest.class, false));127// package-private method from another package128result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,129"defaultAccessMethod", ResolveMethodTest.class, false));130return result;131}132133private void runTest(TestCase tcase) throws NoSuchMethodException {134System.out.println(tcase);135HotSpotResolvedJavaMethod metaspaceMethod = CTVMUtilities136.getResolvedMethod(tcase.holder,137tcase.holder.getDeclaredMethod(tcase.methodName));138HotSpotResolvedObjectType holderMetaspace = CompilerToVMHelper139.lookupTypeHelper(Utils.toJVMTypeSignature(tcase.holder),140getClass(), /* resolve = */ true);141HotSpotResolvedObjectType callerMetaspace = CompilerToVMHelper142.lookupTypeHelper(Utils.toJVMTypeSignature(tcase.caller),143getClass(), /* resolve = */ true);144HotSpotResolvedObjectType receiverMetaspace = CompilerToVMHelper145.lookupTypeHelper(Utils.toJVMTypeSignature(tcase.receiver),146getClass(), /* resolve = */ true);147148// Can only resolve methods on a linked class so force initialization149receiverMetaspace.initialize();150HotSpotResolvedJavaMethod resolvedMetaspaceMethod151= CompilerToVMHelper.resolveMethod(receiverMetaspace,152metaspaceMethod, callerMetaspace);153if (tcase.isPositive) {154Asserts.assertNotNull(resolvedMetaspaceMethod,155"Unexpected null resolved method value for "156+ tcase.methodName);157Asserts.assertEQ(metaspaceMethod.getName(), tcase.methodName,158"Reflection and c2vm method names doesn't match");159} else {160Asserts.assertNull(resolvedMetaspaceMethod,161"Method unexpectedly resolved");162}163}164165private static class TestCase {166public final Class<?> receiver;167public final Class<?> holder;168public final Class<?> caller;169public final String methodName;170public final boolean isPositive;171172public TestCase(Class<?> recv, Class<?> holder, String methodName,173Class<?> caller, boolean isPositive) {174this.receiver = recv;175this.holder = holder;176this.caller = caller;177this.methodName = methodName;178this.isPositive = isPositive;179}180181@Override182public String toString() {183return String.format("CASE: receiver=%s, holder=%s, method=%s,"184+ "caller=%s, isPositive=%s%n", receiver.getName(),185holder.getName(), methodName, caller.getName(), isPositive);186}187}188}189190191