简体   繁体   English

字段上的Java注释数量是否有限制?

[英]Is there a limit on the number of Java annotations on a field?

Is there a limit on the number of Java annotations on a field? 字段上的Java注释数量是否有限制? I couldn't find any information on that point. 我找不到关于这一点的任何信息。

No, there is nothing about a limit in the spec. 不,规范中没有限制。

See here: http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf 参见此处: http : //docs.oracle.com/javase/specs/jls/se7/jls7.pdf

The only limit you encounter for java files in general are os-wide limits like max. 通常,您遇到的Java文件的唯一限制是操作系统范围的限制,例如max。 file size on some filesystems or - in hardcore cases - the disk size. 某些文件系统上的文件大小,或者-在硬盘情况下-磁盘大小。 Some systems might have problems compiling or running a 100gb .java/.class file... 某些系统可能在编译或运行100gb .java / .class文件时遇到问题...

Do as you please. 随便你。 It's just that it's probably bad design. 只是它可能是错误的设计。 Keeping it bellow 10 is a good rule of thumb, I'd say. 我会说,将其保持在10以下是一个很好的经验法则。

Edit: It seems there is, after all, a limit. 编辑:毕竟似乎有一个限制。 At least on runtime annotations on methods. 至少在方法的运行时注释上。

Note that a maximum of 65535 runtime-visible Java programming language annotations may be directly attached to a program element. 注意,最多65535个运行时可见的Java编程语言注释可以直接附加到program元素。

Not sure if this counts for fields too. 不知道这是否也适用于字段。

So, I guess the answer depends on the kind of annotations you want to add. 因此,我猜答案取决于您要添加的注释的类型。

Nothing about that in the language spec though. 不过,在语言规范中,这没什么。 I guess they didn't really think someone would try it. 我猜他们并不真的认为有人会尝试。

Well here is a limit from the class file format specification . 好吧,这是类文件格式规范的限制。

4.7.18. 4.7.18。 The RuntimeVisibleParameterAnnotations attribute RuntimeVisibleParameterAnnotations属性

The RuntimeVisibleParameterAnnotations attribute is a variable-length attribute in the attributes table of the method_info (§4.6) structure. RuntimeVisibleParameterAnnotations属性是method_info(第4.6节)结构的属性表中的可变长度属性。 The RuntimeVisibleParameterAnnotations attribute records runtime-visible Java programming language annotations on the parameters of the corresponding method. RuntimeVisibleParameterAnnotations属性在相应方法的参数上记录运行时可见的Java编程语言注释。

Note that a maximum of 65535 runtime-visible Java programming language annotations may be directly attached to a program element. 注意,最多65535个运行时可见的Java编程语言注释可以直接附加到program元素。

Edit 编辑

The link above also mentions RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations for classes, methods and elements - the limit on each is again 65535. The 65535 is also the max value that the structure can contain (unsigned 16 bit integer), you will likely run into other limitations long before you hit that number. 上面的链接还提到了类,方法和元素的RuntimeVisibleAnnotations和RuntimeInvisibleAnnotations-每个元素的限制再次为65535。65535也是该结构可以包含的最大值(无符号16位整数),您很可能会遇到其他限制在您拨打该号码之前。

I suspect the limit is higher than the numebr of annotations you have (you can only add them once) or would be likely to want to add. 我怀疑此限制高于您拥有的注释数量(您只能添加一次),或者可能要添加。

If you every wanted to add more data, you can value arrays of annotations as values of an annotation, but including rich information in Strings is a common way to add more information eg in JSon format. 如果每个人都想添加更多数据,则可以将批注数组的值作为批注的值,但是在Strings中包含丰富的信息是添加更多信息(例如JSon格式)的常用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM