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 static jdk.dynalink.StandardOperation.CALL_METHOD; 28 29 import java.lang.invoke.MethodHandles; 30 import java.lang.invoke.MethodType; 31 import jdk.dynalink.CallSiteDescriptor; 32 import jdk.dynalink.DynamicLinker; 33 import jdk.dynalink.DynamicLinkerFactory; 34 import jdk.dynalink.NamedOperation; 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 @Test 42 public void testLinkedCallSiteLocation() throws Throwable { 43 final StackTraceElement[] lastLinked = new StackTraceElement[1]; 44 45 final GuardingDynamicLinker testLinker = 46 (r, s) -> { lastLinked[0] = DynamicLinker.getLinkedCallSiteLocation(); return null; }; 47 48 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 49 factory.setPrioritizedLinker(testLinker); 50 final DynamicLinker linker = factory.createLinker(); 51 final SimpleRelinkableCallSite callSite = new SimpleRelinkableCallSite( 52 new CallSiteDescriptor( 53 MethodHandles.lookup(), 54 new NamedOperation(CALL_METHOD, "foo"), 55 MethodType.methodType(void.class, Object.class))); 56 linker.link(callSite); 57 58 // Test initial linking 59 callSite.dynamicInvoker().invoke(new TestClass1()); final int l1 = getLineNumber(); 60 assertLocation(lastLinked[0], l1); 61 62 // Test relinking 63 callSite.dynamicInvoker().invoke(new TestClass2()); final int l2 = getLineNumber(); 64 assertLocation(lastLinked[0], l2); 65 } 66 67 private void assertLocation(final StackTraceElement frame, final int lineNumber) { 68 Assert.assertNotNull(frame); 69 Assert.assertEquals(frame.getLineNumber(), lineNumber); 70 Assert.assertEquals(frame.getClassName(), this.getClass().getName()); 71 } 72 73 private static int getLineNumber() { 74 return StackWalker.getInstance().walk(s -> s.skip(1).findFirst().get().getLineNumber()); 75 } 76 77 public static class TestClass1 { 78 public void foo() { 79 } 80 } 81 82 public static class TestClass2 { 83 public void foo() { 84 } 85 } 86 }