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.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  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *                   -Djvmci.Compiler=null
  38  *                   compiler.jvmci.compilerToVM.ResolveMethodTest
  39  */
  40 
  41 package compiler.jvmci.compilerToVM;
  42 
  43 import compiler.jvmci.common.CTVMUtilities;
  44 import compiler.jvmci.common.testcases.AbstractClass;
  45 import compiler.jvmci.common.testcases.AbstractClassExtender;
  46 import compiler.jvmci.common.testcases.MultipleImplementer1;
  47 import compiler.jvmci.common.testcases.MultipleImplementer2;
  48 import compiler.jvmci.common.testcases.MultipleImplementersInterface;
  49 import compiler.jvmci.common.testcases.SingleImplementer;
  50 import compiler.jvmci.common.testcases.SingleImplementerInterface;
  51 import compiler.jvmci.common.testcases.SingleSubclass;
  52 import compiler.jvmci.common.testcases.SingleSubclassedClass;
  53 import jdk.internal.misc.Unsafe;
  54 import jdk.test.lib.Asserts;
  55 import jdk.test.lib.Utils;
  56 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  57 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  58 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  59 
  60 import java.util.HashSet;
  61 import java.util.Set;
  62 
  63 public class ResolveMethodTest {
  64     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
  65 
  66     public static void main(String args[]) {
  67         ResolveMethodTest test = new ResolveMethodTest();
  68         // positive cases
  69         try {
  70             for (TestCase tcase: createTestCases()) {
  71                 test.runTest(tcase);
  72             }
  73         } catch (NoSuchMethodException e) {
  74             throw new Error("TEST BUG: can't find requested method", e);
  75         }
  76     }
  77 
  78     private static Set<TestCase> createTestCases() {
  79         Set<TestCase> result = new HashSet<>();
  80         // a usual class public method
  81         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
  82                 "usualMethod", ResolveMethodTest.class, true));
  83         // an array method
  84         result.add(new TestCase(int[].class, Object.class, "toString",
  85                 ResolveMethodTest.class, true));
  86         // a method from base class, which was overriden in tested one
  87         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
  88                 "overridenMethod", ResolveMethodTest.class, true));
  89         // a method from base class, which was not overriden in tested one
  90         result.add(new TestCase(SingleSubclass.class,
  91                 SingleSubclassedClass.class, "inheritedMethod",
  92                 ResolveMethodTest.class, true));
  93         /* a method from base class, which was overriden in tested one with
  94            base class as holder */
  95         result.add(new TestCase(SingleSubclass.class,
  96                 SingleSubclassedClass.class, "overridenMethod",
  97                 ResolveMethodTest.class, true));
  98         // an interface method
  99         result.add(new TestCase(SingleImplementer.class,
 100                 SingleImplementerInterface.class, "interfaceMethod",
 101                 ResolveMethodTest.class, true));
 102         // an interface default method overriden in implementer
 103         result.add(new TestCase(MultipleImplementer1.class,
 104                 MultipleImplementersInterface.class, "defaultMethod",
 105                 ResolveMethodTest.class, true));
 106         // an interface default method not overriden in implementer
 107         result.add(new TestCase(MultipleImplementer2.class,
 108                 MultipleImplementersInterface.class, "defaultMethod",
 109                 ResolveMethodTest.class, true));
 110         // an abstract method
 111         result.add(new TestCase(AbstractClassExtender.class, AbstractClass.class,
 112                 "abstractMethod", ResolveMethodTest.class, true));
 113         // private method with right accessor
 114         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
 115                 "privateMethod", SingleSubclass.class, true));
 116         // package-private method with right accessor
 117         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
 118                 "defaultAccessMethod", SingleSubclass.class, true));
 119 
 120         // negative cases
 121 
 122         // private method of another class
 123         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
 124                 "privateMethod", ResolveMethodTest.class, false));
 125         // package-private method from another package
 126         result.add(new TestCase(SingleSubclass.class, SingleSubclass.class,
 127                 "defaultAccessMethod", ResolveMethodTest.class, false));
 128         return result;
 129     }
 130 
 131     private void runTest(TestCase tcase) throws NoSuchMethodException {
 132         System.out.println(tcase);
 133         HotSpotResolvedJavaMethod metaspaceMethod = CTVMUtilities
 134                 .getResolvedMethod(tcase.holder,
 135                         tcase.holder.getDeclaredMethod(tcase.methodName));
 136         HotSpotResolvedObjectType holderMetaspace = CompilerToVMHelper
 137                 .lookupType(Utils.toJVMTypeSignature(tcase.holder),
 138                         getClass(), /* resolve = */ true);
 139         HotSpotResolvedObjectType callerMetaspace = CompilerToVMHelper
 140                 .lookupType(Utils.toJVMTypeSignature(tcase.caller),
 141                         getClass(), /* resolve = */ true);
 142         HotSpotResolvedObjectType receiverMetaspace = CompilerToVMHelper
 143                 .lookupType(Utils.toJVMTypeSignature(tcase.receiver),
 144                         getClass(), /* resolve = */ true);
 145 
 146         // Can only resolve methods on a linked class so force initialization
 147         receiverMetaspace.initialize();
 148         HotSpotResolvedJavaMethod resolvedMetaspaceMethod
 149                 = CompilerToVMHelper.resolveMethod(receiverMetaspace,
 150                         metaspaceMethod, callerMetaspace);
 151         if (tcase.isPositive) {
 152             Asserts.assertNotNull(resolvedMetaspaceMethod,
 153                     "Unexpected null resolved method value for "
 154                             + tcase.methodName);
 155             Asserts.assertEQ(metaspaceMethod.getName(), tcase.methodName,
 156                     "Reflection and c2vm method names doesn't match");
 157         } else {
 158             Asserts.assertNull(resolvedMetaspaceMethod,
 159                     "Method unexpectedly resolved");
 160         }
 161     }
 162 
 163     private static class TestCase {
 164         public final Class<?> receiver;
 165         public final Class<?> holder;
 166         public final Class<?> caller;
 167         public final String methodName;
 168         public final boolean isPositive;
 169 
 170         public TestCase(Class<?> recv, Class<?> holder, String methodName,
 171                 Class<?> caller, boolean isPositive) {
 172             this.receiver = recv;
 173             this.holder = holder;
 174             this.caller = caller;
 175             this.methodName = methodName;
 176             this.isPositive = isPositive;
 177         }
 178 
 179         @Override
 180         public String toString() {
 181             return String.format("CASE: receiver=%s, holder=%s, method=%s,"
 182                 + "caller=%s, isPositive=%s%n", receiver.getName(),
 183                 holder.getName(), methodName, caller.getName(), isPositive);
 184         }
 185     }
 186 }