我找到了解决方案。我使用递归函数在 sourceDirectory 上循环以查找所有文件。以下是我从 mojo 获取 sourceDirectory 的方法:
/**
* Project's source directory as specified in the POM.
*
* @parameter expression="${project.build.sourceDirectory}"
* @readonly
* @required
*/
private final File sourceDirectory = new File("");
fillListWithAllFilesRecursiveTask(sourceDirectory);
我得到了这些文件的路径,然后我使用 com.google.code.javaparser 来解析关联的 FileInputStream。
public void fillListWithAllFilesRecursiveTask(final File root) {
CompilationUnit cu;
FileInputStream in;
try {
// we looped through root and we found a file (not a directory)
in = new FileInputStream(file);
cu = JavaParser.parse(in);
new MethodVisitor().visit(cu, file);
最后,我扩展了 VoidVisitorAdapter 以获取注释和成员...等
private static class MethodVisitor extends VoidVisitorAdapter<File> {
@Override
public void visit(ClassOrInterfaceDeclaration n, File file) {
if (n.getAnnotations() != null) {
// store classes that are annotated
for (AnnotationExpr annotation : n.getAnnotations()) {
//here some logic
}
} ...