Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

javadoc

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?

like image 259
yegor256 Avatar asked Nov 06 '11 12:11

yegor256


People also ask

What does @code mean in Javadoc?

{@code} is a Javadoc tag that came with Java 5. A code snippet embedded within {@code} will display our special characters correctly so they don't need to be manually escaped. However, indentation and line breaks will be lost. This can be rectified by using {@code} together with <pre> , though (see next section).

Can you use HTML tags in Javadoc comments?

There are no real restrictions on the use of HTML in Javadoc comments. The Javadoc documentation states: Comments are written in HTML - The text must be written in HTML, in that they should use HTML entities and can use HTML tags.


2 Answers

This is how it finally works for me:

/**  * <pre>  * &#64;XmlRootElement  * public final class Page {  * }  * </pre>  */ 
like image 174
yegor256 Avatar answered Sep 30 '22 22:09

yegor256


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

This works for me.

like image 31
Patrick Avatar answered Sep 30 '22 21:09

Patrick