/
ByteBuddy.java
1158 lines (1074 loc) · 58.9 KB
/
ByteBuddy.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;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.modifier.*;
import net.bytebuddy.description.type.PackageDescription;
import net.bytebuddy.description.type.TypeDefinition;
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.TargetType;
import net.bytebuddy.dynamic.scaffold.InstrumentedType;
import net.bytebuddy.dynamic.scaffold.MethodGraph;
import net.bytebuddy.dynamic.scaffold.TypeValidation;
import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
import net.bytebuddy.dynamic.scaffold.inline.RebaseDynamicTypeBuilder;
import net.bytebuddy.dynamic.scaffold.inline.RedefinitionDynamicTypeBuilder;
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.attribute.AnnotationRetention;
import net.bytebuddy.implementation.attribute.AnnotationValueFilter;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.Duplication;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.TypeCreation;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import net.bytebuddy.implementation.bytecode.collection.ArrayFactory;
import net.bytebuddy.implementation.bytecode.constant.IntegerConstant;
import net.bytebuddy.implementation.bytecode.constant.TextConstant;
import net.bytebuddy.implementation.bytecode.member.FieldAccess;
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.LatentMatcher;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.*;
import static net.bytebuddy.matcher.ElementMatchers.*;
/**
* Instances of this class serve as a focus point for configuration of the library's behavior and as an entry point
* to any form of code generation using the library. For this purpose, Byte Buddy offers a fluent API which allows
* for the step-wise generation of a new Java type. A type is generated either by:
* <ul>
* <li><b>Subclassing</b> some type: A subclass - as the name suggests - extends another, existing Java type. Virtual
* members of the generated type's super types can be overridden. Subclasses can also be interface extensions of one
* or several interfaces.</li>
* <li><b>Redefining</b> a type: By redefining a type, it is not only possible to override virtual methods of the
* redefined type but also to redefine existing methods. This way, it is also possible to change the behavior of
* non-virtual methods and constructors of the redefined type.</li>
* <li><b>Rebasing</b> a type: Rebasing a type works similar to creating a subclass, i.e. any method being overridden
* is still capable of invoking any original code of the rebased type. Any rebased method is however inlined into the
* rebased type and any original code is preserved automatically. This way, the type's identity does not change.</li>
* </ul>
* Byte Buddy's API does not change when a type is rebased, redefined or subclassed. All types are created via the
* {@link net.bytebuddy.dynamic.DynamicType.Builder} interface. Byte Buddy's API is expressed by fully immutable
* components and is therefore thread-safe. As a consequence, method calls must be chained for all of Byte Buddy's
* component, e.g. a method call like the following has no effect:
* <pre>
* ByteBuddy byteBuddy = new ByteBuddy();
* byteBuddy.foo()</pre>
* Instead, the following method chain is corrent use of the API:
* <pre>
* ByteBuddy byteBuddy = new ByteBuddy().foo();</pre>
* <p>
* For the creation of Java agents, Byte Buddy offers a convenience API implemented by the
* {@link net.bytebuddy.agent.builder.AgentBuilder}. The API wraps a {@link ByteBuddy} instance and offers agent-specific
* configuration opportunities by integrating against the {@link java.lang.instrument.Instrumentation} API.
* </p>
*
* @see net.bytebuddy.agent.builder.AgentBuilder
*/
public class ByteBuddy {
/**
* The default prefix for the default {@link net.bytebuddy.NamingStrategy}.
*/
private static final String BYTE_BUDDY_DEFAULT_PREFIX = "ByteBuddy";
/**
* The default suffix when defining a {@link AuxiliaryType.NamingStrategy}.
*/
private static final String BYTE_BUDDY_DEFAULT_SUFFIX = "auxiliary";
/**
* The class file version to use for types that are not based on an existing class file.
*/
protected final ClassFileVersion classFileVersion;
/**
* The naming strategy to use.
*/
protected final NamingStrategy namingStrategy;
/**
* The naming strategy to use for naming auxiliary types.
*/
protected final AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy;
/**
* The annotation value filter factory to use.
*/
protected final AnnotationValueFilter.Factory annotationValueFilterFactory;
/**
* The annotation retention strategy to use.
*/
protected final AnnotationRetention annotationRetention;
/**
* The implementation context factory to use.
*/
protected final Implementation.Context.Factory implementationContextFactory;
/**
* The method graph compiler to use.
*/
protected final MethodGraph.Compiler methodGraphCompiler;
/**
* A matcher for identifying methods that should be excluded from instrumentation.
*/
protected final LatentMatcher<? super MethodDescription> ignoredMethods;
/**
* Determines if a type should be explicitly validated.
*/
protected final TypeValidation typeValidation;
/**
* <p>
* Creates a new Byte Buddy instance with a default configuration that is suitable for most use cases.
* </p>
* <p>
* When creating this configuration, Byte Buddy attempts to discover the current JVM's version. If this
* is not possible, class files are created Java 6-compatible.
* </p>
*
* @see ClassFileVersion#forThisVm(ClassFileVersion)
*/
public ByteBuddy() {
this(ClassFileVersion.forThisVm(ClassFileVersion.JAVA_V6));
}
/**
* Creates a new Byte Buddy instance with a default configuration that is suitable for most use cases.
*
* @param classFileVersion The class file version to use for types that are not based on an existing class file.
*/
public ByteBuddy(ClassFileVersion classFileVersion) {
this(classFileVersion,
new NamingStrategy.SuffixingRandom(BYTE_BUDDY_DEFAULT_PREFIX),
new AuxiliaryType.NamingStrategy.SuffixingRandom(BYTE_BUDDY_DEFAULT_SUFFIX),
AnnotationValueFilter.Default.APPEND_DEFAULTS,
AnnotationRetention.ENABLED,
Implementation.Context.Default.Factory.INSTANCE,
MethodGraph.Compiler.DEFAULT,
TypeValidation.ENABLED,
new LatentMatcher.Resolved<MethodDescription>(isSynthetic().or(isDefaultFinalizer())));
}
/**
* Creates a new Byte Buddy instance.
*
* @param classFileVersion The class file version to use for types that are not based on an existing class file.
* @param namingStrategy The naming strategy to use.
* @param auxiliaryTypeNamingStrategy The naming strategy to use for naming auxiliary types.
* @param annotationValueFilterFactory The annotation value filter factory to use.
* @param annotationRetention The annotation retention strategy to use.
* @param implementationContextFactory The implementation context factory to use.
* @param methodGraphCompiler The method graph compiler to use.
* @param typeValidation Determines if a type should be explicitly validated.
* @param ignoredMethods A matcher for identifying methods that should be excluded from instrumentation.
*/
protected ByteBuddy(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
AnnotationValueFilter.Factory annotationValueFilterFactory,
AnnotationRetention annotationRetention,
Implementation.Context.Factory implementationContextFactory,
MethodGraph.Compiler methodGraphCompiler,
TypeValidation typeValidation,
LatentMatcher<? super MethodDescription> ignoredMethods) {
this.classFileVersion = classFileVersion;
this.namingStrategy = namingStrategy;
this.auxiliaryTypeNamingStrategy = auxiliaryTypeNamingStrategy;
this.annotationValueFilterFactory = annotationValueFilterFactory;
this.annotationRetention = annotationRetention;
this.implementationContextFactory = implementationContextFactory;
this.ignoredMethods = ignoredMethods;
this.typeValidation = typeValidation;
this.methodGraphCompiler = methodGraphCompiler;
}
/**
* <p>
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
* </p>
* <p>
* When extending a class, Byte Buddy imitates all visible constructors of the subclassed type. Any constructor is implemented
* to only invoke its super type constructor of equal signature. Another behavior can be specified by supplying an explicit
* {@link ConstructorStrategy} by {@link ByteBuddy#subclass(Class, ConstructorStrategy)}.
* </p>
*
* @param superClass The super class or interface type to extend.
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(Class<T> superClass) {
return subclass(new TypeDescription.ForLoadedType(superClass));
}
/**
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
*
* @param superClass The super class or interface type to extend.
* @param constructorStrategy A constructor strategy that determines the
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(Class<T> superClass, ConstructorStrategy constructorStrategy) {
return subclass(new TypeDescription.ForLoadedType(superClass), constructorStrategy);
}
/**
* <p>
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
* </p>
* <p>
* When extending a class, Byte Buddy imitates all visible constructors of the subclassed type. Any constructor is implemented
* to only invoke its super type constructor of equal signature. Another behavior can be specified by supplying an explicit
* {@link ConstructorStrategy} by {@link ByteBuddy#subclass(Class, ConstructorStrategy)}.
* </p>
*
* @param superType The super class or interface type to extend. The type must be a raw type or parameterized type. All type
* variables that are referenced by the generic type must be declared by the generated subclass before creating
* the type.
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(Type superType) {
return subclass(TypeDefinition.Sort.describe(superType));
}
/**
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
*
* @param superType The super class or interface type to extend. The type must be a raw type or parameterized
* type. All type variables that are referenced by the generic type must be declared by the
* generated subclass before creating the type.
* @param constructorStrategy A constructor strategy that determines the
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(Type superType, ConstructorStrategy constructorStrategy) {
return subclass(TypeDefinition.Sort.describe(superType), constructorStrategy);
}
/**
* <p>
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
* </p>
* <p>
* When extending a class, Byte Buddy imitates all visible constructors of the subclassed type. Any constructor is implemented
* to only invoke its super type constructor of equal signature. Another behavior can be specified by supplying an explicit
* {@link ConstructorStrategy} by {@link ByteBuddy#subclass(TypeDefinition, ConstructorStrategy)}.
* </p>
*
* @param superType The super class or interface type to extend. The type must be a raw type or parameterized type. All type
* variables that are referenced by the generic type must be declared by the generated subclass before creating
* the type.
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(TypeDefinition superType) {
return subclass(superType, ConstructorStrategy.Default.IMITATE_SUPER_CLASS);
}
/**
* Creates a new builder for subclassing the provided type. If the provided type is an interface, a new class implementing
* this interface type is created.
*
* @param superType The super class or interface type to extend. The type must be a raw type or parameterized
* type. All type variables that are referenced by the generic type must be declared by the
* generated subclass before creating the type.
* @param constructorStrategy A constructor strategy that determines the
* @param <T> A loaded type that the generated class is guaranteed to inherit.
* @return A type builder for creating a new class extending the provided class or interface.
*/
public <T> DynamicType.Builder<T> subclass(TypeDefinition superType, ConstructorStrategy constructorStrategy) {
TypeDescription.Generic actualSuperType;
TypeList.Generic interfaceTypes;
if (superType.isPrimitive() || superType.isArray() || superType.isFinal()) {
throw new IllegalArgumentException("Cannot subclass primitive, array or final types: " + superType);
} else if (superType.isInterface()) {
interfaceTypes = new TypeList.Generic.Explicit(superType.asGenericType());
actualSuperType = TypeDescription.Generic.OBJECT;
} else {
interfaceTypes = new TypeList.Generic.Empty();
actualSuperType = superType.asGenericType();
}
return new SubclassDynamicTypeBuilder<T>(InstrumentedType.Default.subclass(namingStrategy.subclass(superType.asGenericType()),
ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.PLAIN).resolve(superType.getModifiers()),
actualSuperType).withInterfaces(interfaceTypes),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
constructorStrategy);
}
/**
* Creates a new, plain interface type.
*
* @return A type builder that creates a new interface type.
*/
public DynamicType.Builder<?> makeInterface() {
return makeInterface(Collections.<TypeDescription>emptyList());
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceType An interface type that the generated interface implements.
* @param <T> A loaded type that the generated interface is guaranteed to inherit.
* @return A type builder that creates a new interface type.
*/
@SuppressWarnings("unchecked")
public <T> DynamicType.Builder<T> makeInterface(Class<T> interfaceType) {
return (DynamicType.Builder<T>) makeInterface(Collections.<Type>singletonList(interfaceType));
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceType The interface types to implement. The types must be raw or parameterized types. All type
* variables that are referenced by a parameterized type must be declared by the generated
* subclass before creating the type.
* @return A type builder that creates a new interface type.
*/
public DynamicType.Builder<?> makeInterface(Type... interfaceType) {
return makeInterface(new TypeList.Generic.ForLoadedTypes(interfaceType));
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceTypes The interface types to implement. The types must be raw or parameterized types. All
* type variables that are referenced by a parameterized type must be declared by the
* generated subclass before creating the type.
* @return A type builder that creates a new interface type.
*/
public DynamicType.Builder<?> makeInterface(List<? extends Type> interfaceTypes) {
return makeInterface(new TypeList.Generic.ForLoadedTypes(interfaceTypes));
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceType The interface types to implement. The types must be raw or parameterized types. All
* type variables that are referenced by a parameterized type must be declared by the
* generated subclass before creating the type.
* @return A type builder that creates a new interface type.
*/
public DynamicType.Builder<?> makeInterface(TypeDefinition... interfaceType) {
return makeInterface(Arrays.asList(interfaceType));
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceTypes The interface types to implement. The types must be raw or parameterized types. All
* type variables that are referenced by a parameterized type must be declared by the
* generated subclass before creating the type.
* @return A type builder that creates a new interface type.
*/
public DynamicType.Builder<?> makeInterface(Collection<? extends TypeDefinition> interfaceTypes) {
return subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS).implement(interfaceTypes).modifiers(TypeManifestation.INTERFACE, Visibility.PUBLIC);
}
/**
* Creates a new package definition. Package definitions are defined by classes named {@code package-info}
* without any methods or fields but permit annotations. Any field or method definition will cause an
* {@link IllegalStateException} to be thrown when the type is created.
*
* @param name The fully qualified name of the package.
* @return A type builder that creates a {@code package-info} class file.
*/
public DynamicType.Builder<?> makePackage(String name) {
return new SubclassDynamicTypeBuilder<Object>(InstrumentedType.Default.subclass(name + "." + PackageDescription.PACKAGE_CLASS_NAME,
PackageDescription.PACKAGE_MODIFIERS,
TypeDescription.Generic.OBJECT),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
ConstructorStrategy.Default.NO_CONSTRUCTORS);
}
/**
* Creates a new {@link Annotation} type. Annotation properties are implemented as non-static, public methods with the
* property type being defined as the return type.
*
* @return A type builder that creates a new {@link Annotation} type.
*/
public DynamicType.Builder<? extends Annotation> makeAnnotation() {
return new SubclassDynamicTypeBuilder<Annotation>(InstrumentedType.Default.subclass(namingStrategy.subclass(TypeDescription.Generic.ANNOTATION),
ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.ANNOTATION).resolve(),
TypeDescription.Generic.OBJECT).withInterfaces(new TypeList.Generic.Explicit(TypeDescription.Generic.ANNOTATION)),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
ConstructorStrategy.Default.NO_CONSTRUCTORS);
}
/**
* Creates a new {@link Enum} type.
*
* @param value The names of the type's enumeration constants
* @return A type builder for creating an enumeration type.
*/
public DynamicType.Builder<? extends Enum<?>> makeEnumeration(String... value) {
return makeEnumeration(Arrays.asList(value));
}
/**
* Creates a new {@link Enum} type.
*
* @param values The names of the type's enumeration constants
* @return A type builder for creating an enumeration type.
*/
public DynamicType.Builder<? extends Enum<?>> makeEnumeration(Collection<? extends String> values) {
if (values.isEmpty()) {
throw new IllegalArgumentException("Require at least one enumeration constant");
}
TypeDescription.Generic enumType = TypeDescription.Generic.Builder.parameterizedType(Enum.class, TargetType.class).build();
return new SubclassDynamicTypeBuilder<Enum<?>>(InstrumentedType.Default.subclass(namingStrategy.subclass(enumType),
ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.FINAL, EnumerationState.ENUMERATION).resolve(),
enumType),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
ConstructorStrategy.Default.NO_CONSTRUCTORS)
.defineConstructor(Visibility.PRIVATE).withParameters(String.class, int.class)
.intercept(SuperMethodCall.INSTANCE)
.defineMethod(EnumerationImplementation.ENUM_VALUE_OF_METHOD_NAME,
TargetType.class,
Visibility.PUBLIC, Ownership.STATIC).withParameters(String.class)
.intercept(MethodCall.invoke(enumType.getDeclaredMethods()
.filter(named(EnumerationImplementation.ENUM_VALUE_OF_METHOD_NAME).and(takesArguments(Class.class, String.class))).getOnly())
.withOwnType().withArgument(0)
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC))
.defineMethod(EnumerationImplementation.ENUM_VALUES_METHOD_NAME,
TargetType[].class,
Visibility.PUBLIC, Ownership.STATIC)
.intercept(new EnumerationImplementation(new ArrayList<String>(values)));
}
/**
* <p>
* Redefines the given type where any intercepted method that is declared by the redefined type is fully replaced
* by the new implementation.
* </p>
* <p>
* The class file of the redefined type is located by querying the redefined type's class loader by name. For specifying an
* alternative {@link ClassFileLocator}, use {@link ByteBuddy#redefine(Class, ClassFileLocator)}.
* </p>
* <p>
* <b>Note</b>: When a user redefines a class with the purpose of reloading this class using a {@link net.bytebuddy.dynamic.loading.ClassReloadingStrategy},
* it is important that no fields or methods are added to the redefined class. Note that some {@link Implementation}s implicitly add fields or methods.
* Finally, Byte Buddy might be forced to add a method if a redefined class already defines a class initializer. This can be disabled by setting
* {@link ByteBuddy#with(Implementation.Context.Factory)} to use a {@link net.bytebuddy.implementation.Implementation.Context.Disabled.Factory}
* where the class initializer is retained <i>as is</i>.
* </p>
*
* @param type The type that is being redefined.
* @param <T> The loaded type of the redefined type.
* @return A type builder for redefining the provided type.
*/
public <T> DynamicType.Builder<T> redefine(Class<T> type) {
return redefine(type, ClassFileLocator.ForClassLoader.of(type.getClassLoader()));
}
/**
* <p>
* Redefines the given type where any intercepted method that is declared by the redefined type is fully replaced
* by the new implementation.
* </p>
* <p>
* <b>Note</b>: When a user redefines a class with the purpose of reloading this class using a {@link net.bytebuddy.dynamic.loading.ClassReloadingStrategy},
* it is important that no fields or methods are added to the redefined class. Note that some {@link Implementation}s implicitly add fields or methods.
* Finally, Byte Buddy might be forced to add a method if a redefined class already defines a class initializer. This can be disabled by setting
* {@link ByteBuddy#with(Implementation.Context.Factory)} to use a {@link net.bytebuddy.implementation.Implementation.Context.Disabled.Factory}
* where the class initializer is retained <i>as is</i>.
* </p>
*
* @param type The type that is being redefined.
* @param classFileLocator The class file locator that is queried for the redefined type's class file.
* @param <T> The loaded type of the redefined type.
* @return A type builder for redefining the provided type.
*/
public <T> DynamicType.Builder<T> redefine(Class<T> type, ClassFileLocator classFileLocator) {
return redefine(new TypeDescription.ForLoadedType(type), classFileLocator);
}
/**
* <p>
* Redefines the given type where any intercepted method that is declared by the redefined type is fully replaced
* by the new implementation.
* </p>
* <p>
* <b>Note</b>: When a user redefines a class with the purpose of reloading this class using a {@link net.bytebuddy.dynamic.loading.ClassReloadingStrategy},
* it is important that no fields or methods are added to the redefined class. Note that some {@link Implementation}s implicitly add fields or methods.
* Finally, Byte Buddy might be forced to add a method if a redefined class already defines a class initializer. This can be disabled by setting
* {@link ByteBuddy#with(Implementation.Context.Factory)} to use a {@link net.bytebuddy.implementation.Implementation.Context.Disabled.Factory}
* where the class initializer is retained <i>as is</i>.
* </p>
*
* @param type The type that is being redefined.
* @param classFileLocator The class file locator that is queried for the redefined type's class file.
* @param <T> The loaded type of the redefined type.
* @return A type builder for redefining the provided type.
*/
public <T> DynamicType.Builder<T> redefine(TypeDescription type, ClassFileLocator classFileLocator) {
return new RedefinitionDynamicTypeBuilder<T>(InstrumentedType.Default.of(type),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
type,
classFileLocator);
}
/**
* <p>
* Rebases the given type where any intercepted method that is declared by the redefined type is preserved within the
* rebased type's class such that the class's original can be invoked from the new method implementations. Rebasing a
* type can be seen similarly to creating a subclass where the subclass is later merged with the original class file.
* </p>
* <p>
* The class file of the rebased type is located by querying the rebased type's class loader by name. For specifying an
* alternative {@link ClassFileLocator}, use {@link ByteBuddy#redefine(Class, ClassFileLocator)}.
* </p>
*
* @param type The type that is being rebased.
* @param <T> The loaded type of the rebased type.
* @return A type builder for rebasing the provided type.
*/
public <T> DynamicType.Builder<T> rebase(Class<T> type) {
return rebase(type, ClassFileLocator.ForClassLoader.of(type.getClassLoader()));
}
/**
* <p>
* Rebases the given type where any intercepted method that is declared by the redefined type is preserved within the
* rebased type's class such that the class's original can be invoked from the new method implementations. Rebasing a
* type can be seen similarly to creating a subclass where the subclass is later merged with the original class file.
* </p>
* <p>
* When a method is rebased, the original method is copied into a new method with a different name. These names are
* generated automatically by Byte Buddy unless a {@link MethodNameTransformer} is specified explicitly.
* Use {@link ByteBuddy#rebase(Class, ClassFileLocator, MethodNameTransformer)} for doing so.
* </p>
*
* @param type The type that is being rebased.
* @param classFileLocator The class file locator that is queried for the rebased type's class file.
* @param <T> The loaded type of the rebased type.
* @return A type builder for rebasing the provided type.
*/
public <T> DynamicType.Builder<T> rebase(Class<T> type, ClassFileLocator classFileLocator) {
return rebase(new TypeDescription.ForLoadedType(type), classFileLocator);
}
/**
* Rebases the given type where any intercepted method that is declared by the redefined type is preserved within the
* rebased type's class such that the class's original can be invoked from the new method implementations. Rebasing a
* type can be seen similarly to creating a subclass where the subclass is later merged with the original class file.
*
* @param type The type that is being rebased.
* @param classFileLocator The class file locator that is queried for the rebased type's class file.
* @param methodNameTransformer The method name transformer for renaming a method that is rebased.
* @param <T> The loaded type of the rebased type.
* @return A type builder for rebasing the provided type.
*/
public <T> DynamicType.Builder<T> rebase(Class<T> type, ClassFileLocator classFileLocator, MethodNameTransformer methodNameTransformer) {
return rebase(new TypeDescription.ForLoadedType(type), classFileLocator, methodNameTransformer);
}
/**
* <p>
* Rebases the given type where any intercepted method that is declared by the redefined type is preserved within the
* rebased type's class such that the class's original can be invoked from the new method implementations. Rebasing a
* type can be seen similarly to creating a subclass where the subclass is later merged with the original class file.
* </p>
* <p>
* When a method is rebased, the original method is copied into a new method with a different name. These names are
* generated automatically by Byte Buddy unless a {@link MethodNameTransformer} is specified explicitly.
* Use {@link ByteBuddy#rebase(TypeDescription, ClassFileLocator, MethodNameTransformer)} for doing so.
* </p>
*
* @param type The type that is being rebased.
* @param classFileLocator The class file locator that is queried for the rebased type's class file.
* @param <T> The loaded type of the rebased type.
* @return A type builder for rebasing the provided type.
*/
public <T> DynamicType.Builder<T> rebase(TypeDescription type, ClassFileLocator classFileLocator) {
return rebase(type, classFileLocator, MethodNameTransformer.Suffixing.withRandomSuffix());
}
/**
* Rebases the given type where any intercepted method that is declared by the redefined type is preserved within the
* rebased type's class such that the class's original can be invoked from the new method implementations. Rebasing a
* type can be seen similarly to creating a subclass where the subclass is later merged with the original class file.
*
* @param type The type that is being rebased.
* @param classFileLocator The class file locator that is queried for the rebased type's class file.
* @param methodNameTransformer The method name transformer for renaming a method that is rebased.
* @param <T> The loaded type of the rebased type.
* @return A type builder for rebasing the provided type.
*/
public <T> DynamicType.Builder<T> rebase(TypeDescription type, ClassFileLocator classFileLocator, MethodNameTransformer methodNameTransformer) {
return new RebaseDynamicTypeBuilder<T>(InstrumentedType.Default.of(type),
classFileVersion,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods,
type,
classFileLocator,
methodNameTransformer);
}
/**
* Rebases a package. This offers an opportunity to add annotations to the package definition. Packages are defined
* by classes named {@code package-info} without any methods or fields but permit annotations. Any field or method
* definition will cause an {@link IllegalStateException} to be thrown when the type is created.
*
* @param aPackage The package that is being rebased.
* @param classFileLocator The class file locator to use for locating the package's class file.
* @return A type builder for rebasing the given package.
*/
public DynamicType.Builder<?> rebase(Package aPackage, ClassFileLocator classFileLocator) {
return rebase(new PackageDescription.ForLoadedPackage(aPackage), classFileLocator);
}
/**
* Rebases a package. This offers an opportunity to add annotations to the package definition. Packages are defined
* by classes named {@code package-info} without any methods or fields but permit annotations. Any field or method
* definition will cause an {@link IllegalStateException} to be thrown when the type is created.
*
* @param aPackage The package that is being rebased.
* @param classFileLocator The class file locator to use for locating the package's class file.
* @return A type builder for rebasing the given package.
*/
public DynamicType.Builder<?> rebase(PackageDescription aPackage, ClassFileLocator classFileLocator) {
return rebase(new TypeDescription.ForPackageDescription(aPackage), classFileLocator);
}
/**
* Creates a new configuration where all class files that are not based on an existing class file are created
* using the supplied class file version. When creating a Byte Buddy instance by {@link ByteBuddy#ByteBuddy()}, the class
* file version is detected automatically. If the class file version is known before creating a Byte Buddy instance, the
* {@link ByteBuddy#ByteBuddy(ClassFileVersion)} constructor should be used.
*
* @param classFileVersion The class file version to use for types that are not based on an existing class file.
* @return A new Byte Buddy instance that uses the supplied class file version.
*/
public ByteBuddy with(ClassFileVersion classFileVersion) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where new types are named by applying the given naming strategy. By default, Byte Buddy
* simply retains the name of rebased and redefined types but adds a random suffix to the name of created subclasses or
* -interfaces. If a type is defined within the {@code java.*} namespace, Byte Buddy also adds a suffix to the generated
* class because this namespace is only available for the bootstrap class loader.
*
* @param namingStrategy The naming strategy to apply when creating a new dynamic type.
* @return A new Byte Buddy instance that uses the supplied naming strategy.
*/
public ByteBuddy with(NamingStrategy namingStrategy) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where auxiliary types are named by applying the given naming strategy. Auxiliary types
* are helper types that might be required for implementing certain {@link Implementation}s. By default, Byte Buddy
* adds a random suffix to the instrumented type's name when naming its auxiliary types.
*
* @param auxiliaryTypeNamingStrategy The naming strategy to apply when creating a new auxiliary type.
* @return A new Byte Buddy instance that uses the supplied naming strategy for auxiliary types.
*/
public ByteBuddy with(AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where annotation values are written according to the given filter factory. Using
* a filter factory, it is for example possible not to include certain values into a class file such that the
* runtime returns an annotation type's default value. By default, Byte Buddy includes all values into a class file,
* also such values for which a default value exists.
*
* @param annotationValueFilterFactory The annotation value filter factory to use.
* @return A new Byte Buddy instance that uses the supplied annotation value filter factory.
*/
public ByteBuddy with(AnnotationValueFilter.Factory annotationValueFilterFactory) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* <p>
* Creates a new configuration where annotations that are found in an existing class file are or are not preserved
* in the format they are discovered, i.e. rewritten in the format they were already present in the class file.
* By default, Byte Buddy retains annotations when a class is rebased or redefined.
* </p>
* <p>
* <b>Warning</b>: Retaining annotations can cause problems when annotations of a field or method are added based
* on the annotations of a matched method. Doing so, Byte Buddy might write the annotations of the field or method
* explicitly to a class file while simultaneously retaining the existing annotation what results in duplicates.
* When matching fields or methods while adding annotations, disabling annotation retention might be required.
* </p>
*
* @param annotationRetention The annotation retention strategy to use.
* @return A new Byte Buddy instance that uses the supplied annotation retention strategy.
*/
public ByteBuddy with(AnnotationRetention annotationRetention) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where the {@link net.bytebuddy.implementation.Implementation.Context} of any created
* type is a product of the given implementation context factory. An implementation context might imply unwanted
* side-effects, for example, the creation of an additional synthetic methods in order to support specific features
* for realizing an {@link Implementation}. By default, Byte Buddy supplies a factory that enables all features. When
* redefining a loaded class, it is however required by the JVM that no additional members are added such that a
* {@link net.bytebuddy.implementation.Implementation.Context.Disabled} factory might be more appropriate.
*
* @param implementationContextFactory The implementation context factory to use for defining an instrumented type.
* @return A new Byte Buddy instance that uses the supplied implementation context factory.
*/
public ByteBuddy with(Implementation.Context.Factory implementationContextFactory) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where the {@link MethodGraph.Compiler} is used for creating a {@link MethodGraph}
* of the instrumented type. A method graph is a representation of a type's virtual methods, including all information
* on bridge methods that are inserted by the Java compiler. Creating a method graph is a rather expensive operation
* and more efficient strategies might exist for certain types or ava types that are created by alternative JVM
* languages. By default, a general purpose method graph compiler is used that uses the information that is exposed
* by the generic type information that is embedded in any class file.
*
* @param methodGraphCompiler The method graph compiler to use for analyzing the instrumented type.
* @return A new Byte Buddy instance that uses the supplied method graph compiler.
*/
public ByteBuddy with(MethodGraph.Compiler methodGraphCompiler) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration that applies the supplied type validation. By default, explicitly type validation is applied
* by Byte Buddy but it might be disabled for performance reason or for voluntarily creating illegal types. The Java virtual
* machine applies its own type validation where some {@link Error} is thrown if a type is invalid, while Byte Buddy throws
* some {@link RuntimeException}.
*
* @param typeValidation The type validation to apply during type creation.
* @return A new Byte Buddy instance that applies the supplied type validation.
*/
public ByteBuddy with(TypeValidation typeValidation) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
/**
* Creates a new configuration where any {@link MethodDescription} that matches the provided method matcher is excluded
* from instrumentation. Any previous matcher for ignored methods is replaced. By default, Byte Buddy ignores any
* synthetic method (bridge methods are handled automatically) and the {@link Object#finalize()} method.
*
* @param ignoredMethods A matcher for identifying methods to be excluded from instrumentation.
* @return A new Byte Buddy instance that excludes any method from instrumentation if it is matched by the supplied matcher.
*/
public ByteBuddy ignore(ElementMatcher<? super MethodDescription> ignoredMethods) {
return ignore(new LatentMatcher.Resolved<MethodDescription>(ignoredMethods));
}
/**
* <p>
* Creates a new configuration where any {@link MethodDescription} that matches the provided method matcher is excluded
* from instrumentation. Any previous matcher for ignored methods is replaced. By default, Byte Buddy ignores any
* synthetic method (bridge methods are handled automatically) and the {@link Object#finalize()} method. Using a latent
* matcher gives opportunity to resolve an {@link ElementMatcher} based on the instrumented type before applying the matcher.
* </p>
*
* @param ignoredMethods A matcher for identifying methods to be excluded from instrumentation.
* @return A new Byte Buddy instance that excludes any method from instrumentation if it is matched by the supplied matcher.
*/
public ByteBuddy ignore(LatentMatcher<? super MethodDescription> ignoredMethods) {
return new ByteBuddy(classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
annotationValueFilterFactory,
annotationRetention,
implementationContextFactory,
methodGraphCompiler,
typeValidation,
ignoredMethods);
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
ByteBuddy byteBuddy = (ByteBuddy) other;
return classFileVersion.equals(byteBuddy.classFileVersion)
&& annotationValueFilterFactory.equals(byteBuddy.annotationValueFilterFactory)
&& annotationRetention == byteBuddy.annotationRetention
&& namingStrategy.equals(byteBuddy.namingStrategy)
&& auxiliaryTypeNamingStrategy.equals(byteBuddy.auxiliaryTypeNamingStrategy)
&& implementationContextFactory.equals(byteBuddy.implementationContextFactory)
&& methodGraphCompiler.equals(byteBuddy.methodGraphCompiler)
&& typeValidation.equals(byteBuddy.typeValidation)
&& ignoredMethods.equals(byteBuddy.ignoredMethods);
}
@Override
public int hashCode() {
int result = classFileVersion.hashCode();
result = 31 * result + annotationValueFilterFactory.hashCode();
result = 31 * result + annotationRetention.hashCode();
result = 31 * result + namingStrategy.hashCode();
result = 31 * result + auxiliaryTypeNamingStrategy.hashCode();
result = 31 * result + implementationContextFactory.hashCode();
result = 31 * result + methodGraphCompiler.hashCode();
result = 31 * result + typeValidation.hashCode();
result = 31 * result + ignoredMethods.hashCode();
return result;
}
@Override
public String toString() {
return "ByteBuddy{" +
"classFileVersion=" + classFileVersion +
", annotationValueFilterFactory=" + annotationValueFilterFactory +
", annotationRetention=" + annotationRetention +
", namingStrategy=" + namingStrategy +
", auxiliaryTypeNamingStrategy=" + auxiliaryTypeNamingStrategy +
", implementationContextFactory=" + implementationContextFactory +
", methodGraphCompiler=" + methodGraphCompiler +
", typeValidation=" + typeValidation +
", ignoredMethods=" + ignoredMethods +
'}';
}
/**
* An implementation fo the {@code values} method of an enumeration type.
*/
protected static class EnumerationImplementation implements Implementation {
/**
* The name of the {@link java.lang.Object#clone()} method.
*/
protected static final String CLONE_METHOD_NAME = "clone";
/**
* The name of the {@code valueOf} method that is defined for any enumeration.
*/
protected static final String ENUM_VALUE_OF_METHOD_NAME = "valueOf";
/**
* The name of the {@code values} method that is defined for any enumeration.
*/
protected static final String ENUM_VALUES_METHOD_NAME = "values";
/**
* The field modifiers to use for any field that is added to an enumeration.
*/
private static final int ENUM_FIELD_MODIFIERS = Opcodes.ACC_FINAL | Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC;
/**
* The name of the field containing an array of all enumeration values.
*/
private static final String ENUM_VALUES = "$VALUES";
/**
* The names of the enumerations to define for the enumeration.
*/
private final List<String> values;
/**
* Creates a new implementation of an enumeration type.
*
* @param values The values of the enumeration.
*/
protected EnumerationImplementation(List<String> values) {
this.values = values;
}