This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
MethodType.java
1449 lines (1350 loc) · 63.3 KB
/
MethodType.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008, 2021, 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.invoke;
import java.lang.constant.ClassDesc;
import java.lang.constant.Constable;
import java.lang.constant.MethodTypeDesc;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
import jdk.internal.vm.annotation.Stable;
import sun.invoke.util.BytecodeDescriptor;
import sun.invoke.util.VerifyType;
import sun.invoke.util.Wrapper;
import sun.security.util.SecurityConstants;
import static java.lang.invoke.MethodHandleStatics.UNSAFE;
import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
import static java.lang.invoke.MethodType.fromDescriptor;
/**
* A method type represents the arguments and return type accepted and
* returned by a method handle, or the arguments and return type passed
* and expected by a method handle caller. Method types must be properly
* matched between a method handle and all its callers,
* and the JVM's operations enforce this matching at, specifically
* during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
* and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
* of {@code invokedynamic} instructions.
* <p>
* The structure is a return type accompanied by any number of parameter types.
* The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.
* (For ease of exposition, we treat {@code void} as if it were a type.
* In fact, it denotes the absence of a return type.)
* <p>
* All instances of {@code MethodType} are immutable.
* Two instances are completely interchangeable if they compare equal.
* Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
* <p>
* This type can be created only by factory methods.
* All factory methods may cache values, though caching is not guaranteed.
* Some factory methods are static, while others are virtual methods which
* modify precursor method types, e.g., by changing a selected parameter.
* <p>
* Factory methods which operate on groups of parameter types
* are systematically presented in two versions, so that both Java arrays and
* Java lists can be used to work with groups of parameter types.
* The query methods {@code parameterArray} and {@code parameterList}
* also provide a choice between arrays and lists.
* <p>
* {@code MethodType} objects are sometimes derived from bytecode instructions
* such as {@code invokedynamic}, specifically from the type descriptor strings associated
* with the instructions in a class file's constant pool.
* <p>
* Like classes and strings, method types can also be represented directly
* in a class file's constant pool as constants.
* A method type may be loaded by an {@code ldc} instruction which refers
* to a suitable {@code CONSTANT_MethodType} constant pool entry.
* The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.
* (For full details on method type constants, see sections {@jvms
* 4.4.8} and {@jvms 5.4.3.5} of the Java Virtual Machine
* Specification.)
* <p>
* When the JVM materializes a {@code MethodType} from a descriptor string,
* all classes named in the descriptor must be accessible, and will be loaded.
* (But the classes need not be initialized, as is the case with a {@code CONSTANT_Class}.)
* This loading may occur at any time before the {@code MethodType} object is first derived.
* <p>
* <b><a id="descriptor">Nominal Descriptors</a></b>
* <p>
* A {@code MethodType} can be described in {@linkplain MethodTypeDesc nominal form}
* if and only if all of the parameter types and return type can be described
* with a {@link Class#describeConstable() nominal descriptor} represented by
* {@link ClassDesc}. If a method type can be described nominally, then:
* <ul>
* <li>The method type has a {@link MethodTypeDesc nominal descriptor}
* returned by {@link #describeConstable() MethodType::describeConstable}.</li>
* <li>The descriptor string returned by
* {@link #descriptorString() MethodType::descriptorString} or
* {@link #toMethodDescriptorString() MethodType::toMethodDescriptorString}
* for the method type is a method descriptor (JVMS {@jvms 4.3.3}).</li>
* </ul>
* <p>
* If any of the parameter types or return type cannot be described
* nominally, i.e. {@link Class#describeConstable() Class::describeConstable}
* returns an empty optional for that type,
* then the method type cannot be described nominally:
* <ul>
* <li>The method type has no {@link MethodTypeDesc nominal descriptor} and
* {@link #describeConstable() MethodType::describeConstable} returns
* an empty optional.</li>
* <li>The descriptor string returned by
* {@link #descriptorString() MethodType::descriptorString} or
* {@link #toMethodDescriptorString() MethodType::toMethodDescriptorString}
* for the method type is not a type descriptor.</li>
* </ul>
*
* @author John Rose, JSR 292 EG
* @since 1.7
*/
public final
class MethodType
implements Constable,
TypeDescriptor.OfMethod<Class<?>, MethodType>,
java.io.Serializable {
@java.io.Serial
private static final long serialVersionUID = 292L; // {rtype, {ptype...}}
// The rtype and ptypes fields define the structural identity of the method type:
private final @Stable Class<?> rtype;
private final @Stable Class<?>[] ptypes;
// The remaining fields are caches of various sorts:
private @Stable MethodTypeForm form; // erased form, plus cached data about primitives
private @Stable Object wrapAlt; // alternative wrapped/unwrapped version and
// private communication for readObject and readResolve
private @Stable Invokers invokers; // cache of handy higher-order adapters
private @Stable String methodDescriptor; // cache for toMethodDescriptorString
/**
* Constructor that performs no copying or validation.
* Should only be called from the factory method makeImpl
*/
private MethodType(Class<?> rtype, Class<?>[] ptypes) {
this.rtype = rtype;
this.ptypes = ptypes;
}
/*trusted*/ MethodTypeForm form() { return form; }
/*trusted*/ Class<?> rtype() { return rtype; }
/*trusted*/ Class<?>[] ptypes() { return ptypes; }
void setForm(MethodTypeForm f) { form = f; }
/** This number, mandated by the JVM spec as 255,
* is the maximum number of <em>slots</em>
* that any Java method can receive in its argument list.
* It limits both JVM signatures and method type objects.
* The longest possible invocation will look like
* {@code staticMethod(arg1, arg2, ..., arg255)} or
* {@code x.virtualMethod(arg1, arg2, ..., arg254)}.
*/
/*non-public*/
static final int MAX_JVM_ARITY = 255; // this is mandated by the JVM spec.
/** This number is the maximum arity of a method handle, 254.
* It is derived from the absolute JVM-imposed arity by subtracting one,
* which is the slot occupied by the method handle itself at the
* beginning of the argument list used to invoke the method handle.
* The longest possible invocation will look like
* {@code mh.invoke(arg1, arg2, ..., arg254)}.
*/
// Issue: Should we allow MH.invokeWithArguments to go to the full 255?
/*non-public*/
static final int MAX_MH_ARITY = MAX_JVM_ARITY-1; // deduct one for mh receiver
/** This number is the maximum arity of a method handle invoker, 253.
* It is derived from the absolute JVM-imposed arity by subtracting two,
* which are the slots occupied by invoke method handle, and the
* target method handle, which are both at the beginning of the argument
* list used to invoke the target method handle.
* The longest possible invocation will look like
* {@code invokermh.invoke(targetmh, arg1, arg2, ..., arg253)}.
*/
/*non-public*/
static final int MAX_MH_INVOKER_ARITY = MAX_MH_ARITY-1; // deduct one more for invoker
/** Return number of extra slots (count of long/double args). */
private static int checkPtypes(Class<?>[] ptypes) {
int slots = 0;
for (Class<?> ptype : ptypes) {
Objects.requireNonNull(ptype);
if (ptype == void.class)
throw newIllegalArgumentException("parameter type cannot be void");
if (ptype == double.class || ptype == long.class) {
slots++;
}
}
checkSlotCount(ptypes.length + slots);
return slots;
}
static {
// MAX_JVM_ARITY must be power of 2 minus 1 for following code trick to work:
assert((MAX_JVM_ARITY & (MAX_JVM_ARITY+1)) == 0);
}
static void checkSlotCount(int count) {
if ((count & MAX_JVM_ARITY) != count)
throw newIllegalArgumentException("bad parameter count "+count);
}
private static IndexOutOfBoundsException newIndexOutOfBoundsException(Object num) {
if (num instanceof Integer) num = "bad index: "+num;
return new IndexOutOfBoundsException(num.toString());
}
static final ConcurrentWeakInternSet<MethodType> internTable = new ConcurrentWeakInternSet<>();
static final Class<?>[] NO_PTYPES = {};
/**
* Finds or creates an instance of the given method type.
* @param rtype the return type
* @param ptypes the parameter types
* @return a method type with the given components
* @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
* @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
*/
public static MethodType methodType(Class<?> rtype, Class<?>[] ptypes) {
return makeImpl(rtype, ptypes, false);
}
/**
* Finds or creates a method type with the given components.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param rtype the return type
* @param ptypes the parameter types
* @return a method type with the given components
* @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
* @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
*/
public static MethodType methodType(Class<?> rtype, List<Class<?>> ptypes) {
boolean notrust = false; // random List impl. could return evil ptypes array
return makeImpl(rtype, listToArray(ptypes), notrust);
}
private static Class<?>[] listToArray(List<Class<?>> ptypes) {
// sanity check the size before the toArray call, since size might be huge
checkSlotCount(ptypes.size());
return ptypes.toArray(NO_PTYPES);
}
/**
* Finds or creates a method type with the given components.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* The leading parameter type is prepended to the remaining array.
* @param rtype the return type
* @param ptype0 the first parameter type
* @param ptypes the remaining parameter types
* @return a method type with the given components
* @throws NullPointerException if {@code rtype} or {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is null
* @throws IllegalArgumentException if {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is {@code void.class}
*/
public static MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes) {
Class<?>[] ptypes1 = new Class<?>[1+ptypes.length];
ptypes1[0] = ptype0;
System.arraycopy(ptypes, 0, ptypes1, 1, ptypes.length);
return makeImpl(rtype, ptypes1, true);
}
/**
* Finds or creates a method type with the given components.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* The resulting method has no parameter types.
* @param rtype the return type
* @return a method type with the given return value
* @throws NullPointerException if {@code rtype} is null
*/
public static MethodType methodType(Class<?> rtype) {
return makeImpl(rtype, NO_PTYPES, true);
}
/**
* Finds or creates a method type with the given components.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* The resulting method has the single given parameter type.
* @param rtype the return type
* @param ptype0 the parameter type
* @return a method type with the given return value and parameter type
* @throws NullPointerException if {@code rtype} or {@code ptype0} is null
* @throws IllegalArgumentException if {@code ptype0} is {@code void.class}
*/
public static MethodType methodType(Class<?> rtype, Class<?> ptype0) {
return makeImpl(rtype, new Class<?>[]{ ptype0 }, true);
}
/**
* Finds or creates a method type with the given components.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* The resulting method has the same parameter types as {@code ptypes},
* and the specified return type.
* @param rtype the return type
* @param ptypes the method type which supplies the parameter types
* @return a method type with the given components
* @throws NullPointerException if {@code rtype} or {@code ptypes} is null
*/
public static MethodType methodType(Class<?> rtype, MethodType ptypes) {
return makeImpl(rtype, ptypes.ptypes, true);
}
/**
* Sole factory method to find or create an interned method type. Will perform
* input validation on behalf of factory methods
*
* @param rtype desired return type
* @param ptypes desired parameter types
* @param trusted whether the ptypes can be used without cloning
* @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
* @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
* @return the unique method type of the desired structure
*/
/*trusted*/
static MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
if (ptypes.length == 0) {
ptypes = NO_PTYPES; trusted = true;
}
MethodType primordialMT = new MethodType(rtype, ptypes);
MethodType mt = internTable.get(primordialMT);
if (mt != null)
return mt;
// promote the object to the Real Thing, and reprobe
Objects.requireNonNull(rtype);
if (trusted) {
MethodType.checkPtypes(ptypes);
mt = primordialMT;
} else {
// Make defensive copy then validate
ptypes = Arrays.copyOf(ptypes, ptypes.length);
MethodType.checkPtypes(ptypes);
mt = new MethodType(rtype, ptypes);
}
mt.form = MethodTypeForm.findForm(mt);
return internTable.add(mt);
}
private static final @Stable MethodType[] objectOnlyTypes = new MethodType[20];
/**
* Finds or creates a method type whose components are {@code Object} with an optional trailing {@code Object[]} array.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* All parameters and the return type will be {@code Object},
* except the final array parameter if any, which will be {@code Object[]}.
* @param objectArgCount number of parameters (excluding the final array parameter if any)
* @param finalArray whether there will be a trailing array parameter, of type {@code Object[]}
* @return a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
* @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255 (or 254, if {@code finalArray} is true)
* @see #genericMethodType(int)
*/
public static MethodType genericMethodType(int objectArgCount, boolean finalArray) {
MethodType mt;
checkSlotCount(objectArgCount);
int ivarargs = (!finalArray ? 0 : 1);
int ootIndex = objectArgCount*2 + ivarargs;
if (ootIndex < objectOnlyTypes.length) {
mt = objectOnlyTypes[ootIndex];
if (mt != null) return mt;
}
Class<?>[] ptypes = new Class<?>[objectArgCount + ivarargs];
Arrays.fill(ptypes, Object.class);
if (ivarargs != 0) ptypes[objectArgCount] = Object[].class;
mt = makeImpl(Object.class, ptypes, true);
if (ootIndex < objectOnlyTypes.length) {
objectOnlyTypes[ootIndex] = mt; // cache it here also!
}
return mt;
}
/**
* Finds or creates a method type whose components are all {@code Object}.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* All parameters and the return type will be Object.
* @param objectArgCount number of parameters
* @return a generally applicable method type, for all calls of the given argument count
* @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255
* @see #genericMethodType(int, boolean)
*/
public static MethodType genericMethodType(int objectArgCount) {
return genericMethodType(objectArgCount, false);
}
/**
* Finds or creates a method type with a single different parameter type.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param num the index (zero-based) of the parameter type to change
* @param nptype a new parameter type to replace the old one with
* @return the same type, except with the selected parameter changed
* @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
* @throws IllegalArgumentException if {@code nptype} is {@code void.class}
* @throws NullPointerException if {@code nptype} is null
*/
public MethodType changeParameterType(int num, Class<?> nptype) {
if (parameterType(num) == nptype) return this;
Class<?>[] nptypes = ptypes.clone();
nptypes[num] = nptype;
return makeImpl(rtype, nptypes, true);
}
/**
* Finds or creates a method type with additional parameter types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param num the position (zero-based) of the inserted parameter type(s)
* @param ptypesToInsert zero or more new parameter types to insert into the parameter list
* @return the same type, except with the selected parameter(s) inserted
* @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
* @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
* or if the resulting method type would have more than 255 parameter slots
* @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
*/
public MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert) {
int len = ptypes.length;
if (num < 0 || num > len)
throw newIndexOutOfBoundsException(num);
int ins = checkPtypes(ptypesToInsert);
checkSlotCount(parameterSlotCount() + ptypesToInsert.length + ins);
int ilen = ptypesToInsert.length;
if (ilen == 0) return this;
Class<?>[] nptypes = new Class<?>[len + ilen];
if (num > 0) {
System.arraycopy(ptypes, 0, nptypes, 0, num);
}
System.arraycopy(ptypesToInsert, 0, nptypes, num, ilen);
if (num < len) {
System.arraycopy(ptypes, num, nptypes, num+ilen, len-num);
}
return makeImpl(rtype, nptypes, true);
}
/**
* Finds or creates a method type with additional parameter types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
* @return the same type, except with the selected parameter(s) appended
* @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
* or if the resulting method type would have more than 255 parameter slots
* @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
*/
public MethodType appendParameterTypes(Class<?>... ptypesToInsert) {
return insertParameterTypes(parameterCount(), ptypesToInsert);
}
/**
* Finds or creates a method type with additional parameter types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param num the position (zero-based) of the inserted parameter type(s)
* @param ptypesToInsert zero or more new parameter types to insert into the parameter list
* @return the same type, except with the selected parameter(s) inserted
* @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
* @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
* or if the resulting method type would have more than 255 parameter slots
* @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
*/
public MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert) {
return insertParameterTypes(num, listToArray(ptypesToInsert));
}
/**
* Finds or creates a method type with additional parameter types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
* @return the same type, except with the selected parameter(s) appended
* @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
* or if the resulting method type would have more than 255 parameter slots
* @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
*/
public MethodType appendParameterTypes(List<Class<?>> ptypesToInsert) {
return insertParameterTypes(parameterCount(), ptypesToInsert);
}
/**
* Finds or creates a method type with modified parameter types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param start the position (zero-based) of the first replaced parameter type(s)
* @param end the position (zero-based) after the last replaced parameter type(s)
* @param ptypesToInsert zero or more new parameter types to insert into the parameter list
* @return the same type, except with the selected parameter(s) replaced
* @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
* or if {@code end} is negative or greater than {@code parameterCount()}
* or if {@code start} is greater than {@code end}
* @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
* or if the resulting method type would have more than 255 parameter slots
* @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
*/
/*non-public*/
MethodType replaceParameterTypes(int start, int end, Class<?>... ptypesToInsert) {
if (start == end)
return insertParameterTypes(start, ptypesToInsert);
int len = ptypes.length;
if (!(0 <= start && start <= end && end <= len))
throw newIndexOutOfBoundsException("start="+start+" end="+end);
int ilen = ptypesToInsert.length;
if (ilen == 0)
return dropParameterTypes(start, end);
return dropParameterTypes(start, end).insertParameterTypes(start, ptypesToInsert);
}
/** Replace the last arrayLength parameter types with the component type of arrayType.
* @param arrayType any array type
* @param pos position at which to spread
* @param arrayLength the number of parameter types to change
* @return the resulting type
*/
/*non-public*/
MethodType asSpreaderType(Class<?> arrayType, int pos, int arrayLength) {
assert(parameterCount() >= arrayLength);
int spreadPos = pos;
if (arrayLength == 0) return this; // nothing to change
if (arrayType == Object[].class) {
if (isGeneric()) return this; // nothing to change
if (spreadPos == 0) {
// no leading arguments to preserve; go generic
MethodType res = genericMethodType(arrayLength);
if (rtype != Object.class) {
res = res.changeReturnType(rtype);
}
return res;
}
}
Class<?> elemType = arrayType.getComponentType();
assert(elemType != null);
for (int i = spreadPos; i < spreadPos + arrayLength; i++) {
if (ptypes[i] != elemType) {
Class<?>[] fixedPtypes = ptypes.clone();
Arrays.fill(fixedPtypes, i, spreadPos + arrayLength, elemType);
return methodType(rtype, fixedPtypes);
}
}
return this; // arguments check out; no change
}
/** Return the leading parameter type, which must exist and be a reference.
* @return the leading parameter type, after error checks
*/
/*non-public*/
Class<?> leadingReferenceParameter() {
Class<?> ptype;
if (ptypes.length == 0 ||
(ptype = ptypes[0]).isPrimitive())
throw newIllegalArgumentException("no leading reference parameter");
return ptype;
}
/** Delete the last parameter type and replace it with arrayLength copies of the component type of arrayType.
* @param arrayType any array type
* @param pos position at which to insert parameters
* @param arrayLength the number of parameter types to insert
* @return the resulting type
*/
/*non-public*/
MethodType asCollectorType(Class<?> arrayType, int pos, int arrayLength) {
assert(parameterCount() >= 1);
assert(pos < ptypes.length);
assert(ptypes[pos].isAssignableFrom(arrayType));
MethodType res;
if (arrayType == Object[].class) {
res = genericMethodType(arrayLength);
if (rtype != Object.class) {
res = res.changeReturnType(rtype);
}
} else {
Class<?> elemType = arrayType.getComponentType();
assert(elemType != null);
res = methodType(rtype, Collections.nCopies(arrayLength, elemType));
}
if (ptypes.length == 1) {
return res;
} else {
// insert after (if need be), then before
if (pos < ptypes.length - 1) {
res = res.insertParameterTypes(arrayLength, Arrays.copyOfRange(ptypes, pos + 1, ptypes.length));
}
return res.insertParameterTypes(0, Arrays.copyOf(ptypes, pos));
}
}
/**
* Finds or creates a method type with some parameter types omitted.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param start the index (zero-based) of the first parameter type to remove
* @param end the index (greater than {@code start}) of the first parameter type after not to remove
* @return the same type, except with the selected parameter(s) removed
* @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
* or if {@code end} is negative or greater than {@code parameterCount()}
* or if {@code start} is greater than {@code end}
*/
public MethodType dropParameterTypes(int start, int end) {
int len = ptypes.length;
if (!(0 <= start && start <= end && end <= len))
throw newIndexOutOfBoundsException("start="+start+" end="+end);
if (start == end) return this;
Class<?>[] nptypes;
if (start == 0) {
if (end == len) {
// drop all parameters
nptypes = NO_PTYPES;
} else {
// drop initial parameter(s)
nptypes = Arrays.copyOfRange(ptypes, end, len);
}
} else {
if (end == len) {
// drop trailing parameter(s)
nptypes = Arrays.copyOfRange(ptypes, 0, start);
} else {
int tail = len - end;
nptypes = Arrays.copyOfRange(ptypes, 0, start + tail);
System.arraycopy(ptypes, end, nptypes, start, tail);
}
}
return makeImpl(rtype, nptypes, true);
}
/**
* Finds or creates a method type with a different return type.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* @param nrtype a return parameter type to replace the old one with
* @return the same type, except with the return type change
* @throws NullPointerException if {@code nrtype} is null
*/
public MethodType changeReturnType(Class<?> nrtype) {
if (returnType() == nrtype) return this;
return makeImpl(nrtype, ptypes, true);
}
/**
* Reports if this type contains a primitive argument or return value.
* The return type {@code void} counts as a primitive.
* @return true if any of the types are primitives
*/
public boolean hasPrimitives() {
return form.hasPrimitives();
}
/**
* Reports if this type contains a wrapper argument or return value.
* Wrappers are types which box primitive values, such as {@link Integer}.
* The reference type {@code java.lang.Void} counts as a wrapper,
* if it occurs as a return type.
* @return true if any of the types are wrappers
*/
public boolean hasWrappers() {
return unwrap() != this;
}
/**
* Erases all reference types to {@code Object}.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* All primitive types (including {@code void}) will remain unchanged.
* @return a version of the original type with all reference types replaced
*/
public MethodType erase() {
return form.erasedType();
}
/**
* Erases all reference types to {@code Object}, and all subword types to {@code int}.
* This is the reduced type polymorphism used by private methods
* such as {@link MethodHandle#invokeBasic invokeBasic}.
* @return a version of the original type with all reference and subword types replaced
*/
/*non-public*/
MethodType basicType() {
return form.basicType();
}
private static final @Stable Class<?>[] METHOD_HANDLE_ARRAY
= new Class<?>[] { MethodHandle.class };
/**
* @return a version of the original type with MethodHandle prepended as the first argument
*/
/*non-public*/
MethodType invokerType() {
return insertParameterTypes(0, METHOD_HANDLE_ARRAY);
}
/**
* Converts all types, both reference and primitive, to {@code Object}.
* Convenience method for {@link #genericMethodType(int) genericMethodType}.
* The expression {@code type.wrap().erase()} produces the same value
* as {@code type.generic()}.
* @return a version of the original type with all types replaced
*/
public MethodType generic() {
return genericMethodType(parameterCount());
}
/*non-public*/
boolean isGeneric() {
return this == erase() && !hasPrimitives();
}
/**
* Converts all primitive types to their corresponding wrapper types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* All reference types (including wrapper types) will remain unchanged.
* A {@code void} return type is changed to the type {@code java.lang.Void}.
* The expression {@code type.wrap().erase()} produces the same value
* as {@code type.generic()}.
* @return a version of the original type with all primitive types replaced
*/
public MethodType wrap() {
return hasPrimitives() ? wrapWithPrims(this) : this;
}
/**
* Converts all wrapper types to their corresponding primitive types.
* Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
* All primitive types (including {@code void}) will remain unchanged.
* A return type of {@code java.lang.Void} is changed to {@code void}.
* @return a version of the original type with all wrapper types replaced
*/
public MethodType unwrap() {
MethodType noprims = !hasPrimitives() ? this : wrapWithPrims(this);
return unwrapWithNoPrims(noprims);
}
private static MethodType wrapWithPrims(MethodType pt) {
assert(pt.hasPrimitives());
MethodType wt = (MethodType)pt.wrapAlt;
if (wt == null) {
// fill in lazily
wt = MethodTypeForm.canonicalize(pt, MethodTypeForm.WRAP);
assert(wt != null);
pt.wrapAlt = wt;
}
return wt;
}
private static MethodType unwrapWithNoPrims(MethodType wt) {
assert(!wt.hasPrimitives());
MethodType uwt = (MethodType)wt.wrapAlt;
if (uwt == null) {
// fill in lazily
uwt = MethodTypeForm.canonicalize(wt, MethodTypeForm.UNWRAP);
if (uwt == null)
uwt = wt; // type has no wrappers or prims at all
wt.wrapAlt = uwt;
}
return uwt;
}
/**
* Returns the parameter type at the specified index, within this method type.
* @param num the index (zero-based) of the desired parameter type
* @return the selected parameter type
* @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
*/
public Class<?> parameterType(int num) {
return ptypes[num];
}
/**
* Returns the number of parameter types in this method type.
* @return the number of parameter types
*/
public int parameterCount() {
return ptypes.length;
}
/**
* Returns the return type of this method type.
* @return the return type
*/
public Class<?> returnType() {
return rtype;
}
/**
* Presents the parameter types as a list (a convenience method).
* The list will be immutable.
* @return the parameter types (as an immutable list)
*/
public List<Class<?>> parameterList() {
return Collections.unmodifiableList(Arrays.asList(ptypes.clone()));
}
/**
* Returns the last parameter type of this method type.
* If this type has no parameters, the sentinel value
* {@code void.class} is returned instead.
* @apiNote
* <p>
* The sentinel value is chosen so that reflective queries can be
* made directly against the result value.
* The sentinel value cannot be confused with a real parameter,
* since {@code void} is never acceptable as a parameter type.
* For variable arity invocation modes, the expression
* {@link Class#getComponentType lastParameterType().getComponentType()}
* is useful to query the type of the "varargs" parameter.
* @return the last parameter type if any, else {@code void.class}
* @since 10
*/
public Class<?> lastParameterType() {
int len = ptypes.length;
return len == 0 ? void.class : ptypes[len-1];
}
/**
* Presents the parameter types as an array (a convenience method).
* Changes to the array will not result in changes to the type.
* @return the parameter types (as a fresh copy if necessary)
*/
public Class<?>[] parameterArray() {
return ptypes.clone();
}
/**
* Compares the specified object with this type for equality.
* That is, it returns {@code true} if and only if the specified object
* is also a method type with exactly the same parameters and return type.
* @param x object to compare
* @see Object#equals(Object)
*/
// This implementation may also return true if x is a WeakEntry containing
// a method type that is equal to this. This is an internal implementation
// detail to allow for faster method type lookups.
// See ConcurrentWeakInternSet.WeakEntry#equals(Object)
@Override
public boolean equals(Object x) {
if (this == x) {
return true;
}
if (x instanceof MethodType) {
return equals((MethodType)x);
}
if (x instanceof ConcurrentWeakInternSet.WeakEntry) {
Object o = ((ConcurrentWeakInternSet.WeakEntry)x).get();
if (o instanceof MethodType) {
return equals((MethodType)o);
}
}
return false;
}
private boolean equals(MethodType that) {
return this.rtype == that.rtype
&& Arrays.equals(this.ptypes, that.ptypes);
}
/**
* Returns the hash code value for this method type.
* It is defined to be the same as the hashcode of a List
* whose elements are the return type followed by the
* parameter types.
* @return the hash code value for this method type
* @see Object#hashCode()
* @see #equals(Object)
* @see List#hashCode()
*/
@Override
public int hashCode() {
int hashCode = 31 + rtype.hashCode();
for (Class<?> ptype : ptypes)
hashCode = 31 * hashCode + ptype.hashCode();
return hashCode;
}
/**
* Returns a string representation of the method type,
* of the form {@code "(PT0,PT1...)RT"}.
* The string representation of a method type is a
* parenthesis enclosed, comma separated list of type names,
* followed immediately by the return type.
* <p>
* Each type is represented by its
* {@link java.lang.Class#getSimpleName simple name}.
*/
@Override
public String toString() {
StringJoiner sj = new StringJoiner(",", "(",
")" + rtype.getSimpleName());
for (int i = 0; i < ptypes.length; i++) {
sj.add(ptypes[i].getSimpleName());
}
return sj.toString();
}
/** True if my parameter list is effectively identical to the given full list,
* after skipping the given number of my own initial parameters.
* In other words, after disregarding {@code skipPos} parameters,
* my remaining parameter list is no longer than the {@code fullList}, and
* is equal to the same-length initial sublist of {@code fullList}.
*/
/*non-public*/
boolean effectivelyIdenticalParameters(int skipPos, List<Class<?>> fullList) {
int myLen = ptypes.length, fullLen = fullList.size();
if (skipPos > myLen || myLen - skipPos > fullLen)
return false;
List<Class<?>> myList = Arrays.asList(ptypes);
if (skipPos != 0) {
myList = myList.subList(skipPos, myLen);
myLen -= skipPos;
}
if (fullLen == myLen)
return myList.equals(fullList);
else
return myList.equals(fullList.subList(0, myLen));
}
/** True if the old return type can always be viewed (w/o casting) under new return type,
* and the new parameters can be viewed (w/o casting) under the old parameter types.
*/
/*non-public*/
boolean isViewableAs(MethodType newType, boolean keepInterfaces) {
if (!VerifyType.isNullConversion(returnType(), newType.returnType(), keepInterfaces))
return false;
if (form == newType.form && form.erasedType == this)
return true; // my reference parameters are all Object
if (ptypes == newType.ptypes)
return true;
int argc = parameterCount();
if (argc != newType.parameterCount())
return false;
for (int i = 0; i < argc; i++) {
if (!VerifyType.isNullConversion(newType.parameterType(i), parameterType(i), keepInterfaces))
return false;
}
return true;
}
/*non-public*/
boolean isConvertibleTo(MethodType newType) {
MethodTypeForm oldForm = this.form();
MethodTypeForm newForm = newType.form();
if (oldForm == newForm)
// same parameter count, same primitive/object mix
return true;
if (!canConvert(returnType(), newType.returnType()))
return false;
Class<?>[] srcTypes = newType.ptypes;
Class<?>[] dstTypes = ptypes;
if (srcTypes == dstTypes)
return true;
int argc;
if ((argc = srcTypes.length) != dstTypes.length)
return false;
if (argc <= 1) {
if (argc == 1 && !canConvert(srcTypes[0], dstTypes[0]))
return false;
return true;
}
if ((!oldForm.hasPrimitives() && oldForm.erasedType == this) ||
(!newForm.hasPrimitives() && newForm.erasedType == newType)) {
// Somewhat complicated test to avoid a loop of 2 or more trips.
// If either type has only Object parameters, we know we can convert.
assert(canConvertParameters(srcTypes, dstTypes));
return true;
}
return canConvertParameters(srcTypes, dstTypes);
}
/** Returns true if MHs.explicitCastArguments produces the same result as MH.asType.
* If the type conversion is impossible for either, the result should be false.
*/
/*non-public*/
boolean explicitCastEquivalentToAsType(MethodType newType) {
if (this == newType) return true;
if (!explicitCastEquivalentToAsType(rtype, newType.rtype)) {
return false;
}
Class<?>[] srcTypes = newType.ptypes;
Class<?>[] dstTypes = ptypes;
if (dstTypes == srcTypes) {
return true;
}
assert(dstTypes.length == srcTypes.length);
for (int i = 0; i < dstTypes.length; i++) {
if (!explicitCastEquivalentToAsType(srcTypes[i], dstTypes[i])) {
return false;
}
}
return true;
}
/** Reports true if the src can be converted to the dst, by both asType and MHs.eCE,
* and with the same effect.
* MHs.eCA has the following "upgrades" to MH.asType:
* 1. interfaces are unchecked (that is, treated as if aliased to Object)
* Therefore, {@code Object->CharSequence} is possible in both cases but has different semantics
* 2. the full matrix of primitive-to-primitive conversions is supported