1

我运行以下代码:

    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)
4

1 回答 1

1

这个问题解决了我的问题。由于某种原因,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);
于 2011-08-09T17:01:52.550 回答