Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/ConstantHandle.java
12521 views
1
/*[INCLUDE-IF Sidecar17 & !OPENJDK_METHODHANDLES]*/
2
/*******************************************************************************
3
* Copyright (c) 2009, 2020 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
22
*******************************************************************************/
23
package java.lang.invoke;
24
25
import static java.lang.invoke.MethodType.*;
26
27
/*[IF JAVA_SPEC_VERSION >= 15]*/
28
import java.util.List;
29
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
30
31
/*
32
* MethodHandle subclass responsible for dealing with constant values.
33
* The dispatch targets pop the MethodHandle and then push the constant value.
34
*/
35
abstract class ConstantHandle extends MethodHandle {
36
37
ConstantHandle(MethodType type, byte kind) {
38
super(type, kind, null);
39
}
40
41
ConstantHandle(ConstantHandle originalHandle, MethodType newType) {
42
super(originalHandle, newType);
43
}
44
45
private static final ThunkTable _thunkTable = new ThunkTable();
46
private static final ConstantHandle constantHandleInt_0 = new ConstantIntHandle(methodType(int.class), 0);
47
private static final ConstantHandle constantHandleInt_1 = new ConstantIntHandle(methodType(int.class), 1);
48
private static final ConstantHandle constantHandleDouble_0 = new ConstantDoubleHandle(methodType(double.class), 0.0);
49
private static final ConstantHandle constantHandleDouble_1 = new ConstantDoubleHandle(methodType(double.class), 1.0);
50
private static final ConstantHandle constantHandleLong_0 = new ConstantLongHandle(methodType(long.class), 0L);
51
private static final ConstantHandle constantHandleLong_1 = new ConstantLongHandle(methodType(long.class), 1L);
52
53
/*
54
* This is a single shared thunkTable across all ConstantHandle subclasses
55
* as each subclass only deals with a single signature.
56
*/
57
@Override
58
protected final ThunkTable thunkTable() {
59
return _thunkTable;
60
}
61
62
public static ConstantHandle get(Class<?> constantType, Object constantValue) {
63
MethodType methodType = methodType(constantType);
64
if (!constantType.isPrimitive()) {
65
if (null != constantValue) {
66
return new ConstantObjectHandle(methodType, constantValue);
67
}
68
MethodHandleCache methodHandleCache = MethodHandleCache.getCache(constantType);
69
return (ConstantHandle)methodHandleCache.getNullConstantObjectHandle();
70
}
71
72
char charValue = 0;
73
if (constantValue instanceof Character) {
74
charValue = ((Character)constantValue).charValue();
75
}
76
77
if (constantType == double.class) {
78
double value;
79
if (constantValue instanceof Number) {
80
value = ((Number)constantValue).doubleValue();
81
} else {
82
value = charValue;
83
}
84
if (0.0 == value) {
85
return constantHandleDouble_0;
86
} else if (1.0 == value) {
87
return constantHandleDouble_1;
88
}
89
return new ConstantDoubleHandle(methodType, value);
90
} else if (constantType == long.class) {
91
long value;
92
if (constantValue instanceof Number) {
93
value = ((Number)constantValue).longValue();
94
} else {
95
value = charValue;
96
}
97
if (0L == value) {
98
return constantHandleLong_0;
99
} else if (1L == value) {
100
return constantHandleLong_1;
101
}
102
return new ConstantLongHandle(methodType, value);
103
} else if (constantType == float.class) {
104
float value;
105
if (constantValue instanceof Number) {
106
value = ((Number)constantValue).floatValue();
107
} else {
108
value = charValue;
109
}
110
return new ConstantFloatHandle(methodType, value);
111
} else {
112
// int, short, byte, char, boolean
113
int value;
114
if (constantValue instanceof Number) {
115
value = ((Number)constantValue).intValue();
116
} else if (constantValue instanceof Boolean) {
117
value = ((Boolean)constantValue).booleanValue() ? 1 : 0;
118
} else {
119
value = charValue;
120
}
121
if (int.class == constantType) {
122
if (0 == value) {
123
return constantHandleInt_0;
124
} else if (1 == value) {
125
return constantHandleInt_1;
126
}
127
}
128
return new ConstantIntHandle(methodType, value);
129
}
130
}
131
132
/*[IF JAVA_SPEC_VERSION >= 15]*/
133
@Override
134
boolean addRelatedMHs(List<MethodHandle> relatedMHs) {
135
return false;
136
}
137
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
138
}
139
140
final class ConstantObjectHandle extends ConstantHandle {
141
142
final Object value;
143
144
ConstantObjectHandle(MethodType type, Object value) {
145
super(type, KIND_CONSTANTOBJECT); //$NON-NLS-1$
146
this.value = value;
147
}
148
149
ConstantObjectHandle(ConstantObjectHandle originalHandle, MethodType newType) {
150
super(originalHandle, newType);
151
this.value = originalHandle.value;
152
}
153
154
@FrameIteratorSkip
155
private final Object invokeExact_thunkArchetype_L(int placeholder) {
156
return value;
157
}
158
159
@Override
160
MethodHandle cloneWithNewType(MethodType newType) {
161
return new ConstantObjectHandle(this, newType);
162
}
163
164
@Override
165
MethodHandle permuteArguments(MethodType permuteType, int... permute) {
166
/* Do not use cloneWithNewType() as incoming signatures may be different */
167
return new ConstantObjectHandle(permuteType, this.value);
168
}
169
170
@Override
171
MethodHandle insertArguments(MethodHandle equivalent, MethodHandle unboxingHandle, int location, Object... values) {
172
return new ConstantObjectHandle(equivalent.type, this.value);
173
}
174
175
final void compareWith(MethodHandle right, Comparator c) {
176
if (right instanceof ConstantObjectHandle) {
177
((ConstantObjectHandle)right).compareWithConstantObject(this, c);
178
} else {
179
c.fail();
180
}
181
}
182
183
final void compareWithConstantObject(ConstantObjectHandle left, Comparator c) {
184
c.compareUserSuppliedParameter(left.value, this.value);
185
}
186
}
187
188
final class ConstantIntHandle extends ConstantHandle {
189
190
final int value;
191
192
ConstantIntHandle(MethodType type, int value) {
193
super(type, KIND_CONSTANTINT); //$NON-NLS-1$
194
this.value = value;
195
}
196
197
ConstantIntHandle(ConstantIntHandle originalHandle, MethodType newType) {
198
super(originalHandle, newType);
199
this.value = originalHandle.value;
200
}
201
202
@FrameIteratorSkip
203
private final int invokeExact_thunkArchetype_I(int placeholder) {
204
return value;
205
}
206
207
@FrameIteratorSkip
208
private final byte invokeExact_thunkArchetype_B(int placeholder) {
209
return (byte)value;
210
}
211
212
@FrameIteratorSkip
213
private final char invokeExact_thunkArchetype_C(int placeholder) {
214
return (char)value;
215
}
216
217
@FrameIteratorSkip
218
private final short invokeExact_thunkArchetype_S(int placeholder) {
219
return (short)value;
220
}
221
222
@FrameIteratorSkip
223
private final boolean invokeExact_thunkArchetype_Z(int placeholder) {
224
return value != 0;
225
}
226
227
@Override
228
MethodHandle cloneWithNewType(MethodType newType) {
229
return new ConstantIntHandle(this, newType);
230
}
231
232
@Override
233
MethodHandle permuteArguments(MethodType permuteType, int... permute) {
234
/* Do not use cloneWithNewType() as incoming signatures may be different */
235
return new ConstantIntHandle(permuteType, this.value);
236
}
237
238
@Override
239
MethodHandle insertArguments(MethodHandle equivalent, MethodHandle unboxingHandle, int location, Object... values) {
240
return new ConstantIntHandle(equivalent.type, this.value);
241
}
242
243
final void compareWith(MethodHandle right, Comparator c) {
244
if (right instanceof ConstantIntHandle) {
245
((ConstantIntHandle)right).compareWithConstantInt(this, c);
246
} else {
247
c.fail();
248
}
249
}
250
251
final void compareWithConstantInt(ConstantIntHandle left, Comparator c) {
252
c.compareStructuralParameter(left.value, this.value);
253
}
254
}
255
256
final class ConstantFloatHandle extends ConstantHandle {
257
258
final float value;
259
260
ConstantFloatHandle(MethodType type, float value) {
261
super(type, KIND_CONSTANTFLOAT); //$NON-NLS-1$
262
this.value = value;
263
}
264
265
ConstantFloatHandle(ConstantFloatHandle originalHandle, MethodType newType) {
266
super(originalHandle, newType);
267
this.value = originalHandle.value;
268
}
269
270
@FrameIteratorSkip
271
private final float invokeExact_thunkArchetype_F(int placeholder) {
272
return value;
273
}
274
275
@Override
276
MethodHandle cloneWithNewType(MethodType newType) {
277
return new ConstantFloatHandle(this, newType);
278
}
279
280
@Override
281
MethodHandle permuteArguments(MethodType permuteType, int... permute) {
282
/* Do not use cloneWithNewType() as incoming signatures may be different */
283
return new ConstantFloatHandle(permuteType, this.value);
284
}
285
286
@Override
287
MethodHandle insertArguments(MethodHandle equivalent, MethodHandle unboxingHandle, int location, Object... values) {
288
return new ConstantFloatHandle(equivalent.type, this.value);
289
}
290
291
final void compareWith(MethodHandle right, Comparator c) {
292
if (right instanceof ConstantFloatHandle) {
293
((ConstantFloatHandle)right).compareWithConstantFloat(this, c);
294
} else {
295
c.fail();
296
}
297
}
298
299
final void compareWithConstantFloat(ConstantFloatHandle left, Comparator c) {
300
c.compareStructuralParameter(left.value, this.value);
301
}
302
}
303
304
final class ConstantLongHandle extends ConstantHandle {
305
306
final long value;
307
308
ConstantLongHandle(MethodType type, long value) {
309
super(type, KIND_CONSTANTLONG); //$NON-NLS-1$
310
this.value = value;
311
}
312
313
ConstantLongHandle(ConstantLongHandle originalHandle, MethodType newType) {
314
super(originalHandle, newType);
315
this.value = originalHandle.value;
316
}
317
318
@FrameIteratorSkip
319
private final long invokeExact_thunkArchetype_J(int placeholder) {
320
return value;
321
}
322
323
@Override
324
MethodHandle cloneWithNewType(MethodType newType) {
325
return new ConstantLongHandle(this, newType);
326
}
327
328
@Override
329
MethodHandle permuteArguments(MethodType permuteType, int... permute) {
330
/* Do not use cloneWithNewType() as incoming signatures may be different */
331
return new ConstantLongHandle(permuteType, this.value);
332
}
333
334
@Override
335
MethodHandle insertArguments(MethodHandle equivalent, MethodHandle unboxingHandle, int location, Object... values) {
336
return new ConstantLongHandle(equivalent.type, this.value);
337
}
338
339
final void compareWith(MethodHandle right, Comparator c) {
340
if (right instanceof ConstantLongHandle) {
341
((ConstantLongHandle)right).compareWithConstantLong(this, c);
342
} else {
343
c.fail();
344
}
345
}
346
347
final void compareWithConstantLong(ConstantLongHandle left, Comparator c) {
348
c.compareStructuralParameter(left.value, this.value);
349
}
350
}
351
352
final class ConstantDoubleHandle extends ConstantHandle {
353
354
final double value;
355
356
ConstantDoubleHandle(MethodType type, double value) {
357
super(type, KIND_CONSTANTDOUBLE); //$NON-NLS-1$
358
this.value = value;
359
}
360
361
ConstantDoubleHandle(ConstantDoubleHandle originalHandle, MethodType newType) {
362
super(originalHandle, newType);
363
this.value = originalHandle.value;
364
}
365
366
@FrameIteratorSkip
367
private final double invokeExact_thunkArchetype_D(int placeholder) {
368
return value;
369
}
370
371
@Override
372
MethodHandle cloneWithNewType(MethodType newType) {
373
return new ConstantDoubleHandle(this, newType);
374
}
375
376
@Override
377
MethodHandle permuteArguments(MethodType permuteType, int... permute) {
378
/* Do not use cloneWithNewType() as incoming signatures may be different */
379
return new ConstantDoubleHandle(permuteType, this.value);
380
}
381
382
@Override
383
MethodHandle insertArguments(MethodHandle equivalent, MethodHandle unboxingHandle, int location, Object... values) {
384
return new ConstantDoubleHandle(equivalent.type, this.value);
385
}
386
387
final void compareWith(MethodHandle right, Comparator c) {
388
if (right instanceof ConstantDoubleHandle) {
389
((ConstantDoubleHandle)right).compareWithConstantDouble(this, c);
390
} else {
391
c.fail();
392
}
393
}
394
395
final void compareWithConstantDouble(ConstantDoubleHandle left, Comparator c) {
396
c.compareStructuralParameter(left.value, this.value);
397
}
398
}
399
400