/
TypeDescription.java
3750 lines (3201 loc) · 142 KB
/
TypeDescription.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;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bytebuddy.description.ModifierReviewable;
import net.bytebuddy.description.TypeVariableSource;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.enumeration.EnumerationDescription;
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.dynamic.TargetType;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.utility.CompoundList;
import net.bytebuddy.utility.JavaType;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureVisitor;
import org.objectweb.asm.signature.SignatureWriter;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import static net.bytebuddy.matcher.ElementMatchers.named;
/**
* Implementations of this interface represent a Java type, i.e. a class or interface. Instances of this interface always
* represent non-generic types of sort {@link Generic.Sort#NON_GENERIC}.
*/
public interface TypeDescription extends TypeDefinition, TypeVariableSource {
/**
* A representation of the {@link java.lang.Object} type.
*/
TypeDescription OBJECT = new ForLoadedType(Object.class);
/**
* A representation of the {@link java.lang.String} type.
*/
TypeDescription STRING = new ForLoadedType(String.class);
/**
* A representation of the {@link java.lang.Class} type.
*/
TypeDescription CLASS = new ForLoadedType(Class.class);
/**
* A representation of the {@code void} non-type.
*/
TypeDescription VOID = new ForLoadedType(void.class);
/**
* The modifiers of any array type.
*/
int ARRAY_MODIFIERS = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_ABSTRACT;
/**
* A list of interfaces that are implicitly implemented by any array type.
*/
TypeList.Generic ARRAY_INTERFACES = new TypeList.Generic.ForLoadedTypes(Cloneable.class, Serializable.class);
/**
* Represents any undefined property of a type description that is instead represented as {@code null} in order
* to resemble the Java reflection API which returns {@code null} and is intuitive to many Java developers.
*/
TypeDescription UNDEFINED = null;
@Override
FieldList<FieldDescription.InDefinedShape> getDeclaredFields();
@Override
MethodList<MethodDescription.InDefinedShape> getDeclaredMethods();
/**
* Checks if {@code value} is an instance of the type represented by this instance.
*
* @param value The object of interest.
* @return {@code true} if the object is an instance of the type described by this instance.
*/
boolean isInstance(Object value);
/**
* Checks if {@code value} is an instance of the type represented by this instance or a wrapper instance of the
* corresponding primitive value.
*
* @param value The object of interest.
* @return {@code true} if the object is an instance or wrapper of the type described by this instance.
*/
boolean isInstanceOrWrapper(Object value);
/**
* Checks if this type is assignable from the type described by this instance, for example for
* {@code class Foo} and {@code class Bar extends Foo}, this method would return {@code true} for
* {@code Foo.class.isAssignableFrom(Bar.class)}.
*
* @param type The type of interest.
* @return {@code true} if this type is assignable from {@code type}.
*/
boolean isAssignableFrom(Class<?> type);
/**
* Checks if this type is assignable from the type described by this instance, for example for
* {@code class Foo} and {@code class Bar extends Foo}, this method would return {@code true} for
* {@code Foo.class.isAssignableFrom(Bar.class)}.
* <p> </p>
* Implementations of this methods are allowed to delegate to
* {@link TypeDescription#isAssignableFrom(Class)}
*
* @param typeDescription The type of interest.
* @return {@code true} if this type is assignable from {@code type}.
*/
boolean isAssignableFrom(TypeDescription typeDescription);
/**
* Checks if this type is assignable from the type described by this instance, for example for
* {@code class Foo} and {@code class Bar extends Foo}, this method would return {@code true} for
* {@code Bar.class.isAssignableTo(Foo.class)}.
*
* @param type The type of interest.
* @return {@code true} if this type is assignable to {@code type}.
*/
boolean isAssignableTo(Class<?> type);
/**
* Checks if this type is assignable from the type described by this instance, for example for
* {@code class Foo} and {@code class Bar extends Foo}, this method would return {@code true} for
* {@code Bar.class.isAssignableFrom(Foo.class)}.
* <p> </p>
* Implementations of this methods are allowed to delegate to
* {@link TypeDescription#isAssignableTo(Class)}
*
* @param typeDescription The type of interest.
* @return {@code true} if this type is assignable to {@code type}.
*/
boolean isAssignableTo(TypeDescription typeDescription);
@Override
TypeDescription getComponentType();
@Override
TypeDescription getDeclaringType();
/**
* Returns a list of types that are declared by this type excluding anonymous classes.
*
* @return A list of types that are declared within this type.
*/
TypeList getDeclaredTypes();
/**
* Returns a description of the enclosing method of this type.
*
* @return A description of the enclosing method of this type or {@code null} if there is no such method.
*/
MethodDescription getEnclosingMethod();
/**
* Returns a description of the enclosing type of this type.
*
* @return A description of the enclosing type of this type or {@code null} if there is no such type.
*/
TypeDescription getEnclosingType();
/**
* <p>
* Returns the type's actual modifiers as present in the class file. For example, a type cannot be {@code private}.
* but it modifiers might reflect this property nevertheless if a class was defined as a private inner class.
* </p>
* <p>
* Unfortunately, the modifier for marking a {@code static} class collides with the {@code SUPER} modifier such
* that these flags are indistinguishable. Therefore, the flag must be specified manually.
* </p>
*
* @param superFlag {@code true} if the super flag should be set.
* @return The type's actual modifiers.
*/
int getActualModifiers(boolean superFlag);
/**
* Returns the simple internalName of this type.
*
* @return The simple internalName of this type.
*/
String getSimpleName();
/**
* Returns the canonical name of this type if it exists.
*
* @return The canonical name of this type. Might be {@code null}.
*/
String getCanonicalName();
/**
* Checks if this type description represents an anonymous type.
*
* @return {@code true} if this type description represents an anonymous type.
*/
boolean isAnonymousClass();
/**
* Checks if this type description represents a local type.
*
* @return {@code true} if this type description represents a local type.
*/
boolean isLocalClass();
/**
* Checks if this type description represents a member type.
*
* @return {@code true} if this type description represents a member type.
*/
boolean isMemberClass();
/**
* Returns the package internalName of the type described by this instance.
*
* @return The package internalName of the type described by this instance.
*/
PackageDescription getPackage();
/**
* Returns the annotations that this type declares or inherits from super types.
*
* @return A list of all inherited annotations.
*/
AnnotationList getInheritedAnnotations();
/**
* Checks if two types are defined in the same package.
*
* @param typeDescription The type of interest.
* @return {@code true} if this type and the given type are in the same package.
*/
boolean isSamePackage(TypeDescription typeDescription);
/**
* Checks if instances of this type can be stored in the constant pool of a class. Note that any primitive
* type that is smaller than an {@code int} cannot be stored in the constant pool as those types are represented
* as {@code int} values internally.
*
* @return {@code true} if instances of this type can be stored in the constant pool of a class.
*/
boolean isConstantPool();
/**
* Checks if this type represents a wrapper type for a primitive type. The {@link java.lang.Void} type is
* not considered to be a wrapper type.
*
* @return {@code true} if this type represents a wrapper type.
*/
boolean isPrimitiveWrapper();
/**
* Checks if instances of this type can be returned from an annotation method.
*
* @return {@code true} if instances of this type can be returned from an annotation method.
*/
boolean isAnnotationReturnType();
/**
* Checks if instances of this type can be used for describing an annotation value.
*
* @return {@code true} if instances of this type can be used for describing an annotation value.
*/
boolean isAnnotationValue();
/**
* Checks if instances of this type can be used for describing the given annotation value.
*
* @param value The value that is supposed to describe the annotation value for this instance.
* @return {@code true} if instances of this type can be used for describing the given annotation value..
*/
boolean isAnnotationValue(Object value);
/**
* Represents a generic type of the Java programming language. A non-generic {@link TypeDescription} is considered to be
* a specialization of a generic type.
*/
interface Generic extends TypeDefinition {
/**
* A representation of the {@link Object} type.
*/
Generic OBJECT = new OfNonGenericType.ForLoadedType(Object.class);
/**
* A representation of the {@code void} non-type.
*/
Generic VOID = new OfNonGenericType.ForLoadedType(void.class);
Generic UNDEFINED = null;
Generic asRawType();
/**
* <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.TypeDefinition.Sort#VARIABLE},
* {@link net.bytebuddy.description.type.TypeDefinition.Sort#VARIABLE_DETACHED}) and wildcard types
* ({@link net.bytebuddy.description.type.TypeDefinition.Sort#WILDCARD}) have well-defined upper bounds. For other
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The upper bounds of this type.
*/
TypeList.Generic getUpperBounds();
/**
* <p>
* Returns the lower bounds of this type.
* </p>
* <p>
* Only wildcard types ({@link Sort#WILDCARD}) define a lower bound. For other
* types, an {@link IllegalStateException} is thrown.
* </p>
*
* @return The lower bounds of this type.
*/
TypeList.Generic getLowerBounds();
/**
* <p>
* Returns the type parameters of this type.
* </p>
* <p>
* Parameters are only well-defined for parameterized types
* ({@link Sort#PARAMETERIZED}), generic array types
* ({@link Sort#GENERIC_ARRAY}) and non-generic types
* ({@link Sort#NON_GENERIC}). For non-generic and generic array 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.
*/
TypeList.Generic getParameters();
/**
* <p>
* Returns the owner type of this type.
* </p>
* <p>
* An owner type is only well-defined for parameterized types
* ({@link Sort#PARAMETERIZED}) , generic array types
* ({@link Sort#GENERIC_ARRAY}) and non-generic types
* ({@link Sort#NON_GENERIC}). Non-generic types and generic array 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.
*/
Generic getOwnerType();
/**
* Returns the source of this type variable. A type variable source is only well-defined for an attached type variable
* ({@link 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 Sort#VARIABLE},
* {@link Sort#VARIABLE_DETACHED},
* {@link Sort#VARIABLE_SYMBOLIC}). For other types, this method
* throws an {@link IllegalStateException}.
*
* @return This type's type variable symbol.
*/
String getSymbol();
@Override
Generic getComponentType();
FieldList<FieldDescription.InGenericShape> getDeclaredFields();
MethodList<MethodDescription.InGenericShape> getDeclaredMethods();
/**
* 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);
/**
* A visitor that can be applied to a {@link Generic} 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 Sort#GENERIC_ARRAY}).
*
* @param genericArray The generic array type.
* @return The visitor's return value.
*/
T onGenericArray(Generic genericArray);
/**
* Visits a wildcard ({@link Sort#WILDCARD}).
*
* @param wildcard The wildcard.
* @return The visitor's return value.
*/
T onWildcard(Generic wildcard);
/**
* Visits a parameterized type ({@link Sort#PARAMETERIZED}).
*
* @param parameterizedType The generic array type.
* @return The visitor's return value.
*/
T onParameterizedType(Generic parameterizedType);
/**
* Visits a type variable ({@link Sort#VARIABLE},
* {@link Sort#VARIABLE_DETACHED},
* {@link Sort#VARIABLE_SYMBOLIC}).
*
* @param typeVariable The generic array type.
* @return The visitor's return value.
*/
T onTypeVariable(Generic typeVariable);
/**
* Visits a non-generic type ({@link Sort#NON_GENERIC}).
*
* @param typeDescription The non-generic type.
* @return The visitor's return value.
*/
T onNonGenericType(Generic typeDescription);
/**
* A non-operational generic type visitor. Any visited type is returned in its existing form.
*/
enum NoOp implements Visitor<Generic> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Generic onGenericArray(Generic genericArray) {
return genericArray;
}
@Override
public Generic onWildcard(Generic wildcard) {
return wildcard;
}
@Override
public Generic onParameterizedType(Generic parameterizedType) {
return parameterizedType;
}
@Override
public Generic onTypeVariable(Generic typeVariable) {
return typeVariable;
}
@Override
public Generic onNonGenericType(Generic typeDescription) {
return typeDescription;
}
@Override
public String toString() {
return "TypeDescription.Generic.Visitor.NoOp." + name();
}
}
/**
* A visitor that returns the erasure of any visited type. For wildcard types, an exception is thrown.
*/
enum TypeErasing implements Visitor<Generic> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Generic onGenericArray(Generic genericArray) {
return genericArray.asRawType();
}
@Override
public Generic onWildcard(Generic wildcard) {
throw new IllegalArgumentException("Cannot erase a wildcard type");
}
@Override
public Generic onParameterizedType(Generic parameterizedType) {
return parameterizedType.asRawType();
}
@Override
public Generic onTypeVariable(Generic typeVariable) {
return typeVariable.asRawType();
}
@Override
public Generic onNonGenericType(Generic typeDescription) {
return typeDescription.asRawType();
}
@Override
public String toString() {
return "TypeDescription.Generic.Visitor.TypeErasing." + 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<Generic> {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Generic onGenericArray(Generic genericArray) {
return OfGenericArray.Latent.of(genericArray.getComponentType().accept(this), 1);
}
@Override
public Generic onWildcard(Generic wildcard) {
// Wildcards which are used within parameterized types are taken care of by the calling method.
TypeList.Generic lowerBounds = wildcard.getLowerBounds();
return lowerBounds.isEmpty()
? OfWildcardType.Latent.boundedAbove(wildcard.getUpperBounds().getOnly().accept(this))
: OfWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}
@Override
public Generic onParameterizedType(Generic parameterizedType) {
List<Generic> parameters = new ArrayList<Generic>(parameterizedType.getParameters().size());
for (Generic parameter : parameterizedType.getParameters()) {
if (parameter.accept(TypeVariableErasing.PartialErasureReviser.INSTANCE)) {
return parameterizedType.asRawType();
}
parameters.add(parameter.accept(this));
}
Generic ownerType = parameterizedType.getOwnerType();
return new OfParameterizedType.Latent(parameterizedType.asErasure(),
parameters,
ownerType == null
? UNDEFINED
: ownerType.accept(this));
}
@Override
public Generic onTypeVariable(Generic typeVariable) {
return typeVariable.asRawType();
}
@Override
public Generic onNonGenericType(Generic typeDescription) {
return new OfNonGenericType.Latent(typeDescription.asErasure());
}
@Override
public String toString() {
return "TypeDescription.Generic.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(Generic genericArray) {
return genericArray.getComponentType().accept(this);
}
@Override
public Boolean onWildcard(Generic wildcard) {
TypeList.Generic lowerBounds = wildcard.getLowerBounds();
return lowerBounds.isEmpty()
? wildcard.getUpperBounds().getOnly().accept(this)
: lowerBounds.getOnly().accept(this);
}
@Override
public Boolean onParameterizedType(Generic parameterizedType) {
return false;
}
@Override
public Boolean onTypeVariable(Generic typeVariable) {
return true;
}
@Override
public Boolean onNonGenericType(Generic typeDescription) {
return false;
}
@Override
public String toString() {
return "TypeDescription.Generic.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(Generic genericArray) {
genericArray.getComponentType().accept(new ForSignatureVisitor(signatureVisitor.visitArrayType()));
return signatureVisitor;
}
@Override
public SignatureVisitor onWildcard(Generic wildcard) {
throw new IllegalStateException("Unexpected wildcard: " + wildcard);
}
@Override
public SignatureVisitor onParameterizedType(Generic parameterizedType) {
onOwnableType(parameterizedType);
signatureVisitor.visitEnd();
return signatureVisitor;
}
/**
* Visits a type which might define an owner type.
*
* @param ownableType The visited generic type.
*/
private void onOwnableType(Generic ownableType) {
Generic ownerType = ownableType.getOwnerType();
if (ownerType != null && ownerType.getSort().isParameterized()) {
onOwnableType(ownerType);
signatureVisitor.visitInnerClassType(ownableType.asErasure().getSimpleName());
} else {
signatureVisitor.visitClassType(ownableType.asErasure().getInternalName());
}
for (Generic upperBound : ownableType.getParameters()) {
upperBound.accept(new ForSignatureVisitor.OfParameter(signatureVisitor));
}
}
@Override
public SignatureVisitor onTypeVariable(Generic typeVariable) {
signatureVisitor.visitTypeVariable(typeVariable.getSymbol());
return signatureVisitor;
}
@Override
public SignatureVisitor onNonGenericType(Generic typeDescription) {
if (typeDescription.isArray()) {
typeDescription.getComponentType().accept(new ForSignatureVisitor(signatureVisitor.visitArrayType()));
} else if (typeDescription.isPrimitive()) {
signatureVisitor.visitBaseType(typeDescription.asErasure().getDescriptor().charAt(ONLY_CHARACTER));
} else {
signatureVisitor.visitClassType(typeDescription.asErasure().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 "TypeDescription.Generic.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 onWildcard(Generic wildcard) {
TypeList.Generic upperBounds = wildcard.getUpperBounds();
TypeList.Generic lowerBounds = wildcard.getLowerBounds();
if (lowerBounds.isEmpty() && upperBounds.getOnly().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(Generic genericArray) {
genericArray.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onParameterizedType(Generic parameterizedType) {
parameterizedType.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onTypeVariable(Generic typeVariable) {
typeVariable.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public SignatureVisitor onNonGenericType(Generic typeDescription) {
typeDescription.accept(new ForSignatureVisitor(signatureVisitor.visitTypeArgument(SignatureVisitor.INSTANCEOF)));
return signatureVisitor;
}
@Override
public String toString() {
return "TypeDescription.Generic.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<Generic> {
@Override
public Generic onParameterizedType(Generic parameterizedType) {
Generic ownerType = parameterizedType.getOwnerType();
List<Generic> parameters = new ArrayList<Generic>(parameterizedType.getParameters().size());
for (Generic parameter : parameterizedType.getParameters()) {
parameters.add(parameter.accept(this));
}
return new OfParameterizedType.Latent(parameterizedType.asRawType().accept(this).asErasure(),
parameters,
ownerType == null
? UNDEFINED
: ownerType.accept(this));
}
@Override
public Generic onGenericArray(Generic genericArray) {
return OfGenericArray.Latent.of(genericArray.getComponentType().accept(this), 1);
}
@Override
public Generic onWildcard(Generic wildcard) {
TypeList.Generic lowerBounds = wildcard.getLowerBounds();
return lowerBounds.isEmpty()
? OfWildcardType.Latent.boundedAbove(wildcard.getUpperBounds().getOnly().accept(this))
: OfWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}
@Override
public Generic onNonGenericType(Generic typeDescription) {
int arity = 0;
while (typeDescription.isArray()) {
typeDescription = typeDescription.getComponentType();
arity++;
}
return OfGenericArray.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 Generic onSimpleType(Generic 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 Generic 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(Generic 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().asGenericType(), fieldDescription.getDeclaringType().asErasure());
}
/**
* 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().asGenericType(), 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().asGenericType(), 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.asGenericType(), typeDescription);
}
@Override
public Generic onTypeVariable(Generic genericTypeDescription) {
Generic typeVariable = typeVariableSource.findVariable(genericTypeDescription.getSymbol());
return typeVariable == null
? genericTypeDescription.asRawType()
: typeVariable;
}
@Override
protected Generic onSimpleType(Generic 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 "TypeDescription.Generic.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, Generic> 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, Generic>();
}
@Override
public Generic onTypeVariable(Generic genericTypeDescription) {
Generic typeVariable = detachedVariables.get(genericTypeDescription.getSymbol());
return typeVariable == null
? new ForDetachment.DetachedTypeVariable(genericTypeDescription.getSymbol(), genericTypeDescription.getUpperBounds(), this)
: typeVariable;
}
@Override
protected Generic onSimpleType(Generic typeDescription) {
return typeMatcher.matches(typeDescription.asErasure())
? TargetType.GENERIC_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.