Thu, 31 Aug 2017 15:30:47 +0800
merge
1 /*
2 * Copyright (c) 2010, 2013, 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 */
26 /*
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file, and Oracle licenses the original version of this file under the BSD
31 * license:
32 */
33 /*
34 Copyright 2009-2013 Attila Szegedi
36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
37 and the BSD License (the "BSD License"), with licensee being free to
38 choose either of the two at their discretion.
40 You may not use this file except in compliance with either the Apache
41 License or the BSD License.
43 If you choose to use this file in compliance with the Apache License, the
44 following notice applies to you:
46 You may obtain a copy of the Apache License at
48 http://www.apache.org/licenses/LICENSE-2.0
50 Unless required by applicable law or agreed to in writing, software
51 distributed under the License is distributed on an "AS IS" BASIS,
52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
53 implied. See the License for the specific language governing
54 permissions and limitations under the License.
56 If you choose to use this file in compliance with the BSD License, the
57 following notice applies to you:
59 Redistribution and use in source and binary forms, with or without
60 modification, are permitted provided that the following conditions are
61 met:
62 * Redistributions of source code must retain the above copyright
63 notice, this list of conditions and the following disclaimer.
64 * Redistributions in binary form must reproduce the above copyright
65 notice, this list of conditions and the following disclaimer in the
66 documentation and/or other materials provided with the distribution.
67 * Neither the name of the copyright holder nor the names of
68 contributors may be used to endorse or promote products derived from
69 this software without specific prior written permission.
71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 */
84 package jdk.internal.dynalink.linker;
86 import java.lang.invoke.MethodHandle;
87 import java.lang.invoke.MethodHandles;
88 import java.lang.invoke.MethodType;
89 import java.lang.invoke.SwitchPoint;
90 import java.lang.invoke.WrongMethodTypeException;
91 import java.util.List;
92 import jdk.internal.dynalink.CallSiteDescriptor;
93 import jdk.internal.dynalink.support.Guards;
95 /**
96 * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard
97 * method handle that defines the applicability of the invocation handle, and a switch point that can be used for
98 * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard
99 * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the
100 * switch point are optional; neither, one, or both can be present.
101 *
102 * @author Attila Szegedi
103 */
104 public class GuardedInvocation {
105 private final MethodHandle invocation;
106 private final MethodHandle guard;
107 private final SwitchPoint switchPoint;
109 /**
110 * Creates a new guarded invocation.
111 *
112 * @param invocation the method handle representing the invocation. Must not be null.
113 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
114 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null to represent
115 * an unconditional invocation, although that is unusual.
116 * @throws NullPointerException if invocation is null.
117 */
118 public GuardedInvocation(MethodHandle invocation, MethodHandle guard) {
119 this(invocation, guard, null);
120 }
122 /**
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, except
127 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
128 * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
129 * @param switchPoint the optional switch point that can be used to invalidate this linkage.
130 * @throws NullPointerException if invocation is null.
131 */
132 public GuardedInvocation(MethodHandle invocation, MethodHandle guard, SwitchPoint switchPoint) {
133 invocation.getClass(); // NPE check
134 this.invocation = invocation;
135 this.guard = guard;
136 this.switchPoint = switchPoint;
137 }
139 /**
140 * Creates a new guarded invocation.
141 *
142 * @param invocation the method handle representing the invocation. Must not be null.
143 * @param switchPoint the optional switch point that can be used to invalidate this linkage.
144 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
145 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
146 * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
147 * @throws NullPointerException if invocation is null.
148 */
149 public GuardedInvocation(MethodHandle invocation, SwitchPoint switchPoint, MethodHandle guard) {
150 this(invocation, guard, switchPoint);
151 }
152 /**
153 * Returns the invocation method handle.
154 *
155 * @return the invocation method handle. It will never be null.
156 */
157 public MethodHandle getInvocation() {
158 return invocation;
159 }
161 /**
162 * Returns the guard method handle.
163 *
164 * @return the guard method handle. Can be null.
165 */
166 public MethodHandle getGuard() {
167 return guard;
168 }
170 /**
171 * Returns the switch point that can be used to invalidate the invocation handle.
172 *
173 * @return the switch point that can be used to invalidate the invocation handle. Can be null.
174 */
175 public SwitchPoint getSwitchPoint() {
176 return switchPoint;
177 }
179 /**
180 * Returns true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.
181 * @return true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.
182 */
183 public boolean hasBeenInvalidated() {
184 return switchPoint != null && switchPoint.hasBeenInvalidated();
185 }
187 /**
188 * Asserts that the invocation is of the specified type, and the guard (if present) is of the specified type with a
189 * boolean return type.
190 *
191 * @param type the asserted type
192 * @throws WrongMethodTypeException if the invocation and the guard are not of the expected method type.
193 */
194 public void assertType(MethodType type) {
195 assertType(invocation, type);
196 if(guard != null) {
197 assertType(guard, type.changeReturnType(Boolean.TYPE));
198 }
199 }
201 /**
202 * Creates a new guarded invocation with different methods, preserving the switch point.
203 *
204 * @param newInvocation the new invocation
205 * @param newGuard the new guard
206 * @return a new guarded invocation with the replaced methods and the same switch point as this invocation.
207 */
208 public GuardedInvocation replaceMethods(MethodHandle newInvocation, MethodHandle newGuard) {
209 return new GuardedInvocation(newInvocation, newGuard, switchPoint);
210 }
212 private GuardedInvocation replaceMethodsOrThis(MethodHandle newInvocation, MethodHandle newGuard) {
213 if(newInvocation == invocation && newGuard == guard) {
214 return this;
215 }
216 return replaceMethods(newInvocation, newGuard);
217 }
219 /**
220 * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation
221 * and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for
222 * the guard). If the invocation already is of the required type, returns this object.
223 * @param newType the new type of the invocation.
224 * @return a guarded invocation with the new type applied to it.
225 */
226 public GuardedInvocation asType(MethodType newType) {
227 return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType));
228 }
230 /**
231 * Changes the type of the invocation, as if {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to
232 * its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially
233 * truncated for the guard). If the invocation already is of the required type, returns this object.
234 * @param linkerServices the linker services to use for the conversion
235 * @param newType the new type of the invocation.
236 * @return a guarded invocation with the new type applied to it.
237 */
238 public GuardedInvocation asType(LinkerServices linkerServices, MethodType newType) {
239 return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null :
240 Guards.asType(linkerServices, guard, newType));
241 }
243 /**
244 * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation
245 * and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the
246 * required type, returns this object.
247 * @param desc a call descriptor whose method type is adapted.
248 * @return a guarded invocation with the new type applied to it.
249 */
250 public GuardedInvocation asType(CallSiteDescriptor desc) {
251 return asType(desc.getMethodType());
252 }
254 /**
255 * Applies argument filters to both the invocation and the guard (if there is one).
256 * @param pos the position of the first argumen being filtered
257 * @param filters the argument filters
258 * @return a filtered invocation
259 */
260 public GuardedInvocation filterArguments(int pos, MethodHandle... filters) {
261 return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters), guard == null ? null :
262 MethodHandles.filterArguments(guard, pos, filters));
263 }
265 /**
266 * Makes an invocation that drops arguments in both the invocation and the guard (if there is one).
267 * @param pos the position of the first argument being dropped
268 * @param valueTypes the types of the values being dropped
269 * @return an invocation that drops arguments
270 */
271 public GuardedInvocation dropArguments(int pos, List<Class<?>> valueTypes) {
272 return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :
273 MethodHandles.dropArguments(guard, pos, valueTypes));
274 }
276 /**
277 * Makes an invocation that drops arguments in both the invocation and the guard (if there is one).
278 * @param pos the position of the first argument being dropped
279 * @param valueTypes the types of the values being dropped
280 * @return an invocation that drops arguments
281 */
282 public GuardedInvocation dropArguments(int pos, Class<?>... valueTypes) {
283 return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :
284 MethodHandles.dropArguments(guard, pos, valueTypes));
285 }
288 /**
289 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
290 * @param fallback the fallback method handle in case switchpoint is invalidated or guard returns false.
291 * @return a composite method handle.
292 */
293 public MethodHandle compose(MethodHandle fallback) {
294 return compose(fallback, fallback);
295 }
297 /**
298 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
299 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
300 * @param guardFallback the fallback method handle in case guard returns false.
301 * @return a composite method handle.
302 */
303 public MethodHandle compose(MethodHandle switchpointFallback, MethodHandle guardFallback) {
304 final MethodHandle guarded =
305 guard == null ? invocation : MethodHandles.guardWithTest(guard, invocation, guardFallback);
306 return switchPoint == null ? guarded : switchPoint.guardWithTest(guarded, switchpointFallback);
307 }
309 private static void assertType(MethodHandle mh, MethodType type) {
310 if(!mh.type().equals(type)) {
311 throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type());
312 }
313 }
314 }