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