提取javadoc_使用JavaParser从源文件中提取JavaDoc文档
提取javadoc
很多人正在使用JavaParser實現(xiàn)最不同的目標。 其中之一是提取文檔。 在這篇簡短的文章中,我們將看到如何打印與類或接口關(guān)聯(lián)的所有JavaDoc注釋。
可以在GitHub上找到代碼: https : //github.com/ftomassetti/javadoc-extractor
獲取類的所有Javadoc注釋
我們正在重用DirExplorer,在提出了支持類的介紹JavaParser類 。 此類允許遞歸處理目錄,解析其中包含的所有Java文件。
我們可以從遍歷所有類開始并找到相關(guān)的Javadoc注釋。
/*** Iterate over the classes and print their Javadoc.*/ public class ClassesJavadocExtractor {public static void main(String[] args) {File projectDir = new File("source_to_parse/");new DirExplorer((level, path, file) -> path.endsWith(".java"), (level, path, file) -> {try {new VoidVisitorAdapter<Object>() {@Overridepublic void visit(ClassOrInterfaceDeclaration n, Object arg) {super.visit(n, arg);if (n.getComment() != null && n.getComment() instanceof JavadocComment) {String title = String.format("%s (%s)", n.getName(), path);System.out.println(title);System.out.println(Strings.repeat("=", title.length()));System.out.println(n.getComment());}}}.visit(JavaParser.parse(file), null);} catch (IOException e) {new RuntimeException(e);}}).explore(projectDir);}}如您所見,獲取JavaDoc注釋非常容易。 它產(chǎn)生以下結(jié)果:
ASTParserConstants (/javaparser/javaparser-core/target/generated-sources/javacc/com/github/javaparser/ASTParserConstants.java) ============================================================================================================================== /*** Token literal values and constants.* Generated by org.javacc.parser.OtherFilesGen#start()*/ParseException (/javaparser/javaparser-core/target/generated-sources/javacc/com/github/javaparser/ParseException.java) ====================================================================================================================== /*** This exception is thrown when parse errors are encountered.* You can explicitly create objects of this exception type by* calling the method generateParseException in the generated* parser.** You can modify this class to customize your error reporting* mechanisms so long as you retain the public fields.*/ASTParser (/javaparser/javaparser-core/target/generated-sources/javacc/com/github/javaparser/ASTParser.java) ============================================================================================================ /*** This class was generated automatically by javacc, do not edit.*/ ASTParserTokenManager (/javaparser/javaparser-core/target/generated-sources/javacc/com/github/javaparser/ASTParserTokenManager.java) ==================================================================================================================================== /** Token Manager. */獲取所有Javadoc注釋并找到記錄的元素
在其他情況下,我們可能要開始收集所有Javadoc注釋,然后找到要注釋的元素。 我們也可以使用Javaparser輕松做到這一點:
/*** Iterate over all the Javadoc comments and print them together with a description of the commented element.*/ public class AllJavadocExtractor {public static void main(String[] args) {File projectDir = new File("source_to_parse/");new DirExplorer((level, path, file) -> path.endsWith(".java"), (level, path, file) -> {try {new VoidVisitorAdapter<Object>() {@Overridepublic void visit(JavadocComment comment, Object arg) {super.visit(comment, arg);String title = null;if (comment.getCommentedNode().isPresent()) {title = String.format("%s (%s)", describe(comment.getCommentedNode().get()), path);} else {title = String.format("No element associated (%s)", path);}System.out.println(title);System.out.println(Strings.repeat("=", title.length()));System.out.println(comment);}}.visit(JavaParser.parse(file), null);} catch (IOException e) {new RuntimeException(e);}}).explore(projectDir);}private static String describe(Node node) {if (node instanceof MethodDeclaration) {MethodDeclaration methodDeclaration = (MethodDeclaration)node;return "Method " + methodDeclaration.getDeclarationAsString();}if (node instanceof ConstructorDeclaration) {ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration)node;return "Constructor " + constructorDeclaration.getDeclarationAsString();}if (node instanceof ClassOrInterfaceDeclaration) {ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration)node;if (classOrInterfaceDeclaration.isInterface()) {return "Interface " + classOrInterfaceDeclaration.getName();} else {return "Class " + classOrInterfaceDeclaration.getName();}}if (node instanceof EnumDeclaration) {EnumDeclaration enumDeclaration = (EnumDeclaration)node;return "Enum " + enumDeclaration.getName();}if (node instanceof FieldDeclaration) {FieldDeclaration fieldDeclaration = (FieldDeclaration)node;List<String> varNames = fieldDeclaration.getVariables().stream().map(v -> v.getName().getId()).collect(Collectors.toList());return "Field " + String.join(", ", varNames);}return node.toString();}}在這里,大多數(shù)代碼都是關(guān)于為注釋節(jié)點提供描述的(方法describe )。
結(jié)論
操作AST并找到Javadoc注釋非常容易。 但是,缺少的一項功能是可以以結(jié)構(gòu)化形式提取Javadoc中包含的信息。 例如,您可能只想獲取與某個參數(shù)或返回值關(guān)聯(lián)的Javadoc部分。 Javaparser當前不具有此功能,但是我正在研究此功能,應該在接下來的1-2周內(nèi)將其合并。 如果要關(guān)注開發(fā),請查看問題433 。
感謝您的閱讀和解析!
翻譯自: https://www.javacodegeeks.com/2017/01/extracting-javadoc-documentation-source-files-using-javaparser.html
提取javadoc
總結(jié)
以上是生活随笔為你收集整理的提取javadoc_使用JavaParser从源文件中提取JavaDoc文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: url转发 备案(Url备案库)
- 下一篇: mega2560单片机开发_[MEGA