我有一个小代码示例,我想在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注释中格式化代码示例的最佳方法是什么?


当前回答

在javadoc注释中包含特定的代码示例时,我遇到了很大的困难。我想分享这个。 请注意以下事项:

使用旧的<code> -标签来防止花括号被解释 new {@code…} -标签来获取输出中包含的泛型 @Override中的@符号通过"{@literal @}Override"转义,因为javadoc生成器在那里“倾斜”,因为@直接放在左花括号后面 在{@code和{@literal前面删除一个空格,以补偿内部空格并保持对齐

javadoc代码:

/** this methods adds a specific translator from one type to another type. `
  * i.e.
  * <pre>
  * <code>new BeanTranslator.Builder()
  *   .translate(
  *     new{@code Translator<String, Integer>}(String.class, Integer.class){
  *      {@literal @}Override
  *       public Integer translate(String instance) {
  *         return Integer.valueOf(instance);
  *       }})
  *   .build();
  * </code>
  * </pre>
  * @param translator
  */

被打印为

new BeanTranslator.Builder()
  .translate(
    new Translator<String, Integer>(String.class, Integer.class){
      @Override
      public Integer translate(String instance) {
        return Integer.valueOf(instance);
      }})
  .build();

其他回答

/**
 * <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>包围会产生警告。

用<pre></pre>标签将多行代码括起来。

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

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

and

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

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

另外两种解决方案的结合似乎是完美的:

* <pre>{@code
*     {@literal @}Override
*     public void someMethod() {
*         Set<String> s;
*     }
* }</pre>

ie。使用<pre>{@code开始和}</pre>结束片段。同样,用{@literal @}替换@。

还没有找到更简单的解决办法。对于一门已经活跃开发了几十年的语言来说,这是相当可悲的。

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