FindUniqueConcreteMethodTest.java revision 11707:ad7af1afda7a
112310Sjlahoda/* 212310Sjlahoda * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 312310Sjlahoda * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 412310Sjlahoda * 512310Sjlahoda * This code is free software; you can redistribute it and/or modify it 612310Sjlahoda * under the terms of the GNU General Public License version 2 only, as 712310Sjlahoda * published by the Free Software Foundation. 812310Sjlahoda * 912310Sjlahoda * This code is distributed in the hope that it will be useful, but WITHOUT 1012310Sjlahoda * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1112310Sjlahoda * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1212310Sjlahoda * version 2 for more details (a copy is included in the LICENSE file that 1312310Sjlahoda * accompanied this code). 1412310Sjlahoda * 1512310Sjlahoda * You should have received a copy of the GNU General Public License version 1612310Sjlahoda * 2 along with this work; if not, write to the Free Software Foundation, 1712310Sjlahoda * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1812310Sjlahoda * 1912310Sjlahoda * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2012310Sjlahoda * or visit www.oracle.com if you need additional information or have any 2112310Sjlahoda * questions. 2212310Sjlahoda */ 2312310Sjlahoda 2412310Sjlahoda/* 2512310Sjlahoda * @test 2612310Sjlahoda * @bug 8136421 2712310Sjlahoda * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64") 2812310Sjlahoda * @library / /testlibrary /test/lib 2912310Sjlahoda * @library ../common/patches 3012310Sjlahoda * @modules java.base/jdk.internal.misc 3112310Sjlahoda * @modules java.base/jdk.internal.org.objectweb.asm 3212310Sjlahoda * java.base/jdk.internal.org.objectweb.asm.tree 3312310Sjlahoda * jdk.vm.ci/jdk.vm.ci.hotspot 3412310Sjlahoda * jdk.vm.ci/jdk.vm.ci.code 3512310Sjlahoda * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper 3612310Sjlahoda * @build compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest 3712310Sjlahoda * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 3812310Sjlahoda * compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest 3912310Sjlahoda */ 4012310Sjlahoda 4112310Sjlahodapackage compiler.jvmci.compilerToVM; 4212310Sjlahoda 4312310Sjlahodaimport compiler.jvmci.common.CTVMUtilities; 4412310Sjlahodaimport compiler.jvmci.common.testcases.MultipleImplementer1; 4512310Sjlahodaimport compiler.jvmci.common.testcases.SingleImplementer; 4612310Sjlahodaimport compiler.jvmci.common.testcases.SingleImplementerInterface; 4712310Sjlahodaimport compiler.jvmci.common.testcases.SingleSubclass; 4812310Sjlahodaimport jdk.test.lib.Asserts; 4912310Sjlahodaimport jdk.test.lib.Utils; 5012310Sjlahodaimport jdk.vm.ci.hotspot.CompilerToVMHelper; 5112310Sjlahodaimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 5212310Sjlahodaimport jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 5312310Sjlahoda 5412310Sjlahodaimport java.lang.reflect.Method; 5512310Sjlahodaimport java.util.HashSet; 5612310Sjlahodaimport java.util.Set; 5712310Sjlahoda 5812310Sjlahodapublic class FindUniqueConcreteMethodTest { 5912310Sjlahoda public static void main(String args[]) { 6012310Sjlahoda FindUniqueConcreteMethodTest test = new FindUniqueConcreteMethodTest(); 6112310Sjlahoda try { 6212310Sjlahoda for (TestCase tcase : createTestCases()) { 6312310Sjlahoda test.runTest(tcase); 6412310Sjlahoda } 6512310Sjlahoda } catch (NoSuchMethodException e) { 6612310Sjlahoda throw new Error("TEST BUG: can't find method", e); 6712310Sjlahoda } 6812310Sjlahoda } 6912310Sjlahoda 7012310Sjlahoda private static Set<TestCase> createTestCases() { 7112310Sjlahoda Set<TestCase> result = new HashSet<>(); 7212310Sjlahoda // a public method 7312310Sjlahoda result.add(new TestCase(true, SingleSubclass.class, 7412310Sjlahoda SingleSubclass.class, "usualMethod")); 7512310Sjlahoda // overriden method 7612310Sjlahoda result.add(new TestCase(true, SingleSubclass.class, 7712310Sjlahoda SingleSubclass.class, "overridenMethod")); 7812310Sjlahoda // private method 7912310Sjlahoda result.add(new TestCase(true, SingleSubclass.class, 8012310Sjlahoda SingleSubclass.class, "privateMethod")); 8112310Sjlahoda // protected method 8212310Sjlahoda result.add(new TestCase(true, SingleSubclass.class, 8312310Sjlahoda SingleSubclass.class, "protectedMethod")); 8412310Sjlahoda // default(package-private) method 8512310Sjlahoda result.add(new TestCase(true, SingleSubclass.class, 8612310Sjlahoda SingleSubclass.class, "defaultAccessMethod")); 8712310Sjlahoda // default interface method redefined in implementer 8812310Sjlahoda result.add(new TestCase(true, MultipleImplementer1.class, 8912310Sjlahoda MultipleImplementer1.class, "defaultMethod")); 9012310Sjlahoda // interface method 9112310Sjlahoda result.add(new TestCase(true, MultipleImplementer1.class, 9212310Sjlahoda MultipleImplementer1.class, "testMethod")); 9312310Sjlahoda // default interface method not redefined in implementer 9412310Sjlahoda result.add(new TestCase(true, SingleImplementer.class, 9512310Sjlahoda SingleImplementerInterface.class, "defaultMethod")); 9612310Sjlahoda // static method 9712310Sjlahoda result.add(new TestCase(false, SingleSubclass.class, 9812310Sjlahoda SingleSubclass.class, "staticMethod")); 9912310Sjlahoda return result; 10012310Sjlahoda } 10112310Sjlahoda 10212310Sjlahoda private void runTest(TestCase tcase) throws NoSuchMethodException { 10312310Sjlahoda System.out.println(tcase); 10412310Sjlahoda Method method = tcase.holder.getDeclaredMethod(tcase.methodName); 10512310Sjlahoda HotSpotResolvedJavaMethod testMethod = CTVMUtilities 10612310Sjlahoda .getResolvedMethod(tcase.receiver, method); 10712310Sjlahoda HotSpotResolvedObjectType resolvedType = CompilerToVMHelper 10812310Sjlahoda .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(), 10912310Sjlahoda /* resolve = */ true); 11012310Sjlahoda HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper 11112310Sjlahoda .findUniqueConcreteMethod(resolvedType, testMethod); 11212310Sjlahoda Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null, 11312310Sjlahoda "Unexpected concrete method for " + tcase.methodName); 11412310Sjlahoda } 11512310Sjlahoda 11612310Sjlahoda private static class TestCase { 11712310Sjlahoda public final Class<?> receiver; 11812310Sjlahoda public final Class<?> holder; 11912310Sjlahoda public final String methodName; 12012310Sjlahoda public final boolean isPositive; 12112310Sjlahoda 12212310Sjlahoda public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder, 12312310Sjlahoda String methodName) { 12412310Sjlahoda this.receiver = clazz; 12512310Sjlahoda this.methodName = methodName; 12612310Sjlahoda this.isPositive = isPositive; 12712310Sjlahoda this.holder = holder; 12812310Sjlahoda } 12912310Sjlahoda 13012310Sjlahoda @Override 13112310Sjlahoda public String toString() { 13212310Sjlahoda return String.format("CASE: receiver=%s, holder=%s, method=%s," 13312310Sjlahoda + " isPositive=%s", receiver.getName(), 13412310Sjlahoda holder.getName(), methodName, isPositive); 13512310Sjlahoda } 13612310Sjlahoda } 13712310Sjlahoda} 13812310Sjlahoda