如何使用Byte-Buddy从接口构建Java类的具体实现?
作者:互联网
我有一个界面,可以说它看起来像这样.
public interface TestObject {
String getString();
Long getLong();
}
我实际上想使用ByteBuddy构建该对象的具体实现.
这是我尝试过的.
public class Runme {
public static void main(String[] args) {
ByteBuddy bb = new ByteBuddy();
Class<?> clz = bb
.subclass(TestObject.class)
.method(any()).intercept(MethodDelegation.to(Interceptor.class))
.make()
.load(Object.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
try {
Object test = clz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public class Interceptor {
public Object intercept(@Origin String method, @AllArguments Object[] args) throws Throwable {
System.out.println("I have intercepted a call");
return "Hello";
}
}
}
我得到这个错误
Exception in thread "main" java.lang.IllegalArgumentException: None of [] allows for delegation from public boolean java.lang.Object.equals(java.lang.Object)
at net.bytebuddy.implementation.bind.MethodDelegationBinder$Processor.process(MethodDelegationBinder.java:881)
at net.bytebuddy.implementation.MethodDelegation$Appender.apply(MethodDelegation.java:1218)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod$WithBody.applyBody(TypeWriter.java:510)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod.apply(TypeWriter.java:444)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:3193)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1481)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:234)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$AbstractDelegatingBuilder.make(DynamicType.java:2177)
at com.meta.testbytebuddy.Runme.main(Runme.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
我在做什么错..如果我想实现多个接口怎么办?
我正在使用ByteBuddy进行代码生成.
解决方法:
问题在于,您是通过MethodDelegation.to(Interceptor.class)委托给拦截器的静态方法的,而拦截器类仅声明了非静态成员.您可以将拦截器方法声明为静态方法,也可以委托给实例而不是类.我认为前一种方法会更合适.
但是,仅此一点仍然行不通.您正在从拦截器返回一个Object类型,与此同时,您正在拦截any()方法.这包括您的TestObject接口的方法,还包括由Object(隐式超类)声明的方法.您可以按以下方式定义拦截器,以使您的类得以编译,但是Byte Buddy会将返回类型转换为每个方法的返回类型,这将导致ClassCastException:
public class Interceptor {
@RuntimeType
public static Object intercept(@Origin String method, @AllArguments Object[] args) {
System.out.println("I have intercepted a call");
return "Hello";
}
}
标签:byte-buddy,java,bytecode 来源: https://codeday.me/bug/20191027/1944936.html