Search code examples
javajavadoc

How to add option to display @tags in Javadoc comments


I have two own Annotations that I use for different fields and methods alongside the @Deprecated annotation. However, unlike the @Deprecated annotation, which can be displayed in the Javadoc comment itself with a description, is it not possible for me to do this with my own annotations.

Small example:

/**
 * Sends "bar".
 *
 * @deprecated Use {@link #sendFooBar()} instead.
 */
@Deprecated
@MyAnnotation // I want to add a description to this annotation in the comment above
public void sendBar(){
    System.out.prntln("bar");
}

So my question is what I have to do, in order to display the annotation in the comment itself and to provide comments on it.

I can't just add it into the comment as it would be seen as a "wrong tag".

To clarify: I don't want to just display the @ symbol in the comment. I know how to do this.
I want to have documentation (description) in the Javadoc comment similar to the one available for the @Deprecated annotation.

The annotations themself have the @Documented, @Retention(RetentionPolicy.RUNTIME) and the @Target({ElementType.METHOD, ElementType.FIELD}) annotations attached to them.


Solution

  • It's not possible to use self-defined annotations in javadoc the way you think. This becomes more obvious if you write the annotations per specification. The javadoc @deprecated tag is written lowercase whereas the @Deprecated annotation of the Java Language is written capitalized.

    This is due to the fact that the javadoc @deprecated tag comments on the deprecation of the underlying method and/or constructor rather than representing the Java @Deprecated annotation itself. You're therefore not able to use the annotation in javadoc the way you intended by default.

    However, it IS possible to introduce custom tags to javadoc. How to do so is explained here. You might also consider mentioning the implications your annotation might have somewhere else in the javadoc comment. To safely escape your annotation's name, just use the {@code ...} construct.