我有一个小代码示例,我想在Javadoc注释中包含一个方法。

/**
 * -- ex: looping through List of Map objects --
 * <code>
 * for (int i = 0; i < list.size(); i++) {
 *      Map map = (Map)list.get(i);
 *      System.out.println(map.get("wordID"));
 *      System.out.println(map.get("word"));
 * }
 * </code>
 * 
 * @param query - select statement
 * @return List of Map objects
 */

问题是在Javadoc中显示的代码示例没有换行符,这使得它难以阅读。

-- ex: looping through List of Map objects -- for (int i = 0; i list.size(); i++) { Map map = (Map)list.get(i); System.out.println(map.get("wordID")); System.out.println(map.get("word")); } 
Parameters
query - - select statement 
Returns:
List of Map objects 

我想我假设代码标记可以处理换行符是错误的。在Javadoc注释中格式化代码示例的最佳方法是什么?


当前回答

试着用“pre”代替“code”。HTML中的pre标记将文本标记为预格式化,所有换行符和空格都将在您键入它们时显示。

其他回答

除了前面提到的<pre>标记之外,你还应该使用@code JavaDoc注释,这将使HTML实体问题(特别是泛型)变得更容易,例如:

* <pre>
* {@code
* Set<String> s;
* System.out.println(s);
* }
* </pre>

将给出正确的HTML输出:

Set<String> s;
System.out.println(s);

而省略@code块(或使用<code>标记)将导致这样的HTML:

Set s;
System.out.println(s);

作为参考,可以在这里找到Java SE 8中可用的标记描述的完整列表。

换行符需要<pre></pre>标签,{@code…}用于泛型。但是不允许将开始大括号与<generic>标记放在同一行上,因为这样所有内容将再次显示在一行上。

显示在一行上:

* ..
* <pre>
* {@code
* public List<Object> getObjects() {
*    return objects;
* }
* </pre>
* ..

显示带有换行符:

* ..
* <pre>
* {@code
* public List<Object> getObjects() 
* {
*    return objects;
* }
* </pre>
* ..

另一件奇怪的事情是,当你粘贴{@code的右大括号时,它会显示:

* ..
* <pre>
* {@code
*   public List<Object> getObjects() 
*   {
*     return objects;
*   }
* }
* </pre>
* ..

输出:

public List<Object> getObjects() 
{
   return objects;
}
}

从Java 18 (JEP 413)开始,您可以使用@snippet标记:

/**
 * -- ex: looping through List of Map objects --
 * {@snippet :
 * for (int i = 0; i < list.size(); i++) {
 *      Map map = (Map)list.get(i);
 *      System.out.println(map.get("wordID"));
 *      System.out.println(map.get("word"));
 * }
 * }
 *
 * @param query - select statement
 * @return List of Map objects
 */

我使用这两种方法没有任何问题:

<pre>
<code>
 ... java code, even including annotations
</code>
</pre>

and

<pre class="code">
 ... java code, even including annotations
</pre>

当然,后者更简单,请注意class="code"部分

/**
 * <blockquote><pre>
 * {@code
 * public Foo(final Class<?> klass) {
 *     super();
 *     this.klass = klass;
 * }
 * }
 * </pre></blockquote>
 **/

<pre/>用于保存行。 {@code必须有自己的行 <blockquote/>只是用于缩进。

public Foo(final Class<?> klass) {
    super();
    this.klass = klass;
}

用JDK8更新

正确代码的最低要求是<pre/>和{@code}。

/**
 * test.
 *
 * <pre>{@code
 * <T> void test(Class<? super T> type) {
 *     System.out.printf("hello, world\n");
 * }
 * }</pre>
 */

收益率

 <T> void test(Class<? super T> type) {
     System.out.printf("hello, world\n");
 }

可选的括起来<blockquote/>插入缩进。

/**
 * test.
 *
 * <blockquote><pre>{@code
 * <T> void test(Class<? super T> type) {
 *     System.out.printf("hello, world\n");
 * }
 * }</pre></blockquote>
 */

收益率

     <T> void test(Class<? super T> type) {
         System.out.printf("hello, world\n");
     }

插入<p>或用<p>和</p>包围会产生警告。