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