如何从TypeDescription获取注释类

时间:2019-01-22 01:38:42

标签: byte-buddy

我正在尝试使用ByteBuddy。

在给定的TypeDescription下,如何获得注释类?

到目前为止,我发现了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;
}

实际上,我正在尝试通过某些带有特定注释的字段在编译的类中添加一些方法。

1 个答案:

答案 0 :(得分:0)

AnnotationListAnnotationDescription类型对象的列表,您可以在其中调用getAnnotationType方法以接收其注释类型的TypeDescription