簡體   English   中英

如何使ByteBuddy創建的子接口繼承類型注釋?

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

我有這個偽測試來創建子接口,並且我想從接口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> {
    }

}

更換

attribute(TypeAttributeAppender.ForInstrumentedType.INSTANCE)

命令

annotateType(S.class.getDeclaredAnnotations())

屬性附加器僅應用由檢測類型直接聲明的注釋。 如果使用潛在的多個超級接口創建接口,則無法復制注釋,因為如果多個接口聲明相同的注釋,則可能導致沖突。 請參閱TypeAttributeAppender.ForInstrumentedType文檔以獲取有關屬性附加器的詳細說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM