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,
  78                 SingleSubclass.class, "usualMethod"));
  79         // overriden method
  80         result.add(new TestCase(true, SingleSubclass.class,
  81                 SingleSubclass.class, "overridenMethod"));
  82         // private method
  83         result.add(new TestCase(true, SingleSubclass.class,
  84                 SingleSubclass.class, "privateMethod"));
  85         // protected method
  86         result.add(new TestCase(true, SingleSubclass.class,
  87                 SingleSubclass.class, "protectedMethod"));
  88         // default(package-private) method
  89         result.add(new TestCase(true, SingleSubclass.class,
  90                 SingleSubclass.class, "defaultAccessMethod"));
  91         // default interface method redefined in implementer
  92         result.add(new TestCase(true, MultipleImplementer1.class,
  93                 MultipleImplementer1.class, "defaultMethod"));
  94         // interface method
  95         result.add(new TestCase(true, MultipleImplementer1.class,
  96                 MultipleImplementer1.class, "testMethod"));
  97         // default interface method not redefined in implementer
  98         result.add(new TestCase(true, SingleImplementer.class,
  99                 SingleImplementerInterface.class, "defaultMethod"));
 100         // static method
 101         result.add(new TestCase(false, SingleSubclass.class,
 102                 SingleSubclass.class, "staticMethod"));
 103         // interface method
 104         result.add(new TestCase(false, MultipleSuperImplementers.class,
 105                                 DuplicateSimpleSingleImplementerInterface.class, "interfaceMethod", false));
 106         result.add(new TestCase(false, MultipleSuperImplementers.class,
 107                                 SimpleSingleImplementerInterface.class, "interfaceMethod", false));
 108         return result;
 109     }
 110 
 111     private void runTest(TestCase tcase) throws NoSuchMethodException {
 112         System.out.println(tcase);
 113         Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
 114         HotSpotResolvedJavaMethod testMethod = CTVMUtilities
 115             .getResolvedMethod(tcase.methodFromReceiver ? tcase.receiver : tcase.holder, method);
 116         HotSpotResolvedObjectType resolvedType = CompilerToVMHelper
 117                 .lookupType(Utils.toJVMTypeSignature(tcase.receiver), getClass(),
 118                 /* resolve = */ true);
 119         HotSpotResolvedJavaMethod concreteMethod = CompilerToVMHelper
 120                 .findUniqueConcreteMethod(resolvedType, testMethod);
 121         Asserts.assertEQ(concreteMethod, tcase.isPositive ? testMethod : null,
 122                 "Unexpected concrete method for " + tcase.methodName);
 123     }
 124 
 125     private static class TestCase {
 126         public final Class<?> receiver;
 127         public final Class<?> holder;
 128         public final String methodName;
 129         public final boolean isPositive;
 130         public final boolean methodFromReceiver;
 131 
 132         public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder,
 133                         String methodName, boolean methodFromReceiver) {
 134             this.receiver = clazz;
 135             this.methodName = methodName;
 136             this.isPositive = isPositive;
 137             this.holder = holder;
 138             this.methodFromReceiver = methodFromReceiver;
 139         }
 140 
 141         public TestCase(boolean isPositive, Class<?> clazz, Class<?> holder, String methodName) {
 142             this(isPositive, clazz, holder, methodName, true);
 143         }
 144 
 145         @Override
 146         public String toString() {
 147             return String.format("CASE: receiver=%s, holder=%s, method=%s, isPositive=%s, methodFromReceiver=%s",
 148                                  receiver.getName(), holder.getName(), methodName, isPositive, methodFromReceiver);
 149         }
 150     }
 151 }