简体   繁体   English

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

[英]How to make ByteBuddy created sub-interface inherit type annotations?

I have this pseudo-test to create a sub-interface and I want to copy annotations from interface S but it doesn't work. 我有这个伪测试来创建子接口,并且我想从接口S复制注释,但是它不起作用。 What am I doing wrong? 我究竟做错了什么?

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> {
    }

}

Replace the 更换

attribute(TypeAttributeAppender.ForInstrumentedType.INSTANCE)

command by 命令

annotateType(S.class.getDeclaredAnnotations())

The attribute appender only applies the annotations that are directly declared by the instrumented type. 属性附加器仅应用由检测类型直接声明的注释。 In case of creating an interface with potentially mutliple super-interfaces, copying annotations is not possible as this can cause conflicts if multiple interfaces declare the same annotations. 如果使用潜在的多个超级接口创建接口,则无法复制注释,因为如果多个接口声明相同的注释,则可能导致冲突。 Please refer to the documentation of TypeAttributeAppender.ForInstrumentedType for a detail description of the attribute appender. 请参阅TypeAttributeAppender.ForInstrumentedType文档以获取有关属性附加器的详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM