How can I get Annotation class from TypeDescription

守給你的承諾、 提交于 2019-12-13 03:57:59

问题


I'm trying to work with ByteBuddy.

How, with given TypeDescription, can I get annotation class?

So far I found getActualName.

@Override
public boolean matches(final TypeDescription target) {
    //System.out.printf("matches(%1$s)\n", target);
    //System.out.printf("\tcanonicalName: %1$s\n", target.getCanonicalName());
    //System.out.printf("\tcomponentType: %1$s\n", target.getComponentType());
    {
        final AnnotationList inheritedAnnotations = target.getInheritedAnnotations();
        inheritedAnnotations.forEach(v -> {
            //System.out.printf("\tinherited annotationDescriptor: %1$s\n", v);
        });
    }
    {
        final AnnotationList declaredAnnotations = target.getDeclaredAnnotations();
        declaredAnnotations.forEach(v -> {
            //System.out.printf("\tdeclared annotationDescriptor: %1$s\n", v);
        });
    }
    {
        final FieldList<FieldDescription.InDefinedShape> declaredFields = target.getDeclaredFields();
        declaredFields.forEach(declaredFiled -> {
            System.out.println("declaredField: " + declaredFiled);
            final AnnotationList declaredAnnotations = declaredFiled.getDeclaredAnnotations();
            declaredAnnotations.forEach(declaredAnnotation -> {
                System.out.println("declaredAnnotation: {}" + declaredAnnotation);
                final Set<ElementType> elementTypes = declaredAnnotation.getElementTypes();
                System.out.println("\telementTypes: " + elementTypes);
                final TypeDescription annotationType = declaredAnnotation.getAnnotationType();
                System.out.println("\tannotationType: " + annotationType);
                System.out.println("\tannotationType.actualName: " + annotationType.getActualName());
            });
        });
    }
    return false;
}

I'm, actually, trying to add some methods on compiled classes by some fields annotated with specific annotation.


回答1:


An AnnotationList is a list of AnnotationDescription typed object where you can invoke the getAnnotationType method to receive a TypeDescription of their annotation type.



来源:https://stackoverflow.com/questions/54300074/how-can-i-get-annotation-class-from-typedescription

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!