开发者

Unexpected NullPointerException using Javassist

开发者 https://www.devze.com 2023-03-26 20:18 出处:网络
I run the following code : CfField f = ... CtClass classeEnglobante = f.getDeclaringClass(); ClassPool pool = classeEnglobante.getClassPool();

I run the following code :

    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

And this NPE is raised :

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


This question solves my problem. For some reason there's a bug in javassist 3.1.2.GA. So here is my error and the solution to it :

WRONG : Prone to bug

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

CORRECT : No bug

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

精彩评论

暂无评论...
验证码 换一张
取 消