/
GenericTypeDescription.java
2481 lines (2123 loc) · 93.2 KB
/
GenericTypeDescription.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
package net.bytebuddy.description.type.generic;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.TargetType;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.utility.JavaMethod;
import org.objectweb.asm.signature.SignatureVisitor;
import java.lang.reflect.*;
import java.util.*;
/**
* Represents a generic type of the Java programming language. A non-generic {@link TypeDescription} is considered to be
* a specialization of a generic type.
*/
public interface GenericTypeDescription extends NamedElement {
/**
* Returns the sort of the generic type this instance represents.
*
* @return The sort of the generic type.
*/
Sort getSort();
/**
* Returns the name of the type. For generic types, this name is their {@link Object#toString()} representations. For a non-generic
* type, it is the fully qualified binary name of the type.
*
* @return The name of this type.
*/
String getTypeName();
/**
* Returns the erasure of this type. Wildcard types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#WILDCARD})
* do not have a well-defined erasure and cause an {@link IllegalStateException} to be thrown.
*
* @return The erasure of this type.
*/
TypeDescription asRawType();
/**
* <p>
* Returns the generic super type of this type.
* </p>
* <p>
* Only non-generic types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}) and parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) define a super type. For other generic
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The generic super type of this type or {@code null} if no such type exists.
*/
GenericTypeDescription getSuperType();
/**
* <p>
* Returns the generic interface types of this type.
* </p>
* <p>
* Only non-generic types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}) and parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) define a super type. For other generic
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The generic interface types of this type.
*/
GenericTypeList getInterfaces();
/**
* <p>
* Returns a list of field descriptions that are declared by this type. For parameterized types, all type variables of these fields are
* resolved to the values of the type variables.
* </p>
* <p>
* Only non-generic types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}) and parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) define a super type. For other generic
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return A list of fields that are declared by this type.
*/
FieldList<?> getDeclaredFields();
/**
* <p>
* Returns a list of method descriptions that are declared by this type. For parameterized types, all type variables used by these methods
* are resolved to the values of the type variables.
* </p>
* <p>
* Only non-generic types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}) and parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) define a super type. For other generic
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return A list of methods that are declared by this type.
*/
MethodList<?> getDeclaredMethods();
/**
* <p>
* Returns the upper bounds of this type. Any type with a well-defined upper bound is bound by at least one type. If no such
* type is defined, the bound is implicitly {@link Object}.
* </p>
* <p>
* Only non-symbolic type variables ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE},
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE_DETACHED}) and wildcard types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#WILDCARD}) have well-defined upper bounds. For other
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The upper bounds of this type.
*/
GenericTypeList getUpperBounds();
/**
* <p>
* Returns the lower bounds of this type.
* </p>
* <p>
* Only wildcard types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#WILDCARD}) define a lower bound. For other
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The lower bounds of this type.
*/
GenericTypeList getLowerBounds();
/**
* <p>
* Returns the component type of this type.
* </p>
* <p>
* Only non-generic types ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}) and generic array types
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#GENERIC_ARRAY}) define a component type. For other
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The component type of this type or {@code null} if this type does not represent an array type.
*/
GenericTypeDescription getComponentType();
/**
* <p>
* Returns the type parameters of this type.
* </p>
* <p>
* Parameters are only well-defined for parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) and non-generic types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}). For non-generic types, the
* returned list is always empty. For all other types, this method throws an {@link IllegalStateException}.
* </p>
*
* @return A list of this type's type parameters.
*/
GenericTypeList getParameters();
/**
* <p>
* Returns the owner type of this type.
* </p>
* <p>
* An owner type is only well-defined for parameterized types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}) and non-generic types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}). Non-generic types do never have an
* owner type. For all other types, this method throws an {@link IllegalStateException}.
* </p>
*
* @return This type's owner type or {@code null} if no such owner type exists.
*/
GenericTypeDescription getOwnerType();
/**
* Returns the source of this type variable. A type variable source is only well-defined for an attached type variable
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE}. For other types, this method
* throws an {@link IllegalStateException}.
*
* @return This type's type variable source.
*/
TypeVariableSource getVariableSource();
/**
* Returns the symbol of this type variable. A symbol is only well-defined for type variables
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE},
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE_DETACHED},
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE_SYMBOLIC}). For other types, this method
* throws an {@link IllegalStateException}.
*
* @return This type's type variable symbol.
*/
String getSymbol();
/**
* Returns the size of the type described by this instance. Wildcard types
* ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#WILDCARD} do not have a well-defined a stack size and
* cause an {@link IllegalStateException} to be thrown.
*
* @return The size of the type described by this instance.
*/
StackSize getStackSize();
/**
* Checks if the type described by this entity is an array.
*
* @return {@code true} if this type description represents an array.
*/
boolean isArray();
/**
* Checks if the type described by this entity is a primitive type.
*
* @return {@code true} if this type description represents a primitive type.
*/
boolean isPrimitive();
/**
* Applies a visitor to this generic type description.
*
* @param visitor The visitor to apply.
* @param <T> The value that this visitor yields.
* @return The visitor's return value.
*/
<T> T accept(Visitor<T> visitor);
/**
* Represents a {@link GenericTypeDescription}'s form.
*/
enum Sort {
/**
* Represents a non-generic type.
*/
NON_GENERIC,
/**
* Represents a generic array type.
*/
GENERIC_ARRAY,
/**
* Represents a parameterized type.
*/
PARAMETERIZED,
/**
* Represents a wildcard type.
*/
WILDCARD,
/**
* Represents a type variable that is attached to a {@link TypeVariableSource}.
*/
VARIABLE,
/**
* Represents a type variable that is not attached to a {@link TypeVariableSource} but defines type bounds.
*/
VARIABLE_DETACHED,
/**
* Represents a type variable that is merely symbolic and is not attached to a {@link TypeVariableSource} and does not defined bounds.
*/
VARIABLE_SYMBOLIC;
/**
* Describes a loaded generic type as a {@link GenericTypeDescription}.
*
* @param type The type to describe.
* @return A description of the provided generic type.
*/
public static GenericTypeDescription describe(Type type) {
if (type instanceof Class<?>) {
return new TypeDescription.ForLoadedType((Class<?>) type);
} else if (type instanceof GenericArrayType) {
return new ForGenericArray.OfLoadedType((GenericArrayType) type);
} else if (type instanceof ParameterizedType) {
return new ForParameterizedType.OfLoadedType((ParameterizedType) type);
} else if (type instanceof TypeVariable) {
return new ForTypeVariable.OfLoadedType((TypeVariable<?>) type);
} else if (type instanceof WildcardType) {
return new ForWildcardType.OfLoadedType((WildcardType) type);
} else {
throw new IllegalArgumentException("Unknown type: " + type);
}
}
/**
* Checks if this type form represents a non-generic type.
*
* @return {@code true} if this type form represents a non-generic.
*/
public boolean isNonGeneric() {
return this == NON_GENERIC;
}
/**
* Checks if this type form represents a parameterized type.
*
* @return {@code true} if this type form represents a parameterized type.
*/
public boolean isParameterized() {
return this == PARAMETERIZED;
}
/**
* Checks if this type form represents a generic array.
*
* @return {@code true} if this type form represents a generic array.
*/
public boolean isGenericArray() {
return this == GENERIC_ARRAY;
}
/**
* Checks if this type form represents a wildcard.
*
* @return {@code true} if this type form represents a wildcard.
*/
public boolean isWildcard() {
return this == WILDCARD;
}
/**
* Checks if this type form represents an attached type variable.
*
* @return {@code true} if this type form represents an attached type variable.
*/
public boolean isTypeVariable() {
return this == VARIABLE;
}
/**
* Checks if this type form represents a detached type variable.
*
* @return {@code true} if this type form represents a detached type variable.
*/
public boolean isDetachedTypeVariable() {
return this == VARIABLE_DETACHED;
}
/**
* Checks if this type form represents a symbolic type variable.
*
* @return {@code true} if this type form represents a symbolic type variable.
*/
public boolean isSymbolicTypeVariable() {
return this == VARIABLE_SYMBOLIC;
}
@Override
public String toString() {
return "GenericTypeDescription.Sort." + name();
}
}
/**
* A visitor that can be applied to a {@link GenericTypeDescription} for differentiating on the sort of the visited type.
*
* @param <T> The visitor's return value's type.
*/
interface Visitor<T> {
/**
* Visits a generic array type ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#GENERIC_ARRAY}).
*
* @param genericArray The generic array type.
* @return The visitor's return value.
*/
T onGenericArray(GenericTypeDescription genericArray);
/**
* Visits a wildcard type ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#WILDCARD}).
*
* @param wildcardType The generic array type.
* @return The visitor's return value.
*/
T onWildcardType(GenericTypeDescription wildcardType);
/**
* Visits a parameterized type ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#PARAMETERIZED}).
*
* @param parameterizedType The generic array type.
* @return The visitor's return value.
*/
T onParameterizedType(GenericTypeDescription parameterizedType);
/**
* Visits a type variable ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE},
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE_DETACHED},
* {@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#VARIABLE_SYMBOLIC}).
*
* @param typeVariable The generic array type.
* @return The visitor's return value.
*/
T onTypeVariable(GenericTypeDescription typeVariable);
/**
* Visits a non-generic type ({@link net.bytebuddy.description.type.generic.GenericTypeDescription.Sort#NON_GENERIC}).
*
* @param typeDescription The non-generic type.
* @return The visitor's return value.
*/
T onNonGenericType(GenericTypeDescription typeDescription);
/**
* A non-operational generic type visitor. Any visited type is returned in its existing form.
*/
enum NoOp implements Visitor<GenericTypeDescription> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public GenericTypeDescription onGenericArray(GenericTypeDescription genericArray) {
return genericArray;
}
@Override
public GenericTypeDescription onWildcardType(GenericTypeDescription wildcardType) {
return wildcardType;
}
@Override
public GenericTypeDescription onParameterizedType(GenericTypeDescription parameterizedType) {
return parameterizedType;
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription typeVariable) {
return typeVariable;
}
@Override
public GenericTypeDescription onNonGenericType(GenericTypeDescription typeDescription) {
return typeDescription;
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.NoOp." + name();
}
}
/**
* A visitor for erasing type variables on the most fine-grained level. In practice, this means:
* <ul>
* <li>Parameterized types are reduced to their erasure if one of its parameters represents a type variable or a wildcard with a bound
* that is a type variable.</li>
* <li>Wildcards have their bound erased, if required.</li>
* <li>Type variables are erased.</li>
* <li>Generic arrays have their component type erased, if required.</li>
* <li>Non-generic types are transformed into raw-type representations of the same type.</li>
* </ul>
*/
enum TypeVariableErasing implements Visitor<GenericTypeDescription> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public GenericTypeDescription onGenericArray(GenericTypeDescription genericArray) {
return ForGenericArray.Latent.of(genericArray.getComponentType().accept(this), 1);
}
@Override
public GenericTypeDescription onWildcardType(GenericTypeDescription wildcardType) {
// Wildcards which are used within parameterized types are taken care of by the calling method.
GenericTypeList lowerBounds = wildcardType.getLowerBounds();
return lowerBounds.isEmpty()
? GenericTypeDescription.ForWildcardType.Latent.boundedAbove(wildcardType.getUpperBounds().getOnly().accept(this))
: GenericTypeDescription.ForWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}
@Override
public GenericTypeDescription onParameterizedType(GenericTypeDescription parameterizedType) {
List<GenericTypeDescription> parameters = new ArrayList<GenericTypeDescription>(parameterizedType.getParameters().size());
for (GenericTypeDescription parameter : parameterizedType.getParameters()) {
if (parameter.accept(PartialErasureReviser.INSTANCE)) {
return parameterizedType.asRawType();
}
parameters.add(parameter.accept(this));
}
GenericTypeDescription ownerType = parameterizedType.getOwnerType();
return new GenericTypeDescription.ForParameterizedType.Latent(parameterizedType.asRawType(),
parameters,
ownerType == null
? null
: ownerType.accept(this));
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription typeVariable) {
return typeVariable.asRawType();
}
@Override
public GenericTypeDescription onNonGenericType(GenericTypeDescription typeDescription) {
return new ForParameterizedType.Raw(typeDescription.asRawType());
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.TypeVariableErasing." + name();
}
/**
* A visitor for checking if a type can be erased partially when defined as a parameter of a parameterized type.
* If this condition is true, a parameterized type must be erased instead of erasing the parameterized type's
* parameters.
*/
protected enum PartialErasureReviser implements Visitor<Boolean> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Boolean onGenericArray(GenericTypeDescription genericArray) {
return genericArray.getComponentType().accept(this);
}
@Override
public Boolean onWildcardType(GenericTypeDescription wildcardType) {
GenericTypeList lowerBounds = wildcardType.getLowerBounds();
return lowerBounds.isEmpty()
? wildcardType.getUpperBounds().getOnly().accept(this)
: lowerBounds.getOnly().accept(this);
}
@Override
public Boolean onParameterizedType(GenericTypeDescription parameterizedType) {
return false;
}
@Override
public Boolean onTypeVariable(GenericTypeDescription typeVariable) {
return true;
}
@Override
public Boolean onNonGenericType(GenericTypeDescription typeDescription) {
return false;
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.TypeVariableErasing.PartialErasureReviser." + name();
}
}
}
/**
* Visits a generic type and appends the discovered type to the supplied signature visitor.
*/
class ForSignatureVisitor implements Visitor<SignatureVisitor> {
/**
* Index of a {@link String}'s only character to improve code readabilty.
*/
private static final int ONLY_CHARACTER = 0;
/**
* The signature visitor that receives the discovered generic type.
*/
protected final SignatureVisitor signatureVisitor;
/**
* Creates a new visitor for the given signature visitor.
*
* @param signatureVisitor The signature visitor that receives the discovered generic type.
*/
public ForSignatureVisitor(SignatureVisitor signatureVisitor) {
this.signatureVisitor = signatureVisitor;
}
@Override
public SignatureVisitor onGenericArray(GenericTypeDescription genericArray) {
genericArray.getComponentType().accept(new ForSignatureVisitor(signatureVisitor.visitArrayType()));
return signatureVisitor;
}
@Override
public SignatureVisitor onWildcardType(GenericTypeDescription wildcardType) {
throw new IllegalStateException("Unexpected wildcard: " + wildcardType);
}
@Override
public SignatureVisitor onParameterizedType(GenericTypeDescription parameterizedType) {
onOwnableType(parameterizedType);
signatureVisitor.visitEnd();
return signatureVisitor;
}
/**
* Visits a type which might define an owner type.
*
* @param genericTypeDescription The visited generic type.
*/
private void onOwnableType(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription ownerType = genericTypeDescription.getOwnerType();
if (ownerType != null && ownerType.getSort().isParameterized()) {
onOwnableType(ownerType);
signatureVisitor.visitInnerClassType(genericTypeDescription.asRawType().getSimpleName());
} else {
signatureVisitor.visitClassType(genericTypeDescription.asRawType().getInternalName());
}
for (GenericTypeDescription upperBound : genericTypeDescription.getParameters()) {
upperBound.accept(new OfParameter(signatureVisitor));
}
}
@Override
public SignatureVisitor onTypeVariable(GenericTypeDescription typeVariable) {
signatureVisitor.visitTypeVariable(typeVariable.getSymbol());
return signatureVisitor;
}
@Override
public SignatureVisitor onNonGenericType(GenericTypeDescription typeDescription) {
if(typeDescription.isArray()) {
typeDescription.getComponentType().accept(new ForSignatureVisitor(signatureVisitor.visitArrayType()));
} else if (typeDescription.isPrimitive()) {
signatureVisitor.visitBaseType(typeDescription.asRawType().getDescriptor().charAt(ONLY_CHARACTER));
} else {
signatureVisitor.visitClassType(typeDescription.asRawType().getInternalName());
signatureVisitor.visitEnd();
}
return signatureVisitor;
}
@Override
public boolean equals(Object other) {
return this == other || other instanceof ForSignatureVisitor
&& signatureVisitor.equals(((ForSignatureVisitor) other).signatureVisitor);
}
@Override
public int hashCode() {
return signatureVisitor.hashCode();
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.ForSignatureVisitor{" +
"signatureVisitor=" + signatureVisitor +
'}';
}
/**
* Visits a parameter while visiting a generic type for delegating discoveries to a signature visitor.
*/
protected static class OfParameter extends ForSignatureVisitor {
/**
* Creates a new parameter visitor.
*
* @param signatureVisitor The signature visitor which is notified over visited types.
*/
protected OfParameter(SignatureVisitor signatureVisitor) {
super(signatureVisitor);
}
@Override
public SignatureVisitor onWildcardType(GenericTypeDescription wildcardType) {
GenericTypeList upperBounds = wildcardType.getUpperBounds();
GenericTypeList lowerBounds = wildcardType.getLowerBounds();
if (lowerBounds.isEmpty() && upperBounds.getOnly().asRawType().represents(Object.class)) {
signatureVisitor.visitTypeArgument();
} else if (!lowerBounds.isEmpty() /* && upperBounds.isEmpty() */) {
lowerBounds.getOnly().accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.SUPER)));
} else /* if (!upperBounds.isEmpty() && lowerBounds.isEmpty()) */ {
upperBounds.getOnly().accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.EXTENDS)));
}
return signatureVisitor;
}
@Override
public SignatureVisitor onGenericArray(GenericTypeDescription genericArray) {
genericArray.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onParameterizedType(GenericTypeDescription parameterizedType) {
parameterizedType.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onTypeVariable(GenericTypeDescription typeVariable) {
typeVariable.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onNonGenericType(GenericTypeDescription typeDescription) {
typeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.ForSignatureVisitor.OfParameter{}";
}
}
}
/**
* An abstract implementation of a visitor that substitutes generic types by replacing (nested)
* type variables and/or non-generic component types.
*/
abstract class Substitutor implements Visitor<GenericTypeDescription> {
@Override
public GenericTypeDescription onParameterizedType(GenericTypeDescription parameterizedType) {
GenericTypeDescription ownerType = parameterizedType.getOwnerType();
List<GenericTypeDescription> parameters = new ArrayList<GenericTypeDescription>(parameterizedType.getParameters().size());
for (GenericTypeDescription parameter : parameterizedType.getParameters()) {
parameters.add(parameter.accept(this));
}
return new GenericTypeDescription.ForParameterizedType.Latent(parameterizedType.asRawType().accept(this).asRawType(),
parameters,
ownerType == null
? null
: ownerType.accept(this));
}
@Override
public GenericTypeDescription onGenericArray(GenericTypeDescription genericArray) {
return GenericTypeDescription.ForGenericArray.Latent.of(genericArray.getComponentType().accept(this), 1);
}
@Override
public GenericTypeDescription onWildcardType(GenericTypeDescription wildcardType) {
GenericTypeList lowerBounds = wildcardType.getLowerBounds();
return lowerBounds.isEmpty()
? GenericTypeDescription.ForWildcardType.Latent.boundedAbove(wildcardType.getUpperBounds().getOnly().accept(this))
: GenericTypeDescription.ForWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}
@Override
public GenericTypeDescription onNonGenericType(GenericTypeDescription typeDescription) {
int arity = 0;
while (typeDescription.isArray()) {
typeDescription = typeDescription.getComponentType();
arity++;
}
return ForGenericArray.Latent.of(onSimpleType(typeDescription), arity);
}
/**
* Visits a simple, non-generic type, i.e. either a component type of an array or a non-array type.
*
* @param typeDescription The type that is visited.
* @return The substituted type.
*/
protected abstract GenericTypeDescription onSimpleType(GenericTypeDescription typeDescription);
/**
* A substitutor that attaches type variables to a type variable source and replaces representations of
* {@link TargetType} with a given declaring type.
*/
public static class ForAttachment extends Substitutor {
/**
* The declaring type which is filled in for {@link TargetType}.
*/
private final TypeDescription declaringType;
/**
* The source which is used for locating type variables.
*/
private final TypeVariableSource typeVariableSource;
/**
* Creates a visitor for attaching type variables.
*
* @param declaringType The declaring type which is filled in for {@link TargetType}.
* @param typeVariableSource The source which is used for locating type variables.
*/
protected ForAttachment(TypeDescription declaringType, TypeVariableSource typeVariableSource) {
this.declaringType = declaringType;
this.typeVariableSource = typeVariableSource;
}
/**
* Attaches all types to the given field description.
*
* @param fieldDescription The field description to which visited types should be attached to.
* @return A substitutor that attaches visited types to the given field's type context.
*/
public static ForAttachment of(FieldDescription fieldDescription) {
return new ForAttachment(fieldDescription.getDeclaringType().asRawType(), fieldDescription.getDeclaringType().asRawType());
}
/**
* Attaches all types to the given method description.
*
* @param methodDescription The method description to which visited types should be attached to.
* @return A substitutor that attaches visited types to the given method's type context.
*/
public static ForAttachment of(MethodDescription methodDescription) {
return new ForAttachment(methodDescription.getDeclaringType().asRawType(), methodDescription);
}
/**
* Attaches all types to the given parameter description.
*
* @param parameterDescription The parameter description to which visited types should be attached to.
* @return A substitutor that attaches visited types to the given parameter's type context.
*/
public static ForAttachment of(ParameterDescription parameterDescription) {
return new ForAttachment(parameterDescription.getDeclaringMethod().getDeclaringType().asRawType(), parameterDescription.getDeclaringMethod());
}
/**
* Attaches all types to the given type description.
*
* @param typeDescription The type description to which visited types should be attached to.
* @return A substitutor that attaches visited types to the given type's type context.
*/
public static ForAttachment of(TypeDescription typeDescription) {
return new ForAttachment(typeDescription, typeDescription);
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription typeVariable = typeVariableSource.findVariable(genericTypeDescription.getSymbol());
return typeVariable == null
? genericTypeDescription.asRawType()
: typeVariable;
}
@Override
protected GenericTypeDescription onSimpleType(GenericTypeDescription typeDescription) {
return typeDescription.equals(TargetType.DESCRIPTION)
? declaringType
: typeDescription;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof ForAttachment)) return false;
ForAttachment that = (ForAttachment) other;
return declaringType.equals(that.declaringType)
&& typeVariableSource.equals(that.typeVariableSource);
}
@Override
public int hashCode() {
int result = declaringType.hashCode();
result = 31 * result + typeVariableSource.hashCode();
return result;
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.Substitutor.ForAttachment{" +
"declaringType=" + declaringType +
", typeVariableSource=" + typeVariableSource +
'}';
}
}
/**
* A visitor for detaching a type from its declaration context by detaching type variables. This is achieved by
* detaching type variables and by replacing the declaring type which is identified by a provided {@link ElementMatcher}
* with {@link TargetType}.
*/
public static class ForDetachment extends Substitutor {
/**
* A type matcher for identifying the declaring type.
*/
private final ElementMatcher<? super TypeDescription> typeMatcher;
/**
* A cache of detached type variables in order to resolve recursive types.
*/
private final Map<String, GenericTypeDescription> detachedVariables;
/**
* Creates a visitor for detaching a type.
*
* @param typeMatcher A type matcher for identifying the declaring type.
*/
public ForDetachment(ElementMatcher<? super TypeDescription> typeMatcher) {
this.typeMatcher = typeMatcher;
detachedVariables = new HashMap<String, GenericTypeDescription>();
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription typeVariable = detachedVariables.get(genericTypeDescription.getSymbol());
return typeVariable == null
? new DetachedTypeVariable(genericTypeDescription.getSymbol(), genericTypeDescription.getUpperBounds(), this)
: typeVariable;
}
@Override
protected GenericTypeDescription onSimpleType(GenericTypeDescription typeDescription) {
return typeMatcher.matches(typeDescription.asRawType())
? TargetType.DESCRIPTION
: typeDescription;
}
/**
* Registers a generic type variable that was not yet visited. This addresses the possibility of defining recursive type variables.
* When this method is called, the provided type variable is not yet fully constructed and must not be used.
*
* @param symbol The type variable's symbol.
* @param typeVariable A description of the generic type variable.
*/
protected void register(String symbol, GenericTypeDescription typeVariable) {
detachedVariables.put(symbol, typeVariable);
}
@Override
public String toString() {
return "GenericTypeDescription.Visitor.Substitutor.ForDetachment{" +
"typeMatcher=" + typeMatcher +
", detachedVariables=" + detachedVariables +
'}';
}
/**
* A description of a detached type variable.
*/
protected static class DetachedTypeVariable extends GenericTypeDescription.ForTypeVariable.InDetachedForm {
/**
* The symbol of this variable.
*/
private final String symbol;
/**
* The bounds of the type variable.
*/
private final List<GenericTypeDescription> bounds;
/**
* Creates a description of a detached type variable.
*
* @param symbol The symbol of this variable.
* @param bounds The bounds of the type variable.
* @param visitor The visitor to apply to the bounds.
*/
protected DetachedTypeVariable(String symbol, List<GenericTypeDescription> bounds, ForDetachment visitor) {
this.symbol = symbol;
visitor.register(symbol, this);
this.bounds = new ArrayList<GenericTypeDescription>(bounds.size());
for (GenericTypeDescription bound : bounds) {
this.bounds.add(bound.accept(visitor));
}
}
@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.Explicit(bounds);
}
@Override
public String getSymbol() {
return symbol;
}
}
}
/**
* A visitor for binding type variables to their values.
*/
public static class ForTypeVariableBinding extends Substitutor {
/**
* Bindings of type variables to their substitution values.
*/
private final Map<GenericTypeDescription, GenericTypeDescription> bindings;
/**
* Creates a new visitor for a type variable bindings.
*
* @param bindings Bindings of type variables to their substitution values.
*/
protected ForTypeVariableBinding(Map<GenericTypeDescription, GenericTypeDescription> bindings) {
this.bindings = bindings;
}
/**