繁体   English   中英

如何使用AgentBuilder替换类的方法

[英]How to Use AgentBuilder replace the method of class

我可以重新定义该类以将Foo的方法替换为Bar ,如下所示:

   ByteBuddy byteBuddy = new ByteBuddy();
        byteBuddy
                .redefine(Bar.class)
                .name(Foo.class.getName())
                .make()
                .load(Foo.class.getClassLoader(),
                        ClassReloadingStrategy.fromInstalledAgent());

Foo foo = new Foo()
foo.m()
// output is bar

如何使用AgentBuilder进行相同的工作,替换所有Foo的方法,而不是委托

  new AgentBuilder.Default()
                .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
                .with(RedefinitionStrategy.RETRANSFORMATION)
                .type(ElementMatchers.is(Foo.class))
                .transform(
                        ( builder, typeDescription, classLoader, module ) -> 
                              builder.method(ElementMatchers.any()).intercept( //how to write ?)

).installOnByteBuddyAgent();

THKS!

您可以将各个部分放在一起:

new AgentBuilder.Default()
  .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
  .with(RedefinitionStrategy.RETRANSFORMATION)
  .type(ElementMatchers.is(Foo.class))
  .transform((builder, typeDescription, classLoader, module) -> 
    new ByteBuddy().redefine(Bar.class).name(Foo.class.getName()))
  .installOnByteBuddyAgent();

但是,这仅在替换类型兼容形状时才有效。

暂无
暂无

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

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