简体   繁体   English

Javadoc:static 最终枚举参考的文档值

[英]Javadoc : document value of static final enum reference

Let's say I have:假设我有:

public enum Color {
    RED,
    GREEN,
    YELLOW
}

Then elsewhere in my code I have然后在我的代码的其他地方我有

public static final Color DEFAULT_COLOR = Color.RED;

Now I would like to document to readers of my Javadoc what the value of DEFAULT_COLOR is (of course without repeating myself).现在我想向我的 Javadoc 的读者记录一下DEFAULT_COLOR的值是什么(当然,我不重复自己)。 How to do that?怎么做?

The problem being - as I see it - that such reference (although declared static final and pointing to an enum) will not show up in Javadoc's "constant-values.html".问题是 - 如我所见 - 这样的引用(虽然声明static final并指向枚举)不会出现在 Javadoc 的“constant-values.html”中。 I see no technical reason why it shouldn't but as far as I can tell it doesn't.我看不出它不应该的技术原因,但据我所知,它不应该。 Perhaps I've simply misunderstood?也许我只是误解了?

Refinement细化

To be precise the question is about static final declarations of an Enum variable where the right-hand-side is a single identifier as defined by the JLS, thus excluding cases where the RHS is a more complex expression.确切地说,问题是关于 Enum 变量的static final声明,其中右侧是 JLS 定义的单个标识符,因此不包括 RHS 是更复杂表达式的情况。 This is similar to the current Javadoc behavior for assignments of primitive types where Javadoc will not try to render if the RHS isn't a so-called 'constant expression'.这类似于当前用于原始类型分配的 Javadoc 行为,如果 RHS 不是所谓的“常量表达式”,Javadoc 将不会尝试呈现。 We can surely expect Javadoc to do the same can-I-render-this-unambigiusly?我们当然可以期待 Javadoc 做同样的事情,我可以毫无疑问地渲染这个吗? analysis for Enums, no?.枚举分析,不是吗? By saying the RHS must be a single identifier we limit ourselves to something which - IMO - should be unambiguously renderable for Javadoc.通过说 RHS 必须是单个标识符,我们将自己限制在 IMO 应该可以明确地为 Javadoc 呈现的东西。

As a comment already said, only if the value is a compile time constant does javadoc render it (and very few things are - true , false , number literals, string constants is where it starts. Operators where all operands are constants are also constant. Then any static final field that is initialized with such a constant is itself constant. Thus, static final int foo = SOME_OTHER_FIELD + YET_ANOTHER + 5; can be constant.正如评论已经说过的那样,只有当值是编译时常量时,javadoc才会渲染它(很少有东西是- truefalse ,数字文字,字符串常量是它开始的地方。所有操作数都是常量的运算符也是常量。那么任何用这样一个常量初始化的 static final 字段本身就是常量。因此, static final int foo = SOME_OTHER_FIELD + YET_ANOTHER + 5;可以是常量。

That means Color.RED isn't a constant and thus isn't shown.这意味着 Color.RED 不是常数,因此不会显示。

It's not just a matter of 'resolving things', it's a matter of rendering.这不仅仅是“解决问题”的问题,而是渲染的问题。

Imagine you wrote this:想象一下你写了这个:

private static final List<String> COUNTRIES = List.of(... all 300-or-so countries here_);

should the entire list be injected into the javadoc?是否应该将整个列表注入到 javadoc 中? Hopefully this example makes it clear that the answer isn't always 'yes', and it is not really feasible to draw a line.希望这个例子清楚地表明答案并不总是“是”,画一条线也不是真的可行。

Even if the answer is yes, how do you propose the javadoc renders this information?即使答案是肯定的,您如何建议 javadoc 呈现此信息? Just take the raw source code and dump it straight into the html?只需获取原始源代码并将其直接转储到 html 中? Take the raw source code, and auto-reformat it?获取原始源代码并自动重新格式化它? What other options exist?还有哪些其他选择?

Javadoc can't assume it can resolve the expression. Javadoc 不能假设它可以解析表达式。 Imagine the expression is:想象一下表达式是:

public static final Color DEFAULT_COLOR = Math.random() > 0.5 ? Color.RED : Color.BLUE;

That should make it clear that you have no feasible options to render non-CTCs in any way other than to show either the source with some level of cleanup applied, or nothing at all.这应该清楚地表明,除了显示应用了某种程度的清理的源或根本没有显示之外,您没有任何可行的选择来呈现非 CTC。

What you'd presumably want to see instead is either:您可能希望看到的是:

  • That the JVM spec gains the ability to treat enums as compile time constants (there is a marked difference; at the class level, constants are just stored verbatim, with their actual value, in the class file, whereas non-CTCs aren't stored; instead a static {} block is generated that generates these. For example, public static final long STAMP = System.currentTimeMillis(); is turned into a class file that has a static init 'method' that runs that code - you can't reduce that to a constan). JVM 规范获得了将枚举视为编译时间常量的能力(存在显着差异;在 class 级别,常量只是逐字存储,其实际值在 ZA2F2ED4F8EBTCAB6CBB4C21A2D 文件中存储,而非 CDC ; instead a static {} block is generated that generates these. For example, public static final long STAMP = System.currentTimeMillis(); is turned into a class file that has a static init 'method' that runs that code - you can' t 减少到一个常数)。 That's rather a big update to all of java just for the benefit of javadoc, which is weird.这是对所有 java 的相当大的更新,只是为了 javadoc,这很奇怪。
  • That the javadoc tool parts ways with the JVM spec and goes its own way on CTC. javadoc 工具与 JVM 规范分道扬镳,并在 CTC 上走自己的路。 This seems annoying.这似乎很烦人。 Surely you'd want public static final Color DEFAULT_COLOR = SomeOtherClass.DEFAULT_COLOR;你肯定想要public static final Color DEFAULT_COLOR = SomeOtherClass.DEFAULT_COLOR; to work just as well (it does if those were ints,), so that makes javadoc complicated and inconsistent.工作得一样好(如果它们是整数,它会),所以这使得javadoc变得复杂和不一致。 Just not worth it.只是不值得。
  • An option to tell javadoc to just take the source code of the initializer and render it verbatim (or with a light application of reformatting, perhaps) into the HTML.告诉 javadoc 只获取初始化程序的源代码并将其逐字呈现(或者可能使用重新格式化的轻应用程序)到 HTML 的选项。

That third one seems fair, something like:第三个似乎很公平,例如:

/** {@showDefault} */
public static final Color DEFAULT_COLOR = Color.RED;

but javadoc simply does not work that way.但是 javadoc 根本不能那样工作。

Okay, so how do I do this without repeating myself?好的,那么我该如何在不重复自己的情况下做到这一点?

You can't.你不能。 Sorry about that.对于那个很抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM