简体   繁体   English

无法使用 Bytebuddy 添加 MemberSubstitution

[英]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我正在尝试使用 ByteBuddy 添加MemberSubstitution以拦截对特定字段的任何访问,但是在运行以下代码时出现此错误: 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).如果我删除对visit方法的调用,一切都会按预期进行(每个方法调用都会被拦截)。

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.相反,如果我只保留对visit方法的调用,我也不会出现异常,但替换似乎不起作用。

Try MemberSubstitution.relaxed() .尝试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.因此,宽松的决议可以简单地跳过这些类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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