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