问题
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 <
and >
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 <T> { }
* public interface SomeInterface extends SomeOtherInterface<T> { }
* }
* </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 <
for a left angular brace and >
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 <T> { }
* public interface SomeInterface extends SomeOtherInterface<T> { }
* }
* </pre>
* @param implType
* @param parentType
* @return
*/
See the wikipedia article for more details.
回答3:
How about?
/**
* Explain something...
* <pre>
* public interface SomeInterface <T> { }
* </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