yegor256
yegor256

Reputation: 105203

How to format code snippet in Javadoc with @ and { symbols inside?

This is what I'm trying to do:

/**
 * <pre>
 * {@code
 * &#64;XmlRootElement
 * public final class Page &#123;
 * &#125;
 * }
 * </pre>
 */

I'm expecting it to be formatted like:

@XmlRootElement
public final class Page {
}

But I'm getting:

&#64;XmlRootElement
public final class Page &#123;
&#125;

Replacing these HTML entities with real symbols (@, {, }) leads to javadoc warnings and absolutely incorrect formatting. What is a workaround?

Upvotes: 38

Views: 25509

Answers (6)

Nolequen
Nolequen

Reputation: 4337

Since Java 23 (JEP 467) you may use /// for Markdown comments:

   /// ```
   /// @XmlRootElement
   /// public final class Page {
   /// }
   /// ```

Upvotes: 2

Nolequen
Nolequen

Reputation: 4337

Since Java 18 (JEP 413) you may use @snippet tag:

/**
 * -- ex: looping through List of Map objects --
 * {@snippet :
 * @XmlRootElement
 * public final class Page {
 * }
 * }
 */

Upvotes: 6

Chandresh Mishra
Chandresh Mishra

Reputation: 1189

It is old question but I figured out why it was not working for me.

This was not working:

<pre>{@code 
@Autowired

But when all written in one line it works.

<pre>
{@code @Autowired

Upvotes: 0

Patrick
Patrick

Reputation: 799

<pre>
<code>
{@literal@}Override
public String toString() {
    return "blah";
}
</code>
</pre>

This works for me.

Upvotes: 31

yegor256
yegor256

Reputation: 105203

This is how it finally works for me:

/**
 * <pre>
 * &#64;XmlRootElement
 * public final class Page {
 * }
 * </pre>
 */

Upvotes: 40

AlexR
AlexR

Reputation: 115388

wrap your code snippet with <pre><code></code></pre>. These are special HTML tags that allow you to forget about escaping of special characters.

Upvotes: 7

Related Questions