Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.fitbur.bytebuddy;
import com.fitbur.bytebuddy.description.field.FieldDescription;
import com.fitbur.bytebuddy.description.method.MethodDescription;
import com.fitbur.bytebuddy.description.modifier.*;
import com.fitbur.bytebuddy.description.type.PackageDescription;
import com.fitbur.bytebuddy.description.type.TypeDefinition;
import com.fitbur.bytebuddy.description.type.TypeDescription;
import com.fitbur.bytebuddy.description.type.TypeList;
import com.fitbur.bytebuddy.dynamic.ClassFileLocator;
import com.fitbur.bytebuddy.dynamic.DynamicType;
import com.fitbur.bytebuddy.dynamic.TargetType;
import com.fitbur.bytebuddy.dynamic.scaffold.InstrumentedType;
import com.fitbur.bytebuddy.dynamic.scaffold.MethodGraph;
import com.fitbur.bytebuddy.dynamic.scaffold.TypeValidation;
import com.fitbur.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
import com.fitbur.bytebuddy.dynamic.scaffold.inline.RebaseDynamicTypeBuilder;
import com.fitbur.bytebuddy.dynamic.scaffold.inline.RedefinitionDynamicTypeBuilder;
import com.fitbur.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import com.fitbur.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder;
import com.fitbur.bytebuddy.implementation.Implementation;
import com.fitbur.bytebuddy.implementation.MethodCall;
import com.fitbur.bytebuddy.implementation.SuperMethodCall;
import com.fitbur.bytebuddy.implementation.attribute.AnnotationRetention;
import com.fitbur.bytebuddy.implementation.attribute.AnnotationValueFilter;
import com.fitbur.bytebuddy.implementation.auxiliary.AuxiliaryType;
import com.fitbur.bytebuddy.implementation.bytecode.ByteCodeAppender;
import com.fitbur.bytebuddy.implementation.bytecode.Duplication;
import com.fitbur.bytebuddy.implementation.bytecode.StackManipulation;
import com.fitbur.bytebuddy.implementation.bytecode.TypeCreation;
import com.fitbur.bytebuddy.implementation.bytecode.assign.Assigner;
import com.fitbur.bytebuddy.implementation.bytecode.assign.TypeCasting;
import com.fitbur.bytebuddy.implementation.bytecode.collection.ArrayFactory;
import com.fitbur.bytebuddy.implementation.bytecode.constant.IntegerConstant;
import com.fitbur.bytebuddy.implementation.bytecode.constant.TextConstant;
import com.fitbur.bytebuddy.implementation.bytecode.member.FieldAccess;
import com.fitbur.bytebuddy.implementation.bytecode.member.MethodInvocation;
import com.fitbur.bytebuddy.implementation.bytecode.member.MethodReturn;
import com.fitbur.bytebuddy.matcher.ElementMatcher;
import com.fitbur.bytebuddy.matcher.LatentMatcher;
import com.fitbur.bytebuddy.jar.asm.MethodVisitor;
import com.fitbur.bytebuddy.jar.asm.Opcodes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.*;
import static com.fitbur.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:
*
*
Subclassing 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.
*
Redefining 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.
*
Rebasing 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.
*
* Byte Buddy's API does not change when a type is rebased, redefined or subclassed. All types are created via the
* {@link com.fitbur.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:
*
* ByteBuddy byteBuddy = new ByteBuddy();
* byteBuddy.foo()
* Instead, the following method chain is corrent use of the API:
*
* ByteBuddy byteBuddy = new ByteBuddy().foo();
*
* For the creation of Java agents, Byte Buddy offers a convenience API implemented by the
* {@link com.fitbur.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.
*
*
* @see com.fitbur.bytebuddy.agent.builder.AgentBuilder
*/
public class ByteBuddy {
/**
* The default prefix for the default {@link com.fitbur.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;
/**
*
* Creates a new Byte Buddy instance with a default configuration that is suitable for most use cases.
*
*
* When creating this configuration, Byte Buddy attempts to discover the current JVM's version by parsing
* the {@code java.home} system property. If this is not possible on the executing VM, an
* {@link IllegalStateException} is thrown. To overcome this, a class file version can be specified explicitly
* by {@link ByteBuddy#ByteBuddy(ClassFileVersion)}.
*
*/
public ByteBuddy() {
this(ClassFileVersion.forCurrentJavaVersion());
}
/**
* 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(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;
}
/**
*
* 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.
*
*
* 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)}.
*
*
* @param superClass The super class or interface type to extend.
* @param 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 DynamicType.Builder subclass(Class 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 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 DynamicType.Builder subclass(Class superClass, ConstructorStrategy constructorStrategy) {
return subclass(new TypeDescription.ForLoadedType(superClass), constructorStrategy);
}
/**
*
* 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.
*
*
* 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)}.
*
*
* @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 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 DynamicType.Builder 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 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 DynamicType.Builder subclass(Type superType, ConstructorStrategy constructorStrategy) {
return subclass(TypeDefinition.Sort.describe(superType), constructorStrategy);
}
/**
*
* 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.
*
*
* 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)}.
*
*
* @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 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 DynamicType.Builder 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 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 DynamicType.Builder subclass(TypeDefinition superType, ConstructorStrategy constructorStrategy) {
TypeDescription.Generic actualSuperType;
TypeList.Generic interfaceTypes;
if (superType.isPrimitive() || superType.isArray() || superType.asErasure().isFinal()) {
throw new IllegalArgumentException("Cannot subclass primitive, array or final types: " + superType);
} else if (superType.asErasure().isInterface()) {
interfaceTypes = new TypeList.Generic.Explicit(superType.asGenericType());
actualSuperType = TypeDescription.Generic.OBJECT;
} else {
interfaceTypes = new TypeList.Generic.Empty();
actualSuperType = superType.asGenericType();
}
return new SubclassDynamicTypeBuilder(InstrumentedType.Default.subclass(namingStrategy.subclass(superType.asGenericType()),
ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.PLAIN).resolve(superType.asErasure().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.emptyList());
}
/**
* Creates a new interface type that extends the provided interface.
*
* @param interfaceType An interface type that the generated interface implements.
* @param A loaded type that the generated interface is guaranteed to inherit.
* @return A type builder that creates a new interface type.
*/
@SuppressWarnings("unchecked")
public DynamicType.Builder makeInterface(Class interfaceType) {
return (DynamicType.Builder) makeInterface(Collections.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);
}
/**
* 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