ChatGPT解决这个技术问题 Extra ChatGPT

java中的@Documented注解

java中@Documented注解的目的是什么?

我看到了文档,但无法从中得到太多。有人可以在一个明确的例子的帮助下指出


C
Community

@Documented 是一个元注释。您在定义注释时应用 @Documented,以确保使用您的注释的类在其生成的 JavaDoc 中显示这一点。我没有看到太多使用它,但是there is an example here。较早的一个问题表明它是 doesn't work automatically in Eclipse,但我已经在 Eclipse 3.6 中进行了测试,并且无论我是否将 @Documented 注释附加到它们,我的注释都会出现在 JavaDoc 弹出窗口中。

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

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {

G
Gangnus

如果我们的某些注释(例如 @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();
}

标注目标:

/**
 * Annotated class.
 */
@InWork(value = "")
public class MainApp {...}

javadoc文本:

https://i.stack.imgur.com/QLm1F.png

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

以上信息取自 Oracle documentation

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

对于 4.3 版本仍然是正确的。


S
Sithsu

我在 Java Tutorials 中找到了一个有用的页面,它提供了一些标准注释的示例和更多解释,包括 @Documented 的一种用法。具体来说,请查看序言示例 (section Documentation) 底部的 Note 块。