Multiple line code example in Javadoc comment
I have a small code example I want to include in the Javadoc comment for a method.
/**
* -- 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
*/
The problem is the code example shows up in the Javadoc with no line breaks making it hard to read.
-- 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
I guess I am wrong in assuming the code tag would handle line breaks. What is the best way to format code examples in Javadoc comments ?
In addition to the already mentioned <pre>
tags, you should also use the @code
JavaDoc annotation, which will make life much easier when it comes to HTML entities issues (in particular with Generics), eg:
* <pre>
* {@code
* Set<String> s;
* System.out.println(s);
* }
* </pre>
Will give correct HTML output:
Set<String> s;
System.out.println(s);
While omitting the @code
block (or using a <code>
tag) will result in HTML like this:
Set s;
System.out.println(s);
(For reference, Java SE 8 tag descriptions can be found here: Javadoc Tags)
I had a really tough time with including a specific code example in a javadoc comment. I'd like to share this one.
Please note the following:
<code>
- tag to prevent the curly brackets from being interpreted {@code ...}
- tag to get the generics included in the output @Override
via " {@literal @}Override
" because javadoc generator "tilts" there due to the fact that the @ goes directly after an opening curly bracket {@code
and {@literal
, to compensate inner spaces and keep the alignment javadoc code:
/** 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
*/
gets printed as
new BeanTranslator.Builder()
.translate(
new Translator<String, Integer>(String.class, Integer.class){
@Override
public Integer translate(String instance) {
return Integer.valueOf(instance);
}})
.build();
The java source has lots of good examples for this. Here's an example from the head of "String.java":
....
* is equivalent to:
* <p><blockquote><pre>
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
* </pre></blockquote><p>
* Here are some more examples of how strings can be used:
* <p><blockquote><pre>
* System.out.println("abc");
* String cde = "cde";
* System.out.println("abc" + cde);
* String c = "abc".substring(2,3);
* String d = cde.substring(1, 2);
* </pre></blockquote>
...
链接地址: http://www.djcxy.com/p/50198.html
上一篇: 链接到Javadoc中的外部URL?
下一篇: Javadoc评论中有多行代码示例