/
DynamicCallSiteDesc.java
289 lines (271 loc) · 13.3 KB
/
DynamicCallSiteDesc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.constant;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
import static java.lang.constant.ConstantDescs.CD_String;
import static java.lang.constant.ConstantUtils.EMPTY_CONSTANTDESC;
import static java.lang.constant.ConstantUtils.validateMemberName;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
/**
* A <a href="package-summary.html#nominal">nominal descriptor</a> for an
* {@code invokedynamic} call site.
*
* <p>Concrete subtypes of {@linkplain DynamicCallSiteDesc} must be
* <a href="../doc-files/ValueBased.html">value-based</a>.
*
* @since 12
*/
public class DynamicCallSiteDesc {
private final DirectMethodHandleDesc bootstrapMethod;
private final ConstantDesc[] bootstrapArgs;
private final String invocationName;
private final MethodTypeDesc invocationType;
/**
* Creates a nominal descriptor for an {@code invokedynamic} call site.
*
* @param bootstrapMethod a {@link DirectMethodHandleDesc} describing the
* bootstrap method for the {@code invokedynamic}
* @param invocationName The unqualified name that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @param invocationType a {@link MethodTypeDesc} describing the invocation
* type that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @param bootstrapArgs {@link ConstantDesc}s describing the static arguments
* to the bootstrap, that would appear in the
* {@code BootstrapMethods} attribute
* @throws NullPointerException if any parameter is null
* @throws IllegalArgumentException if the invocation name has the incorrect
* format
* @jvms 4.2.2 Unqualified Names
*/
private DynamicCallSiteDesc(DirectMethodHandleDesc bootstrapMethod,
String invocationName,
MethodTypeDesc invocationType,
ConstantDesc[] bootstrapArgs) {
this.invocationName = validateMemberName(requireNonNull(invocationName), true);
this.invocationType = requireNonNull(invocationType);
this.bootstrapMethod = requireNonNull(bootstrapMethod);
this.bootstrapArgs = requireNonNull(bootstrapArgs.clone());
if (invocationName.length() == 0)
throw new IllegalArgumentException("Illegal invocation name: " + invocationName);
}
/**
* Creates a nominal descriptor for an {@code invokedynamic} call site.
*
* @param bootstrapMethod a {@link DirectMethodHandleDesc} describing the
* bootstrap method for the {@code invokedynamic}
* @param invocationName The unqualified name that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @param invocationType a {@link MethodTypeDesc} describing the invocation
* type that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @param bootstrapArgs {@link ConstantDesc}s describing the static arguments
* to the bootstrap, that would appear in the
* {@code BootstrapMethods} attribute
* @return the nominal descriptor
* @throws NullPointerException if any parameter is null
* @throws IllegalArgumentException if the invocation name has the incorrect
* format
* @jvms 4.2.2 Unqualified Names
*/
public static DynamicCallSiteDesc of(DirectMethodHandleDesc bootstrapMethod,
String invocationName,
MethodTypeDesc invocationType,
ConstantDesc... bootstrapArgs) {
return new DynamicCallSiteDesc(bootstrapMethod, invocationName, invocationType, bootstrapArgs);
}
/**
* Creates a nominal descriptor for an {@code invokedynamic} call site whose
* bootstrap method has no static arguments.
*
* @param bootstrapMethod The bootstrap method for the {@code invokedynamic}
* @param invocationName The invocationName that would appear in the
* {@code NameAndType} operand of the {@code invokedynamic}
* @param invocationType The invocation invocationType that would appear
* in the {@code NameAndType} operand of the {@code invokedynamic}
* @return the nominal descriptor
* @throws NullPointerException if any parameter is null
* @throws IllegalArgumentException if the invocation name has the incorrect
* format
*/
public static DynamicCallSiteDesc of(DirectMethodHandleDesc bootstrapMethod,
String invocationName,
MethodTypeDesc invocationType) {
return new DynamicCallSiteDesc(bootstrapMethod, invocationName, invocationType, EMPTY_CONSTANTDESC);
}
/**
* Creates a nominal descriptor for an {@code invokedynamic} call site whose
* bootstrap method has no static arguments and for which the name parameter
* is {@link ConstantDescs#DEFAULT_NAME}.
*
* @param bootstrapMethod a {@link DirectMethodHandleDesc} describing the
* bootstrap method for the {@code invokedynamic}
* @param invocationType a {@link MethodTypeDesc} describing the invocation
* type that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @return the nominal descriptor
* @throws NullPointerException if any parameter is null
*/
public static DynamicCallSiteDesc of(DirectMethodHandleDesc bootstrapMethod,
MethodTypeDesc invocationType) {
return of(bootstrapMethod, ConstantDescs.DEFAULT_NAME, invocationType);
}
/**
* Returns a nominal descriptor for an {@code invokedynamic} call site whose
* bootstrap method, name, and invocation type are the same as this one, but
* with the specified bootstrap arguments.
*
* @param bootstrapArgs {@link ConstantDesc}s describing the static arguments
* to the bootstrap, that would appear in the
* {@code BootstrapMethods} attribute
* @return the nominal descriptor
* @throws NullPointerException if any parameter is null
*/
public DynamicCallSiteDesc withArgs(ConstantDesc... bootstrapArgs) {
return new DynamicCallSiteDesc(bootstrapMethod, invocationName, invocationType, bootstrapArgs);
}
/**
* Returns a nominal descriptor for an {@code invokedynamic} call site whose
* bootstrap and bootstrap arguments are the same as this one, but with the
* specified invocationName and invocation invocationType
*
* @param invocationName The unqualified name that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @param invocationType a {@link MethodTypeDesc} describing the invocation
* type that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}
* @return the nominal descriptor
* @throws NullPointerException if any parameter is null
* @throws IllegalArgumentException if the invocation name has the incorrect
* format
* @jvms 4.2.2 Unqualified Names
*/
public DynamicCallSiteDesc withNameAndType(String invocationName,
MethodTypeDesc invocationType) {
return new DynamicCallSiteDesc(bootstrapMethod, invocationName, invocationType, bootstrapArgs);
}
/**
* Returns the invocation name that would appear in the {@code NameAndType}
* operand of the {@code invokedynamic}.
*
* @return the invocation name
*/
public String invocationName() {
return invocationName;
}
/**
* Returns a {@link MethodTypeDesc} describing the invocation type that
* would appear in the {@code NameAndType} operand of the {@code invokedynamic}.
*
* @return the invocation type
*/
public MethodTypeDesc invocationType() {
return invocationType;
}
/**
* Returns a {@link MethodHandleDesc} describing the bootstrap method for
* the {@code invokedynamic}.
*
* @return the bootstrap method for the {@code invokedynamic}
*/
public MethodHandleDesc bootstrapMethod() { return bootstrapMethod; }
/**
* Returns {@link ConstantDesc}s describing the bootstrap arguments for the
* {@code invokedynamic}. The returned array is always non-null. A zero
* length array is returned if this {@linkplain DynamicCallSiteDesc} has no
* bootstrap arguments.
*
* @return the bootstrap arguments for the {@code invokedynamic}
*/
public ConstantDesc[] bootstrapArgs() { return bootstrapArgs.clone(); }
/**
* Reflectively invokes the bootstrap method with the specified arguments,
* and return the resulting {@link CallSite}
*
* @param lookup The {@link MethodHandles.Lookup} used to resolve class names
* @return the {@link CallSite}
* @throws Throwable if any exception is thrown by the bootstrap method
*/
public CallSite resolveCallSiteDesc(MethodHandles.Lookup lookup) throws Throwable {
assert bootstrapMethod.invocationType().parameterType(1).equals(CD_String);
MethodHandle bsm = (MethodHandle) bootstrapMethod.resolveConstantDesc(lookup);
Object[] args = new Object[bootstrapArgs.length + 3];
args[0] = lookup;
args[1] = invocationName;
args[2] = invocationType.resolveConstantDesc(lookup);
System.arraycopy(bootstrapArgs, 0, args, 3, bootstrapArgs.length);
return (CallSite) bsm.invokeWithArguments(args);
}
/**
* Compares the specified object with this descriptor for equality. Returns
* {@code true} if and only if the specified object is also a
* {@linkplain DynamicCallSiteDesc}, and both descriptors have equal
* bootstrap methods, bootstrap argument lists, invocation name, and
* invocation type.
*
* @param o the {@code DynamicCallSiteDesc} to compare to this
* {@code DynamicCallSiteDesc}
* @return {@code true} if the specified {@code DynamicCallSiteDesc}
* is equal to this {@code DynamicCallSiteDesc}.
*/
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DynamicCallSiteDesc specifier = (DynamicCallSiteDesc) o;
return Objects.equals(bootstrapMethod, specifier.bootstrapMethod) &&
Arrays.equals(bootstrapArgs, specifier.bootstrapArgs) &&
Objects.equals(invocationName, specifier.invocationName) &&
Objects.equals(invocationType, specifier.invocationType);
}
@Override
public final int hashCode() {
int result = Objects.hash(bootstrapMethod, invocationName, invocationType);
result = 31 * result + Arrays.hashCode(bootstrapArgs);
return result;
}
/**
* Returns a compact textual description of this call site description,
* including the bootstrap method, the invocation name and type, and
* the static bootstrap arguments.
*
* @return A compact textual description of this call site descriptor
*/
@Override
public String toString() {
return String.format("DynamicCallSiteDesc[%s::%s(%s%s):%s]",
bootstrapMethod.owner().displayName(),
bootstrapMethod.methodName(),
invocationName.equals(ConstantDescs.DEFAULT_NAME) ? "" : invocationName + "/",
Stream.of(bootstrapArgs).map(Object::toString).collect(joining(",")),
invocationType.displayDescriptor());
}
}