【问题标题】:How can I get generics in javadoc code block displayed?如何在显示的 javadoc 代码块中获取泛型?
【发布时间】:2015-02-19 23:17:11
【问题描述】:

我有一个 javadoc 代码块,我想在其中编写一个包含这样的泛型的代码示例:

public interface SomeInterface <T> { }
public interface SomeInterface extends SomeOtherInterface<T> { }

这是我的 javadoc 块:

  /**
   * Get the Generic Type T of a Type (Class/Interface) or extended/inherited Subtype: 
   * <pre>
   * {@code
   * public interface SomeInterface <T> { }
   * public interface SomeInterface extends SomeOtherInterface<T> { }
   * }
   * </pre>
   * @param implType
   * @param parentType
   * @return
   */
  public static JClassType findGenericType(JClassType implType, JClassType parentType) { ... }

javadoc 输出为:

Get the Generic Type T of a Type (Class/Interface) or extended/inherited Subtype:


 public interface SomeInterface  { }
 public interface SomeInterface extends SomeOtherInterface { }
 }

Parameters:
implType
parentType
Returns:

输出中缺少泛型。

如何让 javadoc 正确显示泛型?

【问题讨论】:

    标签: java generics javadoc


    【解决方案1】:

    Java 文档被呈现为 HTML,因此尖括号 (&lt;&gt;) 之间的任何内容都将被解释为 HTML 标记,并且不会被打印为文本。 您可以使用&amp;lt;&amp;gt; 来分别渲染HTML &lt;&gt;

     /**
       * Get the Generic Type T of a Type (Class/Interface) or extended/inherited Subtype: 
       * <pre>
       * {@code
       * public interface SomeInterface &lt;T&gt; { }
       * public interface SomeInterface extends SomeOtherInterface&lt;T&gt; { }
       * }
       * </pre>
       * @param implType
       * @param parentType
       * @return
       */
    

    【讨论】:

      【解决方案2】:

      JavaDoc 使用 html 来呈现。因此,如果您希望在 JavaDoc 中出现左尖括号 (),则需要将 &amp;lt; 用于左尖括号,将 &amp;gt; 用于右尖括号。例如:

      /**
      * Get the Generic Type T of a Type (Class/Interface) or extended/inherited Subtype: 
      * <pre>
      * {@code
      * public interface SomeInterface &lt;T&gt; { }
      * public interface SomeInterface extends SomeOtherInterface&lt;T&gt; { }
      * }
      * </pre>
      * @param implType
      * @param parentType
      * @return
      */
      

      更多详情请见the wikipedia article

      【讨论】:

        【解决方案3】:

        怎么样?

        /**
         * Explain something...
         * <pre>
         * public interface SomeInterface &lt;T&gt; { }
         * </pre>
         */
        

        这是因为 JavaDoc 被呈现为 HTML,因此必须对 &lt;&gt; 进行转义才能正确呈现。

        编辑:在 OP 澄清后,我更改了答案。

        【讨论】:

        • 我想在我的 JavaDoc 中写一个代码示例,而不是你所做的?
        【解决方案4】:

        实际上,您可以将泛型放在 javadoc 本身中。我知道这不是有效的 HTML,我假设 javadoc 处理器有特殊情况处理。

        使用您的示例: /** * @param <T> the type */ public interface SomeInterface <T> { }

        来源:https://binkley.blogspot.com/2006/08/javadoc-generic-type-parameters.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多