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 中包含的一些错误假设,以及解决这些假设的机制。不保证编译,因为它只是剪切/粘贴较大重写的元素。
怪异:
- 有趣的是
GroovyRootDoc.classes
总是返回null
(不确定为什么)。有必要遍历每个包,并检查作为每个包子集的类。这令人惊讶,但无论如何都是理想的用法。
- 上面的代码仅适用于 groovy 文件,不适用于 java。我认为 GroovyDoc 对一些 java1.8 语法感到窒息(但我不确定)。