Thu, 24 May 2018 16:39:31 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | /* |
aoqi@0 | 27 | * This file is available under and governed by the GNU General Public |
aoqi@0 | 28 | * License version 2 only, as published by the Free Software Foundation. |
aoqi@0 | 29 | * However, the following notice accompanied the original version of this |
aoqi@0 | 30 | * file, and Oracle licenses the original version of this file under the BSD |
aoqi@0 | 31 | * license: |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | /* |
aoqi@0 | 34 | Copyright 2009-2013 Attila Szegedi |
aoqi@0 | 35 | |
aoqi@0 | 36 | Licensed under both the Apache License, Version 2.0 (the "Apache License") |
aoqi@0 | 37 | and the BSD License (the "BSD License"), with licensee being free to |
aoqi@0 | 38 | choose either of the two at their discretion. |
aoqi@0 | 39 | |
aoqi@0 | 40 | You may not use this file except in compliance with either the Apache |
aoqi@0 | 41 | License or the BSD License. |
aoqi@0 | 42 | |
aoqi@0 | 43 | If you choose to use this file in compliance with the Apache License, the |
aoqi@0 | 44 | following notice applies to you: |
aoqi@0 | 45 | |
aoqi@0 | 46 | You may obtain a copy of the Apache License at |
aoqi@0 | 47 | |
aoqi@0 | 48 | http://www.apache.org/licenses/LICENSE-2.0 |
aoqi@0 | 49 | |
aoqi@0 | 50 | Unless required by applicable law or agreed to in writing, software |
aoqi@0 | 51 | distributed under the License is distributed on an "AS IS" BASIS, |
aoqi@0 | 52 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
aoqi@0 | 53 | implied. See the License for the specific language governing |
aoqi@0 | 54 | permissions and limitations under the License. |
aoqi@0 | 55 | |
aoqi@0 | 56 | If you choose to use this file in compliance with the BSD License, the |
aoqi@0 | 57 | following notice applies to you: |
aoqi@0 | 58 | |
aoqi@0 | 59 | Redistribution and use in source and binary forms, with or without |
aoqi@0 | 60 | modification, are permitted provided that the following conditions are |
aoqi@0 | 61 | met: |
aoqi@0 | 62 | * Redistributions of source code must retain the above copyright |
aoqi@0 | 63 | notice, this list of conditions and the following disclaimer. |
aoqi@0 | 64 | * Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 65 | notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 66 | documentation and/or other materials provided with the distribution. |
aoqi@0 | 67 | * Neither the name of the copyright holder nor the names of |
aoqi@0 | 68 | contributors may be used to endorse or promote products derived from |
aoqi@0 | 69 | this software without specific prior written permission. |
aoqi@0 | 70 | |
aoqi@0 | 71 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 72 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
aoqi@0 | 73 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
aoqi@0 | 74 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER |
aoqi@0 | 75 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
aoqi@0 | 76 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
aoqi@0 | 77 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
aoqi@0 | 78 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
aoqi@0 | 79 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
aoqi@0 | 80 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
aoqi@0 | 81 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | |
aoqi@0 | 84 | package jdk.internal.dynalink.linker; |
aoqi@0 | 85 | |
attila@963 | 86 | import static jdk.nashorn.internal.lookup.Lookup.MH; |
attila@963 | 87 | |
aoqi@0 | 88 | import java.lang.invoke.MethodHandle; |
aoqi@0 | 89 | import java.lang.invoke.MethodHandles; |
aoqi@0 | 90 | import java.lang.invoke.MethodType; |
aoqi@0 | 91 | import java.lang.invoke.SwitchPoint; |
aoqi@0 | 92 | import java.lang.invoke.WrongMethodTypeException; |
aoqi@0 | 93 | import java.util.List; |
sundar@1231 | 94 | import java.util.Objects; |
aoqi@0 | 95 | import jdk.internal.dynalink.CallSiteDescriptor; |
aoqi@0 | 96 | import jdk.internal.dynalink.support.Guards; |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** |
aoqi@0 | 99 | * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard |
aoqi@0 | 100 | * method handle that defines the applicability of the invocation handle, and a switch point that can be used for |
aoqi@0 | 101 | * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard |
aoqi@0 | 102 | * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the |
aoqi@0 | 103 | * switch point are optional; neither, one, or both can be present. |
aoqi@0 | 104 | * |
aoqi@0 | 105 | * @author Attila Szegedi |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | public class GuardedInvocation { |
aoqi@0 | 108 | private final MethodHandle invocation; |
aoqi@0 | 109 | private final MethodHandle guard; |
attila@963 | 110 | private final Class<? extends Throwable> exception; |
attila@963 | 111 | private final SwitchPoint[] switchPoints; |
attila@963 | 112 | |
attila@963 | 113 | /** |
attila@963 | 114 | * Creates a new guarded invocation. This invocation is unconditional as it has no invalidations. |
attila@963 | 115 | * |
attila@963 | 116 | * @param invocation the method handle representing the invocation. Must not be null. |
attila@963 | 117 | * @throws NullPointerException if invocation is null. |
attila@963 | 118 | */ |
attila@963 | 119 | public GuardedInvocation(final MethodHandle invocation) { |
attila@963 | 120 | this(invocation, null, (SwitchPoint)null, null); |
attila@963 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Creates a new guarded invocation. |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * @param invocation the method handle representing the invocation. Must not be null. |
aoqi@0 | 127 | * @param guard the method handle representing the guard. Must have the same method type as the invocation, except |
aoqi@0 | 128 | * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null to represent |
aoqi@0 | 129 | * an unconditional invocation, although that is unusual. |
aoqi@0 | 130 | * @throws NullPointerException if invocation is null. |
aoqi@0 | 131 | */ |
attila@962 | 132 | public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard) { |
attila@963 | 133 | this(invocation, guard, (SwitchPoint)null, null); |
attila@963 | 134 | } |
attila@963 | 135 | |
attila@963 | 136 | /** |
attila@963 | 137 | * Creates a new guarded invocation. |
attila@963 | 138 | * |
attila@963 | 139 | * @param invocation the method handle representing the invocation. Must not be null. |
attila@963 | 140 | * @param switchPoint the optional switch point that can be used to invalidate this linkage. |
attila@963 | 141 | * @throws NullPointerException if invocation is null. |
attila@963 | 142 | */ |
attila@963 | 143 | public GuardedInvocation(final MethodHandle invocation, final SwitchPoint switchPoint) { |
attila@963 | 144 | this(invocation, null, switchPoint, null); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | /** |
aoqi@0 | 148 | * Creates a new guarded invocation. |
aoqi@0 | 149 | * |
aoqi@0 | 150 | * @param invocation the method handle representing the invocation. Must not be null. |
aoqi@0 | 151 | * @param guard the method handle representing the guard. Must have the same method type as the invocation, except |
aoqi@0 | 152 | * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it |
aoqi@0 | 153 | * and the switch point are null, this represents an unconditional invocation, which is legal but unusual. |
aoqi@0 | 154 | * @param switchPoint the optional switch point that can be used to invalidate this linkage. |
aoqi@0 | 155 | * @throws NullPointerException if invocation is null. |
aoqi@0 | 156 | */ |
attila@962 | 157 | public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) { |
attila@963 | 158 | this(invocation, guard, switchPoint, null); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | /** |
aoqi@0 | 162 | * Creates a new guarded invocation. |
aoqi@0 | 163 | * |
aoqi@0 | 164 | * @param invocation the method handle representing the invocation. Must not be null. |
aoqi@0 | 165 | * @param guard the method handle representing the guard. Must have the same method type as the invocation, except |
aoqi@0 | 166 | * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it |
aoqi@0 | 167 | * and the switch point are null, this represents an unconditional invocation, which is legal but unusual. |
attila@963 | 168 | * @param switchPoint the optional switch point that can be used to invalidate this linkage. |
attila@963 | 169 | * @param exception the optional exception type that is expected to be thrown by the invocation and that also |
attila@963 | 170 | * invalidates the linkage. |
aoqi@0 | 171 | * @throws NullPointerException if invocation is null. |
aoqi@0 | 172 | */ |
attila@963 | 173 | public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) { |
sundar@1231 | 174 | this.invocation = Objects.requireNonNull(invocation); |
attila@963 | 175 | this.guard = guard; |
attila@963 | 176 | this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint }; |
attila@963 | 177 | this.exception = exception; |
aoqi@0 | 178 | } |
attila@963 | 179 | |
attila@963 | 180 | /** |
attila@963 | 181 | * Creates a new guarded invocation |
attila@963 | 182 | * |
attila@963 | 183 | * @param invocation the method handle representing the invocation. Must not be null. |
attila@963 | 184 | * @param guard the method handle representing the guard. Must have the same method type as the invocation, except |
attila@963 | 185 | * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it |
attila@963 | 186 | * and the switch point are null, this represents an unconditional invocation, which is legal but unusual. |
attila@963 | 187 | * @param switchPoints the optional switch points that can be used to invalidate this linkage. |
attila@963 | 188 | * @param exception the optional exception type that is expected to be thrown by the invocation and that also |
attila@963 | 189 | * invalidates the linkage. |
attila@963 | 190 | * @throws NullPointerException if invocation is null. |
attila@963 | 191 | */ |
attila@963 | 192 | public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) { |
sundar@1231 | 193 | this.invocation = Objects.requireNonNull(invocation); |
attila@963 | 194 | this.guard = guard; |
attila@963 | 195 | this.switchPoints = switchPoints == null ? null : switchPoints.clone(); |
attila@963 | 196 | this.exception = exception; |
attila@963 | 197 | } |
attila@963 | 198 | |
aoqi@0 | 199 | /** |
aoqi@0 | 200 | * Returns the invocation method handle. |
aoqi@0 | 201 | * |
aoqi@0 | 202 | * @return the invocation method handle. It will never be null. |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | public MethodHandle getInvocation() { |
aoqi@0 | 205 | return invocation; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | /** |
aoqi@0 | 209 | * Returns the guard method handle. |
aoqi@0 | 210 | * |
aoqi@0 | 211 | * @return the guard method handle. Can be null. |
aoqi@0 | 212 | */ |
aoqi@0 | 213 | public MethodHandle getGuard() { |
aoqi@0 | 214 | return guard; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | /** |
aoqi@0 | 218 | * Returns the switch point that can be used to invalidate the invocation handle. |
aoqi@0 | 219 | * |
aoqi@0 | 220 | * @return the switch point that can be used to invalidate the invocation handle. Can be null. |
aoqi@0 | 221 | */ |
attila@963 | 222 | public SwitchPoint[] getSwitchPoints() { |
attila@963 | 223 | return switchPoints == null ? null : switchPoints.clone(); |
attila@963 | 224 | } |
attila@963 | 225 | |
attila@963 | 226 | /** |
attila@963 | 227 | * Returns the exception type that if thrown should be used to invalidate the linkage. |
attila@963 | 228 | * |
attila@963 | 229 | * @return the exception type that if thrown should be used to invalidate the linkage. Can be null. |
attila@963 | 230 | */ |
attila@963 | 231 | public Class<? extends Throwable> getException() { |
attila@963 | 232 | return exception; |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | /** |
aoqi@0 | 236 | * Returns true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated. |
aoqi@0 | 237 | * @return true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated. |
aoqi@0 | 238 | */ |
aoqi@0 | 239 | public boolean hasBeenInvalidated() { |
attila@963 | 240 | if (switchPoints == null) { |
attila@963 | 241 | return false; |
attila@963 | 242 | } |
attila@963 | 243 | for (final SwitchPoint sp : switchPoints) { |
attila@963 | 244 | if (sp.hasBeenInvalidated()) { |
attila@963 | 245 | return true; |
attila@963 | 246 | } |
attila@963 | 247 | } |
attila@963 | 248 | return false; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | /** |
aoqi@0 | 252 | * Asserts that the invocation is of the specified type, and the guard (if present) is of the specified type with a |
aoqi@0 | 253 | * boolean return type. |
aoqi@0 | 254 | * |
aoqi@0 | 255 | * @param type the asserted type |
aoqi@0 | 256 | * @throws WrongMethodTypeException if the invocation and the guard are not of the expected method type. |
aoqi@0 | 257 | */ |
attila@962 | 258 | public void assertType(final MethodType type) { |
aoqi@0 | 259 | assertType(invocation, type); |
attila@963 | 260 | if (guard != null) { |
aoqi@0 | 261 | assertType(guard, type.changeReturnType(Boolean.TYPE)); |
aoqi@0 | 262 | } |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | /** |
aoqi@0 | 266 | * Creates a new guarded invocation with different methods, preserving the switch point. |
aoqi@0 | 267 | * |
aoqi@0 | 268 | * @param newInvocation the new invocation |
aoqi@0 | 269 | * @param newGuard the new guard |
aoqi@0 | 270 | * @return a new guarded invocation with the replaced methods and the same switch point as this invocation. |
aoqi@0 | 271 | */ |
attila@962 | 272 | public GuardedInvocation replaceMethods(final MethodHandle newInvocation, final MethodHandle newGuard) { |
attila@963 | 273 | return new GuardedInvocation(newInvocation, newGuard, switchPoints, exception); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
attila@963 | 276 | /** |
attila@963 | 277 | * Add a switchpoint to this guarded invocation |
attila@963 | 278 | * @param newSwitchPoint new switchpoint, or null for nop |
attila@963 | 279 | * @return new guarded invocation with the extra switchpoint |
attila@963 | 280 | */ |
attila@963 | 281 | public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) { |
attila@963 | 282 | if (newSwitchPoint == null) { |
attila@963 | 283 | return this; |
attila@963 | 284 | } |
attila@963 | 285 | |
attila@963 | 286 | final SwitchPoint[] newSwitchPoints; |
attila@963 | 287 | if (switchPoints != null) { |
attila@963 | 288 | newSwitchPoints = new SwitchPoint[switchPoints.length + 1]; |
attila@963 | 289 | System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length); |
attila@963 | 290 | newSwitchPoints[switchPoints.length] = newSwitchPoint; |
attila@963 | 291 | } else { |
attila@963 | 292 | newSwitchPoints = new SwitchPoint[] { newSwitchPoint }; |
attila@963 | 293 | } |
attila@963 | 294 | |
attila@963 | 295 | return new GuardedInvocation(invocation, guard, newSwitchPoints, exception); |
attila@101 | 296 | } |
attila@101 | 297 | |
attila@962 | 298 | private GuardedInvocation replaceMethodsOrThis(final MethodHandle newInvocation, final MethodHandle newGuard) { |
attila@963 | 299 | if (newInvocation == invocation && newGuard == guard) { |
aoqi@0 | 300 | return this; |
aoqi@0 | 301 | } |
aoqi@0 | 302 | return replaceMethods(newInvocation, newGuard); |
aoqi@0 | 303 | } |
aoqi@0 | 304 | |
aoqi@0 | 305 | /** |
aoqi@0 | 306 | * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation |
aoqi@0 | 307 | * and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for |
aoqi@0 | 308 | * the guard). If the invocation already is of the required type, returns this object. |
aoqi@0 | 309 | * @param newType the new type of the invocation. |
aoqi@0 | 310 | * @return a guarded invocation with the new type applied to it. |
aoqi@0 | 311 | */ |
attila@962 | 312 | public GuardedInvocation asType(final MethodType newType) { |
aoqi@0 | 313 | return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType)); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | |
aoqi@0 | 316 | /** |
aoqi@0 | 317 | * Changes the type of the invocation, as if {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to |
aoqi@0 | 318 | * its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially |
aoqi@0 | 319 | * truncated for the guard). If the invocation already is of the required type, returns this object. |
aoqi@0 | 320 | * @param linkerServices the linker services to use for the conversion |
aoqi@0 | 321 | * @param newType the new type of the invocation. |
aoqi@0 | 322 | * @return a guarded invocation with the new type applied to it. |
aoqi@0 | 323 | */ |
attila@962 | 324 | public GuardedInvocation asType(final LinkerServices linkerServices, final MethodType newType) { |
aoqi@0 | 325 | return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null : |
aoqi@0 | 326 | Guards.asType(linkerServices, guard, newType)); |
aoqi@0 | 327 | } |
aoqi@0 | 328 | |
aoqi@0 | 329 | /** |
attila@963 | 330 | * Changes the type of the invocation, as if {@link LinkerServices#asTypeLosslessReturn(MethodHandle, MethodType)} was |
attila@963 | 331 | * applied to its invocation and {@link LinkerServices#asType(MethodHandle, MethodType)} applied to its guard, if it |
attila@963 | 332 | * has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the |
attila@963 | 333 | * invocation doesn't change its type, returns this object. |
attila@963 | 334 | * @param linkerServices the linker services to use for the conversion |
attila@963 | 335 | * @param newType the new type of the invocation. |
attila@963 | 336 | * @return a guarded invocation with the new type applied to it. |
attila@963 | 337 | */ |
attila@963 | 338 | public GuardedInvocation asTypeSafeReturn(final LinkerServices linkerServices, final MethodType newType) { |
attila@963 | 339 | return replaceMethodsOrThis(linkerServices.asTypeLosslessReturn(invocation, newType), guard == null ? null : |
attila@963 | 340 | Guards.asType(linkerServices, guard, newType)); |
attila@963 | 341 | } |
attila@963 | 342 | |
attila@963 | 343 | /** |
aoqi@0 | 344 | * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation |
aoqi@0 | 345 | * and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the |
aoqi@0 | 346 | * required type, returns this object. |
aoqi@0 | 347 | * @param desc a call descriptor whose method type is adapted. |
aoqi@0 | 348 | * @return a guarded invocation with the new type applied to it. |
aoqi@0 | 349 | */ |
attila@962 | 350 | public GuardedInvocation asType(final CallSiteDescriptor desc) { |
aoqi@0 | 351 | return asType(desc.getMethodType()); |
aoqi@0 | 352 | } |
aoqi@0 | 353 | |
aoqi@0 | 354 | /** |
aoqi@0 | 355 | * Applies argument filters to both the invocation and the guard (if there is one). |
sundar@1530 | 356 | * @param pos the position of the first argument being filtered |
aoqi@0 | 357 | * @param filters the argument filters |
aoqi@0 | 358 | * @return a filtered invocation |
aoqi@0 | 359 | */ |
attila@962 | 360 | public GuardedInvocation filterArguments(final int pos, final MethodHandle... filters) { |
aoqi@0 | 361 | return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters), guard == null ? null : |
aoqi@0 | 362 | MethodHandles.filterArguments(guard, pos, filters)); |
aoqi@0 | 363 | } |
aoqi@0 | 364 | |
aoqi@0 | 365 | /** |
aoqi@0 | 366 | * Makes an invocation that drops arguments in both the invocation and the guard (if there is one). |
aoqi@0 | 367 | * @param pos the position of the first argument being dropped |
aoqi@0 | 368 | * @param valueTypes the types of the values being dropped |
aoqi@0 | 369 | * @return an invocation that drops arguments |
aoqi@0 | 370 | */ |
attila@962 | 371 | public GuardedInvocation dropArguments(final int pos, final List<Class<?>> valueTypes) { |
aoqi@0 | 372 | return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null : |
aoqi@0 | 373 | MethodHandles.dropArguments(guard, pos, valueTypes)); |
aoqi@0 | 374 | } |
aoqi@0 | 375 | |
aoqi@0 | 376 | /** |
aoqi@0 | 377 | * Makes an invocation that drops arguments in both the invocation and the guard (if there is one). |
aoqi@0 | 378 | * @param pos the position of the first argument being dropped |
aoqi@0 | 379 | * @param valueTypes the types of the values being dropped |
aoqi@0 | 380 | * @return an invocation that drops arguments |
aoqi@0 | 381 | */ |
attila@962 | 382 | public GuardedInvocation dropArguments(final int pos, final Class<?>... valueTypes) { |
aoqi@0 | 383 | return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null : |
aoqi@0 | 384 | MethodHandles.dropArguments(guard, pos, valueTypes)); |
aoqi@0 | 385 | } |
aoqi@0 | 386 | |
aoqi@0 | 387 | |
aoqi@0 | 388 | /** |
aoqi@0 | 389 | * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back. |
aoqi@0 | 390 | * @param fallback the fallback method handle in case switchpoint is invalidated or guard returns false. |
aoqi@0 | 391 | * @return a composite method handle. |
aoqi@0 | 392 | */ |
attila@962 | 393 | public MethodHandle compose(final MethodHandle fallback) { |
attila@963 | 394 | return compose(fallback, fallback, fallback); |
aoqi@0 | 395 | } |
aoqi@0 | 396 | |
aoqi@0 | 397 | /** |
aoqi@0 | 398 | * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back. |
aoqi@0 | 399 | * @param switchpointFallback the fallback method handle in case switchpoint is invalidated. |
aoqi@0 | 400 | * @param guardFallback the fallback method handle in case guard returns false. |
attila@963 | 401 | * @param catchFallback the fallback method in case the exception handler triggers |
aoqi@0 | 402 | * @return a composite method handle. |
aoqi@0 | 403 | */ |
attila@963 | 404 | public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) { |
aoqi@0 | 405 | final MethodHandle guarded = |
attila@963 | 406 | guard == null ? |
attila@963 | 407 | invocation : |
attila@963 | 408 | MethodHandles.guardWithTest( |
attila@963 | 409 | guard, |
attila@963 | 410 | invocation, |
attila@963 | 411 | guardFallback); |
attila@963 | 412 | |
attila@963 | 413 | final MethodHandle catchGuarded = |
attila@963 | 414 | exception == null ? |
attila@963 | 415 | guarded : |
attila@963 | 416 | MH.catchException( |
attila@963 | 417 | guarded, |
attila@963 | 418 | exception, |
attila@963 | 419 | MethodHandles.dropArguments( |
attila@963 | 420 | catchFallback, |
attila@963 | 421 | 0, |
attila@963 | 422 | exception)); |
attila@963 | 423 | |
attila@963 | 424 | if (switchPoints == null) { |
attila@963 | 425 | return catchGuarded; |
attila@963 | 426 | } |
attila@963 | 427 | |
attila@963 | 428 | MethodHandle spGuarded = catchGuarded; |
attila@963 | 429 | for (final SwitchPoint sp : switchPoints) { |
attila@963 | 430 | spGuarded = sp.guardWithTest(spGuarded, switchpointFallback); |
attila@963 | 431 | } |
attila@963 | 432 | |
attila@963 | 433 | return spGuarded; |
aoqi@0 | 434 | } |
aoqi@0 | 435 | |
attila@962 | 436 | private static void assertType(final MethodHandle mh, final MethodType type) { |
aoqi@0 | 437 | if(!mh.type().equals(type)) { |
aoqi@0 | 438 | throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type()); |
aoqi@0 | 439 | } |
aoqi@0 | 440 | } |
aoqi@0 | 441 | } |