/
TypeWriter.java
4036 lines (3547 loc) · 193 KB
/
TypeWriter.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.dynamic.scaffold;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.annotation.AnnotationValue;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.description.modifier.ModifierContributor;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.PackageDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.TypeResolutionStrategy;
import net.bytebuddy.dynamic.scaffold.inline.MethodRebaseResolver;
import net.bytebuddy.dynamic.scaffold.inline.RebaseImplementationTarget;
import net.bytebuddy.dynamic.scaffold.subclass.SubclassImplementationTarget;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.LoadedTypeInitializer;
import net.bytebuddy.implementation.attribute.*;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.CompoundList;
import net.bytebuddy.utility.RandomString;
import net.bytebuddy.utility.privilege.GetSystemPropertyAction;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.SimpleRemapper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static net.bytebuddy.matcher.ElementMatchers.is;
/**
* A type writer is a utility for writing an actual class file using the ASM library.
*
* @param <T> The best known loaded type for the dynamically created type.
*/
public interface TypeWriter<T> {
/**
* A system property that indicates a folder for Byte Buddy to dump class files of all types that it creates.
* If this property is not set, Byte Buddy does not dump any class files. This property is only read a single
* time which is why it must be set on application start-up.
*/
String DUMP_PROPERTY = "net.bytebuddy.dump";
/**
* Creates the dynamic type that is described by this type writer.
*
* @param typeResolver The type resolution strategy to use.
* @return An unloaded dynamic type that describes the created type.
*/
DynamicType.Unloaded<T> make(TypeResolutionStrategy.Resolved typeResolver);
/**
* An field pool that allows a lookup for how to implement a field.
*/
interface FieldPool {
/**
* Returns the field attribute appender that matches a given field description or a default field
* attribute appender if no appender was registered for the given field.
*
* @param fieldDescription The field description of interest.
* @return The registered field attribute appender for the given field or the default appender if no such
* appender was found.
*/
Record target(FieldDescription fieldDescription);
/**
* An entry of a field pool that describes how a field is implemented.
*
* @see net.bytebuddy.dynamic.scaffold.TypeWriter.FieldPool
*/
interface Record {
/**
* Determines if this record is implicit, i.e is not defined by a {@link FieldPool}.
*
* @return {@code true} if this record is implicit.
*/
boolean isImplicit();
/**
* Returns the field that this record represents.
*
* @return The field that this record represents.
*/
FieldDescription getField();
/**
* Returns the field attribute appender for a given field.
*
* @return The attribute appender to be applied on the given field.
*/
FieldAttributeAppender getFieldAppender();
/**
* Resolves the default value that this record represents. This is not possible for implicit records.
*
* @param defaultValue The default value that was defined previously or {@code null} if no default value is defined.
* @return The default value for the represented field or {@code null} if no default value is to be defined.
*/
Object resolveDefault(Object defaultValue);
/**
* Writes this entry to a given class visitor.
*
* @param classVisitor The class visitor to which this entry is to be written to.
* @param annotationValueFilterFactory The annotation value filter factory to apply when writing annotations.
*/
void apply(ClassVisitor classVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory);
/**
* Applies this record to a field visitor. This is not possible for implicit records.
*
* @param fieldVisitor The field visitor onto which this record is to be applied.
* @param annotationValueFilterFactory The annotation value filter factory to use for annotations.
*/
void apply(FieldVisitor fieldVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory);
/**
* A record for a simple field without a default value where all of the field's declared annotations are appended.
*/
class ForImplicitField implements Record {
/**
* The implemented field.
*/
private final FieldDescription fieldDescription;
/**
* Creates a new record for a simple field.
*
* @param fieldDescription The described field.
*/
public ForImplicitField(FieldDescription fieldDescription) {
this.fieldDescription = fieldDescription;
}
@Override
public boolean isImplicit() {
return true;
}
@Override
public FieldDescription getField() {
return fieldDescription;
}
@Override
public FieldAttributeAppender getFieldAppender() {
throw new IllegalStateException("An implicit field record does not expose a field appender: " + this);
}
@Override
public Object resolveDefault(Object defaultValue) {
throw new IllegalStateException("An implicit field record does not expose a default value: " + this);
}
@Override
public void apply(ClassVisitor classVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory) {
FieldVisitor fieldVisitor = classVisitor.visitField(fieldDescription.getActualModifiers(),
fieldDescription.getInternalName(),
fieldDescription.getDescriptor(),
fieldDescription.getGenericSignature(),
FieldDescription.NO_DEFAULT_VALUE);
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor,
fieldDescription,
annotationValueFilterFactory.on(fieldDescription));
fieldVisitor.visitEnd();
}
@Override
public void apply(FieldVisitor fieldVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory) {
throw new IllegalStateException("An implicit field record is not intended for partial application: " + this);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& fieldDescription.equals(((ForImplicitField) other).fieldDescription);
}
@Override
public int hashCode() {
return fieldDescription.hashCode();
}
@Override
public String toString() {
return "TypeWriter.FieldPool.Record.ForImplicitField{" +
"fieldDescription=" + fieldDescription +
'}';
}
}
/**
* A record for a rich field with attributes and a potential default value.
*/
class ForExplicitField implements Record {
/**
* The attribute appender for the field.
*/
private final FieldAttributeAppender attributeAppender;
/**
* The field's default value.
*/
private final Object defaultValue;
/**
* The implemented field.
*/
private final FieldDescription fieldDescription;
/**
* Creates a record for a rich field.
*
* @param attributeAppender The attribute appender for the field.
* @param defaultValue The field's default value.
* @param fieldDescription The implemented field.
*/
public ForExplicitField(FieldAttributeAppender attributeAppender, Object defaultValue, FieldDescription fieldDescription) {
this.attributeAppender = attributeAppender;
this.defaultValue = defaultValue;
this.fieldDescription = fieldDescription;
}
@Override
public boolean isImplicit() {
return false;
}
@Override
public FieldDescription getField() {
return fieldDescription;
}
@Override
public FieldAttributeAppender getFieldAppender() {
return attributeAppender;
}
@Override
public Object resolveDefault(Object defaultValue) {
return this.defaultValue == null
? defaultValue
: this.defaultValue;
}
@Override
public void apply(ClassVisitor classVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory) {
FieldVisitor fieldVisitor = classVisitor.visitField(fieldDescription.getActualModifiers(),
fieldDescription.getInternalName(),
fieldDescription.getDescriptor(),
fieldDescription.getGenericSignature(),
resolveDefault(FieldDescription.NO_DEFAULT_VALUE));
attributeAppender.apply(fieldVisitor, fieldDescription, annotationValueFilterFactory.on(fieldDescription));
fieldVisitor.visitEnd();
}
@Override
public void apply(FieldVisitor fieldVisitor, AnnotationValueFilter.Factory annotationValueFilterFactory) {
attributeAppender.apply(fieldVisitor, fieldDescription, annotationValueFilterFactory.on(fieldDescription));
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ForExplicitField that = (ForExplicitField) other;
return attributeAppender.equals(that.attributeAppender)
&& !(defaultValue != null ? !defaultValue.equals(that.defaultValue) : that.defaultValue != null)
&& fieldDescription.equals(that.fieldDescription);
}
@Override
public int hashCode() {
int result = attributeAppender.hashCode();
result = 31 * result + (defaultValue != null ? defaultValue.hashCode() : 0);
result = 31 * result + fieldDescription.hashCode();
return result;
}
@Override
public String toString() {
return "TypeWriter.FieldPool.Record.ForExplicitField{" +
"attributeAppender=" + attributeAppender +
", defaultValue=" + defaultValue +
", fieldDescription=" + fieldDescription +
'}';
}
}
}
}
/**
* An method pool that allows a lookup for how to implement a method.
*/
interface MethodPool {
/**
* Looks up a handler entry for a given method.
*
* @param methodDescription The method being processed.
* @return A handler entry for the given method.
*/
Record target(MethodDescription methodDescription);
/**
* An entry of a method pool that describes how a method is implemented.
*
* @see net.bytebuddy.dynamic.scaffold.TypeWriter.MethodPool
*/
interface Record {
/**
* Returns the sort of this method instrumentation.
*
* @return The sort of this method instrumentation.
*/
Sort getSort();
/**
* Returns the method that is implemented where the returned method resembles a potential transformation. An implemented
* method is only defined if a method is not {@link Record.Sort#SKIPPED}.
*
* @return The implemented method.
*/
MethodDescription getMethod();
/**
* The visibility to enforce for this method.
*
* @return The visibility to enforce for this method.
*/
Visibility getVisibility();
/**
* Prepends the given method appender to this entry.
*
* @param byteCodeAppender The byte code appender to prepend.
* @return This entry with the given code prepended.
*/
Record prepend(ByteCodeAppender byteCodeAppender);
/**
* Applies this method entry. This method can always be called and might be a no-op.
*
* @param classVisitor The class visitor to which this entry should be applied.
* @param implementationContext The implementation context to which this entry should be applied.
* @param annotationValueFilterFactory The annotation value filter factory to apply when writing annotations.
*/
void apply(ClassVisitor classVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory);
/**
* Applies the head of this entry. Applying an entry is only possible if a method is defined, i.e. the sort of this entry is not
* {@link Record.Sort#SKIPPED}.
*
* @param methodVisitor The method visitor to which this entry should be applied.
*/
void applyHead(MethodVisitor methodVisitor);
/**
* Applies the body of this entry. Applying the body of an entry is only possible if a method is implemented, i.e. the sort of this
* entry is {@link Record.Sort#IMPLEMENTED}.
*
* @param methodVisitor The method visitor to which this entry should be applied.
* @param implementationContext The implementation context to which this entry should be applied.
* @param annotationValueFilterFactory The annotation value filter factory to apply when writing annotations.
*/
void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory);
/**
* The sort of an entry.
*/
enum Sort {
/**
* Describes a method that should not be implemented or retained in its original state.
*/
SKIPPED(false, false),
/**
* Describes a method that should be defined but is abstract or native, i.e. does not define any byte code.
*/
DEFINED(true, false),
/**
* Describes a method that is implemented in byte code.
*/
IMPLEMENTED(true, true);
/**
* Indicates if this sort defines a method, with or without byte code.
*/
private final boolean define;
/**
* Indicates if this sort defines byte code.
*/
private final boolean implement;
/**
* Creates a new sort.
*
* @param define Indicates if this sort defines a method, with or without byte code.
* @param implement Indicates if this sort defines byte code.
*/
Sort(boolean define, boolean implement) {
this.define = define;
this.implement = implement;
}
/**
* Indicates if this sort defines a method, with or without byte code.
*
* @return {@code true} if this sort defines a method, with or without byte code.
*/
public boolean isDefined() {
return define;
}
/**
* Indicates if this sort defines byte code.
*
* @return {@code true} if this sort defines byte code.
*/
public boolean isImplemented() {
return implement;
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Entry.Sort." + name();
}
}
/**
* A canonical implementation of a method that is not declared but inherited by the instrumented type.
*/
enum ForNonDefinedMethod implements Record {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public void apply(ClassVisitor classVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
/* do nothing */
}
@Override
public void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
throw new IllegalStateException("Cannot apply headless implementation for method that should be skipped");
}
@Override
public void applyHead(MethodVisitor methodVisitor) {
throw new IllegalStateException("Cannot apply headless implementation for method that should be skipped");
}
@Override
public MethodDescription getMethod() {
throw new IllegalStateException("A method that is not defined cannot be extracted");
}
@Override
public Visibility getVisibility() {
throw new IllegalStateException("A method that is not defined does not require a specific visibility");
}
@Override
public Sort getSort() {
return Sort.SKIPPED;
}
@Override
public Record prepend(ByteCodeAppender byteCodeAppender) {
throw new IllegalStateException("Cannot prepend code to non-implemented method");
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Record.ForNonDefinedMethod." + name();
}
}
/**
* A base implementation of an abstract entry that defines a method.
*/
abstract class ForDefinedMethod implements Record {
@Override
public void apply(ClassVisitor classVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
MethodVisitor methodVisitor = classVisitor.visitMethod(getMethod().getActualModifiers(getSort().isImplemented(), getVisibility()),
getMethod().getInternalName(),
getMethod().getDescriptor(),
getMethod().getGenericSignature(),
getMethod().getExceptionTypes().asErasures().toInternalNames());
ParameterList<?> parameterList = getMethod().getParameters();
if (parameterList.hasExplicitMetaData()) {
for (ParameterDescription parameterDescription : parameterList) {
methodVisitor.visitParameter(parameterDescription.getName(), parameterDescription.getModifiers());
}
}
applyHead(methodVisitor);
applyBody(methodVisitor, implementationContext, annotationValueFilterFactory);
methodVisitor.visitEnd();
}
/**
* Describes an entry that defines a method as byte code.
*/
public static class WithBody extends ForDefinedMethod {
/**
* The implemented method.
*/
private final MethodDescription methodDescription;
/**
* The byte code appender to apply.
*/
private final ByteCodeAppender byteCodeAppender;
/**
* The method attribute appender to apply.
*/
private final MethodAttributeAppender methodAttributeAppender;
/**
* The represented method's minimum visibility.
*/
private final Visibility visibility;
/**
* Creates a new record for an implemented method without attributes or a modifier resolver.
*
* @param methodDescription The implemented method.
* @param byteCodeAppender The byte code appender to apply.
*/
public WithBody(MethodDescription methodDescription, ByteCodeAppender byteCodeAppender) {
this(methodDescription, byteCodeAppender, MethodAttributeAppender.NoOp.INSTANCE, methodDescription.getVisibility());
}
/**
* Creates a new entry for a method that defines a method as byte code.
*
* @param methodDescription The implemented method.
* @param byteCodeAppender The byte code appender to apply.
* @param methodAttributeAppender The method attribute appender to apply.
* @param visibility The represented method's minimum visibility.
*/
public WithBody(MethodDescription methodDescription,
ByteCodeAppender byteCodeAppender,
MethodAttributeAppender methodAttributeAppender,
Visibility visibility) {
this.methodDescription = methodDescription;
this.byteCodeAppender = byteCodeAppender;
this.methodAttributeAppender = methodAttributeAppender;
this.visibility = visibility;
}
@Override
public MethodDescription getMethod() {
return methodDescription;
}
@Override
public Sort getSort() {
return Sort.IMPLEMENTED;
}
@Override
public Visibility getVisibility() {
return visibility;
}
@Override
public void applyHead(MethodVisitor methodVisitor) {
/* do nothing */
}
@Override
public void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilterFactory.on(methodDescription));
methodVisitor.visitCode();
ByteCodeAppender.Size size = byteCodeAppender.apply(methodVisitor, implementationContext, methodDescription);
methodVisitor.visitMaxs(size.getOperandStackSize(), size.getLocalVariableSize());
}
@Override
public Record prepend(ByteCodeAppender byteCodeAppender) {
return new WithBody(methodDescription,
new ByteCodeAppender.Compound(byteCodeAppender, this.byteCodeAppender),
methodAttributeAppender,
visibility);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
WithBody withBody = (WithBody) other;
return methodDescription.equals(withBody.methodDescription)
&& byteCodeAppender.equals(withBody.byteCodeAppender)
&& methodAttributeAppender.equals(withBody.methodAttributeAppender)
&& visibility.equals(withBody.visibility);
}
@Override
public int hashCode() {
int result = methodDescription.hashCode();
result = 31 * result + byteCodeAppender.hashCode();
result = 31 * result + methodAttributeAppender.hashCode();
result = 31 * result + visibility.hashCode();
return result;
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Record.ForDefinedMethod.WithBody{" +
"methodDescription=" + methodDescription +
", byteCodeAppender=" + byteCodeAppender +
", methodAttributeAppender=" + methodAttributeAppender +
", visibility=" + visibility +
'}';
}
}
/**
* Describes an entry that defines a method but without byte code and without an annotation value.
*/
public static class WithoutBody extends ForDefinedMethod {
/**
* The implemented method.
*/
private final MethodDescription methodDescription;
/**
* The method attribute appender to apply.
*/
private final MethodAttributeAppender methodAttributeAppender;
/**
* The represented method's minimum visibility.
*/
private final Visibility visibility;
/**
* Creates a new entry for a method that is defines but does not append byte code, i.e. is native or abstract.
*
* @param methodDescription The implemented method.
* @param methodAttributeAppender The method attribute appender to apply.
* @param visibility The represented method's minimum visibility.
*/
public WithoutBody(MethodDescription methodDescription, MethodAttributeAppender methodAttributeAppender, Visibility visibility) {
this.methodDescription = methodDescription;
this.methodAttributeAppender = methodAttributeAppender;
this.visibility = visibility;
}
@Override
public MethodDescription getMethod() {
return methodDescription;
}
@Override
public Sort getSort() {
return Sort.DEFINED;
}
@Override
public Visibility getVisibility() {
return visibility;
}
@Override
public void applyHead(MethodVisitor methodVisitor) {
/* do nothing */
}
@Override
public void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilterFactory.on(methodDescription));
}
@Override
public Record prepend(ByteCodeAppender byteCodeAppender) {
throw new IllegalStateException("Cannot prepend code to abstract method");
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
WithoutBody that = (WithoutBody) other;
return methodDescription.equals(that.methodDescription)
&& methodAttributeAppender.equals(that.methodAttributeAppender)
&& visibility.equals(that.visibility);
}
@Override
public int hashCode() {
int result = methodDescription.hashCode();
result = 31 * result + methodAttributeAppender.hashCode();
result = 31 * result + visibility.hashCode();
return result;
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Record.ForDefinedMethod.WithoutBody{" +
"methodDescription=" + methodDescription +
", methodAttributeAppender=" + methodAttributeAppender +
", visibility=" + visibility +
'}';
}
}
/**
* Describes an entry that defines a method with a default annotation value.
*/
public static class WithAnnotationDefaultValue extends ForDefinedMethod {
/**
* The implemented method.
*/
private final MethodDescription methodDescription;
/**
* The annotation value to define.
*/
private final AnnotationValue<?, ?> annotationValue;
/**
* The method attribute appender to apply.
*/
private final MethodAttributeAppender methodAttributeAppender;
/**
* Creates a new entry for defining a method with a default annotation value.
*
* @param methodDescription The implemented method.
* @param annotationValue The annotation value to define.
* @param methodAttributeAppender The method attribute appender to apply.
*/
public WithAnnotationDefaultValue(MethodDescription methodDescription,
AnnotationValue<?, ?> annotationValue,
MethodAttributeAppender methodAttributeAppender) {
this.methodDescription = methodDescription;
this.annotationValue = annotationValue;
this.methodAttributeAppender = methodAttributeAppender;
}
@Override
public MethodDescription getMethod() {
return methodDescription;
}
@Override
public Sort getSort() {
return Sort.DEFINED;
}
@Override
public Visibility getVisibility() {
return methodDescription.getVisibility();
}
@Override
public void applyHead(MethodVisitor methodVisitor) {
if (!methodDescription.isDefaultValue(annotationValue)) {
throw new IllegalStateException("Cannot set " + annotationValue + " as default for " + methodDescription);
}
AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotationDefault();
AnnotationAppender.Default.apply(annotationVisitor,
methodDescription.getReturnType().asErasure(),
AnnotationAppender.NO_NAME,
annotationValue.resolve());
annotationVisitor.visitEnd();
}
@Override
public void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilterFactory.on(methodDescription));
}
@Override
public Record prepend(ByteCodeAppender byteCodeAppender) {
throw new IllegalStateException("Cannot prepend code to method that defines a default annotation value");
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
WithAnnotationDefaultValue that = (WithAnnotationDefaultValue) other;
return methodDescription.equals(that.methodDescription)
&& annotationValue.equals(that.annotationValue)
&& methodAttributeAppender.equals(that.methodAttributeAppender);
}
@Override
public int hashCode() {
int result = methodDescription.hashCode();
result = 31 * result + annotationValue.hashCode();
result = 31 * result + methodAttributeAppender.hashCode();
return result;
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Record.ForDefinedMethod.WithAnnotationDefaultValue{" +
"methodDescription=" + methodDescription +
", annotationValue=" + annotationValue +
", methodAttributeAppender=" + methodAttributeAppender +
'}';
}
}
/**
* A record for a visibility bridge.
*/
public static class OfVisibilityBridge extends ForDefinedMethod implements ByteCodeAppender {
/**
* The visibility bridge.
*/
private final MethodDescription visibilityBridge;
/**
* The method the visibility bridge invokes.
*/
private final MethodDescription bridgeTarget;
/**
* The super type of the instrumented type.
*/
private final TypeDescription superClass;
/**
* The attribute appender to apply to the visibility bridge.
*/
private final MethodAttributeAppender attributeAppender;
/**
* Creates a new record for a visibility bridge.
*
* @param visibilityBridge The visibility bridge.
* @param bridgeTarget The method the visibility bridge invokes.
* @param superClass The super type of the instrumented type.
* @param attributeAppender The attribute appender to apply to the visibility bridge.
*/
protected OfVisibilityBridge(MethodDescription visibilityBridge,
MethodDescription bridgeTarget,
TypeDescription superClass,
MethodAttributeAppender attributeAppender) {
this.visibilityBridge = visibilityBridge;
this.bridgeTarget = bridgeTarget;
this.superClass = superClass;
this.attributeAppender = attributeAppender;
}
/**
* Creates a record for a visibility bridge.
*
* @param instrumentedType The instrumented type.
* @param bridgeTarget The target method of the visibility bridge.
* @param attributeAppender The attribute appender to apply to the visibility bridge.
* @return A record describing the visibility bridge.
*/
public static Record of(TypeDescription instrumentedType, MethodDescription bridgeTarget, MethodAttributeAppender attributeAppender) {
return new OfVisibilityBridge(new VisibilityBridge(instrumentedType, bridgeTarget),
bridgeTarget,
instrumentedType.getSuperClass().asErasure(),
attributeAppender);
}
@Override
public MethodDescription getMethod() {
return visibilityBridge;
}
@Override
public Sort getSort() {
return Sort.IMPLEMENTED;
}
@Override
public Visibility getVisibility() {
return bridgeTarget.getVisibility();
}
@Override
public Record prepend(ByteCodeAppender byteCodeAppender) {
return new ForDefinedMethod.WithBody(visibilityBridge,
new ByteCodeAppender.Compound(this, byteCodeAppender),
attributeAppender,
bridgeTarget.getVisibility());
}
@Override
public void applyHead(MethodVisitor methodVisitor) {
/* do nothing */
}
@Override
public void applyBody(MethodVisitor methodVisitor, Implementation.Context implementationContext, AnnotationValueFilter.Factory annotationValueFilterFactory) {
attributeAppender.apply(methodVisitor, visibilityBridge, annotationValueFilterFactory.on(visibilityBridge));
methodVisitor.visitCode();
ByteCodeAppender.Size size = apply(methodVisitor, implementationContext, visibilityBridge);
methodVisitor.visitMaxs(size.getOperandStackSize(), size.getLocalVariableSize());
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) {
return new ByteCodeAppender.Simple(
MethodVariableAccess.allArgumentsOf(instrumentedMethod).prependThisReference(),
MethodInvocation.invoke(bridgeTarget).special(superClass),
MethodReturn.of(instrumentedMethod.getReturnType())
).apply(methodVisitor, implementationContext, instrumentedMethod);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
OfVisibilityBridge that = (OfVisibilityBridge) other;
return visibilityBridge.equals(that.visibilityBridge)
&& bridgeTarget.equals(that.bridgeTarget)
&& superClass.equals(that.superClass)
&& attributeAppender.equals(that.attributeAppender);
}
@Override
public int hashCode() {
int result = visibilityBridge.hashCode();
result = 31 * result + bridgeTarget.hashCode();
result = 31 * result + superClass.hashCode();
result = 31 * result + attributeAppender.hashCode();
return result;
}
@Override
public String toString() {
return "TypeWriter.MethodPool.Record.ForDefinedMethod.OfVisibilityBridge{" +
"visibilityBridge=" + visibilityBridge +
", bridgeTarget=" + bridgeTarget +
", superClass=" + superClass +
", attributeAppender=" + attributeAppender +
'}';
}
/**
* A method describing a visibility bridge.
*/
protected static class VisibilityBridge extends MethodDescription.InDefinedShape.AbstractBase {
/**
* The instrumented type.
*/
private final TypeDescription instrumentedType;
/**
* The method that is the target of the bridge.
*/
private final MethodDescription bridgeTarget;
/**
* Creates a new visibility bridge.
*
* @param instrumentedType The instrumented type.