如何使用Byte-Buddy从界面构建Java类的具体实现?

时间:2016-01-07 15:37:50

标签: java bytecode byte-buddy

我有一个界面,让我们说它看起来像这样。

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进行代码生成。

1 个答案:

答案 0 :(得分:4)

问题是你通过MethodDelegation.to(Interceptor.class)委托拦截器的静态方法,而拦截器类只声明了非静态成员。您可以将拦截器方法声明为static,也可以将其委托给实例而不是类。我认为前一种方法会更合适。

然而,仅这一点仍然无效。您正在从拦截器返回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";
  }
}