ByteBuddy - rebase已经加载了类

问题描述 投票:1回答:1

我在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
1个回答
0
投票

这是不可能的,不是因为Byte Buddy,而是在常规VM上不允许使用任何工具。 (存在所谓的动态代码演进VM)。

如果你想避免这个问题,请使用redefine而不是rebase。无论何时使用方法,现在都可以替换原始方法。

如果这是不可接受的,请查看Advice类,您可以使用.visit-API在原始代码周围包装逻辑而不替换它。

© www.soinside.com 2019 - 2024. All rights reserved.