一:簡介
Javadoc用於描述類或者方法的作用。Javadoc可以寫在類上面和方法上面。
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html
二:寫在類上面的Javadoc
寫在類上的文檔標註一般分爲三段:
第一段:概要描述,通常用一句或者一段話簡要描述該類的作用,以英文句號作爲結束
第二段:詳細描述,通常用一段或者多段話來詳細描述該類的作用,一般每段話都以英文句號作爲結束
第三段:文檔標註,用於標註作者、創建時間、參閱類等信息
第一段:概要描述
單行示例:
package org.springframework.util;
/**
* Miscellaneous {@link String} utility methods.
*
*/
public abstract class StringUtils {
多行示例:
package java.lang;
/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*/
public class Object {}
在註釋中出現以@開頭東東被稱之爲Javadoc文檔標記,是JDK定義好的,如@author、@version、@since、@see、@link、@code、@param、@return、@exception、@throws等。
1. @link:{@link 包名.類名#方法名(參數類型)} 用於快速鏈接到相關代碼
@link的使用語法{@link 包名.類名#方法名(參數類型)},其中當包名在當前類中已經導入了包名可以省略,可以只是一個類名,也可以是僅僅是一個方法名,也可以是類名.方法名,使用此文檔標記的類或者方法,可用通過按住Ctrl鍵+單擊 可以快速跳到相應的類或者方法上,解析成html其實就是使用< code> 包名.類名#方法名(參數類型)< /code>
@link示例
// 完全限定的類名
{@link java.lang.Character}
// 省略包名
{@link String}
// 省略類名,表示指向當前的某個方法
{@link #length()}
// 包名.類名.方法名(參數類型)
{@link java.lang.String#charAt(int)}
2. @code: {@code text} 將文本標記爲code
{@code text} 會被解析成<code> text </code>
將文本標記爲代碼樣式的文本,在code內部可以使用 < 、> 等不會被解釋成html標籤, code標籤有自己的樣式
一般在Javadoc中只要涉及到類名或者方法名,都需要使用@code進行標記。
第二段:詳細描述
詳細描述一般用一段或者幾個鍛鍊來詳細描述類的作用,詳細描述中可以使用html標籤,如<p>、<pre>、<a>、<ul>、<i>等標籤, 通常詳細描述都以段落p標籤開始。
詳細描述和概要描述中間通常有一個空行來分割
package org.springframework.util;
/**
* Miscellaneous {@link String} utility methods.
*
* <p>Mainly for internal use within the framework; consider
* <a href="http://commons.apache.org/proper/commons-lang/">Apache's Commons Lang</a>
* for a more comprehensive suite of {@code String} utilities.
*
* <p>This class delivers some simple functionality that should really be
* provided by the core Java {@link String} and {@link StringBuilder}
* classes. It also provides easy-to-use methods to convert between
* delimited strings, such as CSV strings, and collections and arrays.
*
*/
public abstract class StringUtils {
一般段落都用p標籤來標記,凡涉及到類名和方法名都用@code標記,凡涉及到組織的,一般用a標籤提供出來鏈接地址。
3. @param
一般類中支持泛型時會通過@param來解釋泛型的類型
/**
* @param <E> the type of elements in this list
*/
public interface List<E> extends Collection<E> {}
4. @author
詳細描述後面一般使用@author來標記作者,如果一個文件有多個作者來維護就標記多個@author,@author 後面可以跟作者姓名(也可以附帶郵箱地址)、組織名稱(也可以附帶組織官網地址)
// 純文本作者
@author Rod Johnson
// 純文本作者,郵件
@author Igor Hersht, [email protected]
// 超鏈接郵件 純文本作者
@author <a href="mailto:[email protected]">Ovidiu Predescu</a>
// 純文本郵件
@author [email protected]
// 純文本 組織
@author Apache Software Foundation
// 超鏈接組織地址 純文本組織
@author <a href="https://jakarta.apache.org/turbine"> Apache Jakarta Turbine</a>
5. @see 另請參閱
@see 一般用於標記該類相關聯的類,@see即可以用在類上,也可以用在方法上。
/**
* @see IntStream
* @see LongStream
* @see DoubleStream
* @see <a href="package-summary.html">java.util.stream</a>
* /
public interface Stream<T> extends BaseStream<T, Stream<T>> {}
6. @since 從以下版本開始
@since 一般用於標記文件創建時項目當時對應的版本,一般後面跟版本號,也可以跟是一個時間,表示文件當前創建的時間
package java.util.stream;
/**
* @since 1.8
*/
public interface Stream<T> extends BaseStream<T, Stream<T>> {}
package org.springframework.util;
/**
* @since 16 April 2001
*/
public abstract class StringUtils {}
7. @version 版本
@version 用於標記當前版本,默認爲1.0
package com.sun.org.apache.xml.internal.resolver;
/**
* @version 1.0
*/
public class Resolver extends Catalog {}
三:寫在方法上的Javadoc
寫在方法上的文檔標註一般分爲三段:
第一段:概要描述,通常用一句或者一段話簡要描述該方法的作用,以英文句號作爲結束
第二段:詳細描述,通常用一段或者多段話來詳細描述該方法的作用,一般每段話都以英文句號作爲結束
第三段:文檔標註,用於標註參數、返回值、異常、參閱等
方法詳細描述上經常使用html標籤來,通常都以p標籤開始,而且p標籤通常都是單標籤,不使用結束標籤,其中使用最多的就是p標籤和pre標籤,ul標籤, i標籤。
pre元素可定義預格式化的文本。被包圍在pre元素中的文本通常會保留空格和換行符。而文本也會呈現爲等寬字體,pre標籤的一個常見應用就是用來表示計算機的源代碼。
一般p經常結合pre使用,或者pre結合@code共同使用(推薦@code方式)
一般經常使用pre來舉例如何使用方法
注意:pre>標籤中如果有小於號、大於號、例如泛型 在生產javadoc時會報錯
/**
* Check whether the given {@code CharSequence} contains actual <em>text</em>.
* <p>More specifically, this method returns {@code true} if the
* {@code CharSequence} is not {@code null}, its length is greater than
* 0, and it contains at least one non-whitespace character.
* <p><pre class="code">
* StringUtils.hasText(null) = false
* StringUtils.hasText("") = false
* StringUtils.hasText(" ") = false
* StringUtils.hasText("12345") = true
* StringUtils.hasText(" 12345 ") = true
* </pre>
* @param str the {@code CharSequence} to check (may be {@code null})
* @return {@code true} if the {@code CharSequence} is not {@code null},
* its length is greater than 0, and it does not contain whitespace only
* @see Character#isWhitespace
*/
public static boolean hasText(@Nullable CharSequence str) {
return (str != null && str.length() > 0 && containsText(str));
}
<pre>{@code
Person[] men = people.stream()
.filter(p -> p.getGender() == MALE)
.toArray(Person[]::new);
}</pre>
8. @param
@param 後面跟參數名,再跟參數描述
/**
* @param str the {@code CharSequence} to check (may be {@code null})
*/
public static boolean containsWhitespace(@Nullable CharSequence str) {}
9. @return
@return 跟返回值的描述
/**
* @return {@code true} if the {@code String} is not {@code null}, its
*/
public static boolean hasText(@Nullable String str){}
10. @throws
@throws 跟異常類型 異常描述 , 用於描述方法內部可能拋出的異常
/**
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
*/
public static String uriDecode(String source, Charset charset){}
11. @exception
用於描述方法簽名throws對應的異常
/**
* @exception IllegalArgumentException if <code>key</code> is null.
*/
public static Object get(String key) throws IllegalArgumentException {}
12. @see
@see既可以用來類上也可以用在方法上,表示可以參考的類或者方法
/**
* @see java.net.URLDecoder#decode(String, String)
*/
public static String uriDecode(String source, Charset charset){}
13. @value
用於標註在常量上,{@value} 用於表示常量的值
/** 默認數量 {@value} */
private static final Integer QUANTITY = 1;
14. @inheritDoc
@inheritDoc用於註解在重寫方法或者子類上,用於繼承父類中的Javadoc
基類的文檔註釋被繼承到了子類
子類可以再加入自己的註釋(特殊化擴展)
@return @param @throws 也會被繼承
四:示例
spring-core中的StringUtils 示例
package org.springframework.util;
/**
* Miscellaneous {@link String} utility methods.
*
* <p>Mainly for internal use within the framework; consider
* <a href="http://commons.apache.org/proper/commons-lang/">Apache's Commons Lang</a>
* for a more comprehensive suite of {@code String} utilities.
*
* <p>This class delivers some simple functionality that should really be
* provided by the core Java {@link String} and {@link StringBuilder}
* classes. It also provides easy-to-use methods to convert between
* delimited strings, such as CSV strings, and collections and arrays.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Keith Donald
* @author Rob Harrop
* @author Rick Evans
* @author Arjen Poutsma
* @author Sam Brannen
* @author Brian Clozel
* @since 16 April 2001
*/
public abstract class StringUtils {
/**
* Decode the given encoded URI component value. Based on the following rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"},
* and {@code "0"} through {@code "9"} stay the same.</li>
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
* <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li>
* </ul>
*
* @param source the encoded String
* @param charset the character set
* @return the decoded value
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
* @since 5.0
* @see java.net.URLDecoder#decode(String, String)
*/
public static String uriDecode(String source, Charset charset) {}
package com.example.demo;
/**
* 類 {@code OrderService} 訂單服務層.
*
* <p> 主要包括 創建訂單、取消訂單、查詢訂單等功能更
*
* @see Order
* @author <a href="mailto:[email protected]">Mengday Zhang</a>
* @since 2018/5/12
*/
public class OrderService {
/** 默認數量 {@value} */
private static final Integer QUANTITY = 1;
/**
* 創建訂單.
*
* <p> 創建訂單需要傳用戶id和商品列表(商品id和商品數量).
*
* <p><pre>{@code
* 演示如何使用該方法
* List<Goods> items = new ArrayList<>();
* Goods goods = new Goods(1L, BigDecimal.ONE);
* Goods goods2 = new Goods(2L, BigDecimal.TEN);
* items.add(goods);
* items.add(goods2);
*
* Order order1 = new Order();
* order.setUserId("1");
* order.setItems(items);
* OrderService#createOrder(order);
* }
* </pre>
*
* @param order 訂單信息
* @throws NullPointerException 參數信息爲空
* @exception IllegalArgumentException 數量不合法
* @return 是否創建成功
* @version 1.0
* @see {@link Order}
*/
public boolean createOrder(Order order) throws IllegalArgumentException{
Objects.requireNonNull(order);
List<Goods> items = order.getItems();
items.forEach(goods -> {
BigDecimal quantity = goods.getQuantity();
if (quantity == null || BigDecimal.ZERO.compareTo(quantity) == 0) {
throw new IllegalArgumentException();
}
});
System.out.println("create order...");
return true;
}
}
五:生成Javadoc
idea生成javadoc https://www.cnblogs.com/cyberniuniu/p/5021910.html
通過IDEA生成Javadoc: Tools --> Generate JavaDoc -->
注意要配置編碼,如果不配送爲生成亂碼,還需要配置Output directory