Java文档 : How to Create a Custom Markup

标签 java android kotlin integration-testing javadoc

我正在创建一套用 Kotlin 编写的仪器测试,它将影响大量 Web API。我计划将这些测试实现到我们的 CI/CD 过程中。话虽如此,我想为每个测试添加详细的文档,用于可维护性、验证场景覆盖率等。

目前,我正在使用 JavaDocs 编写文档;然而,只有少数标记,其中大部分与测试文档无关(@return、@see、@author、@param、@exception、@sample、@simple、@since、@suppress 和 @throws ).因此,我想知道是否有办法创建自定义标记并将它们实现到我的文档中?例如,@scenario 或@expected?

最佳答案

您需要使用自定义 doclet。参见 'Creating and handling custom tags'

Suppose, for example, that you want use a custom tag, say @mytag, in your documentation comments in addition to the standard tags like @param and @return. To make use of the information in your custom tags, you need to have your doclet use instances of Tag that represent your custom tags. One of the easiest ways to do that is to use the tags(String) method of Doc or one of Doc's subclasses. This method returns an array of Tag objects representing any tags whose name matches the string argument. For example, if method is an instance of MethodDoc, then

method.tags("mytag")

would return an array of Tag objects representing any @mytag tags in the method's documentation comment. You can then access the information in your @mytag tags with Tag's text method. That method returns a string representing the content of the tag which you can parse or use as needed. For example, if a documentation comment contained one of your custom tags like this:

@mytag Some dummy text.

then the text method would return the string "Some dummy text.". Here's a standalone doclet (not a subclass of the standard doclet) that uses these ideas to print out the text associated with all instances of a specified tag that it finds in method comments. It could be extended to find all instances of that tag in all comments.

import com.sun.javadoc.*;

public class ListTags {
    public static boolean start(RootDoc root){ 
        String tagName = "mytag";
        writeContents(root.classes(), tagName);
        return true;
    }

    private static void writeContents(ClassDoc[] classes, String tagName) {
        for (int i=0; i < classes.length; i++) {
            boolean classNamePrinted = false;
            MethodDoc[] methods = classes[i].methods();
            for (int j=0; j < methods.length; j++) {
                Tag[] tags = methods[j].tags(tagName);
                if (tags.length > 0) {
                    if (!classNamePrinted) {
                        System.out.println("\n" + classes[i].name() + "\n");
                        classNamePrinted = true;
                    }
                    System.out.println(methods[j].name());
                    for (int k=0; k < tags.length; k++) {
                        System.out.println("   " + tags[k].name() + ": " 
                        + tags[k].text());
                    }
                } 
            }
        }
    }
}

The tag for which this doclet searches is specified by the variable tagName. The value of the tagName string can be any tag name, custom or standard. This doclet writes to standard out, but its output format could be modified, for example, to write HTML output to a file.

关于Java文档 : How to Create a Custom Markup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51467580/

相关文章:

android - Cordova 构建和 gradle 依赖项

android - 它如何使用微调器过滤掉我的 sqlite 数据?

android - LazyColumn 内的 LazyHorizo​​ntalGrid

java - 在 eclipse 中使用 Spring MVC 5 的新项目,但出现错误 404

java - 冒泡异常 : Java

java - 要求用户输入构造函数的参数?

android - Recyclerview插入数据时自动向上滚动

Kotlin ConflatedBroadcastChannel.offer() 不起作用?

android - 修剪字符串函数中的 "it <= ' '"是什么意思

java - 如何使用 Undertow 服务器在 IIS 上托管 Spring Boot 可执行 jar 文件?