36

my JavaDoc doesn't work when I have a code example with an annotation.

Any suggestions?

/**
 * <pre>
 * public class Demo {
 *    @DemoAnnotation
 *    public void demoMethod() {
 *    }
 * }
 * </pre>
 */ 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DemoAnnotation {
John
  • 373
  • 1
  • 3
  • 6

5 Answers5

55

A more general solution: {@literal @}

The {@literal} tag denotes literal text. The enclosed text is interpreted as not containing HTML markup or nested javadoc tags. For example, the doc comment text: {@literal a<B>c} displays in the generated HTML page unchanged: a<B>c -- that is, the <B> is not interpreted as bold.

Requires Java 5+

Joe Coder
  • 4,498
  • 31
  • 41
  • 4
    Upvoted it before actually trying, but then I noticed a problem: using `{@literal @}` adds an ugly space before the `@` (at least when viewed in NetBeans). `@` doesn't (and it is used in JUnit javadoc, for example). Oh, *and* it doesn't work inside `@code` (`@` does). – Sergei Tachenov Sep 29 '16 at 13:26
  • @SergeyTachenov Couldn't reproduce your space problem using command line javadoc. The `@code` behavior is by design; refer to this answer for a good hint on how to embed complex code snippets in javadoc: http://stackoverflow.com/a/13512524/159570 – Joe Coder Sep 30 '16 at 08:14
  • I also tried code in
     with annotation and {@literal @} added a leading space in the output (Java 9). I went for ugly HTML entity. This does not work in {@code } block though. It seems there is no universal good way how to insert code verbatim into Javadoc without the need to escape something.
    – virgo47 Apr 11 '18 at 06:50
  • Yes, but I don't think you'll find any templating language, Javadoc or otherwise, which allows you to inline arbitrary text without escaping of delimiter characters. See link in my previous comment for proper escaping guidelines. – Joe Coder Apr 11 '18 at 12:23
35

You must replace @ with &#064; in your JavaDoc.

Espen
  • 10,545
  • 5
  • 33
  • 39
0

use <code> like this:

/**
 * <pre><code>
 *    public class Demo {
 *      @DemoAnnotation
 *      public void demoMethod() {
 *      }
 *    }
 * </code></pre>
 */ 

produces a paragraph while alone can also be used inline.

avogt
  • 81
  • 7
0

You can also use @code to escape the annotation, but you must do each one individually like this:

/**
 * <pre>
 * public class Demo {
 *   {@code @DemoAnnotation }
 *   {@code @AnotherAnnotation }
 *    public void demoMethod() {
 *    }
 * }
 * </pre>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DemoAnnotation {

will render like this:

public class Demo {
    @DemoAnnotation
    @AnotherAnnotation
    public void demoMethod() {
    }
}

Note: It will not work to simply wrap both annotations - or the entire code sample - in one @code block.

Gary
  • 6,357
  • 5
  • 30
  • 36
-4

You must use the @Documented annotation for adding annotations in the javadoc. Check the implementation on the API

RNJ
  • 15,272
  • 18
  • 86
  • 131
  • 1
    This is incorrect: please read https://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Documented.html – Marco May 13 '15 at 09:58