-
-
Notifications
You must be signed in to change notification settings - Fork 777
/
InstrumentedType.java
958 lines (859 loc) · 40.5 KB
/
InstrumentedType.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
package net.bytebuddy.dynamic.scaffold;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
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.PackageDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.description.type.TypeVariableToken;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.LoadedTypeInitializer;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.utility.CompoundList;
import org.objectweb.asm.MethodVisitor;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static net.bytebuddy.matcher.ElementMatchers.is;
/**
* Implementations of this interface represent an instrumented type that is subject to change. Implementations
* should however be immutable and return new instance when its builder methods are invoked.
*/
public interface InstrumentedType extends TypeDescription {
/**
* Creates a new instrumented type that includes a new field.
*
* @param token A token that represents the field's shape. This token must represent types in their detached state.
* @return A new instrumented type that is equal to this instrumented type but with the additional field.
*/
InstrumentedType withField(FieldDescription.Token token);
/**
* Creates a new instrumented type that includes a new method or constructor.
*
* @param token A token that represents the method's shape. This token must represent types in their detached state.
* @return A new instrumented type that is equal to this instrumented type but with the additional method.
*/
InstrumentedType withMethod(MethodDescription.Token token);
/**
* Creates a new instrumented type with changed modifiers.
*
* @param modifiers The instrumented type's modifiers.
* @return A new instrumented type that is equal to this instrumented type but with the given modifiers.
*/
InstrumentedType withModifiers(int modifiers);
/**
* Creates a new instrumented type with the given interfaces implemented.
*
* @param interfaceTypes The interface types to implement.
* @return A new instrumented type that is equal to this instrumented type but with the given interfaces implemented.
*/
InstrumentedType withInterfaces(TypeList.Generic interfaceTypes);
/**
* Creates a new instrumented type with the given type variable defined.
*
* @param typeVariable The type variable to declare.
* @return A new instrumented type that is equal to this instrumented type but with the given type variable declared.
*/
InstrumentedType withTypeVariable(TypeVariableToken typeVariable);
/**
* Creates a new instrumented type with the given annotations.
*
* @param annotationDescriptions The annotations to add to the instrumented type.
* @return A new instrumented type that is equal to this instrumented type but annotated with the given annotations
*/
InstrumentedType withAnnotations(List<? extends AnnotationDescription> annotationDescriptions);
/**
* Creates a new instrumented type that includes the given {@link net.bytebuddy.implementation.LoadedTypeInitializer}.
*
* @param loadedTypeInitializer The type initializer to include.
* @return A new instrumented type that is equal to this instrumented type but with the additional type initializer.
*/
InstrumentedType withInitializer(LoadedTypeInitializer loadedTypeInitializer);
/**
* Creates a new instrumented type that executes the given initializer in the instrumented type's
* type initializer.
*
* @param byteCodeAppender The byte code to add to the type initializer.
* @return A new instrumented type that is equal to this instrumented type but with the given stack manipulation
* attached to its type initializer.
*/
InstrumentedType withInitializer(ByteCodeAppender byteCodeAppender);
/**
* Returns the {@link net.bytebuddy.implementation.LoadedTypeInitializer}s that were registered
* for this instrumented type.
*
* @return The registered loaded type initializers for this instrumented type.
*/
LoadedTypeInitializer getLoadedTypeInitializer();
/**
* Returns this instrumented type's type initializer.
*
* @return This instrumented type's type initializer.
*/
TypeInitializer getTypeInitializer();
/**
* Validates the instrumented type to define a legal Java type.
*
* @return This instrumented type as a non-modifiable type description.
*/
TypeDescription validated();
/**
* A type initializer is responsible for defining a type's static initialization block.
*/
interface TypeInitializer extends ByteCodeAppender {
/**
* Indicates if this type initializer is defined.
*
* @return {@code true} if this type initializer is defined.
*/
boolean isDefined();
/**
* Expands this type initializer with another byte code appender. For this to be possible, this type initializer must
* be defined.
*
* @param byteCodeAppender The byte code appender to apply within the type initializer.
* @return A defined type initializer.
*/
TypeInitializer expandWith(ByteCodeAppender byteCodeAppender);
/**
* Returns this type initializer with an ending return statement. For this to be possible, this type initializer must
* be defined.
*
* @return This type initializer with an ending return statement.
*/
ByteCodeAppender withReturn();
/**
* Canonical implementation of a non-defined type initializer.
*/
enum None implements TypeInitializer {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public boolean isDefined() {
return false;
}
@Override
public TypeInitializer expandWith(ByteCodeAppender byteCodeAppender) {
return new TypeInitializer.Simple(byteCodeAppender);
}
@Override
public ByteCodeAppender withReturn() {
throw new IllegalStateException("Cannot append return to non-defined type initializer");
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) {
throw new IllegalStateException("Cannot apply a non-defined type initializer");
}
@Override
public String toString() {
return "InstrumentedType.TypeInitializer.None." + name();
}
}
/**
* A simple, defined type initializer that executes a given {@link net.bytebuddy.implementation.bytecode.ByteCodeAppender}.
*/
class Simple implements TypeInitializer {
/**
* The stack manipulation to apply within the type initializer.
*/
private final ByteCodeAppender byteCodeAppender;
/**
* Creates a new simple type initializer.
*
* @param byteCodeAppender The byte code appender manipulation to apply within the type initializer.
*/
public Simple(ByteCodeAppender byteCodeAppender) {
this.byteCodeAppender = byteCodeAppender;
}
@Override
public boolean isDefined() {
return true;
}
@Override
public TypeInitializer expandWith(ByteCodeAppender byteCodeAppender) {
return new TypeInitializer.Simple(new ByteCodeAppender.Compound(this.byteCodeAppender, byteCodeAppender));
}
@Override
public ByteCodeAppender withReturn() {
return new ByteCodeAppender.Compound(byteCodeAppender, new ByteCodeAppender.Simple(MethodReturn.VOID));
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) {
return byteCodeAppender.apply(methodVisitor, implementationContext, instrumentedMethod);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& byteCodeAppender.equals(((TypeInitializer.Simple) other).byteCodeAppender);
}
@Override
public int hashCode() {
return byteCodeAppender.hashCode();
}
@Override
public String toString() {
return "InstrumentedType.TypeInitializer.Simple{" +
"byteCodeAppender=" + byteCodeAppender +
'}';
}
}
}
/**
* Implementations represent an {@link InstrumentedType} with a flexible name.
*/
interface WithFlexibleName extends InstrumentedType {
@Override
WithFlexibleName withField(FieldDescription.Token token);
@Override
WithFlexibleName withMethod(MethodDescription.Token token);
@Override
WithFlexibleName withModifiers(int modifiers);
@Override
WithFlexibleName withInterfaces(TypeList.Generic interfaceTypes);
@Override
WithFlexibleName withTypeVariable(TypeVariableToken typeVariable);
@Override
WithFlexibleName withAnnotations(List<? extends AnnotationDescription> annotationDescriptions);
@Override
WithFlexibleName withInitializer(LoadedTypeInitializer loadedTypeInitializer);
@Override
WithFlexibleName withInitializer(ByteCodeAppender byteCodeAppender);
/**
* Creates a new instrumented type with a changed name.
*
* @param name The name of the instrumented type.
* @return A new instrumented type that has the given name.
*/
WithFlexibleName withName(String name);
}
/**
* Implementations are able to prepare an {@link InstrumentedType}.
*/
interface Prepareable {
/**
* Prepares a given instrumented type.
*
* @param instrumentedType The instrumented type in its current form.
* @return The prepared instrumented type.
*/
InstrumentedType prepare(InstrumentedType instrumentedType);
}
/**
* A default implementation of an instrumented type.
*/
class Default extends AbstractBase.OfSimpleType implements InstrumentedType.WithFlexibleName {
/**
* The binary name of the instrumented type.
*/
private final String name;
/**
* The modifiers of the instrumented type.
*/
private final int modifiers;
/**
* The generic super type of the instrumented type.
*/
private final Generic superType;
/**
* The instrumented type's type variables in their tokenized form.
*/
private final List<? extends TypeVariableToken> typeVariables;
/**
* A list of interfaces of the instrumented type.
*/
private final List<? extends Generic> interfaceTypes;
/**
* A list of field tokens describing the fields of the instrumented type.
*/
private final List<? extends FieldDescription.Token> fieldTokens;
/**
* A list of method tokens describing the methods of the instrumented type.
*/
private final List<? extends MethodDescription.Token> methodTokens;
/**
* A list of annotations of the annotated type.
*/
private final List<? extends AnnotationDescription> annotationDescriptions;
/**
* The type initializer of the instrumented type.
*/
private final TypeInitializer typeInitializer;
/**
* The loaded type initializer of the instrumented type.
*/
private final LoadedTypeInitializer loadedTypeInitializer;
/**
* The declaring type of the instrumented type or {@code null} if no such type exists.
*/
private final TypeDescription declaringType;
/**
* The enclosing method of the instrumented type or {@code null} if no such type exists.
*/
private final MethodDescription enclosingMethod;
/**
* The enclosing type of the instrumented type or {@code null} if no such type exists.
*/
private final TypeDescription enclosingType;
/**
* A list of types that are declared by this type.
*/
private final List<? extends TypeDescription> declaredTypes;
/**
* {@code true} if this type is a member class.
*/
private final boolean memberClass;
/**
* {@code true} if this type is a anonymous class.
*/
private final boolean anonymousClass;
/**
* {@code true} if this type is a local class.
*/
private final boolean localClass;
/**
* Creates a new instrumented type.
*
* @param name The binary name of the instrumented type.
* @param modifiers The modifiers of the instrumented type.
* @param typeVariables The instrumented type's type variables in their tokenized form.
* @param superType The generic super type of the instrumented type.
* @param interfaceTypes A list of interfaces of the instrumented type.
* @param fieldTokens A list of field tokens describing the fields of the instrumented type.
* @param methodTokens A list of method tokens describing the methods of the instrumented type.
* @param annotationDescriptions A list of annotations of the annotated type.
* @param typeInitializer The type initializer of the instrumented type.
* @param loadedTypeInitializer The loaded type initializer of the instrumented type.
* @param declaringType The declaring type of the instrumented type or {@code null} if no such type exists.
* @param enclosingMethod The enclosing method of the instrumented type or {@code null} if no such type exists.
* @param enclosingType The enclosing type of the instrumented type or {@code null} if no such type exists.
* @param declaredTypes A list of types that are declared by this type.
* @param memberClass {@code true} if this type is a member class.
* @param anonymousClass {@code true} if this type is a anonymous class.
* @param localClass {@code true} if this type is a local class.
*/
protected Default(String name,
int modifiers,
Generic superType,
List<? extends TypeVariableToken> typeVariables,
List<? extends Generic> interfaceTypes,
List<? extends FieldDescription.Token> fieldTokens,
List<? extends MethodDescription.Token> methodTokens,
List<? extends AnnotationDescription> annotationDescriptions,
TypeInitializer typeInitializer,
LoadedTypeInitializer loadedTypeInitializer,
TypeDescription declaringType,
MethodDescription enclosingMethod,
TypeDescription enclosingType,
List<? extends TypeDescription> declaredTypes,
boolean memberClass,
boolean anonymousClass,
boolean localClass) {
this.name = name;
this.modifiers = modifiers;
this.typeVariables = typeVariables;
this.superType = superType;
this.interfaceTypes = interfaceTypes;
this.fieldTokens = fieldTokens;
this.methodTokens = methodTokens;
this.annotationDescriptions = annotationDescriptions;
this.typeInitializer = typeInitializer;
this.loadedTypeInitializer = loadedTypeInitializer;
this.declaringType = declaringType;
this.enclosingMethod = enclosingMethod;
this.enclosingType = enclosingType;
this.declaredTypes = declaredTypes;
this.memberClass = memberClass;
this.anonymousClass = anonymousClass;
this.localClass = localClass;
}
/**
* Creates an instrumented type that is a subclass of the given super type named as given and with the modifiers.
*
* @param name The name of the instrumented type.
* @param modifiers The modifiers of the instrumented type.
* @param superType The super type of the instrumented type.
* @return An instrumented type as a subclass of the given type with the given name and modifiers.
*/
public static InstrumentedType.WithFlexibleName subclass(String name, int modifiers, Generic superType) {
return new Default(name,
modifiers,
superType,
Collections.<TypeVariableToken>emptyList(),
Collections.<Generic>emptyList(),
Collections.<FieldDescription.Token>emptyList(),
Collections.<MethodDescription.Token>emptyList(),
Collections.<AnnotationDescription>emptyList(),
TypeInitializer.None.INSTANCE,
LoadedTypeInitializer.NoOp.INSTANCE,
UNDEFINED,
MethodDescription.UNDEFINED,
UNDEFINED,
Collections.<TypeDescription>emptyList(),
false,
false,
false);
}
/**
* Creates an instrumented type that represents the given type description.
*
* @param typeDescription A description of the type to represent.
* @return An instrumented type of the given type.
*/
public static InstrumentedType.WithFlexibleName of(TypeDescription typeDescription) {
return new Default(typeDescription.getName(),
typeDescription.getModifiers(),
typeDescription.getSuperType(),
typeDescription.getTypeVariables().asTokenList(is(typeDescription)),
typeDescription.getInterfaces().accept(Generic.Visitor.Substitutor.ForDetachment.of(typeDescription)),
typeDescription.getDeclaredFields().asTokenList(is(typeDescription)),
typeDescription.getDeclaredMethods().asTokenList(is(typeDescription)),
typeDescription.getDeclaredAnnotations(),
TypeInitializer.None.INSTANCE,
LoadedTypeInitializer.NoOp.INSTANCE,
typeDescription.getDeclaringType(),
typeDescription.getEnclosingMethod(),
typeDescription.getEnclosingType(),
typeDescription.getDeclaredTypes(),
typeDescription.isMemberClass(),
typeDescription.isAnonymousClass(),
typeDescription.isLocalClass());
}
@Override
public WithFlexibleName withModifiers(int modifiers) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withField(FieldDescription.Token token) {
return new Default(this.name,
modifiers,
superType,
typeVariables,
interfaceTypes,
CompoundList.of(fieldTokens, token.accept(Generic.Visitor.Substitutor.ForDetachment.of(this))),
methodTokens,
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withMethod(MethodDescription.Token token) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
CompoundList.of(methodTokens, token.accept(Generic.Visitor.Substitutor.ForDetachment.of(this))),
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withInterfaces(TypeList.Generic interfaceTypes) {
return new Default(name,
modifiers,
superType,
typeVariables,
CompoundList.of(this.interfaceTypes, interfaceTypes.accept(Generic.Visitor.Substitutor.ForDetachment.of(this))),
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withAnnotations(List<? extends AnnotationDescription> annotationDescriptions) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
methodTokens,
CompoundList.of(this.annotationDescriptions, annotationDescriptions),
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withTypeVariable(TypeVariableToken typeVariable) {
return new Default(name,
modifiers,
superType,
CompoundList.of(typeVariables, typeVariable.accept(Generic.Visitor.Substitutor.ForDetachment.of(this))),
interfaceTypes,
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withName(String name) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer,
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withInitializer(LoadedTypeInitializer loadedTypeInitializer) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer,
new LoadedTypeInitializer.Compound(this.loadedTypeInitializer, loadedTypeInitializer),
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public WithFlexibleName withInitializer(ByteCodeAppender byteCodeAppender) {
return new Default(name,
modifiers,
superType,
typeVariables,
interfaceTypes,
fieldTokens,
methodTokens,
annotationDescriptions,
typeInitializer.expandWith(byteCodeAppender),
loadedTypeInitializer,
declaringType,
enclosingMethod,
enclosingType,
declaredTypes,
memberClass,
anonymousClass,
localClass);
}
@Override
public LoadedTypeInitializer getLoadedTypeInitializer() {
return loadedTypeInitializer;
}
@Override
public TypeInitializer getTypeInitializer() {
return typeInitializer;
}
@Override
public MethodDescription getEnclosingMethod() {
return enclosingMethod;
}
@Override
public TypeDescription getEnclosingType() {
return enclosingType;
}
@Override
public TypeList getDeclaredTypes() {
return new TypeList.Explicit(declaredTypes);
}
@Override
public boolean isAnonymousClass() {
return anonymousClass;
}
@Override
public boolean isLocalClass() {
return localClass;
}
@Override
public boolean isMemberClass() {
return memberClass;
}
@Override
public PackageDescription getPackage() {
int packageIndex = name.lastIndexOf('.');
return packageIndex == -1
? PackageDescription.UNDEFINED
: new PackageDescription.Simple(name.substring(0, packageIndex));
}
@Override
public AnnotationList getDeclaredAnnotations() {
return new AnnotationList.Explicit(annotationDescriptions);
}
@Override
public TypeDescription getDeclaringType() {
return declaringType;
}
@Override
public Generic getSuperType() {
return superType == null
? Generic.UNDEFINED
: superType.accept(Generic.Visitor.Substitutor.ForAttachment.of(this));
}
@Override
public TypeList.Generic getInterfaces() {
return TypeList.Generic.ForDetachedTypes.attach(this, interfaceTypes);
}
@Override
public FieldList<FieldDescription.InDefinedShape> getDeclaredFields() {
return new FieldList.ForTokens(this, fieldTokens);
}
@Override
public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
return new MethodList.ForTokens(this, methodTokens);
}
@Override
public TypeList.Generic getTypeVariables() {
return TypeList.Generic.ForDetachedTypes.attachVariables(this, typeVariables);
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public String getName() {
return name;
}
@Override
public TypeDescription validated() {
if (!isValidIdentifier(getName().split("\\."))) {
throw new IllegalStateException("Illegal type name: " + getName());
}
TypeDescription.Generic superType = getSuperType();
if (superType != null && !superType.accept(Generic.Visitor.Validator.SUPER_CLASS)) {
throw new IllegalStateException("Illegal super class " + getSuperType() + " for " + this);
}
Set<TypeDescription> interfaceErasures = new HashSet<TypeDescription>();
for (TypeDescription.Generic interfaceType : getInterfaces()) {
if (!interfaceErasures.add(interfaceType.asErasure())) {
throw new IllegalStateException("Already implemented interface " + interfaceType + " for " + this);
} else if (!interfaceType.accept(Generic.Visitor.Validator.INTERFACE)) {
throw new IllegalStateException("Illegal interface " + interfaceType + " for " + this);
}
}
Set<String> typeVariableNames = new HashSet<String>();
for (TypeDescription.Generic typeVariable : getTypeVariables()) {
String variableSymbol = typeVariable.getSymbol();
if (!typeVariableNames.add(variableSymbol)) {
throw new IllegalStateException("Duplicate type variable symbol pf " + typeVariable + " for " + this);
} else if (!isValidIdentifier(variableSymbol)) {
throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + this);
}
boolean interfaceBound = false;
for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) {
if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) {
throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + " for " + this);
} else if (interfaceBound && (bound.getSort().isTypeVariable() || !bound.asErasure().isInterface())) {
throw new IllegalStateException("Illegal interface bound " + bound + " of " + typeVariable + " for " + this);
}
interfaceBound = true;
}
if (!interfaceBound) {
throw new IllegalStateException("Type variable " + typeVariable + " for " + this + " does not define at least one bound");
}
}
Set<TypeDescription> typeAnnotationTypes = new HashSet<TypeDescription>();
for (AnnotationDescription annotationDescription : getDeclaredAnnotations()) {
if (!typeAnnotationTypes.add(annotationDescription.getAnnotationType())) {
throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + this);
}
}
Set<String> fieldNames = new HashSet<String>();
for (FieldDescription.InDefinedShape fieldDescription : getDeclaredFields()) {
String fieldName = fieldDescription.getName();
if (!fieldNames.add(fieldName)) {
throw new IllegalStateException("Duplicate field definition for " + fieldDescription);
} else if (!isValidIdentifier(fieldName)) {
throw new IllegalStateException("Illegal field name for " + fieldDescription);
}
Set<TypeDescription> fieldAnnotationTypes = new HashSet<TypeDescription>();
for (AnnotationDescription annotationDescription : fieldDescription.getDeclaredAnnotations()) {
if (!fieldAnnotationTypes.add(annotationDescription.getAnnotationType())) {
throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + fieldDescription);
}
}
if (!fieldDescription.getType().accept(Generic.Visitor.Validator.FIELD)) {
throw new IllegalStateException("Illegal type variable bound " + fieldDescription.getType() + " for " + fieldDescription);
}
}
Set<MethodDescription.SignatureToken> methodSignatureTokens = new HashSet<MethodDescription.SignatureToken>();
for (MethodDescription.InDefinedShape methodDescription : getDeclaredMethods()) {
if (!methodSignatureTokens.add(methodDescription.asSignatureToken())) {
throw new IllegalStateException("Duplicate method signature for " + methodDescription);
}
Set<String> methodTypeVariableNames = new HashSet<String>();
for (TypeDescription.Generic typeVariable : methodDescription.getTypeVariables()) {
String variableSymbol = typeVariable.getSymbol();
if (!methodTypeVariableNames.add(variableSymbol)) {
throw new IllegalStateException("Duplicate type variable symbol of " + typeVariable + " for " + methodDescription);
} else if (!isValidIdentifier(variableSymbol)) {
throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + methodDescription);
}
boolean interfaceBound = false;
for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) {
if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) {
throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + " for " + methodDescription);
} else if (interfaceBound && (bound.getSort().isTypeVariable() || !bound.asErasure().isInterface())) {
throw new IllegalStateException("Illegal interface bound " + bound + " of " + typeVariable + " for " + methodDescription);
}
interfaceBound = true;
}
if (!interfaceBound) {
throw new IllegalStateException("Type variable " + typeVariable + " for " + methodDescription + " does not define at least one bound");
}
}
Set<TypeDescription> methodAnnotationTypes = new HashSet<TypeDescription>();
for (AnnotationDescription annotationDescription : methodDescription.getDeclaredAnnotations()) {
if (!methodAnnotationTypes.add(annotationDescription.getAnnotationType())) {
throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + methodDescription);
}
}
if (methodDescription.isTypeInitializer()) {
throw new IllegalStateException("Illegal explicit declaration of a type initializer by " + this);
} else if (methodDescription.isConstructor()) {
if (!methodDescription.getReturnType().represents(void.class)) {
throw new IllegalStateException("A constructor must return void " + methodDescription);
}
} else if (!methodDescription.getReturnType().accept(Generic.Visitor.Validator.METHOD_RETURN)) {
throw new IllegalStateException("Illegal return type " + methodDescription.getReturnType() + " for " + methodDescription);
} else if (!isValidIdentifier(methodDescription.getInternalName())) {
throw new IllegalStateException("Illegal method name for: " + methodDescription);
}
Set<String> parameterNames = new HashSet<String>();
for (ParameterDescription.InDefinedShape parameterDescription : methodDescription.getParameters()) {
if (parameterDescription.isNamed()) {
String parameterName = parameterDescription.getName();
if (!parameterNames.add(parameterName)) {
throw new IllegalStateException("Duplicate parameter name for " + parameterDescription);
} else if (!isValidIdentifier(parameterName)) {
throw new IllegalStateException("Illegal parameter name for " + parameterDescription);
}
}
Set<TypeDescription> parameterAnnotationTypes = new HashSet<TypeDescription>();
for (AnnotationDescription annotationDescription : parameterDescription.getDeclaredAnnotations()) {
if (!parameterAnnotationTypes.add(annotationDescription.getAnnotationType())) {
throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + parameterDescription);
}
}
if (!parameterDescription.getType().accept(Generic.Visitor.Validator.METHOD_PARAMETER)) {
throw new IllegalStateException("Illegal parameter type " + parameterDescription.getType() + " for " + parameterDescription);
}
}
Set<TypeDescription.Generic> exceptionTypes = new HashSet<Generic>();
for (TypeDescription.Generic exceptionType : methodDescription.getExceptionTypes()) {
if (!exceptionTypes.add(exceptionType)) {
throw new IllegalStateException("Duplicate exception type " + exceptionType + " for " + methodDescription);
} else if (!exceptionType.accept(Generic.Visitor.Validator.EXCEPTION)) {
throw new IllegalStateException("Illegal exception type " + exceptionType + " for " + methodDescription);
}
}
}
return this;
}
/**
* Checks if an array of identifiers is a valid compound Java identifier.
*
* @param identifier an array of potentially invalid Java identifiers.
* @return {@code true} if all identifiers are valid and the array is not empty.
*/
private static boolean isValidIdentifier(String[] identifier) {
if (identifier.length == 0) {
return false;
}
for (String part : identifier) {
if (!isValidIdentifier(part)) {
return false;
}
}
return true;
}
/**
* Checks if a Java identifier is valid.
*
* @param identifier The identifier to check for validity.
* @return {@code true} if the given identifier is valid.
*/
private static boolean isValidIdentifier(String identifier) {
if (identifier.isEmpty() || !Character.isJavaIdentifierStart(identifier.charAt(0))) {
return false;
} else if (identifier.equals(PackageDescription.PACKAGE_CLASS_NAME)) {
return true;
}
for (int index = 1; index < identifier.length(); index++) {
if (!Character.isJavaIdentifierPart(identifier.charAt(index))) {
return false;
}
}
return true;
}
}
}