1 /* 2 * Copyright (c) 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.dynalink.test; 26 27 import java.lang.invoke.MethodHandles; 28 import java.lang.invoke.MethodType; 29 import jdk.dynalink.CallSiteDescriptor; 30 import jdk.dynalink.DynamicLinker; 31 import jdk.dynalink.DynamicLinkerFactory; 32 import jdk.dynalink.Operation; 33 import jdk.dynalink.StandardNamespace; 34 import jdk.dynalink.StandardOperation; 35 import jdk.dynalink.linker.GuardingDynamicLinker; 36 import jdk.dynalink.support.SimpleRelinkableCallSite; 37 import org.testng.Assert; 38 import org.testng.annotations.Test; 39 40 public class LinkedCallSiteLocationTest { 41 private static final Operation GET_METHOD = StandardOperation.GET.withNamespace(StandardNamespace.METHOD); 42 @Test 43 public void testLinkedCallSiteLocation() throws Throwable { 44 final StackTraceElement[] lastLinked = new StackTraceElement[1]; 45 46 final GuardingDynamicLinker testLinker = 47 (r, s) -> { lastLinked[0] = DynamicLinker.getLinkedCallSiteLocation(); return null; }; 48 49 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 50 factory.setPrioritizedLinker(testLinker); 51 final DynamicLinker linker = factory.createLinker(); 52 final SimpleRelinkableCallSite callSite = new SimpleRelinkableCallSite( 53 new CallSiteDescriptor( 54 MethodHandles.lookup(), 55 GET_METHOD.named("foo"), 56 MethodType.methodType(void.class, Object.class))); 57 linker.link(callSite); 58 59 // Test initial linking 60 callSite.dynamicInvoker().invoke(new TestClass1()); final int l1 = getLineNumber(); 61 assertLocation(lastLinked[0], l1); 62 63 // Test relinking 64 callSite.dynamicInvoker().invoke(new TestClass2()); final int l2 = getLineNumber(); 65 assertLocation(lastLinked[0], l2); 66 } 67 68 private void assertLocation(final StackTraceElement frame, final int lineNumber) { 69 Assert.assertNotNull(frame); 70 Assert.assertEquals(frame.getLineNumber(), lineNumber); 71 Assert.assertEquals(frame.getClassName(), this.getClass().getName()); 72 } 73 74 private static int getLineNumber() { 75 return StackWalker.getInstance().walk(s -> s.skip(1).findFirst().get().getLineNumber()); 76 } 77 78 public static class TestClass1 { 79 public void foo() { 80 } 81 } 82 83 public static class TestClass2 { 84 public void foo() { 85 } 86 } 87 }