Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 788
Vince Avatar asked Nov 12 '12 13:11

Vince


People also ask

Can I override valueOf enum Java?

Since you cannot override valueOf method you have to define a custom method ( getEnum in the sample code below) which returns the value that you need and change your client to use this method instead. getEnum could be shortened if you do the following: v. getValue().

What is the purpose of values () method in enum?

The valueOf() method returns the value of given constant enum.

How do you override an enum?

You can use the "new" keyword to override the field you defined in the base class. I used this, and it says it hides the inherited member.

Can we override enum valueOf?

By default, the enum value is its method name. You can however override it, for example if you want to store enums as integers in a database, instead of using their method name.


1 Answers

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.

like image 153
Marko Topolnik Avatar answered Oct 13 '22 02:10

Marko Topolnik