/
GenericTypeDescription.java
1358 lines (1079 loc) · 48.3 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.*;
import static net.bytebuddy.matcher.ElementMatchers.is;
public interface GenericTypeDescription extends NamedElement {
Sort getSort();
String getTypeName();
TypeDescription asRawType();
GenericTypeDescription getSuperType();
GenericTypeList getInterfaces();
FieldList getDeclaredFields();
MethodList getDeclaredMethods();
GenericTypeList getUpperBounds();
GenericTypeList getLowerBounds();
GenericTypeDescription getComponentType();
GenericTypeList getParameters();
TypeVariableSource getVariableSource();
GenericTypeDescription getOwnerType();
String getSymbol();
/**
* Returns the size of the type described by this instance.
*
* @return The size of the type described by this instance.
*/
StackSize getStackSize();
<T> T accept(Visitor<T> visitor);
enum Sort {
RAW,
GENERIC_ARRAY,
PARAMETERIZED,
WILDCARD,
VARIABLE,
DETACHED_VARIABLE,
SYMBOLIZING_VARIABLE;
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 IllegalStateException("Unknown type: " + type);
}
}
public boolean isRawType() {
return this == RAW;
}
public boolean isParameterized() {
return this == PARAMETERIZED;
}
public boolean isGenericArray() {
return this == GENERIC_ARRAY;
}
public boolean isWildcard() {
return this == WILDCARD;
}
public boolean isTypeVariable() {
return this == VARIABLE;
}
public boolean isDetachedTypeVariable() {
return this == DETACHED_VARIABLE;
}
public boolean isSymbolizingTypeVariable() {
return this == SYMBOLIZING_VARIABLE;
}
}
interface Visitor<T> {
T onGenericArray(GenericTypeDescription genericTypeDescription);
T onWildcardType(GenericTypeDescription genericTypeDescription);
T onParameterizedType(GenericTypeDescription genericTypeDescription);
T onTypeVariable(GenericTypeDescription genericTypeDescription);
T onRawType(TypeDescription typeDescription);
class ForSignatureVisitor implements Visitor<SignatureVisitor> {
private static final int ONLY_CHARACTER = 0;
protected final SignatureVisitor signatureVisitor;
public ForSignatureVisitor(SignatureVisitor signatureVisitor) {
this.signatureVisitor = signatureVisitor;
}
@Override
public SignatureVisitor onGenericArray(GenericTypeDescription genericTypeDescription) {
genericTypeDescription.getComponentType().accept(new ForSignatureVisitor(signatureVisitor.visitArrayType()));
return signatureVisitor;
}
@Override
public SignatureVisitor onWildcardType(GenericTypeDescription genericTypeDescription) {
throw new IllegalStateException("Unexpected wildcard: " + genericTypeDescription);
}
@Override
public SignatureVisitor onParameterizedType(GenericTypeDescription genericTypeDescription) {
onOwnableType(genericTypeDescription);
signatureVisitor.visitEnd();
return signatureVisitor;
}
private void onOwnableType(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription ownerType = genericTypeDescription.getOwnerType();
if (ownerType != null) {
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 genericTypeDescription) {
signatureVisitor.visitTypeVariable(genericTypeDescription.getSymbol());
return signatureVisitor;
}
@Override
public SignatureVisitor onRawType(TypeDescription typeDescription) {
if (typeDescription.isPrimitive()) {
signatureVisitor.visitBaseType(typeDescription.getDescriptor().charAt(ONLY_CHARACTER));
} else {
signatureVisitor.visitClassType(typeDescription.getInternalName());
signatureVisitor.visitEnd();
}
return signatureVisitor;
}
protected static class OfParameter extends ForSignatureVisitor {
protected OfParameter(SignatureVisitor signatureVisitor) {
super(signatureVisitor);
}
@Override
public SignatureVisitor onWildcardType(GenericTypeDescription genericTypeDescription) {
GenericTypeList upperBounds = genericTypeDescription.getUpperBounds();
GenericTypeList lowerBounds = genericTypeDescription.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.EXTENDS)));
} else /* if (!upperBounds.isEmpty() && lowerBounds.isEmpty()) */ {
upperBounds.getOnly().accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.SUPER)));
}
return signatureVisitor;
}
@Override
public SignatureVisitor onGenericArray(GenericTypeDescription genericTypeDescription) {
genericTypeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onParameterizedType(GenericTypeDescription genericTypeDescription) {
genericTypeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onTypeVariable(GenericTypeDescription genericTypeDescription) {
genericTypeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onRawType(TypeDescription typeDescription) {
typeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
}
}
enum ForErasure implements Visitor<TypeDescription> {
INSTANCE;
@Override
public TypeDescription onGenericArray(GenericTypeDescription genericTypeDescription) {
return genericTypeDescription.asRawType();
}
@Override
public TypeDescription onWildcardType(GenericTypeDescription genericTypeDescription) {
throw new IllegalArgumentException("Cannot erase wildcard: " + genericTypeDescription);
}
@Override
public TypeDescription onParameterizedType(GenericTypeDescription genericTypeDescription) {
return genericTypeDescription.asRawType();
}
@Override
public TypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
return genericTypeDescription.asRawType();
}
@Override
public TypeDescription onRawType(TypeDescription typeDescription) {
return typeDescription;
}
}
abstract class Substitutor implements Visitor<GenericTypeDescription> {
@Override
public GenericTypeDescription onParameterizedType(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription ownerType = genericTypeDescription.getOwnerType();
List<GenericTypeDescription> parameters = new ArrayList<GenericTypeDescription>(genericTypeDescription.getParameters().size());
for (GenericTypeDescription parameter : genericTypeDescription.getParameters()) {
parameters.add(parameter.accept(this));
}
return new GenericTypeDescription.ForParameterizedType.Latent(genericTypeDescription.asRawType(),
parameters,
ownerType == null
? null
: ownerType.accept(this));
}
@Override
public GenericTypeDescription onGenericArray(GenericTypeDescription genericTypeDescription) {
return GenericTypeDescription.ForGenericArray.Latent.of(genericTypeDescription.getComponentType().accept(this), 1);
}
@Override
public GenericTypeDescription onWildcardType(GenericTypeDescription genericTypeDescription) {
GenericTypeList lowerBounds = genericTypeDescription.getLowerBounds(), upperBounds = genericTypeDescription.getUpperBounds();
return lowerBounds.isEmpty()
? GenericTypeDescription.ForWildcardType.Latent.boundedAbove(upperBounds.getOnly().accept(this))
: GenericTypeDescription.ForWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}
protected static abstract class ComponentTypeResolving extends Substitutor {
@Override
public GenericTypeDescription onRawType(TypeDescription typeDescription) {
int arity = 0;
while (typeDescription.isArray()) {
typeDescription = typeDescription.getComponentType();
arity++;
}
return TypeDescription.ArrayProjection.of(onComponentType(typeDescription), arity);
}
protected abstract TypeDescription onComponentType(TypeDescription typeDescription);
}
public static class ForAttachment extends ComponentTypeResolving {
public static ForAttachment of(FieldDescription fieldDescription) {
return new ForAttachment(fieldDescription.getDeclaringType(), fieldDescription.getDeclaringType());
}
public static ForAttachment of(MethodDescription methodDescription) {
return new ForAttachment(methodDescription.getDeclaringType(), methodDescription);
}
public static ForAttachment of(ParameterDescription parameterDescription) {
return new ForAttachment(parameterDescription.getDeclaringMethod().getDeclaringType(), parameterDescription.getDeclaringMethod());
}
public static ForAttachment of(TypeDescription typeDescription) {
return new ForAttachment(typeDescription, typeDescription);
}
private final TypeDescription declaringType;
private final TypeVariableSource typeVariableSource;
protected ForAttachment(TypeDescription declaringType, TypeVariableSource typeVariableSource) {
this.declaringType = declaringType;
this.typeVariableSource = typeVariableSource;
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription typeVariable = typeVariableSource.findVariable(genericTypeDescription.getSymbol());
if (typeVariable == null) {
throw new IllegalStateException("Cannot attach undefined type variable: " + genericTypeDescription);
}
return typeVariable;
}
@Override
protected TypeDescription onComponentType(TypeDescription typeDescription) {
return typeDescription.equals(TargetType.DESCRIPTION)
? declaringType
: typeDescription;
}
}
public static class ForDetachment extends ComponentTypeResolving {
private final ElementMatcher<? super TypeDescription> typeMatcher;
private final Map<String, GenericTypeDescription> detachedVariables;
public ForDetachment(ElementMatcher<? super TypeDescription> typeMatcher) {
this.typeMatcher = typeMatcher;
detachedVariables = new HashMap<String, GenericTypeDescription>();
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
if (genericTypeDescription.getSort().isDetachedTypeVariable()) {
throw new IllegalStateException("Type variable already detached: " + genericTypeDescription);
}
GenericTypeDescription typeVariable = detachedVariables.get(genericTypeDescription.getSymbol());
if (typeVariable == null) {
typeVariable = new DetachedTypeVariable(genericTypeDescription.getSymbol(), genericTypeDescription.getUpperBounds(), this);
}
return typeVariable;
}
@Override
protected TypeDescription onComponentType(TypeDescription typeDescription) {
return typeMatcher.matches(typeDescription)
? TargetType.DESCRIPTION
: typeDescription;
}
protected void register(String symbol, GenericTypeDescription typeVariable) {
detachedVariables.put(symbol, typeVariable);
}
protected static class DetachedTypeVariable extends GenericTypeDescription.ForTypeVariable.InDetachedForm {
private final String symbol;
private final List<GenericTypeDescription> bounds;
public 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;
}
}
}
public static class ForTypeVariable extends Substitutor {
public static Visitor<GenericTypeDescription> bind(GenericTypeDescription typeDescription) {
Map<GenericTypeDescription, GenericTypeDescription> bindings = new HashMap<GenericTypeDescription, GenericTypeDescription>();
do {
GenericTypeList parameters = typeDescription.getParameters();
int index = 0;
for (GenericTypeDescription typeVariable : typeDescription.asRawType().getTypeVariables()) {
bindings.put(typeVariable, parameters.get(index++));
}
typeDescription = typeDescription.getOwnerType();
} while (typeDescription != null);
return new ForTypeVariable(bindings);
}
private final Map<GenericTypeDescription, GenericTypeDescription> bindings;
protected ForTypeVariable(Map<GenericTypeDescription, GenericTypeDescription> bindings) {
this.bindings = bindings;
}
@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription genericTypeDescription) {
GenericTypeDescription substitution = bindings.get(genericTypeDescription);
if (substitution == null) {
throw new IllegalArgumentException("Cannot substitute " + genericTypeDescription + " with " + bindings);
}
return substitution;
}
@Override
public GenericTypeDescription onRawType(TypeDescription typeDescription) {
return typeDescription;
}
}
}
}
abstract class ForGenericArray implements GenericTypeDescription {
@Override
public Sort getSort() {
return Sort.GENERIC_ARRAY;
}
@Override
public TypeDescription asRawType() {
return TypeDescription.ArrayProjection.of(getComponentType().asRawType(), 1);
}
@Override
public GenericTypeDescription getSuperType() {
throw new IllegalStateException("A generic array does not imply a super type definition: " + this);
}
@Override
public GenericTypeList getInterfaces() {
throw new IllegalStateException("A generic array does not imply an interface type definition: " + this);
}
@Override
public FieldList getDeclaredFields() {
throw new IllegalStateException("A generic array does not imply field definitions: " + this);
}
@Override
public MethodList getDeclaredMethods() {
throw new IllegalStateException("A generic array does not imply method definitions: " + this);
}
@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.Empty();
}
@Override
public GenericTypeList getLowerBounds() {
return new GenericTypeList.Empty();
}
@Override
public TypeVariableSource getVariableSource() {
return null;
}
@Override
public GenericTypeList getParameters() {
return new GenericTypeList.Empty();
}
@Override
public GenericTypeDescription getOwnerType() {
return null;
}
@Override
public String getSymbol() {
return null;
}
@Override
public String getTypeName() {
return toString();
}
@Override
public String getSourceCodeName() {
return toString();
}
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.onGenericArray(this);
}
@Override
public StackSize getStackSize() {
return StackSize.SINGLE;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof GenericTypeDescription)) return false;
GenericTypeDescription genericTypeDescription = (GenericTypeDescription) other;
return genericTypeDescription.getSort().isGenericArray() && getComponentType().equals(genericTypeDescription.getComponentType());
}
@Override
public int hashCode() {
return getComponentType().hashCode();
}
@Override
public String toString() {
return getComponentType().getTypeName() + "[]";
}
public static class OfLoadedType extends ForGenericArray {
private final GenericArrayType genericArrayType;
public OfLoadedType(GenericArrayType genericArrayType) {
this.genericArrayType = genericArrayType;
}
@Override
public GenericTypeDescription getComponentType() {
return Sort.describe(genericArrayType.getGenericComponentType());
}
}
public static class Latent extends ForGenericArray {
public static GenericTypeDescription of(GenericTypeDescription componentType, int arity) {
if (arity < 0) {
throw new IllegalArgumentException("Arity cannot be negative");
}
while (componentType.getSort().isGenericArray()) {
arity++;
componentType = componentType.getComponentType();
}
return arity == 0
? componentType
: new Latent(componentType, arity);
}
private final GenericTypeDescription componentType;
private final int arity;
protected Latent(GenericTypeDescription componentType, int arity) {
this.componentType = componentType;
this.arity = arity;
}
@Override
public GenericTypeDescription getComponentType() {
return arity == 1
? componentType
: new Latent(componentType, arity - 1);
}
}
}
abstract class ForWildcardType implements GenericTypeDescription {
public static final String SYMBOL = "?";
@Override
public Sort getSort() {
return Sort.WILDCARD;
}
@Override
public TypeDescription asRawType() {
throw new IllegalStateException("A wildcard does not represent an erasable type: " + this);
}
@Override
public GenericTypeDescription getSuperType() {
throw new IllegalStateException("A wildcard does not imply a super type definition: " + this);
}
@Override
public GenericTypeList getInterfaces() {
throw new IllegalStateException("A wildcard does not imply an interface type definition: " + this);
}
@Override
public FieldList getDeclaredFields() {
throw new IllegalStateException("A wildcard does not imply field definitions: " + this);
}
@Override
public MethodList getDeclaredMethods() {
throw new IllegalStateException("A wildcard does not imply method definitions: " + this);
}
@Override
public GenericTypeDescription getComponentType() {
return null;
}
@Override
public TypeVariableSource getVariableSource() {
return null;
}
@Override
public GenericTypeList getParameters() {
return new GenericTypeList.Empty();
}
@Override
public GenericTypeDescription getOwnerType() {
return null;
}
@Override
public String getSymbol() {
return null;
}
@Override
public String getTypeName() {
return toString();
}
@Override
public String getSourceCodeName() {
return toString();
}
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.onWildcardType(this);
}
@Override
public StackSize getStackSize() {
throw new IllegalStateException("A wildcard does not imply an operand stack size: " + this);
}
@Override
public int hashCode() {
int lowerHash = 1, upperHash = 1;
for (GenericTypeDescription genericTypeDescription : getLowerBounds()) {
lowerHash = 31 * lowerHash + genericTypeDescription.hashCode();
}
for (GenericTypeDescription genericTypeDescription : getUpperBounds()) {
upperHash = 31 * upperHash + genericTypeDescription.hashCode();
}
return lowerHash ^ upperHash;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof GenericTypeDescription)) return false;
GenericTypeDescription genericTypeDescription = (GenericTypeDescription) other;
return genericTypeDescription.getSort().isWildcard()
&& getUpperBounds().equals(genericTypeDescription.getUpperBounds())
&& getLowerBounds().equals(genericTypeDescription.getLowerBounds());
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder(SYMBOL);
GenericTypeList bounds = getLowerBounds();
if (!bounds.isEmpty()) {
stringBuilder.append(" super ");
} else {
bounds = getUpperBounds();
if (bounds.getOnly().equals(TypeDescription.OBJECT)) {
return SYMBOL;
}
stringBuilder.append(" extends ");
}
return stringBuilder.append(bounds.getOnly().getTypeName()).toString();
}
public static class OfLoadedType extends ForWildcardType {
private final WildcardType wildcardType;
public OfLoadedType(WildcardType wildcardType) {
this.wildcardType = wildcardType;
}
@Override
public GenericTypeList getLowerBounds() {
return new GenericTypeList.ForLoadedType(wildcardType.getLowerBounds());
}
@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.ForLoadedType(wildcardType.getUpperBounds());
}
}
public static class Latent extends ForWildcardType {
public static GenericTypeDescription unbounded() {
return new Latent(Collections.singletonList(TypeDescription.OBJECT), Collections.<GenericTypeDescription>emptyList());
}
public static GenericTypeDescription boundedAbove(GenericTypeDescription upperBound) {
return new Latent(Collections.singletonList(upperBound), Collections.<GenericTypeDescription>emptyList());
}
public static GenericTypeDescription boundedBelow(GenericTypeDescription lowerBound) {
return new Latent(Collections.singletonList(TypeDescription.OBJECT), Collections.singletonList(lowerBound));
}
private final List<? extends GenericTypeDescription> upperBounds;
private final List<? extends GenericTypeDescription> lowerBounds;
protected Latent(List<? extends GenericTypeDescription> upperBounds, List<? extends GenericTypeDescription> lowerBounds) {
this.upperBounds = upperBounds;
this.lowerBounds = lowerBounds;
}
@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.Explicit(upperBounds);
}
@Override
public GenericTypeList getLowerBounds() {
return new GenericTypeList.Explicit(lowerBounds);
}
}
}
abstract class ForParameterizedType implements GenericTypeDescription {
@Override
public Sort getSort() {
return Sort.PARAMETERIZED;
}
@Override
public GenericTypeDescription getSuperType() {
return asRawType().getSuperType().accept(Visitor.Substitutor.ForTypeVariable.bind(this));
}
@Override
public GenericTypeList getInterfaces() {
return asRawType().getInterfaces().accept(Visitor.Substitutor.ForTypeVariable.bind(this));
}
@Override
public FieldList getDeclaredFields() {
FieldList declaredFields = asRawType().getDeclaredFields();
List<FieldDescription> resolved = new ArrayList<FieldDescription>(declaredFields.size());
Visitor<GenericTypeDescription> visitor = Visitor.Substitutor.ForTypeVariable.bind(this);
for (FieldDescription fieldDescription : declaredFields) {
TypeDescription declaringType = fieldDescription.getDeclaringType();
resolved.add(new FieldDescription.Latent(declaringType, fieldDescription.asToken(is(declaringType)).accept(visitor)));
}
return new FieldList.Explicit(resolved);
}
@Override
public MethodList getDeclaredMethods() {
MethodList declaredMethods = asRawType().getDeclaredMethods();
List<MethodDescription> resolved = new ArrayList<MethodDescription>(declaredMethods.size());
Visitor<GenericTypeDescription> visitor = Visitor.Substitutor.ForTypeVariable.bind(this);
for (MethodDescription methodDescription : declaredMethods) {
TypeDescription declaringType = methodDescription.getDeclaringType();
resolved.add(new MethodDescription.Latent(declaringType, methodDescription.asToken(is(declaringType)).accept(visitor)));
}
return new MethodList.Explicit(resolved);
}
@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.Empty();
}
@Override
public GenericTypeList getLowerBounds() {
return new GenericTypeList.Empty();
}
@Override
public GenericTypeDescription getComponentType() {
return null;
}
@Override
public TypeVariableSource getVariableSource() {
return null;
}
@Override
public String getTypeName() {
return toString();
}
@Override
public String getSymbol() {
return null;
}
@Override
public String getSourceCodeName() {
return toString();
}
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.onParameterizedType(this);
}
@Override
public StackSize getStackSize() {
return StackSize.SINGLE;
}
@Override
public int hashCode() {
int result = 1;
for (GenericTypeDescription genericTypeDescription : getLowerBounds()) {
result = 31 * result + genericTypeDescription.hashCode();
}
GenericTypeDescription ownerType = getOwnerType();
return result ^ (ownerType == null
? asRawType().hashCode()
: ownerType.hashCode());
}
@Override
public boolean equals(Object other) {
if (!(other instanceof GenericTypeDescription)) return false;
GenericTypeDescription genericTypeDescription = (GenericTypeDescription) other;
if (!genericTypeDescription.getSort().isParameterized()) return false;
GenericTypeDescription ownerType = getOwnerType(), otherOwnerType = genericTypeDescription.getOwnerType();
return asRawType().equals(genericTypeDescription.asRawType())
&& !(ownerType == null && otherOwnerType != null) && !(ownerType != null && !ownerType.equals(otherOwnerType))
&& getParameters().equals(genericTypeDescription.getParameters());
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
GenericTypeDescription ownerType = getOwnerType();
if (ownerType != null) {
stringBuilder.append(ownerType.getTypeName());
stringBuilder.append(".");
stringBuilder.append(ownerType.getSort().isParameterized()
? asRawType().getName().replace(ownerType.asRawType().getName() + "$", "")
: asRawType().getName());
} else {
stringBuilder.append(asRawType().getName());
}
GenericTypeList actualTypeArguments = getParameters();
if (!actualTypeArguments.isEmpty()) {
stringBuilder.append("<");
boolean multiple = false;
for (GenericTypeDescription genericTypeDescription : actualTypeArguments) {
if (multiple) {
stringBuilder.append(", ");
}
stringBuilder.append(genericTypeDescription.getTypeName());
multiple = true;
}
stringBuilder.append(">");
}
return stringBuilder.toString();
}
public static class OfLoadedType extends ForParameterizedType {
private final ParameterizedType parameterizedType;
public OfLoadedType(ParameterizedType parameterizedType) {
this.parameterizedType = parameterizedType;
}
@Override
public GenericTypeList getParameters() {
return new GenericTypeList.ForLoadedType(parameterizedType.getActualTypeArguments());
}
@Override
public GenericTypeDescription getOwnerType() {
Type ownerType = parameterizedType.getOwnerType();
return ownerType == null
? null
: Sort.describe(ownerType);
}
@Override
public TypeDescription asRawType() {
return new TypeDescription.ForLoadedType((Class<?>) parameterizedType.getRawType());
}
}
public static class Latent extends ForParameterizedType {
private final TypeDescription rawType;
private final List<? extends GenericTypeDescription> parameters;
private final GenericTypeDescription ownerType;
public Latent(TypeDescription rawType, List<? extends GenericTypeDescription> parameters, GenericTypeDescription ownerType) {
this.rawType = rawType;
this.parameters = parameters;
this.ownerType = ownerType;
}
@Override
public TypeDescription asRawType() {
return rawType;
}
@Override
public GenericTypeList getParameters() {
return new GenericTypeList.Explicit(parameters);
}
@Override
public GenericTypeDescription getOwnerType() {
return ownerType;
}
}
}
abstract class ForTypeVariable implements GenericTypeDescription {
@Override
public Sort getSort() {
return Sort.VARIABLE;
}
@Override
public TypeDescription asRawType() {
GenericTypeList upperBounds = getUpperBounds();
return upperBounds.isEmpty()
? TypeDescription.OBJECT
: upperBounds.get(0).asRawType();
}
@Override
public GenericTypeDescription getSuperType() {
throw new IllegalStateException("A type variable does not imply a super type definition: " + this);
}
@Override
public GenericTypeList getInterfaces() {
throw new IllegalStateException("A type variable does not imply an interface type definition: " + this);
}
@Override
public FieldList getDeclaredFields() {
throw new IllegalStateException("A type variable does not imply field definitions: " + this);
}
@Override
public MethodList getDeclaredMethods() {
throw new IllegalStateException("A type variable does not imply method definitions: " + this);
}
@Override
public GenericTypeDescription getComponentType() {
return null;
}
@Override