1.什么是java doc
当我们写完java代码,别人要调用我们的代码的时候要是没有api文档是很痛苦的,只能跟进源码去一个个的看,一个个方法的猜,并且java本来就不是一个重复造轮子的游戏,一般一些常用的轮子早就已经早好了,直接拿来用就是。但是拿来用的时候往往由于api文档的缺失或者不规范,造成使用上的很多痛苦,大家在很多实际工作中经常也会遇到类似的场景:
公司多年累积下来的工具类或者提供底层能力的公共模块里面积累了很多能力,公司为了代码规范也要求我们尽量去调用这些工具类或者公共模块。但是:
没有api文档或者文档写的很烂
参数列表动不动就很长,数十个甚至几十个参数
参数列表没有注释,出现一些莫名其妙的参数,都不知道怎么传
方法名也不能见名知意
造成往往要用这些公共能力的时候甚至还要去读源码
有读源码这个时间,可能自己都重新写了一个了,而且自己写的,可能比祖传下来的那些工具类还更“清爽”一些。于是系统内越来越多工具类堆积,重复造的轮子越来越多,“屎山”越堆越高。
即使有时候我们有api文档,但是各类api文档,格式,内容都不相同,没有统一的规范,读起来其实也很慢。所以有没有一个统一的规范喃?java官方其实早就想到了这个问题,在jdk1.1发布的时候就附带了java doc,支持用标签注释的方式给各个方法做好规范的说明,然后用java命令一键生成可视化的html页面作为api。
2.标签
标签是java doc的核心,用什么标签,java doc最后就会对应生成哪些api文档内容:
@author:
/** * @author john doe */ public class myclass { }
@version:
/** * @version 1.0.1 */ public class myclass { }
@param:
/** * concatenates two strings. * @param string1 the first string to concatenate. * @param string2 the second string to concatenate. * @return the concatenated string. */ public string concatenatestrings(string string1, string string2) { return string1 + string2; }
@return:
/** * returns the sum of two integers. * @param num1 the first integer. * @param num2 the second integer. * @return the sum of num1 and num2. */ public int add(int num1, int num2) { return num1 + num2; }
@throws 或 @exception: 描述方法可能抛出的异常。
/** * divides two numbers and throws an exception if the divisor is zero. * @param dividend the number to be divided. * @param divisor the divisor. * @return the result of the division. * @throws arithmeticexception if the divisor is zero. */ public double safedivide(double dividend, double divisor) { if (divisor == 0) { throw new arithmeticexception("divisor cannot be zero."); } return dividend / divisor; }
@see:
/** * see {@link java.util.arraylist} for more information on dynamic arrays. */ public class mydynamicarray { }
@link: 创建一个链接到其他类、方法或字段的链接。
/** * this method uses the {@link java.util.collections#shuffle(list)} method to randomize the list. */ public void shufflelist(list<?> list) { collections.shuffle(list); }
@since: 指定该元素是从哪个版本开始引入的。
/** * a utility class for working with dates. * @since 1.5 */ public class dateutils { }
@deprecated: 标记不再推荐使用的元素。
/** * old method that should not be used anymore. * @deprecated use the {@link #newmethod()} instead. */ @deprecated public void oldmethod() { }
@inheritdoc: 继承父类或接口的 javadoc。
/** * {@inheritdoc} */ @override public void somemethod() { // implementation }
@parametrictype: 用于描述泛型类型参数。
/** * represents a generic collection. * @param <e> the type of elements in this collection. */ public class mycollection<e> { }
@serialfield 和 @serialdata: 用于序列化类的字段和数据。
/** * a serializable class. * @serialfield name the name of the object. * @serialdata the length of the name. */ @serial private static final long serialversionuid = 1l; private string name; // serialization logic
3.命令
javadoc命令用于生成api文档,其支持多种参数:
javadoc [options] [source files]
常用参数:
- -d <directory>: 指定输出目录,存放生成的 html 文件。
- -sourcepath <pathlist>: 指定源文件路径,可以是多个路径,用分隔符(如冒号或分号)分隔。
- -subpackages <packagename>: 递归处理指定包及其子包下的所有类。
- -classpath <classpath>: 设置类路径,用于解析类型引用。
- -encoding <encoding>: 指定源文件的字符编码。
- -charset <charset>: 指定生成文档时使用的字符集。
- -windowtitle <text>: 设置文档窗口标题。
- -doctitle <text>: 设置文档页面的标题。
- -overview <filename>: 指定概述文件,用于文档的首页内容。
- -exclude <patternlist>: 指定要排除的包或类的模式列表。
- -private: 包含私有成员的文档。
- -protected: 默认行为,包含受保护和公开成员的文档。
- -public: 只包含公共成员的文档。
示例:
假设你有一个名为 com.example 的包,位于 /src/main/java 目录下,你想生成包含所有公共和受保护成员的 api 文档,并将输出文件保存在 /docs/api 目录下,同时指定字符编码为 utf-8,可以使用以下命令:
javadoc -encoding utf-8 -charset utf-8 -d /docs/api -sourcepath /src/main/java -subpackages com.example
搞一个类来把注解都用上,然后生成api文档看看:
package com.eryi.config; import java.io.file; import java.io.filewriter; import java.io.ioexception; /** * @author john doe * @version 1.0.0 * @since 2022-04-01 * @deprecated since version 1.1.0, use the {@link betterfilemanager} instead. * this class provides basic file management operations. */ @deprecated public class filemanager { /** * @param filepath the path of the file to check. * @return true if the file exists, false otherwise. * @see file#exists() * @throws nullpointerexception if the filepath is null. */ public boolean fileexists(string filepath) { if (filepath == null) { throw new nullpointerexception("filepath cannot be null."); } file file = new file(filepath); return file.exists(); } /** * @param filename the name of the file to create. * @param content the content to write into the file. * @return the newly created file. * @throws ioexception if there's any issue creating or writing to the file. * @see file#createnewfile() * @see filewriter */ public file createfilewithcontent(string filename, string content) throws ioexception { file file = new file(filename); if (!file.createnewfile()) { throw new ioexception("failed to create file: " + filename); } try (filewriter writer = new filewriter(file)) { writer.write(content); } return file; } /** * a sample file path constant. * @since 1.0.0 */ @deprecated public static final string sample_file_path = "resources/sample.txt"; /** * @param args command-line arguments (not used in this example). */ public static void main(string[] args) { filemanager filemanager = new filemanager(); system.out.println("does sample file exist? " + filemanager.fileexists(sample_file_path)); } }
直接javadoc:
会在路径下生成一堆东西,需要用的只有index.html,其它的都是为了支持这个index.html的资源文件而已:
看看效果:
可以看到关于这个类的什么都有:
总结
到此这篇关于java如何用javadoc生成标准的java api文档的文章就介绍到这了,更多相关javadoc生成java api文档内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论