@documented注释在java中

pra*_*jvs 59 java eclipse annotations

@Documentedjava 中注释的目的是什么?

我看到了文档,但从中得不到多少.有人可以借助一个明确的例子指出

Adr*_*Cox 54

@Documented是一个元注释.您@Documented在定义注释时应用,以确保使用注释的类在其生成的JavaDoc中显示此内容.我没有看到太多使用它,但这里有一个例子.之前的一个问题表明它在Eclipse中不能自动运行,但是我已经在Eclipse 3.6中进行了测试,并且我的注释出现在JavaDoc弹出窗口中,无论我是否将@Documented注释附加到它们.

这是Spring的一个例子,它确保事务方法在JavaDoc中标记为:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {
Run Code Online (Sandbox Code Playgroud)


Gan*_*nus 35

如果我们的注释(例如@InWork)是@Documented,那么对于具有该@InWork注释的每个类,由javadoc生成的文本将包含@InWork文本,作为对注释的引用.

注解:

@Documented
@Inherited  // for descenders of the annotation to have the @Documented feature automatically
@Retention(RetentionPolicy.RUNTIME) // must be there
public @interface InWork {
    String value();
}
Run Code Online (Sandbox Code Playgroud)

注释目标:

/**
 * Annotated class.
 */
@InWork(value = "")
public class MainApp {...}
Run Code Online (Sandbox Code Playgroud)

javadoc文本:

在此输入图像描述

因此,您必须决定是否应在javadoc文本中显示注释,如果是,则设置@Documented为它.

以上信息摘自Oracle文档.


请注意,在Eclipse中,您将在javadoc生成的文本中看到所有注释,无论是否@Documented.

4.3版仍然是正确的.


kdo*_*pen 5

我在Java 教程中发现了一个有用的页面,其中提供了许多标准注释的示例和更多解释,包括@Documented. 具体来说,请查看底部的注释块,了解序言示例(文档部分)。