How to quote “*/” in JavaDocs

不问归期 提交于 2019-11-26 11:19:38

问题


I have a need to include */ in my JavaDoc comment. The problem is that this is also the same sequence for closing a comment. What the proper way to quote/escape this?

Example:

/**
 * Returns true if the specified string contains \"*/\".
 */
public boolean containsSpecialSequence(String str)

Follow up: It appears I can use / for the slash. The only downside is that this isn\'t all that readable when viewing the code directly in a text editor.

/**
 * Returns true if the specified string contains \"*/\".
 */

回答1:


Use HTML escaping.

So in your example:

/**
 * Returns true if the specified string contains "*/".
 */
public boolean containsSpecialSequence(String str)

/ escapes as a "/" character.

Javadoc should insert the escaped sequence unmolested into the HTML it generates, and that should render as "*/" in your browser.

If you want to be very careful, you could escape both characters: */ translates to */

Edit:

Follow up: It appears I can use / for the slash. The only downside is that this isn't all that readable when view the code directly.

So? The point isn't for your code to be readable, the point is for your code documentation to be readable. Most Javadoc comments embed complex HTML for explaination. Hell, C#'s equivalent offers a complete XML tag library. I've seen some pretty intricate structures in there, let me tell you.

Edit 2: If it bothers you too much, you might embed a non-javadoc inline comment that explains the encoding:

/**
 * Returns true if the specified string contains "*/".
 */
// returns true if the specified string contains "*/"
public boolean containsSpecialSequence(String str)



回答2:


Nobody mentioned {@literal}. This is another way to go:

/**
 * Returns true if the specified string contains "*{@literal /}".
 */

Unfortunately you cannot escape */ at a time. With some drawbacks, this also fixes:

The only downside is that this isn't all that readable when viewing the code directly in a text editor.




回答3:


/**
 * Returns true if the specified string contains "*/".
 */

This is the ‘right’ solution, but for readability's sake I'd probably go for:

/**
 * Returns true if the string contains an asterisk followed by slash.
 */



回答4:


Use the entity

*/ 

In your documentation it will show up as a "*/"




回答5:


Another way I stumbled upon, just for completeness: add some HTML markup which doesn't alter the output between the * and /.

  /**
   * *<b/>/
   */

Compared to the HTML escape solution, this seems something of an ugly hack, but it also yields the right result in HTML output.




回答6:


I would suggest you also add a line comment somewhere near saying something like

// *&#47; is html for */


来源:https://stackoverflow.com/questions/631817/how-to-quote-in-javadocs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!