1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8136421
  27  * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.misc
  31  * @modules java.base/jdk.internal.org.objectweb.asm
  32  *          java.base/jdk.internal.org.objectweb.asm.tree
  33  *          jdk.vm.ci/jdk.vm.ci.hotspot
  34  *          jdk.vm.ci/jdk.vm.ci.code
  35  *          jdk.vm.ci/jdk.vm.ci.meta
  36  *          jdk.vm.ci/jdk.vm.ci.runtime
  37  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  38  * @build compiler.jvmci.common.testcases.DuplicateSimpleSingleImplementerInterface
  39  * @build compiler.jvmci.common.testcases.SimpleSingleImplementerInterface
  40  * @build compiler.jvmci.common.testcases.MultipleImplementer1
  41  * @build compiler.jvmci.common.testcases.MultipleSuperImplementers
  42  * @build compiler.jvmci.common.testcases.SingleImplementer
  43  * @build compiler.jvmci.common.testcases.SingleImplementerInterface
  44  * @build compiler.jvmci.common.testcases.SingleSubclass
  45  * @build compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
  46  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  47  *                   compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
  48  */
  49 
  50 package compiler.jvmci.compilerToVM;
  51 
  52 import compiler.jvmci.common.CTVMUtilities;
  53 import compiler.jvmci.common.testcases.DuplicateSimpleSingleImplementerInterface;
  54 import compiler.jvmci.common.testcases.SimpleSingleImplementerInterface;
  55 import compiler.jvmci.common.testcases.MultipleImplementer1;
  56 import compiler.jvmci.common.testcases.MultipleSuperImplementers;
  57 import compiler.jvmci.common.testcases.SingleImplementer;
  58 import compiler.jvmci.common.testcases.SingleImplementerInterface;
  59 import compiler.jvmci.common.testcases.SingleSubclass;
  60 import jdk.test.lib.Asserts;
  61 import jdk.test.lib.Utils;
  62 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  63 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  64 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  65 
  66 import java.lang.reflect.Method;
  67 import java.util.HashSet;
  68 import java.util.Set;
  69 
  70 public class FindUniqueConcreteMethodTest {
  71     public static void main(String args[]) {
  72         FindUniqueConcreteMethodTest test = new FindUniqueConcreteMethodTest();
  73         try {
  74             for (TestCase tcase : createTestCases()) {
  75                 test.runTest(tcase);
  76             }
  77         } catch (NoSuchMethodException e) {
  78             throw new Error("TEST BUG: can't find method", e);
  79         }
  80     }
  81 
  82     private static Set<TestCase> createTestCases() {
  83         Set<TestCase> result = new HashSet<>();
  84         // a public method
  85         result.add(new TestCase(true, SingleSubclass.class,
  86                 SingleSubclass.class, "usualMethod"));
  87         // overriden method
  88         result.add(new TestCase(true, SingleSubclass.class,
  89                 SingleSubclass.class, "overridenMethod"));
  90         // private method
  91         result.add(new TestCase(true, SingleSubclass.class,
  92                 SingleSubclass.class, "privateMethod"));
  93         // protected method
  94         result.add(new TestCase(true, SingleSubclass.class,
  95                 SingleSubclass.class, "protectedMethod"));
  96         // default(package-private) method
  97         result.add(new TestCase(true, SingleSubclass.class,
  98                 SingleSubclass.class, "defaultAccessMethod"));
  99         // default interface method redefined in implementer
 100         result.add(new TestCase(true, MultipleImplementer1.class,
 101                 MultipleImplementer1.class, "defaultMethod"));
 102         // interface method
 103         result.add(new TestCase(true, MultipleImplementer1.class,
 104                 MultipleImplementer1.class, "testMethod"));
 105         // default interface method not redefined in implementer
 106         result.add(new TestCase(true, SingleImplementer.class,
 107                 SingleImplementerInterface.class, "defaultMethod"));
 108         // static method
 109         result.add(new TestCase(false, SingleSubclass.class,
 110                 SingleSubclass.class, "staticMethod"));
 111         // interface method
 112         result.add(new TestCase(false, MultipleSuperImplementers.class,
 113                                 DuplicateSimpleSingleImplementerInterface.class, "interfaceMethod", false));
 114         result.add(new TestCase(false, MultipleSuperImplementers.class,
 115                                 SimpleSingleImplementerInterface.class, "interfaceMethod", false));
 116         return result;
 117     }
 118 
 119     private void runTest(TestCase tcase) throws NoSuchMethodException {
 120         System.out.println(tcase);
 121         Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
 122         HotSpotResolvedJavaMethod testMethod = CTVMUtilities
 123             .getResolvedMethod(tcase.methodFromReceiver ? tcase.receiver : tcase.holder, method);
 124         HotSpotResolvedObjectType resolvedType = CompilerToVMHelper
 125                 .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(),
 126                 /* resolve = */ true);
 127         HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper
 128                 .findUniqueConcreteMethod(resolvedType, testMethod);
 129         Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null,
 130                 "Unexpected concrete method for " + tcase.methodName);
 131     }
 132 
 133     private static class TestCase {
 134         public final Class<?> receiver;
 135         public final Class<?> holder;
 136         public final String methodName;
 137         public final boolean isPositive;
 138         public final boolean methodFromReceiver;
 139 
 140         public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder,
 141                         String methodName, boolean methodFromReceiver) {
 142             this.receiver = clazz;
 143             this.methodName = methodName;
 144             this.isPositive = isPositive;
 145             this.holder = holder;
 146             this.methodFromReceiver = methodFromReceiver;
 147         }
 148 
 149         public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder, String methodName) {
 150             this(isPositive, clazz, holder, methodName, true);
 151         }
 152 
 153         @Override
 154         public String toString() {
 155             return String.format("CASE: receiver=%s, holder=%s, method=%s, isPositive=%s, methodFromReceiver=%s",
 156                                  receiver.getName(), holder.getName(), methodName, isPositive, methodFromReceiver);
 157         }
 158     }
 159 }