我找到了解決方案。 我使用遞歸函數在sourceDirectory上循環查找所有文件。 這裏是我從我的魔力得到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
}
} ...