Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/linker/GuardedInvocation.java
41161 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.linker;6162import java.lang.invoke.MethodHandle;63import java.lang.invoke.MethodHandles;64import java.lang.invoke.MethodType;65import java.lang.invoke.SwitchPoint;66import java.util.List;67import java.util.Objects;68import java.util.function.Supplier;69import jdk.dynalink.CallSiteDescriptor;70import jdk.dynalink.linker.support.Guards;7172/**73* Represents a conditionally valid method handle. Usually produced as a return74* value of75* {@link GuardingDynamicLinker#getGuardedInvocation(LinkRequest, LinkerServices)}76* and77* {@link GuardingTypeConverterFactory#convertToType(Class, Class, Supplier)}.78* It is an immutable tuple of an invocation method handle, a guard method79* handle that defines the applicability of the invocation handle, zero or more80* switch points that can be used for external invalidation of the invocation81* handle, and an exception type that if thrown during an invocation of the82* method handle also invalidates it. The invocation handle is suitable for83* invocation if the guard handle returns true for its arguments, and as long84* as any of the switch points are not invalidated, and as long as it does not85* throw an exception of the designated type. The guard, the switch points, and86* the exception type are all optional (a guarded invocation having none of them87* is unconditionally valid).88*/89public class GuardedInvocation {90private final MethodHandle invocation;91private final MethodHandle guard;92private final Class<? extends Throwable> exception;93private final SwitchPoint[] switchPoints;9495/**96* Creates a new unconditional guarded invocation. It is unconditional as it97* has no invalidations.98*99* @param invocation the method handle representing the invocation. Must not100* be null.101* @throws NullPointerException if invocation is null.102*/103public GuardedInvocation(final MethodHandle invocation) {104this(invocation, null, (SwitchPoint)null, null);105}106107/**108* Creates a new guarded invocation, with a guard method handle.109*110* @param invocation the method handle representing the invocation. Must not111* be null.112* @param guard the method handle representing the guard. Must have be113* compatible with the {@code invocation} handle as per114* {@link MethodHandles#guardWithTest(MethodHandle, MethodHandle, MethodHandle)}.115* For some useful guards, check out the {@link Guards} class. It can be116* null to represent an unconditional invocation.117* @throws NullPointerException if invocation is null.118*/119public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard) {120this(invocation, guard, (SwitchPoint)null, null);121}122123/**124* Creates a new guarded invocation that can be invalidated by a switch125* point.126*127* @param invocation the method handle representing the invocation. Must128* not be null.129* @param switchPoint the optional switch point that can be used to130* invalidate this linkage. It can be null. If it is null, this represents131* an unconditional invocation.132* @throws NullPointerException if invocation is null.133*/134public GuardedInvocation(final MethodHandle invocation, final SwitchPoint switchPoint) {135this(invocation, null, switchPoint, null);136}137138/**139* Creates a new guarded invocation, with both a guard method handle and a140* switch point that can be used to invalidate it.141*142* @param invocation the method handle representing the invocation. Must143* not be null.144* @param guard the method handle representing the guard. Must have be145* compatible with the {@code invocation} handle as per146* {@link MethodHandles#guardWithTest(MethodHandle, MethodHandle, MethodHandle)}.147* For some useful guards, check out the {@link Guards} class. It can be148* null. If both it and the switch point are null, this represents an149* unconditional invocation.150* @param switchPoint the optional switch point that can be used to151* invalidate this linkage.152* @throws NullPointerException if invocation is null.153*/154public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) {155this(invocation, guard, switchPoint, null);156}157158/**159* Creates a new guarded invocation, with a guard method handle, a160* switch point that can be used to invalidate it, and an exception that if161* thrown when invoked also invalidates it.162*163* @param invocation the method handle representing the invocation. Must not164* be null.165* @param guard the method handle representing the guard. Must have be166* compatible with the {@code invocation} handle as per167* {@link MethodHandles#guardWithTest(MethodHandle, MethodHandle, MethodHandle)}.168* For some useful guards, check out the {@link Guards} class. It can be169* null. If it and the switch point and the exception are all null, this170* represents an unconditional invocation.171* @param switchPoint the optional switch point that can be used to172* invalidate this linkage.173* @param exception the optional exception type that is when thrown by the174* invocation also invalidates it.175* @throws NullPointerException if invocation is null.176*/177public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) {178this.invocation = Objects.requireNonNull(invocation);179this.guard = guard;180this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint };181if (exception != null && !Throwable.class.isAssignableFrom(exception)) {182throw new IllegalArgumentException(exception.getName() + " is not assignable from Throwable");183}184this.exception = exception;185}186187/**188* Creates a new guarded invocation, with a guard method handle, any number189* of switch points that can be used to invalidate it, and an exception that190* if thrown when invoked also invalidates it.191*192* @param invocation the method handle representing the invocation. Must not193* be null.194* @param guard the method handle representing the guard. Must have be195* compatible with the {@code invocation} handle as per196* {@link MethodHandles#guardWithTest(MethodHandle, MethodHandle, MethodHandle)}.197* For some useful guards, check out the {@link Guards} class. It can be198* null. If it and the exception are both null, and no switch points were199* specified, this represents an unconditional invocation.200* @param switchPoints optional switch points that can be used to201* invalidate this linkage.202* @param exception the optional exception type that is when thrown by the203* invocation also invalidates it.204* @throws NullPointerException if invocation is null.205*/206public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) {207this.invocation = Objects.requireNonNull(invocation);208this.guard = guard;209this.switchPoints = switchPoints == null ? null : switchPoints.clone();210if (exception != null && !Throwable.class.isAssignableFrom(exception)) {211throw new IllegalArgumentException(exception.getName() + " is not assignable from Throwable");212}213this.exception = exception;214}215216/**217* Returns the invocation method handle.218*219* @return the invocation method handle. It will never be null.220*/221public MethodHandle getInvocation() {222return invocation;223}224225/**226* Returns the guard method handle.227*228* @return the guard method handle. Can be null.229*/230public MethodHandle getGuard() {231return guard;232}233234/**235* Returns the switch points that can be used to invalidate the linkage of236* this invocation handle.237*238* @return the switch points that can be used to invalidate the linkage of239* this invocation handle. Can be null.240*/241public SwitchPoint[] getSwitchPoints() {242return switchPoints == null ? null : switchPoints.clone();243}244245/**246* Returns the exception type that if thrown by the invocation should247* invalidate the linkage of this guarded invocation.248*249* @return the exception type that if thrown should be used to invalidate250* the linkage. Can be null.251*/252public Class<? extends Throwable> getException() {253return exception;254}255256/**257* Returns true if and only if this guarded invocation has at least one258* invalidated switch point.259* @return true if and only if this guarded invocation has at least one260* invalidated switch point.261*/262public boolean hasBeenInvalidated() {263if (switchPoints == null) {264return false;265}266for (final SwitchPoint sp : switchPoints) {267if (sp.hasBeenInvalidated()) {268return true;269}270}271return false;272}273274/**275* Creates a new guarded invocation with different methods, preserving the switch point.276*277* @param newInvocation the new invocation278* @param newGuard the new guard279* @return a new guarded invocation with the replaced methods and the same switch point as this invocation.280*/281public GuardedInvocation replaceMethods(final MethodHandle newInvocation, final MethodHandle newGuard) {282return new GuardedInvocation(newInvocation, newGuard, switchPoints, exception);283}284285/**286* Create a new guarded invocation with an added switch point.287* @param newSwitchPoint new switch point. Can be null in which case this288* method return the current guarded invocation with no changes.289* @return a guarded invocation with the added switch point.290*/291public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) {292if (newSwitchPoint == null) {293return this;294}295296final SwitchPoint[] newSwitchPoints;297if (switchPoints != null) {298newSwitchPoints = new SwitchPoint[switchPoints.length + 1];299System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length);300newSwitchPoints[switchPoints.length] = newSwitchPoint;301} else {302newSwitchPoints = new SwitchPoint[] { newSwitchPoint };303}304305return new GuardedInvocation(invocation, guard, newSwitchPoints, exception);306}307308private GuardedInvocation replaceMethodsOrThis(final MethodHandle newInvocation, final MethodHandle newGuard) {309if (newInvocation == invocation && newGuard == guard) {310return this;311}312return replaceMethods(newInvocation, newGuard);313}314315/**316* Changes the type of the invocation, as if317* {@link MethodHandle#asType(MethodType)} was applied to its invocation318* and its guard, if it has one (with return type changed to boolean, and319* parameter count potentially truncated for the guard). If the invocation320* already is of the required type, returns this object.321* @param newType the new type of the invocation.322* @return a guarded invocation with the new type applied to it.323*/324public GuardedInvocation asType(final MethodType newType) {325return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType));326}327328/**329* Changes the type of the invocation, as if330* {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to331* its invocation and its guard, if it has one (with return type changed to332* boolean, and parameter count potentially truncated for the guard). If the333* invocation already is of the required type, returns this object.334* @param linkerServices the linker services to use for the conversion335* @param newType the new type of the invocation.336* @return a guarded invocation with the new type applied to it.337*/338public GuardedInvocation asType(final LinkerServices linkerServices, final MethodType newType) {339return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null :340Guards.asType(linkerServices, guard, newType));341}342343/**344* Changes the type of the invocation, as if345* {@link LinkerServices#asTypeLosslessReturn(MethodHandle, MethodType)} was346* applied to its invocation and347* {@link LinkerServices#asType(MethodHandle, MethodType)} applied to its348* guard, if it has one (with return type changed to boolean, and parameter349* count potentially truncated for the guard). If the invocation doesn't350* change its type, returns this object.351* @param linkerServices the linker services to use for the conversion352* @param newType the new type of the invocation.353* @return a guarded invocation with the new type applied to it.354*/355public GuardedInvocation asTypeSafeReturn(final LinkerServices linkerServices, final MethodType newType) {356return replaceMethodsOrThis(linkerServices.asTypeLosslessReturn(invocation, newType), guard == null ? null :357Guards.asType(linkerServices, guard, newType));358}359360/**361* Changes the type of the invocation, as if362* {@link MethodHandle#asType(MethodType)} was applied to its invocation363* and its guard, if it has one (with return type changed to boolean for364* guard). If the invocation already is of the required type, returns this365* object.366* @param desc a call descriptor whose method type is adapted.367* @return a guarded invocation with the new type applied to it.368*/369public GuardedInvocation asType(final CallSiteDescriptor desc) {370return asType(desc.getMethodType());371}372373/**374* Applies argument filters to both the invocation and the guard375* (if it exists and has at least {@code pos + 1} parameters) with376* {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)}.377* @param pos the position of the first argument being filtered378* @param filters the argument filters379* @return a filtered invocation380*/381public GuardedInvocation filterArguments(final int pos, final MethodHandle... filters) {382return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters),383guard == null || pos >= guard.type().parameterCount() ?384guard : MethodHandles.filterArguments(guard, pos, filters));385}386387/**388* Makes an invocation that drops arguments in both the invocation and the389* guard (if it exists and has at least {@code pos} parameters) with390* {@link MethodHandles#dropArguments(MethodHandle, int, List)}.391* @param pos the position of the first argument being dropped392* @param valueTypes the types of the values being dropped393* @return an invocation that drops arguments394*/395public GuardedInvocation dropArguments(final int pos, final List<Class<?>> valueTypes) {396return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes),397guard == null || pos > guard.type().parameterCount() ?398guard : MethodHandles.dropArguments(guard, pos, valueTypes));399}400401/**402* Makes an invocation that drops arguments in both the invocation and the403* guard (if it exists and has at least {@code pos} parameters) with404* {@link MethodHandles#dropArguments(MethodHandle, int, Class...)}.405* @param pos the position of the first argument being dropped406* @param valueTypes the types of the values being dropped407* @return an invocation that drops arguments408*/409public GuardedInvocation dropArguments(final int pos, final Class<?>... valueTypes) {410return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes),411guard == null || pos > guard.type().parameterCount() ?412guard : MethodHandles.dropArguments(guard, pos, valueTypes));413}414415416/**417* Composes the invocation, guard, switch points, and the exception into a418* composite method handle that knows how to fall back when the guard fails419* or the invocation is invalidated.420* @param fallback the fallback method handle for when a switch point is421* invalidated, a guard returns false, or invalidating exception is thrown.422* @return a composite method handle.423*/424public MethodHandle compose(final MethodHandle fallback) {425return compose(fallback, fallback, fallback);426}427428/**429* Composes the invocation, guard, switch points, and the exception into a430* composite method handle that knows how to fall back when the guard fails431* or the invocation is invalidated.432* @param switchpointFallback the fallback method handle in case a switch433* point is invalidated.434* @param guardFallback the fallback method handle in case guard returns435* false.436* @param catchFallback the fallback method in case the exception handler437* triggers.438* @return a composite method handle.439*/440public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {441final MethodHandle guarded =442guard == null ?443invocation :444MethodHandles.guardWithTest(445guard,446invocation,447guardFallback);448449final MethodHandle catchGuarded =450exception == null ?451guarded :452MethodHandles.catchException(453guarded,454exception,455MethodHandles.dropArguments(456catchFallback,4570,458exception));459460if (switchPoints == null) {461return catchGuarded;462}463464MethodHandle spGuarded = catchGuarded;465for (final SwitchPoint sp : switchPoints) {466spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);467}468469return spGuarded;470}471}472473474