如何从源文件中提取JavaDoc注释

如何从Java源文件中提取JavaDoc注释? 以及按我想要格式化它们?

有关标准方法,请参阅Javadoc工具主页的Doclets部分。

Doclet标准doclet生成HTML并内置到Javadoc工具中。 此处列出了Java Software开发的其他doclet。 ..

特别参见示例 – 对标准Doclet和Doclet API进行 子类化 。

使用:javadoc * .java生成它们,然后根据需要重写stylesheet.css …

作为替代方案,您可以考虑我编写的一个名为FilteredLineIterator的类,它可用于从源文件中删除所有JavaDoc行。

(这个答案类似于我在这个问题中写的那个。)

FilteredLineIterator是一个字符串迭代器,它根据每行存在的“实体”(单行,块和“秘密”块)过滤(保留或抑制)另一个迭代器中的元素。 可以选择修改保留线。

FilteredLineIterator是XBN-Java的一部分。可以在这里下载Jars。)

示例顶部和设置:

  import com.github.xbn.linefilter.FilteredLineIterator; import com.github.xbn.linefilter.KeepUnmatched; import com.github.xbn.linefilter.Returns; import com.github.xbn.linefilter.entity.BlockEntity; import com.github.xbn.linefilter.entity.EntityRequired; import com.github.xbn.linefilter.entity.KeepMatched; import com.github.xbn.linefilter.entity.NewBlockEntityFor; import com.github.xbn.linefilter.entity.NewStealthBlockEntityFor; import com.github.xbn.linefilter.entity.StealthBlockEntity; import com.github.xbn.testdev.GetFromCommandLineAtIndex; import com.github.xbn.util.IncludeJavaDoc; import java.util.Iterator; /** 

{@code java ExtractAllJavaDocBlockTextRaw examples\com\github\xbn\examples\linefilter\JavaClassWithOneCommentAndTwoJavaDocBlocks_input.txt}

**/ public class ExtractAllJavaDocBlockTextRaw { public static final void main(String[] cmd_lineParams) { //Example setup: Iterator rawInputLineItr = GetFromCommandLineAtIndex.fileLineIterator( cmd_lineParams, 0, null); //debugPath

主要部分从下面开始。 JavaDoc块被定义为块实体 ,其中仅保留中间(不是开始或结束)行。 为了防止在块打开之前找到错误的“结束行”错误 – 因为JavaDoc和“普通”(非JavaDoc)多行注释的结束行是*/ 隐藏块用于正常的多行注释必须是声明。

输入的原始行迭代器和两个实体都被提供给过滤的行迭代器。

  StealthBlockEntity javaMlcBlock = NewStealthBlockEntityFor.javaComment( "comment", IncludeJavaDoc.NO, null, //dbgStart (on=System.out, off=null) null, //dbgEnd KeepMatched.NO, EntityRequired.YES, null, null); //dbgLineNums BlockEntity javaDocBlock = NewBlockEntityFor.javaDocComment_Cfg( "doccomment", null, //dbgStart null, //dbgEnd EntityRequired.YES, null, null). //dbgLineNums keepMidsOnly().build(); FilteredLineIterator filteredItr = new FilteredLineIterator( rawInputLineItr, Returns.KEPT, KeepUnmatched.NO, null, null, //dbgEveryLine and its line-range javaMlcBlock, javaDocBlock); while(filteredItr.hasNext()) { System.out.println(filteredItr.next()); } } } 

输出(输入文件位于此答案的底部):

  

The main class JavaDoc block.

Constructor JavaDoc block

*

Function JavaDoc block.

*

This function does some stuff.

*

Lots and lots of stuff.

要从每一行中删除可选的星号,包括任何前面的空格,请在JavaDoc块实体中添加“mid line alterer”:

 TextLineAlterer asteriskStripper = NewTextLineAltererFor.replacement( Pattern.compile("[ \t]*(?:\\*[ \t]*)?(.*)"), "$1", ReplacedInEachInput.FIRST, null, //debug null); 

通过更改keepMidsOnly().build();将更改keepMidsOnly().build();添加到块实体 至

 midAlter(asteriskStripper).keepMidsOnly().build(); 

输出:

 

The main class JavaDoc block.

Constructor JavaDoc block

Function JavaDoc block.

This function does some stuff.

Lots and lots of stuff.

输入文件:

 /* A Java comment block. */ package fully.qualified.package.name; /** 

The main class JavaDoc block.

*/ public class StayClassy { /**

Constructor JavaDoc block

*/ public StayClassy() { //Do stuff } /** *

Function JavaDoc block.

*

This function does some stuff.

*

Lots and lots of stuff.

*/ public void doStuff() { } }