Java注释顺序是否持久? [英] Is Java annotation order persistent?

查看:186
本文介绍了Java注释顺序是否持久?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java注释顺序是否在运行时持久存在?我检查了OpenJDK 1.7.0_21 - 它保留了注释顺序。我可以期待所有Java VM上的持久性吗?

Is Java annotation order persistent at runtime? I've checked OpenJDK 1.7.0_21 - it preserves annotations order. Can I expect that persistence on all java VMs?

推荐答案

取决于你所说的持久性。我想你可能意味着这个问题隐含着什么,所以这里有几个Q& A:

Depends on what you mean by "persistent". I think you might have meant something implied in the question, so here's few Q&A:

注释顺序是否持久?

是的,它是在 .class 文件中以不变的顺序写的。

Is the annotation order persistent?
Yes, it's written in the .class file in an order that doesn't change.

.class 文件中的注释顺序是否反映了源代码中的注释顺序?

是。如果你编译你的代码......

Does the annotations order in the .class file reflect the annotations order in the source code?
Yes. If you compile your code...

@Column(length = 256)
@NotBlankConstraint(message = "The application title must be set.")
@Size(min = 1, max = 256, message = "The application title must be set and not longer than 250 characters.")
private String title;

并查看 javap -v

 #67 = Utf8               Ljavax/validation/constraints/Size;
...
private java.lang.String title;
  descriptor: Ljava/lang/String;
  flags: ACC_PRIVATE
  RuntimeVisibleAnnotations:
    0: #50(#62=I#63)
    1: #64(#65=s#66)
    2: #67(#68=I#69,#70=I#63,#65=s#71

如果更改注释的顺序,字节代码中的顺序也会改变。使用OpenJDK 1.8.0_121进行测试,但我认为早期的JDK也是如此。

If you change the order of the annotations, the order in the byte code will change as well. Tested with OpenJDK 1.8.0_121, but I think that's the case for earlier JDKs too.

字节码的顺序是由 getAnnotations()方法保存的吗?

是的,来自我可以看到,它始终返回与 .class 文件中的字节代码相同的顺序。

Is the order from the bytecode kept by the getAnnotations() method?
Yes, from what I can see, it is consistently returning the same order as is in the byte code in the .class file.

getAnnotations()保证在将来反映字节码注释顺序吗?

No.作为回复者其他答案,这种行为没有记录,因此不能在公开的库中依赖。

但是,有一个以某种方式保存订单的微妙迹象。

Is getAnnotations() guaranteed to reflect the bytecode annotations order in the future?
No. As replied by the other answer, this behavior is not documented, so can't be relied on in a publicly available library.
However, there are subtle signs that the order should be kept somehow.

现在达到你的目的,
如果有保证,我是否应该使用该订单将某些注释应用于之前的某些注释?

这是一个品味问题,但我会说大多数Java程序员都不喜欢这样。它不会是直观和可读的代码。我建议将注释分组,例如

Now to your purpose, If it was guaranteed, should I use the order to have some annotation apply to some previous annotations?
That's a matter of taste, but I would say that most Java programmers wouldn't like that. It wouldn't be much intuitive and readable code. I would suggest to group the annotations such like

@ValidationGroup(
     validators = { @NotNull, @NotEmpty },
     message = "User name must be filled."
)
public String getUsername(); 

这个问题是你不能有一个任何注释数组,因为注释没有继承,这是不可能完成的(Java 1.8)。所以各种框架都采用了Bean Validation所使用的 - 看它的组概念。

The problem with this is that you can't have an array of "any annotation", and since there's no inheritance for annotations, this can't be done (Java 1.8). So various frameworks resort to what Bean Validation uses - see it's concept of groups.

最后,我的第二个是Eric的评论,检查JSR 303,它做得很好,还集成到许多其他库中,如RestEasy。

Lastly, I'd second to Eric's comment to check JSR 303, it's quite well done and also integrated into many other libraries like RestEasy.

这篇关于Java注释顺序是否持久?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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