【发布时间】:2020-12-22 07:10:56
【问题描述】:
我有一个建议类,它使用代理 jar 中的另一个类,但是这个其他类依赖于代理 jar 中不存在的类。代理 jar 中不存在的此类存在于应用程序类加载器上的另一个 jar 中。代理 jar 在系统类加载器上。所以我遇到了 NoClassDefFoundError。我已尝试按照this other post 中的建议使用 Transformer.ForAdvice,但这仅适用于建议类。
// In agent jar on system class loader
MyAdviceClass {
@Advice.OnMethodEnter
public static void onMethodEnter() {
YetAnotherClass.doSomething(); // works fine as Transformer.ForAdvice makes use of the correct class loader while inlining this code
AnotherClass.doSomething(); // NoClassDefFoundError happens inside this method call
}
}
// In agent jar on system class loader
public class AnotherClass {
public static void doSomething() {
YetAnotherClass.doSomething(); // NoClassDefFoundError happens here because the system class loader is used to load YetAnotherClass, but that class is only present on the application class loader
}
}
// In user jar on application class loader
public class YetAnotherClass {
public static void doSomething() {
// do something else
}
}
变换如下所示:
.transform(new AgentBuilder.Transformer
.ForAdvice()
.include(getClass().getClassLoader())
.advice(methodMatcher, "com.something.advice.MyAdvice.class"));
代理 jar 中存在 MyAdviceClass 和 AnotherClass。 YetAnotherClass 存在于用户 jar 中,它不在系统类加载器上,它在应用程序类加载器上。我怎么解决这个问题?即有没有一种方法可以强制在 AnotherClass 中使用应用程序类加载器?
【问题讨论】:
标签: byte-buddy