问题描述
我试图用了Javassist生成我的实体类。一切都很顺利,直到我加入了GeneratedValue注释ID字段。 @Id注释工作正常,但是当我加入@GeneeratedValue我得到一个异常。这是我的code:
I'm trying to generate 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 is the 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)
这似乎是与@GeneratedValue一个问题。当我使用它独自ID内部消除我要么得到此异常。当我使用Eclipse调试器来观看变量的值,我得到得到这个
It seems to be a problem with @GeneratedValue. When I use it alone whithout id I get this exception either. When I use eclipse debugger to watch variable values, I get get this
com.sun.jdi.InvocationException occurred invoking method.
而不是注释值。但对于标识标注就说明了Javassist注释对象。
instead of the annotation value. but for Id annotation it shows a javassist annotation object.
我是新来了Javassist。谁能帮我?
I'm new to javassist. Can anyone help me?
推荐答案
我猜你不是在找什么发生了(我有同样的问题今天),但如果你这样做......
I guess you're not looking for what happened anymore (I had the same problem today), but if you do...
在使用构造注释(ConstPool CP,CtClass clazz所)Javassist是pre-会为注记类所有成员(见Annotation.java,第98行)。
When using constructor Annotation(ConstPool cp, CtClass clazz) javassist pre-creates all members for that Annotation Class (see Annotation.java, line 98).
在这种情况下,很容易因为有一个明确的评论:// TODO枚举不支持现在 (线101),正如你可以看到javax.persistence.GeneratedValue有一个叫类型GenerationType这是一个枚举的战略成员。
In this case it's 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 strategy of type GenerationType which is an Enum.
不过,如果注解类有型类的任何成员,它不会工作,在子类的MemberValue.write方法引起一个NullPointerException异常。
Though if the Annotation class have any Members of type class it won't work, causing a NullPointerException on the MemberValue.write method of descendant classes.
解决方案或解决方法是你做了什么,用另一个构造留下手动添加成员,或(不认为这是一个不错的选择),设置在每个注释类成员的实例。
The solution or workaround is what you have done, using another constructor that leaves the members to be manually added, or (don't think this is a good option) set an instance for each class member in Annotation.
PS:我使用了Javassist 3.12.1.GA
PS: I'm using javassist 3.12.1.GA
这篇关于Javassist进行annoations问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!