Path: blob/jdk8u272-b10-aarch32-20201026/nashorn/src/jdk/internal/dynalink/linker/GuardedInvocation.java
48797 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 Szegedi3435Licensed under both the Apache License, Version 2.0 (the "Apache License")36and the BSD License (the "BSD License"), with licensee being free to37choose either of the two at their discretion.3839You may not use this file except in compliance with either the Apache40License or the BSD License.4142If you choose to use this file in compliance with the Apache License, the43following notice applies to you:4445You may obtain a copy of the Apache License at4647http://www.apache.org/licenses/LICENSE-2.04849Unless required by applicable law or agreed to in writing, software50distributed under the License is distributed on an "AS IS" BASIS,51WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or52implied. See the License for the specific language governing53permissions and limitations under the License.5455If you choose to use this file in compliance with the BSD License, the56following notice applies to you:5758Redistribution and use in source and binary forms, with or without59modification, are permitted provided that the following conditions are60met:61* Redistributions of source code must retain the above copyright62notice, this list of conditions and the following disclaimer.63* Redistributions in binary form must reproduce the above copyright64notice, this list of conditions and the following disclaimer in the65documentation and/or other materials provided with the distribution.66* Neither the name of the copyright holder nor the names of67contributors may be used to endorse or promote products derived from68this software without specific prior written permission.6970THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS71IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED72TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A73PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER74BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR75CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF76SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR77BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,78WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR79OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF80ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.81*/8283package jdk.internal.dynalink.linker;8485import static jdk.nashorn.internal.lookup.Lookup.MH;8687import java.lang.invoke.MethodHandle;88import java.lang.invoke.MethodHandles;89import java.lang.invoke.MethodType;90import java.lang.invoke.SwitchPoint;91import java.lang.invoke.WrongMethodTypeException;92import java.util.List;93import java.util.Objects;94import jdk.internal.dynalink.CallSiteDescriptor;95import jdk.internal.dynalink.support.Guards;9697/**98* Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard99* method handle that defines the applicability of the invocation handle, and a switch point that can be used for100* external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard101* handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the102* switch point are optional; neither, one, or both can be present.103*104* @author Attila Szegedi105*/106public class GuardedInvocation {107private final MethodHandle invocation;108private final MethodHandle guard;109private final Class<? extends Throwable> exception;110private final SwitchPoint[] switchPoints;111112/**113* Creates a new guarded invocation. This invocation is unconditional as it has no invalidations.114*115* @param invocation the method handle representing the invocation. Must not be null.116* @throws NullPointerException if invocation is null.117*/118public GuardedInvocation(final MethodHandle invocation) {119this(invocation, null, (SwitchPoint)null, null);120}121122/**123* Creates a new guarded invocation.124*125* @param invocation the method handle representing the invocation. Must not be null.126* @param guard the method handle representing the guard. Must have the same method type as the invocation, except127* it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null to represent128* an unconditional invocation, although that is unusual.129* @throws NullPointerException if invocation is null.130*/131public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard) {132this(invocation, guard, (SwitchPoint)null, null);133}134135/**136* Creates a new guarded invocation.137*138* @param invocation the method handle representing the invocation. Must not be null.139* @param switchPoint the optional switch point that can be used to invalidate this linkage.140* @throws NullPointerException if invocation is null.141*/142public GuardedInvocation(final MethodHandle invocation, final SwitchPoint switchPoint) {143this(invocation, null, switchPoint, null);144}145146/**147* Creates a new guarded invocation.148*149* @param invocation the method handle representing the invocation. Must not be null.150* @param guard the method handle representing the guard. Must have the same method type as the invocation, except151* it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it152* and the switch point are null, this represents an unconditional invocation, which is legal but unusual.153* @param switchPoint the optional switch point that can be used to invalidate this linkage.154* @throws NullPointerException if invocation is null.155*/156public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) {157this(invocation, guard, switchPoint, null);158}159160/**161* Creates a new guarded invocation.162*163* @param invocation the method handle representing the invocation. Must not be null.164* @param guard the method handle representing the guard. Must have the same method type as the invocation, except165* it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it166* and the switch point are null, this represents an unconditional invocation, which is legal but unusual.167* @param switchPoint the optional switch point that can be used to invalidate this linkage.168* @param exception the optional exception type that is expected to be thrown by the invocation and that also169* invalidates the linkage.170* @throws NullPointerException if invocation is null.171*/172public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) {173this.invocation = Objects.requireNonNull(invocation);174this.guard = guard;175this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint };176this.exception = exception;177}178179/**180* Creates a new guarded invocation181*182* @param invocation the method handle representing the invocation. Must not be null.183* @param guard the method handle representing the guard. Must have the same method type as the invocation, except184* it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it185* and the switch point are null, this represents an unconditional invocation, which is legal but unusual.186* @param switchPoints the optional switch points that can be used to invalidate this linkage.187* @param exception the optional exception type that is expected to be thrown by the invocation and that also188* invalidates the linkage.189* @throws NullPointerException if invocation is null.190*/191public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) {192this.invocation = Objects.requireNonNull(invocation);193this.guard = guard;194this.switchPoints = switchPoints == null ? null : switchPoints.clone();195this.exception = exception;196}197198/**199* Returns the invocation method handle.200*201* @return the invocation method handle. It will never be null.202*/203public MethodHandle getInvocation() {204return invocation;205}206207/**208* Returns the guard method handle.209*210* @return the guard method handle. Can be null.211*/212public MethodHandle getGuard() {213return guard;214}215216/**217* Returns the switch point that can be used to invalidate the invocation handle.218*219* @return the switch point that can be used to invalidate the invocation handle. Can be null.220*/221public SwitchPoint[] getSwitchPoints() {222return switchPoints == null ? null : switchPoints.clone();223}224225/**226* Returns the exception type that if thrown should be used to invalidate the linkage.227*228* @return the exception type that if thrown should be used to invalidate the linkage. Can be null.229*/230public Class<? extends Throwable> getException() {231return exception;232}233234/**235* Returns true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.236* @return true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.237*/238public boolean hasBeenInvalidated() {239if (switchPoints == null) {240return false;241}242for (final SwitchPoint sp : switchPoints) {243if (sp.hasBeenInvalidated()) {244return true;245}246}247return false;248}249250/**251* Asserts that the invocation is of the specified type, and the guard (if present) is of the specified type with a252* boolean return type.253*254* @param type the asserted type255* @throws WrongMethodTypeException if the invocation and the guard are not of the expected method type.256*/257public void assertType(final MethodType type) {258assertType(invocation, type);259if (guard != null) {260assertType(guard, type.changeReturnType(Boolean.TYPE));261}262}263264/**265* Creates a new guarded invocation with different methods, preserving the switch point.266*267* @param newInvocation the new invocation268* @param newGuard the new guard269* @return a new guarded invocation with the replaced methods and the same switch point as this invocation.270*/271public GuardedInvocation replaceMethods(final MethodHandle newInvocation, final MethodHandle newGuard) {272return new GuardedInvocation(newInvocation, newGuard, switchPoints, exception);273}274275/**276* Add a switchpoint to this guarded invocation277* @param newSwitchPoint new switchpoint, or null for nop278* @return new guarded invocation with the extra switchpoint279*/280public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) {281if (newSwitchPoint == null) {282return this;283}284285final SwitchPoint[] newSwitchPoints;286if (switchPoints != null) {287newSwitchPoints = new SwitchPoint[switchPoints.length + 1];288System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length);289newSwitchPoints[switchPoints.length] = newSwitchPoint;290} else {291newSwitchPoints = new SwitchPoint[] { newSwitchPoint };292}293294return new GuardedInvocation(invocation, guard, newSwitchPoints, exception);295}296297private GuardedInvocation replaceMethodsOrThis(final MethodHandle newInvocation, final MethodHandle newGuard) {298if (newInvocation == invocation && newGuard == guard) {299return this;300}301return replaceMethods(newInvocation, newGuard);302}303304/**305* Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation306* and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for307* the guard). If the invocation already is of the required type, returns this object.308* @param newType the new type of the invocation.309* @return a guarded invocation with the new type applied to it.310*/311public GuardedInvocation asType(final MethodType newType) {312return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType));313}314315/**316* Changes the type of the invocation, as if {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to317* its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially318* truncated for the guard). If the invocation already is of the required type, returns this object.319* @param linkerServices the linker services to use for the conversion320* @param newType the new type of the invocation.321* @return a guarded invocation with the new type applied to it.322*/323public GuardedInvocation asType(final LinkerServices linkerServices, final MethodType newType) {324return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null :325Guards.asType(linkerServices, guard, newType));326}327328/**329* Changes the type of the invocation, as if {@link LinkerServices#asTypeLosslessReturn(MethodHandle, MethodType)} was330* applied to its invocation and {@link LinkerServices#asType(MethodHandle, MethodType)} applied to its guard, if it331* has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the332* invocation doesn't change its type, returns this object.333* @param linkerServices the linker services to use for the conversion334* @param newType the new type of the invocation.335* @return a guarded invocation with the new type applied to it.336*/337public GuardedInvocation asTypeSafeReturn(final LinkerServices linkerServices, final MethodType newType) {338return replaceMethodsOrThis(linkerServices.asTypeLosslessReturn(invocation, newType), guard == null ? null :339Guards.asType(linkerServices, guard, newType));340}341342/**343* Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation344* and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the345* required type, returns this object.346* @param desc a call descriptor whose method type is adapted.347* @return a guarded invocation with the new type applied to it.348*/349public GuardedInvocation asType(final CallSiteDescriptor desc) {350return asType(desc.getMethodType());351}352353/**354* Applies argument filters to both the invocation and the guard (if there is one).355* @param pos the position of the first argument being filtered356* @param filters the argument filters357* @return a filtered invocation358*/359public GuardedInvocation filterArguments(final int pos, final MethodHandle... filters) {360return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters), guard == null ? null :361MethodHandles.filterArguments(guard, pos, filters));362}363364/**365* Makes an invocation that drops arguments in both the invocation and the guard (if there is one).366* @param pos the position of the first argument being dropped367* @param valueTypes the types of the values being dropped368* @return an invocation that drops arguments369*/370public GuardedInvocation dropArguments(final int pos, final List<Class<?>> valueTypes) {371return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :372MethodHandles.dropArguments(guard, pos, valueTypes));373}374375/**376* Makes an invocation that drops arguments in both the invocation and the guard (if there is one).377* @param pos the position of the first argument being dropped378* @param valueTypes the types of the values being dropped379* @return an invocation that drops arguments380*/381public GuardedInvocation dropArguments(final int pos, final Class<?>... valueTypes) {382return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :383MethodHandles.dropArguments(guard, pos, valueTypes));384}385386387/**388* Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.389* @param fallback the fallback method handle in case switchpoint is invalidated or guard returns false.390* @return a composite method handle.391*/392public MethodHandle compose(final MethodHandle fallback) {393return compose(fallback, fallback, fallback);394}395396/**397* Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.398* @param switchpointFallback the fallback method handle in case switchpoint is invalidated.399* @param guardFallback the fallback method handle in case guard returns false.400* @param catchFallback the fallback method in case the exception handler triggers401* @return a composite method handle.402*/403public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {404final MethodHandle guarded =405guard == null ?406invocation :407MethodHandles.guardWithTest(408guard,409invocation,410guardFallback);411412final MethodHandle catchGuarded =413exception == null ?414guarded :415MH.catchException(416guarded,417exception,418MethodHandles.dropArguments(419catchFallback,4200,421exception));422423if (switchPoints == null) {424return catchGuarded;425}426427MethodHandle spGuarded = catchGuarded;428for (final SwitchPoint sp : switchPoints) {429spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);430}431432return spGuarded;433}434435private static void assertType(final MethodHandle mh, final MethodType type) {436if(!mh.type().equals(type)) {437throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type());438}439}440}441442443