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