为什么 package-info.java 有用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22095487/
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-08-13 13:34:28  来源:igfitidea点击:

Why is package-info.java useful?

javamavencheckstyle

提问by Socrates

When I run CheckStyle over my Java project it says Missing package-info.java file.for some classes, but not all of them. I can't really figure out why this message appears only sometimes. Furthermore my project runs perfectly fine without the package-info.java.

当我在我的 Java 项目上运行 CheckStyle 时,它​​会Missing package-info.java file.显示某些类,但不是所有类。我真的不明白为什么这条消息只是有时出现。此外,我的项目在没有 package-info.java 的情况下运行得非常好。

What does the package-info.java do? Do I really need it for my Java projects?

package-info.java 有什么作用?我的 Java 项目真的需要它吗?

采纳答案by m-szalik

It is used to generate javadocs for a package.

它用于为包生成 javadoc。

/**
* Domain classes used to produce .....
* <p>
* These classes contain the ......
* </p>
*
* @since 1.0
* @author somebody
* @version 1.0
*/
package com.domain;

Will generate package info for com.domainpackage:

将为包生成包信息com.domain

Example result: https://docs.oracle.com/javase/7/docs/api/java/awt/package-summary.html

结果示例:https: //docs.oracle.com/javase/7/docs/api/java/awt/package-summary.html

回答by JB Nizet

A package-info.java file allows adding javadoc to document a whole package. See http://docs.oracle.com/javase/7/docs/api/java/applet/package-summary.htmlfor example.

package-info.java 文件允许添加 javadoc 来记录整个包。例如,请参见http://docs.oracle.com/javase/7/docs/api/java/applet/package-summary.html

If you don't care about missing package documentation, then ignore the warning or disable the JavadocPackage check.

如果您不关心缺少包文档,则忽略警告或禁用JavadocPackage check

回答by mdhirsch

Annotations

注释

Another good reason to use package-info.java is to add default annotationsfor use by FindBugs. For instance, if you put this in your package-info file:

使用 package-info.java 的另一个很好的理由是添加默认注释以供FindBugs使用。例如,如果你把它放在你的 package-info 文件中:

@DefaultAnnotation(NonNull.class)
package com.my.package;

then when findbugs runs on the code in that package, all methods and fields are assumed to be non-null unless you annotate them with @CheckForNull. This is much nicer and more foolproof than requiring developers to add @NonNullannotations to each method and field.

然后当 findbugs 在该包中的代码上运行时,所有方法和字段都被假定为非空,除非您使用@CheckForNull. 这比要求开发人员@NonNull为每个方法和字段添加注释要好得多,也更万无一失。

回答by Rene Mazala

Not only some findbugs annotations, but a lot of java annotations in common libraries have the java.lang.annotation.ElementType.PACKAGEtype as one of the possible values of their own java.lang.annotation.Targetannotation, e.g.:

不仅是一些 findbugs 注释,而且很多公共库中的 Java 注释都将java.lang.annotation.ElementType.PACKAGEtype 作为它们自己的java.lang.annotation.Target注释的可能值之一,例如:

com.google.gwt.core.client.js.JsNamespace
com.querydsl.core.annotations.Config
com.sun.xml.bind.XmlAccessorFactory
groovy.transform.BaseScript
java.lang.Deprecated
javax.annotation.Generated
javax.xml.bind.annotation.XmlAccessorOrder
org.hibernate.annotations.TypeDef
net.sf.ehcache.pool.sizeof.annotations.IgnoreSizeOf
org.apache.hive.common.HiveVersionAnnotation
org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeAction
org.codehaus.commons.nullanalysis.NotNullByDefault
org.eclipse.persistence.oxm.annotations.XmlNameTransformer
org.glassfish.jersey.Beta
org.jgroups.annotations.Experimental

and much more.

以及更多。

This package-info.javafile would be the file, where you can place such annotations (along with the javadoc).

package-info.java文件将是您可以在其中放置此类注释(以及 javadoc)的文件。

回答by subodhkarwa

The package-info.javais a Java file that can be added to any Java source package. It is used to provide info at a "package" level as per its name. It contains documentation and annotations used in the package.

package-info.java是可以添加到任何Java源代码包的Java文件。它用于根据其名称在“包”级别提供信息。它包含包中使用的文档和注释。

javadoc example is already provided in the answer, the below part explains how it works incase of annotations.

答案中已经提供了 javadoc 示例,以下部分解释了在注释的情况下它是如何工作的。

For example, in the below file it is used to "substitute" the occurance of joda.time.DateTime with org.jadira.usertype.dateandtime.joda.PersistentDateTime

例如,在下面的文件中,它用于“替换” joda.time.DateTime 与 org.jadira.usertype.dateandtime.joda.PersistentDateTime 的出现

@TypeDefs({
    @TypeDef(name = "PersistentDateTime", typeClass = PersistentDateTime.class, defaultForType=DateTime.class)})

package xyz.abc;

import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.jadira.usertype.dateandtime.joda.PersistentDateTime;
import org.joda.time.DateTime; 

There are a number of annotations available with which can be used to perform different things at "package" level. It can be found at https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/package-summary.html

有许多注释可用于在“包”级别执行不同的操作。它可以在https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/annotations/package-summary.html找到