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 {

5 Answers 5

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+

Sign up to request clarification or add additional context in comments.

4 Comments

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). &#064; doesn't (and it is used in JUnit javadoc, for example). Oh, and it doesn't work inside @code (&#064; does).
@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: stackoverflow.com/a/13512524/159570
I also tried code in <pre> 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.
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.
35

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

Comments

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.

Comments

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.

Comments

-4

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

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.