java 你如何在 javadoc 内联标签中转义花括号,比如 {@code} 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/647195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:12:00  来源:igfitidea点击:

How do you escape curly braces in javadoc inline tags, such as the {@code} tag

javajavadocescapingcurly-braces

提问by

/**
 * Gets the meatball icon for a nincompoop.
 * 
 * <p>
 * Example: {@code <custom:meatball color="<%= Meatball.RED %> nincompoop="${person}" />}
 * 
 * @author King Cong
 * 
 */

The "${person}" part breaks the doc comment because it uses curly braces.

"${person}" 部分破坏了文档注释,因为它使用了花括号。

回答by Joe Kearney

Not so much an answer as a workaround, but if you replace {@code ...}with the old version <code>...</code>it will render curly braces how you expect.

与其说是解决方法,不如说是解决方法,但是如果您替换{@code ...}为旧版本,<code>...</code>它会按照您的预期呈现花括号。

<code>{person} == ${person}</code>

Unfortunately, this breaks angle brackets, so to the original question you need to escape these:

不幸的是,这会破坏尖括号,因此对于原始问题,您需要转义这些:

<code>&lt;custom:meatball color="&lt;%= Meatball.RED %&gt; nincompoop="${person}" /&gt;</code>

You can even cheat here by getting Notepad++ to do that for you, with the handy TextFX -> convert -> Encode HTML (&<>").

你甚至可以通过让 Notepad++ 为你做这件事来作弊,使用方便的 TextFX -> 转换 -> 编码 HTML (&<>")。

This does at least have the benefit that everything renders nicely both in the generated Javadoc and in Eclipse in the Javadoc view, which doesn't appear to understand &#125;and friends.

这至少有一个好处,即在生成的 Javadoc 和 Eclipse 中的 Javadoc 视图中,所有内容都可以很好地呈现,这似乎不太理解&#125;和朋友。

回答by John Feminella

Try using HTML escapes:

尝试使用 HTML 转义:

$&#123;person&#125; == ${person}

回答by trung

bodunbodun solution works as it usually happens that you have newline as well in the javadocs. HTML escapes won't work if you want both { and newline

bodunbodun 解决方案是有效的,因为它通常发生在 javadocs 中也有换行符。如果您同时需要 { 和换行符,HTML 转义将不起作用

<pre>
{@code
<foo bar="}${bar}{@code"/>
<bar foo="}${foo}{@code"/>
}
</pre>

will give you

会给你

<foo bar="${bar}" />
<bar foo="${foo}" />

回答by antoni.rasul

I had the same problem actually - none of propositions have worked for me (HTML escapes do not work for whatever reason). If that helps - try closing the {@code} before problematic symbol and reopen it after, like this:

我实际上遇到了同样的问题 - 没有一个命题对我有用(HTML 转义无论出于何种原因都不起作用)。如果这有帮助 - 尝试在有问题的符号之前关闭 {@code} 并在之后重新打开它,如下所示:

{@code nincompoop="}${person}{@code" />}

{@code nincompoop=" }${person}{@code" />}

This doesn't seem the solution, but it works, and does not break formatting if used carefully :)

这似乎不是解决方案,但它有效,并且如果小心使用不会破坏格式:)

回答by Trevor Young

Found another less than stellar workaround for this. Is it better or worse than the other ones? I'll let you decide. Take the {@code }part and replace it with <code> </code>. (This makes it all disappear because of the angle braces.) The take the very first <and wrap it in {@literal }, making it look like this {@literal<}. Now everything will show up fine, and it's not too horribly slaughtered in the code. The final result looks like this

为此找到了另一个不太好的解决方法。它比其他的好还是差?我会让你决定的。取出{@code }零件并将其替换为<code> </code>。(由于尖括号,这使得它全部消失。)取第一个<并将其包裹起来{@literal },使其看起来像这样{@literal<}。现在一切都会好起来的,而且在代码中并没有太可怕地被屠杀。最终结果看起来像这样

/**
 * Gets the meatball icon for a nincompoop.
 * 
 * <p>
 * Example: <code>{@literal<}custom:meatball color="<%= Meatball.RED %> nincompoop="${person}" /></code>
 * 
 * @author King Cong
 * 
 */

Alternatively, if you don't like the {@literal<}, then you could use &lt;instead. The result would look like this:

或者,如果您不喜欢{@literal<},则可以&lt;改用。结果如下所示:

/**
 * Gets the meatball icon for a nincompoop.
 * 
 * <p>
 * Example: <code> &lt;custom:meatball color="<%= Meatball.RED %> nincompoop="${person}" /></code>
 * 
 * @author King Cong
 * 
 */

Neither are great solutions, but they do work.

两者都不是很好的解决方案,但它们确实有效。

回答by Trevor Young

Use the {@literal}, so do this {@literal } }. Works in my tests.

使用{@literal},所以这样做{@literal } }。在我的测试中有效。

So for your case, it would look like this:

所以对于你的情况,它看起来像这样:

/**
 * Gets the meatball icon for a nincompoop.
 * 
 * <p>
 * Example: {@code <custom:meatball color="<%= Meatball.RED %> nincompoop="${person{@literal } }" />}
 * 
 * @author King Cong
 * 
 */