如何记录Java Record参数?

40

如何记录Java Record参数?我指的是最终成为构造函数参数和类字段的参数。

我尝试过:

/**
 * @param name the name of the animal
 * @param age the age of the animal
 */
public record Animal(String name, int age)
{
}

但是IntelliJ IDEA将@param标记为错误。我找不到关于它应该如何工作的在线示例。我找到的最接近的讨论是https://bugs.openjdk.java.net/browse/JDK-8225055
我在JDK中找到了一些unit tests,这似乎意味着这应该能够工作。也许这是一个IDE的错误?
我使用的是OpenJDK 14+36-1461,IDEA 2020.1。
我已经针对IDEA提交了bug report
2个回答

14

IntelliJ Bug / Missing Feature

使用内置的 JDK 工具javadoc,版本号为14-ea及以上,可以轻松地为record生成 Javadoc。

enter image description here

用于此操作的命令是\

/jdk-14.jdk/.../javadoc --release=14 --enable-preview .../src/main/java/.../CityRecord.java

因此,这肯定是IntelliJ中缺少的东西。(由于“添加Javadoc”选项也不包括组件。)

当然,从IntelliJ开发的角度来看,将工作重点放在此功能上作为预览功能也需要谨慎考虑。


更新:我可以确认这已经在IntelliJ的最新更新中修复了:

IntelliJ IDEA 2020.2.2 Preview (Community Edition)
Build #IC-202.7319.5, built on September 1, 2020
Runtime version: 11.0.8+10-b944.31 x86_64

1
更新 - 显然,提问者报告的错误已被跟踪器确认为已修复。 - Naman
1
更新,该漏洞似乎已在构建202.3267中修复,当前版本201.8538.31于7月7日发布,但Java文档仍无法在记录上工作。我将尝试使用2020.2 EAP来验证是否已修复。 - OctavioCega
不能说关于IntelliJ,但是在Eclipse中@param也不起作用。在Eclipse中起作用的是给每个参数/属性声明添加JavaDoc注释: record MyRecord { /** JavaDoc for a */ String a, ... } - undefined
适用于IntelliJ 2023 - undefined

1

您还可以覆盖生成的记录方法并提供关于它们的文档:

public record Animal(String name, int age) {

  /**
   * The name of the animal
   *
   * @return the name
   */
  public String name() {
    return name;
  }

  /**
   * Gets the age of the animal
   *
   * @return the age
   */
  public int age() {
    return age;
  }
}

请记住,这只是一个例子。如果您的getter方法的文档仅为@return 方法名称,那么添加javadoc除了可能是工作环境的要求外,几乎没有任何价值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接