Javassist annotation issue

I am trying to create my Entity class using javassist. Everything went well until I added the GeneratedValue annotation to the Id field. The @Id annotation works fine, but when I add @GeneeratedValue, I get an exception. This is my code:

ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.makeClass("test.Snake"); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); Annotation idAnnotation = new Annotation(classFile.getConstPool(), ClassPool.getDefault().get("javax.persistence.Id")); attribute.addAnnotation(idAnnotation); Annotation gvAnnotation = new Annotation(classFile.getConstPool(), ClassPool.getDefault().get("javax.persistence.GeneratedValue")); attribute.addAnnotation(gvAnnotation); CtField idField = new CtField(ClassPool.getDefault().get("java.lang.Long"), "id", ctClass); idField.getFieldInfo().addAttribute(attribute); ctClass.addField(idField); CtField nameField = new CtField(ClassPool.getDefault().get("java.lang.String"), "name", ctClass); ctClass.addField(nameField); AnnotationsAttribute attr = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); Annotation annotation = new Annotation(classFile.getConstPool(), ClassPool.getDefault().get("javax.persistence.Entity")); attr.addAnnotation(annotation); classFile.addAttribute(attr); snakeClass = ctClass.toClass(); newInstance = snakeClass.newInstance(); 

And this exception I get:

 java.lang.NullPointerException at javassist.bytecode.ConstPool.getUtf8Info(ConstPool.java:565) at javassist.bytecode.annotation.EnumMemberValue.getValue(EnumMemberValue.java:98) at javassist.bytecode.annotation.EnumMemberValue.write(EnumMemberValue.java:116) at javassist.bytecode.annotation.Annotation.write(Annotation.java:316) at javassist.bytecode.AnnotationsAttribute.setAnnotations(AnnotationsAttribute.java:246) at javassist.bytecode.AnnotationsAttribute.addAnnotation(AnnotationsAttribute.java:211) at ClassLoadingTest.javassitTest(ClassLoadingTest.java:56) 

There seems to be a problem with @GeneratedValue. When I use this separately, I get this exception too. When I use the eclipse debugger to view variable values, I get this

 com.sun.jdi.InvocationException occurred invoking method. 

instead of annotation value. but for Id annotation, it shows javassist annotation object.

I am new to javassist. Can anybody help me?

+4
source share
1 answer

I think you are no longer looking for what has already happened (today I had the same problem), but if you do this ...

When using the Annotation constructor (ConstPool cp, CtClass clazz), javassist pre-creates all the elements for this annotation class (see Annotation.java, line 98).

In this case, this is easy because there is an explicit comment: "// todo Enums are not supported right now." (line 101), and as you can see in javax.persistence.GeneratedValue, there is a member called a strategy of type GenerationType, which is Enum.

Although if the Annotation class has members of a type class, it will not work, throwing a NullPointerException in the MemberValue.write method of the descendant classes.

A solution or workaround is what you did using a different constructor that leaves the members to be added manually, or (I don't think this is a good option) sets an instance for each member of the class in Annotation.

PS: I am using javassist 3.12.1.GA

+5
source

Source: https://habr.com/ru/post/1312635/


All Articles