Java 如何使ByteBuddy创建的子接口继承类型注释?

Java 如何使ByteBuddy创建的子接口继承类型注释?,java,byte-buddy,Java,Byte Buddy,我有这个伪测试来创建一个子接口,我想从接口S复制注释,但它不起作用。我做错了什么 import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import net.bytebuddy.ByteBuddy; import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; import net.bytebuddy.implementation.

我有这个伪测试来创建一个子接口,我想从接口S复制注释,但它不起作用。我做错了什么

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
import net.bytebuddy.implementation.attribute.TypeAttributeAppender;
import net.bytebuddy.matcher.ElementMatchers;

import org.apache.tapestry5.ioc.annotations.EagerLoad;
import org.apache.tapestry5.ioc.annotations.NotLazy;

public class InheritedMethodTest {

    public static void main(String... args) throws Exception {
        Method im = I.class.getMethod("list");
        System.out.println(Arrays.toString(I.class.getAnnotations()));
        System.out.println(im.getDeclaringClass());
        System.out.println(im.getGenericReturnType());
        System.out.println(Arrays.toString(im.getAnnotations()));
        System.out.println();

        Method sm = S.class.getMethod("list");
        System.out.println(Arrays.toString(S.class.getAnnotations()));
        System.out.println(sm.getDeclaringClass());
        System.out.println(sm.getGenericReturnType());
        System.out.println(Arrays.toString(sm.getAnnotations()));
        System.out.println();

        Class<? extends Object> f = new ByteBuddy()
                .makeInterface(S.class)
                .attribute(TypeAttributeAppender.ForInstrumentedType.INSTANCE)
                .method(ElementMatchers.not(ElementMatchers
                        .isDeclaredBy(Object.class)))
                .withoutCode()
                .attribute(
                        MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER)
                .make()
                .load(InheritedMethodTest.class.getClassLoader(),
                        ClassLoadingStrategy.Default.INJECTION).getLoaded();

        Method fm = f.getMethod("list");
        System.out.println(Arrays.toString(f.getAnnotations()));
        System.out.println(fm.getDeclaringClass());
        System.out.println(fm.getGenericReturnType());
        System.out.println(Arrays.toString(fm.getAnnotations()));

        System.out.println(Arrays.toString(f.getInterfaces()));
    }

    public static interface I<A> {
        @NotLazy
        List<A> list();
    }

    @EagerLoad
    public static interface S extends I<String> {
    }

}
import java.lang.reflect.Method;
导入java.util.array;
导入java.util.List;
导入net.bytebuddy.bytebuddy;
导入net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
导入net.bytebuddy.implementation.attribute.MethodAttributeAppender;
导入net.bytebuddy.implementation.attribute.TypeAttributeAppender;
导入net.bytebuddy.matcher.ElementMatchers;
导入org.apache.tapestry5.ioc.annotations.Load;
导入org.apache.tapestry5.ioc.annotations.NotLazy;
公共类InheritedMethodTest{
公共静态void main(字符串…参数)引发异常{
方法im=I.class.getMethod(“列表”);
System.out.println(Arrays.toString(I.class.getAnnotations());
System.out.println(im.getDeclaringClass());
System.out.println(im.getGenericReturnType());
System.out.println(Arrays.toString(im.getAnnotations());
System.out.println();
方法sm=S.class.getMethod(“列表”);
System.out.println(Arrays.toString(S.class.getAnnotations());
System.out.println(sm.getDeclaringClass());
System.out.println(sm.getGenericReturnType());
System.out.println(Arrays.toString(sm.getAnnotations());
System.out.println();
类替换

attribute(TypeAttributeAppender.ForInstrumentedType.INSTANCE)
指挥

annotateType(S.class.getDeclaredAnnotations())
属性appender仅应用由插入指令的类型直接声明的注释。如果创建具有潜在多个超级接口的接口,则无法复制注释,因为如果多个接口声明相同的注释,这可能会导致冲突。请参阅
类型的文档属性appender.ForInstrumentedType查看属性appender的详细说明。

替换

attribute(TypeAttributeAppender.ForInstrumentedType.INSTANCE)
指挥

annotateType(S.class.getDeclaredAnnotations())

属性appender仅应用由插入指令的类型直接声明的注释。如果创建具有潜在多个超级接口的接口,则无法复制注释,因为如果多个接口声明相同的注释,这可能会导致冲突。请参阅
类型的文档AttributeAppender.ForInstrumentedType查看属性appender的详细说明。

您遇到了什么错误?或者到底是什么错误。此行
System.out.println(Arrays.toString(f.getAnnotations()));
应该返回一个注释的列表:@eangerload,它不知道您得到的是什么错误?或者到底是什么错误。这一行
System.out.println(Arrays.toString(f.getAnnotations());
应该返回一个注释的列表:@eangerload,它没有