代码之家  ›  专栏  ›  技术社区  ›  Jin Kwon

如何从typedescription获取注释类

  •  0
  • Jin Kwon  · 技术社区  · 6 年前

    我在试着和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 回复  |  直到 6 年前
        1
  •  0
  •   Rafael Winterhalter    6 年前

    AnnotationList 是一个列表 AnnotationDescription 可在其中调用 getAnnotationType 接收的方法 TypeDescription 它们的注释类型。