Package net.bytebuddy.asm
Class MemberSubstitution
- java.lang.Object
-
- net.bytebuddy.asm.MemberSubstitution
-
- All Implemented Interfaces:
AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
public class MemberSubstitution extends java.lang.Object implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
Substitutes field access or method invocations within a method's body.
Important: This component relies on using a
TypePool
for locating types within method bodies. Within a redefinition or a rebasement, this type pool normally resolved correctly by Byte Buddy. When subclassing a type, the type pool must be set explicitly, usingDynamicType.Builder.make(TypePool)
or any similar method. It is however not normally necessary to use this component when subclassing a type where methods are only defined explicitly.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
MemberSubstitution.SubstitutingMethodVisitor
A method visitor that applies a substitution for matched methods.protected static interface
MemberSubstitution.Substitution
Resolves an actual substitution.static interface
MemberSubstitution.TypePoolResolver
A type pool resolver is responsible for resolving aTypePool
for locating substituted members.static class
MemberSubstitution.WithoutSpecification
A member substitution that lacks a specification for how to substitute the matched members references within a method body.
-
Constructor Summary
Constructors Modifier Constructor Description protected
MemberSubstitution(boolean strict)
Creates a default member substitution.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MemberSubstitution.WithoutSpecification
constructor(ElementMatcher<? super MethodDescription> matcher)
Substitutes any constructor invocation that matches the given matcher.MemberSubstitution.WithoutSpecification
element(ElementMatcher<? super ByteCodeElement> matcher)
Substitutes any interaction with a field or method that matches the given matcher.MemberSubstitution.WithoutSpecification.ForMatchedField
field(ElementMatcher<? super FieldDescription.InDefinedShape> matcher)
Substitutes any field access that matches the given matcher.MemberSubstitution.WithoutSpecification
invokable(ElementMatcher<? super MethodDescription> matcher)
Substitutes any method or constructor invocation that matches the given matcher.MemberSubstitution.WithoutSpecification.ForMatchedMethod
method(ElementMatcher<? super MethodDescription> matcher)
Substitutes any method invocation that matches the given matcher.AsmVisitorWrapper.ForDeclaredMethods
on(ElementMatcher<? super MethodDescription> matcher)
Applies this member substitutor to any method that matches the supplied matcher.static MemberSubstitution
relaxed()
Creates a member substitutor that skips any unresolvable fields or methods that are referenced within a method body.static MemberSubstitution
strict()
Creates a member substitutor that requires the resolution of all fields and methods that are referenced within a method body.MemberSubstitution
with(MemberSubstitution.TypePoolResolver typePoolResolver)
Specifies a type pool resolver to be used for locating members.MemberSubstitution
with(MethodGraph.Compiler methodGraphCompiler)
Specifies the use of a specific method graph compiler for the resolution of virtual methods.org.objectweb.asm.MethodVisitor
wrap(TypeDescription instrumentedType, MethodDescription instrumentedMethod, org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, TypePool typePool, int writerFlags, int readerFlags)
Wraps a method visitor.
-
-
-
Method Detail
-
strict
public static MemberSubstitution strict()
Creates a member substitutor that requires the resolution of all fields and methods that are referenced within a method body. Doing so, this component raises an exception if any member cannot be resolved what makes this component unusable when facing optional types.- Returns:
- A strict member substitutor.
-
relaxed
public static MemberSubstitution relaxed()
Creates a member substitutor that skips any unresolvable fields or methods that are referenced within a method body. Using a relaxed member substitutor, methods containing optional types are supported. In the process, it is however possible that misconfigurations of this component remain undiscovered.- Returns:
- A relaxed member substitutor.
-
element
public MemberSubstitution.WithoutSpecification element(ElementMatcher<? super ByteCodeElement> matcher)
Substitutes any interaction with a field or method that matches the given matcher.- Parameters:
matcher
- The matcher to determine what access to byte code elements to substitute.- Returns:
- A specification that allows to determine how to substitute any interaction with byte code elements that match the supplied matcher.
-
field
public MemberSubstitution.WithoutSpecification.ForMatchedField field(ElementMatcher<? super FieldDescription.InDefinedShape> matcher)
Substitutes any field access that matches the given matcher.- Parameters:
matcher
- The matcher to determine what fields to substitute.- Returns:
- A specification that allows to determine how to substitute any field access that match the supplied matcher.
-
method
public MemberSubstitution.WithoutSpecification.ForMatchedMethod method(ElementMatcher<? super MethodDescription> matcher)
Substitutes any method invocation that matches the given matcher.- Parameters:
matcher
- The matcher to determine what methods to substitute.- Returns:
- A specification that allows to determine how to substitute any method invocations that match the supplied matcher.
-
constructor
public MemberSubstitution.WithoutSpecification constructor(ElementMatcher<? super MethodDescription> matcher)
Substitutes any constructor invocation that matches the given matcher.- Parameters:
matcher
- The matcher to determine what constructors to substitute.- Returns:
- A specification that allows to determine how to substitute any constructor invocations that match the supplied matcher.
-
invokable
public MemberSubstitution.WithoutSpecification invokable(ElementMatcher<? super MethodDescription> matcher)
Substitutes any method or constructor invocation that matches the given matcher.- Parameters:
matcher
- The matcher to determine what method or constructors to substitute.- Returns:
- A specification that allows to determine how to substitute any constructor invocations that match the supplied matcher.
-
with
public MemberSubstitution with(MethodGraph.Compiler methodGraphCompiler)
Specifies the use of a specific method graph compiler for the resolution of virtual methods.- Parameters:
methodGraphCompiler
- The method graph compiler to use.- Returns:
- A new member substitution that is equal to this but uses the specified method graph compiler.
-
with
public MemberSubstitution with(MemberSubstitution.TypePoolResolver typePoolResolver)
Specifies a type pool resolver to be used for locating members.- Parameters:
typePoolResolver
- The type pool resolver to use.- Returns:
- A new instance of this member substitution that uses the supplied type pool resolver.
-
on
public AsmVisitorWrapper.ForDeclaredMethods on(ElementMatcher<? super MethodDescription> matcher)
Applies this member substitutor to any method that matches the supplied matcher.- Parameters:
matcher
- The matcher to determine this substitutors application.- Returns:
- An ASM visitor wrapper that applies all specified substitutions for any matched method.
-
wrap
public org.objectweb.asm.MethodVisitor wrap(TypeDescription instrumentedType, MethodDescription instrumentedMethod, org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, TypePool typePool, int writerFlags, int readerFlags)
Description copied from interface:AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
Wraps a method visitor.- Specified by:
wrap
in interfaceAsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
- Parameters:
instrumentedType
- The instrumented type.instrumentedMethod
- The method that is currently being defined.methodVisitor
- The original field visitor that defines the given method.implementationContext
- The implementation context to use.typePool
- The type pool to use.writerFlags
- The ASMClassWriter
reader flags to consider.readerFlags
- The ASMClassReader
reader flags to consider.- Returns:
- The wrapped method visitor.
-
-