java 包注释有什么意义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:26:00  来源:igfitidea点击:

What's the point of package annotations?

javaannotations

提问by Gili

I understand the purpose of class annotations, thanks to How and where are Annotations used in Java?. What is the purpose of package annotations, as described in this blog postand §7.4.1 of the Java Language Specification?

由于Java 中如何以及在何处使用注释,我了解类注释的用途. 如本博客文章Java 语言规范 §7.4.1 中所述,包注释的目的是什么?

Why would you want to associate metadata with a package? What kinds of things could you do?

为什么要将元数据与包相关联?你可以做什么样的事情?

回答by viaclectic

  1. bnd tool(and maven-bundle-plugin) makes use of package annotations. Putting @Versionand @Export annotation in package-info.java allows it to generate OSGi metadata.
  2. javadoc uses package annotations.
  3. JAXB uses package-level annotations, for example, to specify mapping of a Java type to XML Schema type package-wide. Package annotations are also used in JBoss's xml binding.
  4. Struts 2 Convention plugin uses an annotationto specify a default interceptor for all actions in a package.
  5. There are some package-level Hibernate Annotations. An example of those annotations' usage can be found here.
  1. bnd 工具(和 maven-bundle-plugin)使用包注释。在 package-info.java 中放置@Version和 @Export 注释允许它生成 OSGi 元数据。
  2. javadoc 使用包注释。
  3. 例如,JAXB 使用包级别的注释来指定 Java 类型到包范围的 XML 模式类型的映射。JBoss 的 xml 绑定中也使用了包注释。
  4. Struts 2 约定插件使用注释为包中的所有操作指定默认拦截器。
  5. 有一些包级别的Hibernate Annotations。可以在此处找到这些注释用法的示例。

回答by Thilo

I suppose @Deprecatedwould make sense. And maybe something like @Generatedif the whole package was generated by some tool from non-Java source. Or @Internalif this package is not part of a public API.

我想@Deprecated应该是有道理的。也许就像@Generated整个包是由来自非 Java 源的某个工具生成的一样。或者,@Internal如果此包不是公共 API 的一部分。

Maybe OSGi tools (where you need to declare the versions of your packages, and the packages you depend on) could make use of this, too.

也许 OSGi 工具(您需要在其中声明包的版本以及依赖的包)也可以使用它。

Has anyone seen those in the wild?

有人在野外见过吗?

回答by Amir Moghimi

Two reasons that I can think of:

我能想到的两个原因:

  • Annotating special packages to let some aspects (for example using AspectJ) to weave the classes in them for specific functionality.
  • Annotating some packages that are to be read by some tools, for example for source, meta-data or other kinds of resource generation.
  • 注释特殊包以让某些方面(例如使用 AspectJ)为特定功能编织其中的类。
  • 注释一些工具要读取的包,例如源、元数据或其他类型的资源生成。

回答by Joachim Sauer

JAXB for example allows most annotationsthat are normally used on a type to be equally well applied to a package. The meaning in that case would be to specify the default for all classes in that package.

例如,JAXB 允许通常在类型上使用的大多数注释同样适用于包。这种情况下的含义是为该包中的所有类指定默认值。

For example, if you want all properties of all classes in a package that are exposed via getter/setters to be mapped in XML you could specify @XmlAccessorType(XMLAccessType.PROPERTY)on each class orsimply specify it on the package.

例如,如果您希望在 XML 中映射通过 getter/setter 公开的包中所有类的所有属性,您可以@XmlAccessorType(XMLAccessType.PROPERTY)在每个类上指定简单地在包上指定它。

回答by Pa?lo Ebermann

This is not the real purpose, but I'm using them as a workaround to avoid recompilation of the package-info.java files.

这不是真正的目的,但我将它们用作避免重新编译 package-info.java 文件的解决方法。

The problem is that javac(and the Ant task <javac>) creates no class file for the package-info.java if there is only documentation (the reason for their existence) and the package bla;statement, and that the ant task recompiles every file for which there is no (or an older) corresponding .classfile.

问题是javac(和 Ant 任务<javac>)如果只有文档(它们存在的原因)和package bla;语句,则不为 package-info.java 创建任何类文件,并且 ant 任务重新编译每个没有的文件(或较旧的)相应.class文件。

Adding a dummy annotation there (like SuppressWarnings) had the effect that a package-info.classis produced and thus the file is not recompiled until changed again.

在那里添加一个虚拟注释(如SuppressWarnings)会产生 a 的效果,package-info.class因此在再次更改之前不会重新编译文件。

(Ant 1.8.0 solved this by creating an empty package-info.class, even if there was no annotation, but I'm using an older anthere.)

(Ant 1.8.0 通过创建一个空的 package-info.class 解决了这个问题,即使没有注释,但我在ant这里使用的是旧的。)

回答by Will

Test metadata -- that is metadata around test packages (unit tests or otherwise). You can ascribe various pieces of test metadata that are appropriate at the package level, such as: features, owners, versions, bugs/issues, etc. These could be refined at the class or method level, but having package-level definitions or defaults could be handy for brevity. I've utilized a variant of this approach (before the benefit of annotations).

测试元数据——即围绕测试包(单元测试或其他)的元数据。您可以归因于适合包级别的各种测试元数据,例如:特性、所有者、版本、错误/问题等。这些可以在类或方法级别进行细化,但具有包级别的定义或默认值为简洁起见可能很方便。我已经使用了这种方法的一个变体(在注释的好处之前)。

A similar argument could be made for generalized code metadata around the same sorts of things: features, ownership, defects, historical information, etc.

对于围绕相同类型事物的通用代码元数据,可以提出类似的论点:特性、所有权、缺陷、历史信息等。