简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM