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