How can I get generics in javadoc code block displayed?

纵饮孤独 提交于 2019-12-23 13:06:42

问题


I have a javadoc code block where I want to write a code sample that includes generics like this:

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

Here is my javadoc block:

  /**
   * 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) { ... }

The javadoc output is:

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:

The Generic is missing in the output.

How can I get javadoc to display the generics correctly?


回答1:


Java doc is rendered to HTML, so anything between angular brackets (<>) would be interpreted as an HTML tag, and won't be printed as text. You can use &lt; and &gt; in order to render HTML < and > respectively:

 /**
   * 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 uses html to render. Therefore, if you want a left angular brace (<) and right angular brace (>) to appear in the JavaDoc, you need to use &lt; for a left angular brace and &gt; for a right angular brace. For instance:

/**
* 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
*/

See the wikipedia article for more details.




回答3:


How about?

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

This is since JavaDoc is rendered as HTML and <> must therefore be escaped to be properly rendered.

Edit: after clarification from the OP I changed the answer.




回答4:


Actually you can place the generic in the javadoc itself. I know this isn't valid HTML, I assume javadoc processor has special case handling for it.

Using your example: /** * @param <T> the type */ public interface SomeInterface <T> { }

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



来源:https://stackoverflow.com/questions/27592828/how-can-i-get-generics-in-javadoc-code-block-displayed

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