2020-06-30 12:47:33 +02:00
Try to imagine using the standard Java API (Collections, JDBC, IO, ...) without Javadoc. It would be a nightmare, because Javadoc is the only way to understand of the contract of the API. Documenting an API with Javadoc increases the productivity of the developers consuming it.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
On top of a main description for each member of a public API, the following Javadoc elements are required to be described:
2020-06-30 14:49:38 +02:00
2021-01-27 13:42:22 +01:00
* Parameters, using ``++@param parameterName++``.
* Thrown exceptions, using ``++@throws exceptionName++``.
* Method return values, using ``++@return++``.
2021-02-05 10:34:25 +01:00
* Generic types, using ``++@param <T>++``.
2020-06-30 12:47:33 +02:00
Furthermore the following guidelines should be followed:
2020-06-30 14:49:38 +02:00
2020-06-30 12:47:33 +02:00
* At least 1 line of description.
2021-01-27 13:42:22 +01:00
* All parameters documented with ``++@param++``, and names should match.
* All checked exceptions documented with ``++@throws++``
* ``++@return++`` present and documented when not ``++void++``.
2021-06-08 10:28:35 +02:00
* Placeholders like ``++"TODO"++``, ``++"FIXME"++``, ``++"..."++`` should be avoided.
2020-06-30 12:47:33 +02:00
The following public methods and constructors are not taken into account by this rule:
2020-06-30 14:49:38 +02:00
2020-06-30 12:47:33 +02:00
* Getters and setters.
2021-01-27 13:42:22 +01:00
* Methods overriding another method (usually decorated with ``++@Override++``).
2020-06-30 12:47:33 +02:00
* Empty constructors.
* Static constants.
2020-12-21 15:38:52 +01:00
For the parameters of the rule, the following rules are applied:
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
* ``++?++`` matches a single character
* ``++*++`` matches zero or more characters
* ``++**++`` matches zero or more packages
2020-12-21 15:38:52 +01:00
Examples:
2021-01-27 13:42:22 +01:00
* ``++java.internal.InternalClass++`` will match only ``++InternalClass++`` class.
* ``++java.internal.*++`` will match any member of ``++java.internal++`` package.
* ``++java.internal.**++`` same as above, but including sub-packages.
2020-12-21 15:38:52 +01:00
2020-06-30 12:47:33 +02:00
== Noncompliant Code Example
----
/**
* This is a Javadoc comment
*/
public class MyClass<T> implements Runnable { // Noncompliant - missing '@param <T>'
public static final DEFAULT_STATUS = 0; // Compliant - static constant
private int status; // Compliant - not public
public String message; // Noncompliant
public MyClass() { // Noncompliant - missing documentation
this.status = DEFAULT_STATUS;
}
public void setStatus(int status) { // Compliant - setter
this.status = status;
}
@Override
public void run() { // Compliant - has @Override annotation
}
protected void doSomething() { // Compliant - not public
}
public void doSomething2(int value) { // Noncompliant
}
public int doSomething3(int value) { // Noncompliant
return value;
}
}
----
== Compliant Solution
----
/**
* This is a Javadoc comment
* @param <T> the parameter of the class
*/
public class MyClass<T> implements Runnable {
public static final DEFAULT_STATUS = 0;
private int status;
/**
* This is a Javadoc comment
*/
public String message;
/**
* Class comment...
*/
public MyClass() {
this.status = DEFAULT_STATUS;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public void run() {
}
protected void doSomething() {
}
/**
* Will do something.
* @param value the value to be used
*/
public void doSomething(int value) {
/**
* {@inheritDoc}
*/
public int doSomething(int value) {
return value;
}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]