I often find myself with a dilemma when writing javadoc for properties/members of a "simple" POJO class holding only properties and getters and setters (DTO-style)....
1) Write javadoc for the property
or...
2) Write javadoc for the getter
If I write javadoc for the property, my IDE (Eclipse) will (naturally) not be able to display this when I later access the POJO via code completion. And there is no standard javadoc tag that lets me link the getter-javadoc to the actual property javadoc.
An example:
public class SomeDomainClass {
/**
* The name of bla bla bla
*/
private String name;
/**
* @return INSERT SOME SMART JAVADOC TAG LINKING TO name's javadoc
*/
public String getName() {
return name;
}
So, basically it would be interesting to hear how others go about for having your Eclipse IDE display the javadoc property description for your getters - without having to duplicate the javadoc comment.
As of now I'm considering making my practise to only document the getters and not the properties. But it doesn't seem like the best solution...
-
1Interesting discussion on this here: stackoverflow.com/questions/1028967/…. The accepted answer addresses what you asked about Eclipse / javadoc.b.roth– b.roth2010年02月16日 13:40:42 +00:00Commented Feb 16, 2010 at 13:40
-
Seems like they concluded with what I was considering... write property javadoc only in the getters.user274360– user2743602010年02月16日 15:07:06 +00:00Commented Feb 16, 2010 at 15:07
-
I found a way to do this with annotations that work in eclipse and can even be collected at runtime, would that be an option?Aquarius Power– Aquarius Power2015年12月04日 01:49:37 +00:00Commented Dec 4, 2015 at 1:49
-
private members need javadoc ?Tito– Tito2016年08月11日 23:47:12 +00:00Commented Aug 11, 2016 at 23:47
-
The name of bla bla bla : best exampleRodrigo Espinoza– Rodrigo Espinoza2019年03月06日 18:52:16 +00:00Commented Mar 6, 2019 at 18:52
4 Answers 4
You can include private members while generating Javadocs (using -private) and then use @link to link to that fields property.
public class SomeDomainClass {
/**
* The name of bla bla bla
*/
private String name;
/**
* {@link SomeDomainClass#name}
*/
public String getName() {
return name;
}
}
Alternatively, if you do not want to generate the Javadoc for all private members, you can have a convention to document all getters and use @link on setters.
public class SomeDomainClass {
private String name;
/**
* The name of bla bla bla
*/
public String getName() {
return name;
}
/**
* {@link SomeDomainClass#getName}
*/
public void setName(String name) {
this.name = name;
}
}
3 Comments
@link
means a link which has to be clicked to see the actual javadoc. It's not an Eclipse usability issue, it's the wrong solution for providing javadocs that are easy to use.Lombok is a very convenient library for such tasks.
@Getter
@Setter
public class Example {
/**
* The account identifier (i.e. phone number, user name or email) to be identified for the account you're
* requesting the name for
*/
private String name;
}
That is all you need! The @Getter
annotation creates a getter method for each private field and attach the javadoc to it.
PS: The library has many cool features you might want to checkout
Comments
I do both, aided by Eclipse's autocomplete.
First, I document the property:
/**
* The {@link String} instance representing something.
*/
private String someString;
Then, I copy and paste this to the getter:
/**
* The {@link String} instance representing something.
*/
public String getSomeString() {
return someString;
}
With eclipse, @return statements have an autocomplete - so, I add the word Gets, lowercase the "t", and copy the sentence with the lowercase "t". I then use @return (with Eclipse autocomplete), paste the sentence, and then uppercase the T in the return. It then looks like this:
/**
* Gets the {@link String} instance representing something.
* @return The {@link String} instance representing something.
*/
public String getSomeString() {
return someString;
}
Finally, I copy that documentation to the setter:
/**
* Gets the {@link String} instance representing something.
* @return The {@link String} instance representing something.
*/
public void setSomeString(String someString) {
this.someString = someString;
}
Then, I modify it and with Eclipse autocomplete you can get not only the @param tag but also the name of the parameter:
/**
* Sets the {@link String} instance representing something.
* @param someString The {@link String} instance representing something.
*/
public void setSomeString(String someString) {
this.someString = someString;
}
Then, I'm done. In my opinion, this templating makes it a lot easier, in the long run, to not only remind yourself what the property means through repetition, but also it makes it easier to add additional comments to the getter and setter if you want to add side effects (such as not allowing null properties, turning strings to uppercase, etc). I investigated making an Eclipse plugin for this purpose but I couldn't find the appropriate extension point for the JDT, so I gave up.
Note that the sentence might not always start with a T - it's just the first letter has to be uncapitalized/recapitalized in pasting.
10 Comments
I really think it's a problem and the official Javadoc guide does not tell anything about it. C# can solve this in an elegant fashion with the Properties usage (I don't code in C#, but I really think it's a nice feature).
But I have a guess: if you need to explain what someString is, maybe it is a ``bad small'' about your code. It can mean that you should write SomeClass to type someString, so you would explain what is someString in the SomeClass documentation, and just so the javadocs in getter/setter would be not necessary.