|
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 */ |
|
25 |
|
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 |
|
35 |
|
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. |
|
39 |
|
40 You may not use this file except in compliance with either the Apache |
|
41 License or the BSD License. |
|
42 |
|
43 If you choose to use this file in compliance with the Apache License, the |
|
44 following notice applies to you: |
|
45 |
|
46 You may obtain a copy of the Apache License at |
|
47 |
|
48 http://www.apache.org/licenses/LICENSE-2.0 |
|
49 |
|
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. |
|
55 |
|
56 If you choose to use this file in compliance with the BSD License, the |
|
57 following notice applies to you: |
|
58 |
|
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. |
|
70 |
|
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 */ |
|
83 |
|
84 package jdk.dynalink; |
|
85 |
|
86 import java.lang.invoke.MethodHandles; |
|
87 import java.lang.invoke.MethodHandles.Lookup; |
|
88 import java.lang.invoke.MethodType; |
|
89 import java.util.Objects; |
|
90 import java.util.function.Supplier; |
|
91 |
|
92 /** |
|
93 * Call site descriptors contain all the information necessary for linking a |
|
94 * call site. This information is normally passed as parameters to bootstrap |
|
95 * methods and consists of the {@code MethodHandles.Lookup} object on the caller |
|
96 * class in which the call site occurs, the dynamic operation at the call |
|
97 * site, and the method type of the call site. {@code CallSiteDescriptor} |
|
98 * objects are used in Dynalink to capture and store these parameters for |
|
99 * subsequent use by the {@link DynamicLinker}. |
|
100 * <p> |
|
101 * The constructors of built-in {@link RelinkableCallSite} implementations all |
|
102 * take a call site descriptor. |
|
103 * <p> |
|
104 * Call site descriptors must be immutable. You can use this class as-is or you |
|
105 * can subclass it, especially if you need to add further information to the |
|
106 * descriptors (typically, values passed in additional parameters to the |
|
107 * bootstrap method. Since the descriptors must be immutable, you can set up a |
|
108 * cache for equivalent descriptors to have the call sites share them. |
|
109 * <p> |
|
110 * The class extends {@link SecureLookupSupplier} for security-checked access to |
|
111 * the {@code MethodHandles.Lookup} object it carries. This lookup should be used |
|
112 * to find method handles to set as targets of the call site described by this |
|
113 * descriptor. |
|
114 */ |
|
115 public class CallSiteDescriptor extends SecureLookupSupplier { |
|
116 private final Operation operation; |
|
117 private final MethodType methodType; |
|
118 |
|
119 /** |
|
120 * Creates a new call site descriptor. |
|
121 * @param lookup the lookup object describing the class the call site belongs to. |
|
122 * When creating descriptors from a {@link java.lang.invoke} bootstrap method, |
|
123 * it should be the lookup passed to the bootstrap. |
|
124 * @param operation the dynamic operation at the call site. |
|
125 * @param methodType the method type of the call site. When creating |
|
126 * descriptors from a {@link java.lang.invoke} bootstrap method, it should be |
|
127 * the method type passed to the bootstrap. |
|
128 */ |
|
129 public CallSiteDescriptor(final Lookup lookup, final Operation operation, final MethodType methodType) { |
|
130 super(lookup); |
|
131 this.operation = Objects.requireNonNull(operation, "name"); |
|
132 this.methodType = Objects.requireNonNull(methodType, "methodType"); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Returns the operation at the call site. |
|
137 * @return the operation at the call site. |
|
138 */ |
|
139 public final Operation getOperation() { |
|
140 return operation; |
|
141 } |
|
142 |
|
143 /** |
|
144 * The type of the method at the call site. |
|
145 * |
|
146 * @return type of the method at the call site. |
|
147 */ |
|
148 public final MethodType getMethodType() { |
|
149 return methodType; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Finds or creates a call site descriptor that only differs in its |
|
154 * method type from this descriptor. |
|
155 * Invokes {@link #changeMethodTypeInternal(MethodType)}. |
|
156 * |
|
157 * @param newMethodType the new method type |
|
158 * @return a call site descriptor with changed method type. |
|
159 * @throws NullPointerException if {@code newMethodType} is null. |
|
160 */ |
|
161 public final CallSiteDescriptor changeMethodType(final MethodType newMethodType) { |
|
162 final CallSiteDescriptor changed = changeMethodTypeInternal(newMethodType); |
|
163 |
|
164 if (getClass() != CallSiteDescriptor.class) { |
|
165 assertChangeInvariants(changed, "changeMethodTypeInternal"); |
|
166 alwaysAssert(operation == changed.operation, () -> "changeMethodTypeInternal must not change the descriptor's operation"); |
|
167 alwaysAssert(newMethodType == changed.methodType, () -> "changeMethodTypeInternal didn't set the correct new method type"); |
|
168 } |
|
169 return changed; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Finds or creates a call site descriptor that only differs in its |
|
174 * method type from this descriptor. Subclasses must override this method |
|
175 * to return an object of their exact class. If an overridden method changes |
|
176 * something other than the method type in the descriptor (its class, lookup, |
|
177 * or operation), or returns null, an {@code AssertionError} will be thrown |
|
178 * from {@link #changeMethodType(MethodType)}. |
|
179 * |
|
180 * @param newMethodType the new method type |
|
181 * @return a call site descriptor with the changed method type. |
|
182 */ |
|
183 protected CallSiteDescriptor changeMethodTypeInternal(final MethodType newMethodType) { |
|
184 return new CallSiteDescriptor(getLookupPrivileged(), operation, newMethodType); |
|
185 } |
|
186 |
|
187 /** |
|
188 * Finds or creates a call site descriptor that only differs in its |
|
189 * operation from this descriptor. |
|
190 * Invokes {@link #changeOperationInternal(Operation)}. |
|
191 * |
|
192 * @param newOperation the new operation |
|
193 * @return a call site descriptor with the changed operation. |
|
194 * @throws NullPointerException if {@code newOperation} is null. |
|
195 * @throws SecurityException if the descriptor's lookup isn't the |
|
196 * {@link MethodHandles#publicLookup()}, and a security manager is present, |
|
197 * and a check for {@code RuntimePermission("dynalink.getLookup")} fails. |
|
198 * This is necessary as changing the operation in the call site descriptor |
|
199 * allows fabrication of descriptors for arbitrary operations with the lookup. |
|
200 */ |
|
201 public final CallSiteDescriptor changeOperation(final Operation newOperation) { |
|
202 getLookup(); // force security check |
|
203 final CallSiteDescriptor changed = changeOperationInternal(newOperation); |
|
204 |
|
205 if (getClass() != CallSiteDescriptor.class) { |
|
206 assertChangeInvariants(changed, "changeOperationInternal"); |
|
207 alwaysAssert(methodType == changed.methodType, () -> "changeOperationInternal must not change the descriptor's method type"); |
|
208 alwaysAssert(newOperation == changed.operation, () -> "changeOperationInternal didn't set the correct new operation"); |
|
209 } |
|
210 return changed; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Finds or creates a call site descriptor that only differs in its |
|
215 * operation from this descriptor. Subclasses must override this method |
|
216 * to return an object of their exact class. If an overridden method changes |
|
217 * something other than the operation in the descriptor (its class, lookup, |
|
218 * or method type), or returns null, an {@code AssertionError} will be thrown |
|
219 * from {@link #changeOperation(Operation)}. |
|
220 * |
|
221 * @param newOperation the new operation |
|
222 * @return a call site descriptor with the changed operation. |
|
223 */ |
|
224 protected CallSiteDescriptor changeOperationInternal(final Operation newOperation) { |
|
225 return new CallSiteDescriptor(getLookupPrivileged(), newOperation, methodType); |
|
226 } |
|
227 |
|
228 /** |
|
229 * Returns true if this call site descriptor is equal to the passed object. |
|
230 * It is considered equal if the other object is of the exact same class, |
|
231 * their operations and method types are equal, and their lookups have the |
|
232 * same {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and |
|
233 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}. |
|
234 */ |
|
235 @Override |
|
236 public boolean equals(final Object obj) { |
|
237 if (obj == this) { |
|
238 return true; |
|
239 } else if (obj == null) { |
|
240 return false; |
|
241 } else if (obj.getClass() != getClass()) { |
|
242 return false; |
|
243 } |
|
244 final CallSiteDescriptor other = (CallSiteDescriptor)obj; |
|
245 return operation.equals(other.operation) && |
|
246 methodType.equals(other.methodType) && |
|
247 lookupsEqual(getLookupPrivileged(), other.getLookupPrivileged()); |
|
248 } |
|
249 |
|
250 /** |
|
251 * Compares two lookup objects for value-based equality. They are considered |
|
252 * equal if they have the same |
|
253 * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and |
|
254 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}. |
|
255 * @param l1 first lookup |
|
256 * @param l2 second lookup |
|
257 * @return true if the two lookups are equal, false otherwise. |
|
258 */ |
|
259 private static boolean lookupsEqual(final Lookup l1, final Lookup l2) { |
|
260 return l1.lookupClass() == l2.lookupClass() && l1.lookupModes() == l2.lookupModes(); |
|
261 } |
|
262 |
|
263 /** |
|
264 * Returns a value-based hash code of this call site descriptor computed |
|
265 * from its operation, method type, and lookup object's lookup class and |
|
266 * lookup modes. |
|
267 * @return value-based hash code for this call site descriptor. |
|
268 */ |
|
269 @Override |
|
270 public int hashCode() { |
|
271 return operation.hashCode() + 31 * methodType.hashCode() + 31 * 31 * lookupHashCode(getLookupPrivileged()); |
|
272 } |
|
273 |
|
274 /** |
|
275 * Returns a value-based hash code for the passed lookup object. It is |
|
276 * based on the lookup object's |
|
277 * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and |
|
278 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()} values. |
|
279 * @param lookup the lookup object. |
|
280 * @return a hash code for the object.. |
|
281 */ |
|
282 private static int lookupHashCode(final Lookup lookup) { |
|
283 return lookup.lookupClass().hashCode() + 31 * lookup.lookupModes(); |
|
284 } |
|
285 |
|
286 /** |
|
287 * Returns the string representation of this call site descriptor, of the |
|
288 * format {@code name(parameterTypes)returnType@lookup}. |
|
289 */ |
|
290 @Override |
|
291 public String toString() { |
|
292 final String mt = methodType.toString(); |
|
293 final String l = getLookupPrivileged().toString(); |
|
294 final String o = operation.toString(); |
|
295 final StringBuilder b = new StringBuilder(o.length() + mt.length() + 1 + l.length()); |
|
296 return b.append(o).append(mt).append('@').append(l).toString(); |
|
297 } |
|
298 |
|
299 private void assertChangeInvariants(final CallSiteDescriptor changed, final String caller) { |
|
300 alwaysAssert(changed != null, () -> caller + " must not return null."); |
|
301 alwaysAssert(getClass() == changed.getClass(), () -> caller + " must not change the descriptor's class"); |
|
302 alwaysAssert(lookupsEqual(getLookupPrivileged(), changed.getLookupPrivileged()), () -> caller + " must not change the descriptor's lookup"); |
|
303 } |
|
304 |
|
305 private static void alwaysAssert(final boolean cond, final Supplier<String> errorMessage) { |
|
306 if (!cond) { |
|
307 throw new AssertionError(errorMessage.get()); |
|
308 } |
|
309 } |
|
310 } |