小编use*_*211的帖子

CGLib - 创建一个带有一些字段的 bean 并在其上放置注释?

是否可以生成一个 bean 类,其中的字段用指定的注释进行注释?我知道可以创建 bean,但是注释呢...?我找不到任何关于它的信息,所以我对此有强烈的怀疑,唯一确定的方法就是在这里询问......

// 我发现了一些可能有用的东西...请验证此代码。它使用 javassist 功能。

    // pool creation
    ClassPool pool = ClassPool.getDefault();
    // extracting the class
    CtClass cc = pool.getCtClass(clazz.getName());
    // looking for the method to apply the annotation on
    CtField fieldDescriptor = cc.getDeclaredField(fieldName);
    // create the annotation
    ClassFile ccFile = cc.getClassFile();
    ConstPool constpool = ccFile.getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constpool,
            AnnotationsAttribute.visibleTag);

    Annotation annot = new Annotation("sample.PersonneName", constpool);
    annot.addMemberValue("name",
            new StringMemberValue("World!! (dynamic annotation)", ccFile.getConstPool()));
    attr.addAnnotation(annot);

    // add the annotation to the method descriptor
    fieldDescriptor.getFieldInfo().addAttribute(attr);
Run Code Online (Sandbox Code Playgroud)

它的问题是我不知道如何在新创建的类型上应用现有注释......有没有办法做到这一点?

java annotations cglib

1
推荐指数
1
解决办法
2123
查看次数

标签 统计

annotations ×1

cglib ×1

java ×1