Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/RelinkableCallSite.java
41154 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink;6162import java.lang.invoke.CallSite;63import java.lang.invoke.MethodHandle;64import jdk.dynalink.linker.GuardedInvocation;65import jdk.dynalink.support.ChainedCallSite;66import jdk.dynalink.support.SimpleRelinkableCallSite;6768/**69* Interface for call sites managed by a {@link DynamicLinker}. Users of70* Dynalink must use subclasses of {@link CallSite} that also implement this71* interface as their call site implementations. There is a readily usable72* {@link SimpleRelinkableCallSite} subclass that implements monomorphic inline73* caching strategy as well as {@link ChainedCallSite} that implements a74* polymorphic inline caching strategy and retains a chain of previously linked75* method handles. A relinkable call site will be managed by a76* {@link DynamicLinker} object after being associated with it using its77* {@link DynamicLinker#link(RelinkableCallSite)} method.78*/79public interface RelinkableCallSite {80/**81* Invoked by dynamic linker to initialize the relinkable call site by82* setting a relink-and-invoke method handle. The call site implementation83* is supposed to set this method handle as its target using84* {@link CallSite#setTarget(MethodHandle)}. Relink-and-invoke is the85* initial method handle set by86* {@link DynamicLinker#link(RelinkableCallSite)} that will cause the call87* site to be relinked to an appropriate target on its first invocation88* based on its arguments, and that linked target will then be invoked89* (hence the name). This linking protocol effectively delays linking until90* the call site is invoked with actual arguments and thus ensures that91* linkers can make nuanced linking decisions based on those arguments and92* not just on the static method type of the call site.93* @param relinkAndInvoke a relink-and-invoke method handle supplied by94* Dynalink.95*/96public void initialize(MethodHandle relinkAndInvoke);9798/**99* Returns the descriptor for this call site.100*101* @return the descriptor for this call site.102*/103public CallSiteDescriptor getDescriptor();104105/**106* This method will be called by the dynamic linker every time the call site107* is relinked (but see108* {@link #resetAndRelink(GuardedInvocation, MethodHandle)} for an109* exception). It will be passed a {@code GuardedInvocation} that the call110* site should incorporate into its target method handle. When this method111* is called, the call site is allowed to keep other non-invalidated112* invocations around for implementation of polymorphic inline caches and113* compose them with this invocation to form its final target.114*115* @param guardedInvocation the guarded invocation that the call site should116* incorporate into its target method handle.117* @param relinkAndInvoke a relink-and-invoke method handle. This is a118* method handle matching the method type of the call site that is supplied119* by the {@link DynamicLinker} as a callback. It should be used by this120* call site as the ultimate fallback when it can't invoke its target with121* the passed arguments. The fallback method is such that when it's invoked,122* it'll try to obtain an adequate target {@link GuardedInvocation} for the123* invocation, and subsequently invoke124* {@link #relink(GuardedInvocation, MethodHandle)} or125* {@link #resetAndRelink(GuardedInvocation, MethodHandle)}, and finally126* invoke the target.127*/128public void relink(GuardedInvocation guardedInvocation, MethodHandle relinkAndInvoke);129130/**131* This method will be called by the dynamic linker every time the call site132* is relinked <b>and</b> the linker wishes the call site to throw away any133* prior linkage state (that is how it differs from134* {@link #relink(GuardedInvocation, MethodHandle)}). It will be passed a135* {@code GuardedInvocation} that the call site should use to build its new136* target method handle. When this method is called, the call site is137* discouraged from keeping any previous state, and is supposed to only138* link the current invocation.139*140* @param guardedInvocation the guarded invocation that the call site should141* use to build its target method handle.142* @param relinkAndInvoke a relink-and-invoke method handle. This is a143* method handle matching the method type of the call site that is supplied144* by the {@link DynamicLinker} as a callback. It should be used by this145* call site as the ultimate fallback when it can't invoke its target with146* the passed arguments. The fallback method is such that when it's invoked,147* it'll try to obtain an adequate target {@link GuardedInvocation} for the148* invocation, and subsequently invoke149* {@link #relink(GuardedInvocation, MethodHandle)} or150* {@link #resetAndRelink(GuardedInvocation, MethodHandle)}, and finally151* invoke the target.152*/153public void resetAndRelink(GuardedInvocation guardedInvocation, MethodHandle relinkAndInvoke);154}155156157