Search code examples
javagenericsjavadoc

How can I get generics in javadoc code block displayed?


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?


Solution

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