37

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?

yegor256
  • 102,010
  • 123
  • 446
  • 597

5 Answers5

40

This is how it finally works for me:

/**
 * <pre>
 * &#64;XmlRootElement
 * public final class Page {
 * }
 * </pre>
 */
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • Your answer works for me. THX. If use "{@literal @}Override", I have to remove one space before "{@literal @}Override" to line up. – Allen Kerr Jun 24 '20 at 10:02
31
<pre>
<code>
{@literal@}Override
public String toString() {
    return "blah";
}
</code>
</pre>

This works for me.

Patrick
  • 799
  • 1
  • 10
  • 14
7

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

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 7
    Huh. There is nothing in the definition of `pre` or `code` which escapes special characters. They only change the font and the whitespace interpretation. (Also, `@` and `{` might get interpreted by Javadoc before the HTML browser even gets them.) – Paŭlo Ebermann Nov 06 '11 at 15:45
3

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

/**
 * -- ex: looping through List of Map objects --
 * {@snippet :
 * @XmlRootElement
 * public final class Page {
 * }
 * }
 */
Nolequen
  • 3,032
  • 6
  • 36
  • 55
  • You can also use the same syntax in Java 8+ with this maven doclet plugin https://github.com/jtulach/codesnippet4javadoc – Saljack Aug 29 '22 at 12:27
0

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
Chandresh Mishra
  • 1,081
  • 3
  • 24
  • 45