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