1 /*
   2  * Copyright (c) 2014, 2018, 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  * @requires vm.jvmci
  27  * @modules jdk.internal.vm.ci/jdk.vm.ci.meta
  28  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  29  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -Djvmci.Compiler=null jdk.vm.ci.runtime.test.ResolvedJavaTypeResolveConcreteMethodTest
  30  */
  31 
  32 package jdk.vm.ci.runtime.test;
  33 
  34 import jdk.vm.ci.meta.MetaAccessProvider;
  35 import jdk.vm.ci.meta.ResolvedJavaMethod;
  36 import jdk.vm.ci.meta.ResolvedJavaType;
  37 import jdk.vm.ci.runtime.JVMCI;
  38 import org.junit.Test;
  39 
  40 import static org.junit.Assert.assertEquals;
  41 import static org.junit.Assert.assertNull;
  42 import static org.junit.Assert.assertNotNull;
  43 
  44 public class ResolvedJavaTypeResolveConcreteMethodTest {
  45     public final MetaAccessProvider metaAccess;
  46 
  47     public ResolvedJavaTypeResolveConcreteMethodTest() {
  48         metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
  49     }
  50 
  51     protected abstract static class A {
  52         @SuppressWarnings("unused")
  53         private void priv() {
  54         }
  55 
  56         public void v1() {
  57         }
  58 
  59         public void v2() {
  60         }
  61 
  62         public abstract void abs();
  63     }
  64 
  65     protected static class B extends A implements I {
  66         public void i() {
  67         }
  68 
  69         @Override
  70         public void v2() {
  71         }
  72 
  73         @Override
  74         public void abs() {
  75 
  76         }
  77     }
  78 
  79     protected static class C extends B {
  80         public void d() {
  81         }
  82     }
  83 
  84     protected abstract static class D extends A {
  85 
  86     }
  87 
  88     protected static class E extends D {
  89         @Override
  90         public void abs() {
  91         }
  92     }
  93 
  94     protected interface I {
  95         void i();
  96 
  97         default void d() {
  98         }
  99     }
 100 
 101     @Test
 102     public void testDefaultMethod() {
 103         ResolvedJavaType i = getType(I.class);
 104         ResolvedJavaType b = getType(B.class);
 105         ResolvedJavaType c = getType(C.class);
 106         ResolvedJavaMethod di = getMethod(i, "d");
 107         ResolvedJavaMethod dc = getMethod(c, "d");
 108 
 109         assertEquals(null, i.resolveConcreteMethod(di, c));
 110         assertEquals(di, b.resolveConcreteMethod(di, c));
 111         assertEquals(dc, c.resolveConcreteMethod(di, c));
 112     }
 113 
 114     @Test
 115     public void testPrivateMethod() {
 116         ResolvedJavaType a = getType(A.class);
 117         ResolvedJavaType b = getType(B.class);
 118         ResolvedJavaType c = getType(C.class);
 119         ResolvedJavaMethod priv = getMethod(a, "priv");
 120 
 121         // nestmates have access to private methods
 122         assertNotNull(a.resolveConcreteMethod(priv, c));
 123         assertNotNull(b.resolveConcreteMethod(priv, c));
 124     }
 125 
 126     @Test
 127     public void testAbstractMethod() {
 128         ResolvedJavaType a = getType(A.class);
 129         ResolvedJavaType b = getType(B.class);
 130         ResolvedJavaType c = getType(C.class);
 131         ResolvedJavaType d = getType(D.class);
 132         ResolvedJavaType e = getType(E.class);
 133         ResolvedJavaMethod absa = getMethod(a, "abs");
 134         ResolvedJavaMethod absb = getMethod(b, "abs");
 135         ResolvedJavaMethod abse = getMethod(e, "abs");
 136 
 137         assertNull(a.resolveConcreteMethod(absa, c));
 138         assertNull(d.resolveConcreteMethod(absa, c));
 139 
 140         assertEquals(absb, b.resolveConcreteMethod(absa, c));
 141         assertEquals(absb, b.resolveConcreteMethod(absb, c));
 142         assertEquals(absb, c.resolveConcreteMethod(absa, c));
 143         assertEquals(absb, c.resolveConcreteMethod(absb, c));
 144         assertEquals(abse, e.resolveConcreteMethod(absa, c));
 145         assertNull(e.resolveConcreteMethod(absb, c));
 146         assertEquals(abse, e.resolveConcreteMethod(abse, c));
 147     }
 148 
 149     @Test
 150     public void testVirtualMethod() {
 151         ResolvedJavaType a = getType(A.class);
 152         ResolvedJavaType b = getType(B.class);
 153         ResolvedJavaType c = getType(C.class);
 154         ResolvedJavaMethod v1a = getMethod(a, "v1");
 155         ResolvedJavaMethod v2a = getMethod(a, "v2");
 156         ResolvedJavaMethod v2b = getMethod(b, "v2");
 157 
 158         assertEquals(v1a, a.resolveConcreteMethod(v1a, c));
 159         assertEquals(v1a, b.resolveConcreteMethod(v1a, c));
 160         assertEquals(v1a, c.resolveConcreteMethod(v1a, c));
 161         assertEquals(v2a, a.resolveConcreteMethod(v2a, c));
 162         assertEquals(v2b, b.resolveConcreteMethod(v2a, c));
 163         assertEquals(v2b, b.resolveConcreteMethod(v2b, c));
 164         assertEquals(v2b, c.resolveConcreteMethod(v2a, c));
 165         assertEquals(v2b, c.resolveConcreteMethod(v2b, c));
 166 
 167     }
 168 
 169     static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) {
 170         for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
 171             if (method.getName().equals(methodName)) {
 172                 return method;
 173             }
 174         }
 175         throw new IllegalArgumentException();
 176     }
 177 
 178     protected ResolvedJavaType getType(Class<?> clazz) {
 179         ResolvedJavaType type = metaAccess.lookupJavaType(clazz);
 180         type.initialize();
 181         return type;
 182     }
 183 }