'Cannot add MemberSubstitution using Bytebuddy
I'm trying to add a MemberSubstitution using ByteBuddy to intercept any access to a specific field, but when running the following code I get this error: Could not resolve com.test.MyClass$ByteBuddy$PdQdmF1w using net.bytebuddy.pool.TypePool$ClassLoading@5e712ea6
final var bytebuddy = new ByteBuddy();
var instrumentedType =
bytebuddy
.subclass(MyClass.class)
.method(ElementMatchers.any())
.intercept(MethodDelegation.to(MethodInterceptor.class))
.visit(MemberSubstitution.strict()
.field(ElementMatchers.named("fieldName"))
.onRead()
.stub()
.on(ElementMatchers.any()))
.make()
.load(MyClass.class.getClassLoader())
.getLoaded();
If I remove the call to the visit method, everything works as expected (every method call gets intercepted).
On the contrary, if I retain only the call to the visit method, I get no exception, but the substitution does not seem to work.
Solution 1:[1]
Try MemberSubstitution.relaxed(). There is a generated type without manifest byte code that the substitution struggles to resolve. Therefore, the relaxed resolution can simply skip those types.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafael Winterhalter |
