Javassist注解问题 [英] Javassist annoations problem

查看:52
本文介绍了Javassist注解问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 javassist 生成我的实体类.一切都很顺利,直到我将 GeneratedValue 注释添加到 Id 字段.@Id 注释工作正常,但是当我添加 @GeneeratedValue 时出现异常.这是我的代码:

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();

这是我得到的例外:

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.

而不是注释值.但是对于 Id 注释,它显示了一个 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...

当使用构造函数 Annotation(ConstPool cp, CtClass clazz) 时,javassist 会预先创建该 Annotation Class 的所有成员(参见 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 Enums are not supported now."(第 101 行),正如您在 javax.persistence.GeneratedValue 中看到的,有一个名为 strategy 的 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.

虽然如果 Annotation 类有任何类型为 class 的成员,它也不会工作,导致子类的 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.

解决方案或变通方法是您所做的,使用另一个构造函数,该构造函数让成员手动添加,或者(不认为这是一个好的选择)为 Annotation 中的每个类成员设置一个实例.

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注解问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆