gpt4 book ai didi

java - 字节好友导致 IncompatibleClassChangeError

转载 作者:行者123 更新时间:2023-11-30 08:57:31 25 4
gpt4 key购买 nike

我使用 Byte Buddy (v0.5.2) 动态创建一个接口(interface)的“子类”(实际上,我想创建一个实现该接口(interface)的类)。在此类实例上调用的所有方法都应重定向到另一个(拦截器)类。我使用了以下代码(“TestInterface”是一个只声明一个方法“sayHello”的接口(interface)):

final Interceptor interceptor = new Interceptor();
Class<?> clazz = new ByteBuddy()
.subclass(TestInterface.class)
.method(any()).intercept(MethodDelegation.to(interceptor))
.make()
.load(TestInterface.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
TestInterface instance = (TestInterface) clazz.newInstance();
instance.sayHello();

拦截器类如下所示:

public class Interceptor {

public Object intercept(@Origin MethodHandle method, @AllArguments Object[] args) throws Throwable {
...
}

}

但是,当我尝试调用“sayHello”方法(我的代码示例的最后一行)时,我得到一个“IncompatibleClassChangeError”。堆栈跟踪如下:

Exception in thread "main" java.lang.IllegalAccessError: no such method: byteuddytest.TestInterface.sayHello()void/invokeVirtual
at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:448)
at bytebuddytest.TestInterface$ByteBuddy$0E9xusGs.sayHello(Unknown Source)
at bytebuddytest.Main.main(Main.java:32)
Caused by: java.lang.IncompatibleClassChangeError: Found interface bytebuddytest.TestInterface, but class was expected
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:965)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1387)
at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1732)
at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:442)
... 2 more

问题似乎与我的拦截器方法中“MethodHandle”参数的使用有关。当我将类型更改为“方法”时,一切正常。但是根据文档,由于性能原因,“MethodHandle”应该比“Method”更受欢迎。

错误是由 Byte Buddy 中的错误引起的,还是我应该在这种情况下实际使用“方法”参数?

最佳答案

使用方法 参数并启用缓存。这应该可以解决您的大部分性能问题,如果您首先遇到任何问题的话。

参见 javadoc对于 @Origin:

public abstract boolean cacheMethod

If this value is set to true and the annotated parameter is a Method type, the value that is assigned to this parameter is cached in a static field. Otherwise, the instance is looked up from its defining Class on every invocation of the intercepted method.

Method look-ups are normally cached by its defining Class what makes a repeated look-up of a method little expensive. However, because Method instances are mutable by their AccessibleObject contact, any looked-up instance needs to be copied by its defining Class before exposing it. This can cause performance deficits when a method is for example called repeatedly in a loop. By enabling the method cache, this performance penalty can be avoided by caching a single Method instance for any intercepted method as a static field in the instrumented type.

关于java - 字节好友导致 IncompatibleClassChangeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28099696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com