Can I redefine private methods using Byte Buddy?

北慕城南 提交于 2019-12-07 15:33:13

问题


Is it possible to use Byte Buddy to redefine a private method of a class? It seems that the entry point into using Byte Buddy is always sub-classing an existing class. When doing this, it is obviously not possible to redefine a private method of the parent class (at least not in a way that the redefined method is used in the parent class).

Consider the following example:

public class Foo {
    public void sayHello() {
        System.out.println(getHello());
    }

    private String getHello() {
        return "Hello World!";
    }
}

Foo foo = new ByteBuddy()
    .subclass(Foo.class)
    .method(named("getHello")).intercept(FixedValue.value("Byte Buddy!"))
    .make()
    .load(Main.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
    .getLoaded()
    .newInstance();
foo.sayHello();

The output will be "Hello World!". Is there any chance to get "Byte Buddy!" as output?


回答1:


You are correct that subclassing is the currently only option for creating classes with Byte Buddy. However, starting with version 0.3 which is released in the next weeks, this will change such that you can also redefine existing classes. A class redefition would then look like this:

ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy
                                                   .fromInstalledAgent();
new ByteBuddy()
  .redefine(Foo.class)
  .method(named("getHello"))
  .intercept(FixedValue.value("Byte Buddy!"))
  .make()
  .load(Foo.class.getClassLoader(), classReloadingStrategy);
assertThat(foo.getHello(), is("Byte Buddy!"));
classReloadingStrategy.reset(Foo.class);
assertThat(foo.getHello(), is("Hello World"));

This approach makes use of HotSpot's HotSwap mechanism which is very limited as you cannot add methods or fields. With Byte Buddy version 0.4, Byte Buddy will be able to redefine unloaded classes and provide an agent builder for implementing custom Java Agents to make this sort of redefinition more flexible.



来源:https://stackoverflow.com/questions/25795109/can-i-redefine-private-methods-using-byte-buddy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!