我运行以下代码:

    CfField f = ...
    CtClass classeEnglobante = f.getDeclaringClass();
    ClassPool pool = classeEnglobante.getClassPool();
    ConstPool constPool = classeEnglobante.getClassFile().getConstPool();

    AnnotationsAttribute attr = new AnnotationsAttribute(constPool , AnnotationsAttribute.visibleTag);
    Annotation a = new Annotation(constPool, pool.get("org.hibernate.annotations.Index"));
    a.addMemberValue("name", new StringMemberValue("idx_" + p.getNomMinuscule(), constPool));
    attr.addAnnotation(a); // Here is the line 245


NPE提出了:

java.lang.NullPointerException
    在javassist.bytecode.annotation.ArrayMemberValue.write(ArrayMemberValue.java:132)
    在javassist.bytecode.annotation.Annotation.write(Annotation.java:317)
    在javassist.bytecode.AnnotationsAttribute.setAnnotations(AnnotationsAttribute.java:246)
    在com.mycompany.MyClass(MyClass.java:245)

最佳答案

This question解决了我的问题。
由于某种原因,javassist 3.1.2.GA中存在一个错误。
所以这是我的错误及其解决方案:

错误:容易出错

Annotation a = new Annotation(constPool, pool.get("org.hibernate.annotations.Index"));


正确:无错误

Annotation a = new Annotation("org.hibernate.annotations.Index", constPool);

09-06 23:33