Overriding Javadoc comment on java.lang.Enum.values()

最后都变了- 提交于 2020-01-03 08:40:12

问题


I have a very specific problem about the method java.lang.Enum.values().

I would like to override its javadoc. Very precisely, the current javadoc for this is, after I created my own enum:

public static MyClass.MyEnum[] values()

  ... 
  This method may be used to iterate over the constants as follows:

    for (MyClass.MyEnum c : MyClass.MyEnum.values())
    System.out.println(c);

  Returns:
    ...

But in my company System.out calls are considered bad practice so I would like it not to be shown. My first try was to override values() but it is apparently not possible. Is there another way I can do this? Or is the only possibility to update the generated doc ?

I am also curious about why values() is not overridable. I read on other questions that "it is generated by the compiler". But can someone be more precise? It seems that it's generated from the enum's name, but it does not explain why.


回答1:


values is a static method and is not subject to overriding. You cannot provide your own method to replace the generated one, and this is by specification.

There is no standard mechanism to replace the Javadoc of a method whose source code you don't control, but you could probably mess around with either the build tool, or, if all else fails, the final Javadoc HTML.




回答2:


I don't think this is possible, but you could file a JDK issue and maybe provide an OpenJDK fix, if you like.




回答3:


From the Oracle Java Tutorials:

The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

So, the method values cannot be overridden, since it's a special method created by the compiler. The Eclipse IDE generates this error when you try to do so:

The enum (your enum) already defines the method values() implicitly



来源:https://stackoverflow.com/questions/13344780/overriding-javadoc-comment-on-java-lang-enum-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!