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

8

我该如何从Java源文件中提取JavaDoc注释?并且按照自己的格式进行排版呢?

3个回答

3

1
使用以下命令生成:javadoc *.java,然后按照您的要求重写stylesheet.css...

不,楼主说的不仅仅是更改样式。 - Andrew Thompson

0
作为替代选择,您可以考虑我编写的一个类,名为FilteredLineIterator,它可用于从源文件中提取所有JavaDoc行。
(此答案与我在this question中编写的答案类似。) FilteredLineIterator是一个字符串迭代器,它根据每一行所处的“实体”(单行、块和“隐形”块)对另一个迭代器中的元素进行过滤(保留或抑制)。可以选择修改保留的行。
FilteredLineIteratorXBN-Java的一部分。可以从here下载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;
/**
  <P>{@code java ExtractAllJavaDocBlockTextRaw examples\com\github\xbn\examples\linefilter\JavaClassWithOneCommentAndTwoJavaDocBlocks_input.txt}</P>
 **/
public class ExtractAllJavaDocBlockTextRaw  {
  public static final void main(String[] cmd_lineParams)  {
     //Example setup:
        Iterator<String> rawInputLineItr = GetFromCommandLineAtIndex.fileLineIterator(
           cmd_lineParams, 0,
           null);   //debugPath

主要部分如下。JavaDoc块被定义为block entity,其中仅保留中间行(而不是开头或结尾)。为了防止出现“在块打开之前找到结束行”的错误 - 因为JavaDoc和“普通”(非JavaDoc)多行注释的结束行都是*/ - 必须声明一个stealth block用于普通多行注释。

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

     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());
     }
  }
}

输出(输入文件位于此回答帖子的底部):

        <P>The main class JavaDoc block.</P>
      <P>Constructor JavaDoc block</P>
      * <P>Function JavaDoc block.</P>

      * <P>This function does some stuff.</P>

      * <P>Lots and lots of stuff.</P>

为了从每行中去除可选的星号,包括任何前导空格,请向JavaDoc块实体添加“中间行修改器”:

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

通过将 keepMidsOnly().build(); 更改为以下内容,将修改器添加到块实体中:

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

输出:

<P>The main class JavaDoc block.</P>
<P>Constructor JavaDoc block</P>
<P>Function JavaDoc block.</P>

<P>This function does some stuff.</P>

<P>Lots and lots of stuff.</P>

输入文件:

/*
   A Java comment block.
 */
package  fully.qualified.package.name;
/**
   <P>The main class JavaDoc block.</P>
 */
public class StayClassy  {
   /**
      <P>Constructor JavaDoc block</P>
    */
   public StayClassy()  {
      //Do stuff
   }
   /**
      * <P>Function JavaDoc block.</P>

      * <P>This function does some stuff.</P>

      * <P>Lots and lots of stuff.</P>
    */
   public void doStuff()  {
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接