Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @value tag in javadoc?

I am using a class with private constructor instead of an enum (this is a requirement). And now I am trying to add javadoc tags to document each public static final entity.

1) What is prefered place to put javadoc tags: like ob1 or ob2?

2) Both options generate error in IDEA @value tag must reference field with a constant intializer.

/**  * {@value #ob1} object1 description  */  public class MyClass {     public static final Object ob1 = new Object();      /**      * {@value #ob2} object2 description      */      public static final Object ob2 = new Object();      private MyClass() {}    } 
like image 655
Nikolay Kuznetsov Avatar asked Nov 06 '13 09:11

Nikolay Kuznetsov


People also ask

How do you add a tag in Javadoc?

Go to Project > Generate Javadoc.. in Eclipse (Indigo in my case). You should notice that the "Extra Javadoc options.." text box has the value you must add for the Javadoc exporter to create the HTML equivalent of your tag.

Can you use HTML tags in Javadoc comments?

There are no real restrictions on the use of HTML in Javadoc comments. The Javadoc documentation states: Comments are written in HTML - The text must be written in HTML, in that they should use HTML entities and can use HTML tags.

How do you link a method in Javadoc?

Javadoc provides the @link inline tag for referencing the members in the Java classes. We can think of the @link tag as similar to the anchor tag in HTML, which is used to link one page to another via hyperlinks. Similar to the anchor tag, the path_to_member is the destination, and the label is the display text.

What is @since in Javadoc?

Specify the product version when the Java name was added to the API specification (if different from the implementation). For example, if a package, class, interface or member was added to the Java 2 Platform, Standard Edition, API Specification at version 1.2, use: /** * @since 1.2 */


2 Answers

I don't think Kayaman's answer is sufficient as the question is how to use the @value tag in javadocs.

I think the problem lies in the fact that the value of the field being referenced is not a literal value.

In eclipse, when you have

/**  * {@value #ob2} object2 description  */  public static final Object ob2 = new Object(); 

the generated Javadocs are {@value #ob2} object2 description. However, when you have

/**  * {@value #ob2} object2 description  */  public static final String ob2 = "hello"; 

the generated Javadocs are "hello" object2 description (the expected output).

So, in summary, you are using the @value tag correctly in the javadocs but the value will only be rendered correctly if the field has been initialised with a literal value.

like image 75
JamesB Avatar answered Sep 30 '22 20:09

JamesB


2) Both options generate error in IDEA @value tag must reference field with a constant intializer.

It does not make much sense to add non-constant expressions to the Javadoc.

At first, one might think that the most sensible behavior would be to add a toString to the Javadoc. But then, what happens if you had a mutable object like:

class MutableInteger {     public int i;     public String toString() { return Integer.toString(i); } } 

and a Javadoc like:

/**  * {@value #obj}  */ class Class {     public static final MutableInteger obj = new MutableInteger(0); } 

Then one could simply do later on:

Class.obj.i = 1; 

so adding 0 to the Javadoc wouldn't mean much.

It only works for strings because they are immutable and the JLS explicitly says so: there is no way for you to tell the compiler that on a custom class.