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 * compiler.jvmci.compilerToVM.GetVtableIndexForInterfaceTest 38 */ 39 40 package compiler.jvmci.compilerToVM; 41 42 import compiler.jvmci.common.CTVMUtilities; 43 import compiler.jvmci.common.testcases.AbstractClass; 44 import compiler.jvmci.common.testcases.AnotherSingleImplementer; 45 import compiler.jvmci.common.testcases.AnotherSingleImplementerInterface; 46 import compiler.jvmci.common.testcases.DoNotExtendClass; 47 import compiler.jvmci.common.testcases.MultipleAbstractImplementer; 48 import compiler.jvmci.common.testcases.MultipleImplementersInterface; 49 import compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender; 50 import compiler.jvmci.common.testcases.SingleImplementer; 51 import compiler.jvmci.common.testcases.SingleImplementerInterface; 52 import compiler.jvmci.common.testcases.SingleSubclass; 53 import compiler.jvmci.common.testcases.SingleSubclassedClass; 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.lang.reflect.Method; 61 import java.util.HashSet; 62 import java.util.Set; 63 import java.util.stream.Stream; 64 65 public class GetVtableIndexForInterfaceTest { 66 private static final int INVALID_VTABLE_INDEX = -4; // see method.hpp: VtableIndexFlag 67 68 public static void main(String args[]) { 69 GetVtableIndexForInterfaceTest test 70 = new GetVtableIndexForInterfaceTest(); 71 try { 72 for (TestCase tcase : createTestCases()) { 73 test.runTest(tcase); 74 } 75 } catch (NoSuchMethodException e) { 76 throw new Error("TEST BUG: can't find requested method", e); 77 } 78 } 79 80 private static Set<TestCase> createTestCases() { 81 Set<TestCase> result = new HashSet<>(); 82 Stream.of( 83 AbstractClass.class, 84 SingleImplementer.class, 85 SingleImplementerInterface.class, 86 MultipleImplementersInterface.class, 87 MultipleImplementersInterfaceExtender.class, 88 SingleSubclass.class, 89 SingleSubclassedClass.class, 90 DoNotExtendClass.class, 91 MultipleAbstractImplementer.class 92 ) 93 .forEach(Utils::ensureClassIsLoaded); 94 // non iface method 95 result.add(new TestCase(SingleImplementer.class, 96 SingleImplementer.class, "nonInterfaceMethod", 97 false, InternalError.class)); 98 // iface method w/o default implementation 99 result.add(new TestCase(SingleImplementer.class, 100 SingleImplementerInterface.class, "interfaceMethod", false)); 101 /* another iface which provides default implementation for the 102 original iface*/ 103 result.add(new TestCase(MultipleImplementersInterfaceExtender.class, 104 MultipleImplementersInterface.class, "testMethod", false, 105 InternalError.class)); 106 // iface method w/ default implementation 107 result.add(new TestCase(SingleImplementer.class, 108 SingleImplementerInterface.class, "defaultMethod", true)); 109 // non iface class 110 result.add(new TestCase(SingleSubclass.class, 111 SingleSubclassedClass.class, "inheritedMethod", false, 112 InternalError.class)); 113 // class not implementing iface 114 result.add(new TestCase(DoNotExtendClass.class, 115 SingleImplementerInterface.class, "defaultMethod", false)); 116 // abstract class which doesn't implement iface 117 result.add(new TestCase(AbstractClass.class, 118 SingleImplementerInterface.class, "defaultMethod", false)); 119 // abstract class which implements iface 120 result.add(new TestCase(MultipleAbstractImplementer.class, 121 MultipleImplementersInterface.class, "defaultMethod", true)); 122 // class not initialized 123 result.add(new TestCase(AnotherSingleImplementer.class, 124 AnotherSingleImplementerInterface.class, "defaultMethod", 125 false, InternalError.class)); 126 return result; 127 } 128 129 private void runTest(TestCase tcase) throws NoSuchMethodException { 130 System.out.println(tcase); 131 Method method = tcase.holder.getDeclaredMethod(tcase.methodName); 132 HotSpotResolvedObjectType metaspaceKlass = CompilerToVMHelper 133 .lookupType(Utils.toJVMTypeSignature(tcase.receiver), 134 getClass(), /* resolve = */ true); 135 HotSpotResolvedJavaMethod metaspaceMethod = CTVMUtilities 136 .getResolvedMethod(tcase.holder, method); 137 int index = 0; 138 try { 139 index = CompilerToVMHelper 140 .getVtableIndexForInterfaceMethod(metaspaceKlass, 141 metaspaceMethod); 142 } catch (Throwable t) { 143 if (tcase.isPositive || tcase.expectedException == null) { 144 throw new Error("Caught unexpected exception " + t); 145 } 146 if (!tcase.expectedException.equals(t.getClass())) { 147 throw new Error(String.format("Caught %s while expected %s", 148 t.getClass().getName(), 149 tcase.expectedException.getName())); 150 } 151 return; 152 } 153 if (tcase.expectedException != null) { 154 throw new AssertionError("Expected exception wasn't caught: " 155 + tcase.expectedException.getName()); 156 } 157 if (tcase.isPositive) { 158 Asserts.assertNE(index, INVALID_VTABLE_INDEX, 159 "Unexpected: got invalid index"); 160 } else { 161 Asserts.assertEQ(index, INVALID_VTABLE_INDEX, 162 "Unexpected: got valid index "); 163 } 164 } 165 166 private static class TestCase { 167 public final Class<?> receiver; 168 public final Class<?> holder; 169 public final String methodName; 170 public final boolean isPositive; 171 public final Class<? extends Throwable> expectedException; 172 173 public TestCase(Class<?> receiver, Class<?> holder, String methodName, 174 boolean isPositive, 175 Class<? extends Throwable> expectedException) { 176 this.receiver = receiver; 177 this.holder = holder; 178 this.methodName = methodName; 179 this.isPositive = isPositive; 180 this.expectedException = expectedException; 181 } 182 183 public TestCase(Class<?> receiver, Class<?> holder, String methodName, 184 boolean isPositive) { 185 this(receiver, holder, methodName, isPositive, null); 186 } 187 188 @Override 189 public String toString() { 190 return String.format("CASE: receiver=%s, holder=%s, method=%s," 191 + " isPositive=%s%n", receiver.getName(), holder.getName(), 192 methodName, isPositive); 193 } 194 } 195 }