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

时间:2012-10-23 06:44:35

标签: java javadoc

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

3 个答案:

答案 0 :(得分:3)

请参阅Javadoc Tool Home Page Doclets 部分了解标准方法。

  

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

特别参见Example - Subclassing the Standard Doclet& Doclet API

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

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

FilteredLineIteratorXBN-Java的一部分.Jars可以下载here。)

示例顶部和设置:

   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,其中仅保留中间(不是开始或结束)行。为了防止在阻止打开之前找到错误的&#34;结束行&#34;错误 - 因为JavaDoc和&#34;正常&#34;的结束行(非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>

要从每一行中删除可选的星号,包括任何前面的空格,请添加一个&#34;中线变换器&#34;到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()  {
   }
}