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  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  36  * @build compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
  37  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  38  *                   compiler.jvmci.compilerToVM.FindUniqueConcreteMethodTest
  39  */
  40 
  41 package compiler.jvmci.compilerToVM;
  42 
  43 import compiler.jvmci.common.CTVMUtilities;
  44 import compiler.jvmci.common.testcases.MultipleImplementer1;
  45 import compiler.jvmci.common.testcases.SingleImplementer;
  46 import compiler.jvmci.common.testcases.SingleImplementerInterface;
  47 import compiler.jvmci.common.testcases.SingleSubclass;
  48 import jdk.test.lib.Asserts;
  49 import jdk.test.lib.Utils;
  50 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  51 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  52 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  53 
  54 import java.lang.reflect.Method;
  55 import java.util.HashSet;
  56 import java.util.Set;
  57 
  58 public class FindUniqueConcreteMethodTest {
  59     public static void main(String args[]) {
  60         FindUniqueConcreteMethodTest test = new FindUniqueConcreteMethodTest();
  61         try {
  62             for (TestCase tcase : createTestCases()) {
  63                 test.runTest(tcase);
  64             }
  65         } catch (NoSuchMethodException e) {
  66             throw new Error("TEST BUG: can't find method", e);
  67         }
  68     }
  69 
  70     private static Set<TestCase> createTestCases() {
  71         Set<TestCase> result = new HashSet<>();
  72         // a public method
  73         result.add(new TestCase(true, SingleSubclass.class,
  74                 SingleSubclass.class, "usualMethod"));
  75         // overriden method
  76         result.add(new TestCase(true, SingleSubclass.class,
  77                 SingleSubclass.class, "overridenMethod"));
  78         // private method
  79         result.add(new TestCase(true, SingleSubclass.class,
  80                 SingleSubclass.class, "privateMethod"));
  81         // protected method
  82         result.add(new TestCase(true, SingleSubclass.class,
  83                 SingleSubclass.class, "protectedMethod"));
  84         // default(package-private) method
  85         result.add(new TestCase(true, SingleSubclass.class,
  86                 SingleSubclass.class, "defaultAccessMethod"));
  87         // default interface method redefined in implementer
  88         result.add(new TestCase(true, MultipleImplementer1.class,
  89                 MultipleImplementer1.class, "defaultMethod"));
  90         // interface method
  91         result.add(new TestCase(true, MultipleImplementer1.class,
  92                 MultipleImplementer1.class, "testMethod"));
  93         // default interface method not redefined in implementer
  94         result.add(new TestCase(true, SingleImplementer.class,
  95                 SingleImplementerInterface.class, "defaultMethod"));
  96         // static method
  97         result.add(new TestCase(false, SingleSubclass.class,
  98                 SingleSubclass.class, "staticMethod"));
  99         return result;
 100     }
 101 
 102     private void runTest(TestCase tcase) throws NoSuchMethodException {
 103         System.out.println(tcase);
 104         Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
 105         HotSpotResolvedJavaMethod testMethod = CTVMUtilities
 106                 .getResolvedMethod(tcase.receiver, method);
 107         HotSpotResolvedObjectType resolvedType = CompilerToVMHelper
 108                 .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(),
 109                 /* resolve = */ true);
 110         HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper
 111                 .findUniqueConcreteMethod(resolvedType, testMethod);
 112         Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null,
 113                 "Unexpected concrete method for " + tcase.methodName);
 114     }
 115 
 116     private static class TestCase {
 117         public final Class<?> receiver;
 118         public final Class<?> holder;
 119         public final String methodName;
 120         public final boolean isPositive;
 121 
 122         public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder,
 123                 String methodName) {
 124             this.receiver = clazz;
 125             this.methodName = methodName;
 126             this.isPositive = isPositive;
 127             this.holder = holder;
 128         }
 129 
 130         @Override
 131         public String toString() {
 132             return String.format("CASE: receiver=%s, holder=%s, method=%s,"
 133                     + " isPositive=%s", receiver.getName(),
 134                     holder.getName(), methodName, isPositive);
 135         }
 136     }
 137 }