javadoc:jdk 9通过javadoc API读取java源码中的注释信息(comment)

时间:2024-09-29 19:16:29
import org.junit.Test; import static org.junit.Assert.*; import java.util.List; import java.util.Locale; import java.util.Set; import java.util.spi.ToolProvider; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import javax.tools.Diagnostic.Kind; import com.sun.source.doctree.BlockTagTree; import com.sun.source.doctree.DocCommentTree; import com.sun.source.util.DocTrees; import jdk.javadoc.doclet.Doclet; import jdk.javadoc.doclet.DocletEnvironment; import jdk.javadoc.doclet.Reporter; /** * JDK 9的javadoc tool 调用测试 * @author guyadong * */ public class JavadocToolTest { @Test public void testJavadocTool() { try { /** 获取 javadoc tool */ ToolProvider javadocTool = ToolProvider.findFirst("javadoc").orElseThrow(); int returnCode = javadocTool.run(System.out,System.err,new String[] { /** 指定自定义的 Doclet 接口实现类(全名) */ "-doclet", DocletExample.class.getName(), /** 指定-doclet选项定义类名的所在的类搜索路径 */ "-docletpath", DocletExample.class.getProtectionDomain().getCodeSource().getLocation().getPath(), /** --subpackages 要获取注释的包名 */ "-subpackages", "net.gdface.utils", /** --sourcepath 要源码路径 */ "-sourcepath","D:/j/common-java/common-base/src/main/java", /** --classpath 指定javadoc执行时搜索引用类的路径 */ "-classpath","D:/j/common-java/common-base/target/classes", "-encoding","utf-8"}); if(0 != returnCode){ System.out.printf("javadoc ERROR CODE = %d\n", returnCode); throw new IllegalStateException(); } } catch (Throwable e) { e.printStackTrace(); fail(); } } public static class DocletExample implements Doclet { private Reporter reporter; private Elements elementUtils; @Override public void init(Locale locale, Reporter reporter) { reporter.print(Kind.NOTE, "Doclet using locale: " + locale); this.reporter = reporter; } /** 输出类成员的注释 */ public void printElement(DocTrees trees, Element e) { DocCommentTree docCommentTree = trees.getDocCommentTree(e); if (docCommentTree != null) { reporter.print(Kind.NOTE, "Element " + e.getKind() + ": " + e); // 输出对应的注解 reporter.print(Kind.NOTE,elementUtils.getDocComment(e)); } } @Override public boolean run(DocletEnvironment docEnv) { // get the DocTrees utility class to access document comments DocTrees docTrees = docEnv.getDocTrees(); elementUtils = docEnv.getElementUtils(); // 循环调用printElement输出每个类成员的注释 for (TypeElement t : ElementFilter.typesIn(docEnv.getIncludedElements())) { reporter.print(Kind.NOTE, t.getKind() + ":" + t.getQualifiedName()); reporter.print(Kind.NOTE, "getDocComment:"+elementUtils.getDocComment(t)); for (Element e : t.getEnclosedElements()) { printElement(docTrees, e); } } return true; } @Override public String getName() { return "DocletExample"; } @Override public Set<? extends Option> getSupportedOptions() { Option[] options = {}; return Set.of(options); } @Override public SourceVersion getSupportedSourceVersion() { // support the latest release return SourceVersion.latest(); } } }