【问题标题】:Custom GroovyDocTool not finding anything自定义 GroovyDocTool 没有找到任何东西
【发布时间】:2016-01-26 22:59:35
【问题描述】:

我在使用 GroovyDocTool 时遇到困难。

无论我传入什么路径,我的rootDoc 都不会解析为任何包、类或方法。我不得不假设我只是走错了路,但我这辈子不知道要通过什么。

class TestGroovyDoclet extends GroovyDocTool{
    public TestGroovyDoclet() {
        super([
            '~/ ... /src/'
            ,'/home/ ... /src/ ... /Sample/'
            ,'/home/ ... /src/ ... /Sample/Results.groovy'
        ]);
    }
    public void Execute(){
        System.out.println('Begin Processing Javadoc');
        System.out.format(' p %d\n', rootDoc.specifiedPackages().length);
        System.out.format(' c %d\n', rootDoc.classes().length);
        System.out.println('Finished Processing Javadoc');
    }
    public static void main(String[] args){
        (new TestGroovyDoclet()).with{
            it.Execute();
        }
    }
}

哪些参数被认为是传递给GroovyRootDocBuilder的有效参数?

【问题讨论】:

    标签: groovy groovydoc


    【解决方案1】:

    GroovyDoc 的作者似乎打算从 Ant 中使用它。这意味着无法查找要处理的文件,因为您应该已经知道正在处理的文件。消费者必须指定要处理的单个文件,并且调用buildPath来实际处理文件。 (参见 buildPath 的覆盖)

    对于那些只想与文档树交互的人来说,GroovyDocTool 确实没有任何价值,用户可能会更好地扩展实际上包含处理内容的GroovyRootDocBuilder。如果你这样做,你还不如重写构造函数来完全隐藏GroovyDocTool。 (参见新的构造函数)

    class TestGroovyDoclet extends GroovyRootDocBuilder{
        protected def sourcepaths = [];
        public TestGroovyDoclet() {
            //HARDCODE: some test values for my testing
            this([
                '~/ ... /src/'
                ,'/home/ ... /src/ ... /Sample/'
                ,'/home/ ... /src/ ... /Sample/Results.groovy'
            ]);
        }
        public TestGroovyDoclet(String[] sourcepaths) {
            //hide the unused GroovyDocTool
            super(new GroovyDocTool(), sourcepaths, new ArrayList<LinkArgument>(), new Properties());
            this.sourcepaths = sourcepaths;
        }
        /**
         * Builds the tree by recursively searching for files
         * <p>
         * It is likely useful to override the original buildTree 
         * method as well to put some safeties in place. The parsing 
         * routines do not do a lot of validation. For those of us 
         * inheritting, it would be a good idea to override build 
         * tree ot sanitize the input list as much as possible.
         * </p>
         */
        @Override
        public void buildTree(){
            def list = [];
            // loop through the sourcepaths to recursively find the files
            for (String sourcepath : sourcepaths) {
                def root = new File(sourcepath);
                if(root.exists()){
                    if(root.isFile()){
                        list << root.absolutePath;
                    }
                    else{
                        root.eachFileRecurse (FileType.FILES) { file -> list << file.absolutePath; };
                    }
                }
            }
            buildTree(list);
        }
        /**
         * Method to actually do the processing. Sample only, does not demonstrate anything useful.
         */
        public void Execute(){
            buildTree();
            System.out.println('Begin Processing GroovyDoc');
            System.out.format(' p %d\n', rootDoc.specifiedPackages().length);
            //System.out.format(' c %d\n', rootDoc.classes().length);
            int count = 0;
            for(def p : rootDoc.specifiedPackages()){
                count += p.allClasses().length;
            }
            System.out.format(' c %d\n', count);
    
            System.out.println('Finished Processing GroovyDoc');
        }
    
        public static void main(String[] args){
            (new TestGroovyDoclet()).with{
                it.Execute();
            }
        }
    }
    

    上面的代码并不完美,也不完全是我想出的;相反,该代码旨在突出显示 OP 中包含的一些错误假设,以及解决这些假设的机制。不保证编译,因为它只是剪切/粘贴较大重写的元素。

    怪事:

    1. 有趣的是GroovyRootDoc.classes 总是返回null(不确定为什么)。有必要遍历每个包,并检查作为每个包子集的类。这令人惊讶,但无论如何都是理想的用法。
    2. 以上代码仅适用于 groovy 文件,不适用于 java。我认为 GroovyDoc 对一些 java1.8 语法感到窒息(但我不确定)。

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多