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