我想重新定义已在现有框架中定义的Source类。我想用我的增强类自动替换Source类的原始实例。我不能访问创建Source对象的代码,所以类替换必须在幕后自动执行。有没有可能使用byte-buddy来做这件事呢?
ByteBuddyAgent.install();
Source source = new ByteBuddy()
.subclass(Source.class)
.method(named("hello")).intercept(MethodDelegation.to(Target.class))
.defineMethod("myNewMethod", void.class).intercept(MethodDelegation.to(Target.class))
.make()
.load(Source.class.getClassLoader(),
ClassReloadingStrategy.fromInstalledAgent())
.getLoaded()
.newInstance();发布于 2017-03-15 15:14:21
可以使用Byte Buddy重新定义类。为此,您将使用ByteBuddy::redefine或ByteBuddy::rebase方法,而不是子类化。使用这些特性的最规范方法是定义一个Java Agent,用于您可以使用AgentBuilder的内容。
https://stackoverflow.com/questions/42787286
复制相似问题