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

查看:21
本文介绍了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?

推荐答案

取决于您所说的持久"是什么意思.我认为您可能在问题中暗示了某些内容,因此这里有一些问答:

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 文件.

.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() 是否保证在未来反映字节码注释顺序?
不.正如另一个答案所回答的那样,此行为未记录在案,因此不能依赖于公开可用的库.
但是,有 应该以某种方式保留订单的微妙迹象.

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天全站免登陆