【发布时间】:2017-12-18 22:57:20
【问题描述】:
我有以下代码在 SpringBoot 应用程序中运行,它可以满足我的期望。
TypePool typePool = TypePool.Default.ofClassPath();
ByteBuddyAgent.install();
new ByteBuddy()
.rebase(typePool.describe("com.foo.Bar").resolve(), ClassFileLocator.ForClassLoader.ofClassPath())
.implement(typePool.describe("com.foo.SomeInterface").resolve())
.make()
.load(ClassLoader.getSystemClassLoader());
它的作用是让 com.foo.Bar 类实现接口 com.foo.SomeInterface(它有一个默认实现)
我愿意。通过将类称为 Bar.class 来使用上面的代码,而不是使用名称的字符串表示。但如果我这样做,我会得到以下异常。
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change superclass or interfaces
我相信由于它会导致类在重新定义之前被加载。我现在正在学习使用 ByteBuddy。
我想通过添加接口和使用 ByteBuddy 的实现来避免运行时的一些反射。我还有一些其他代码可以检查这个接口。
【问题讨论】:
标签: byte-buddy