<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.25.1</version>
</dependency>
public class JavaParserExample {
@Test
public void parseMethodBody() throws FileNotFoundException {
// 读取Java源文件
File file = new File("D:\\silence\\fffffffff.java");
JavaParser javaParser = new JavaParser();
// 解析源文件
ParseResult<CompilationUnit> parse = javaParser.parse(file);
Optional<CommentsCollection> commentsCollection = parse.getCommentsCollection();
CommentsCollection commentsCollection1 = commentsCollection.get();
for (Comment comment : commentsCollection1.getComments()) {
Node node = comment.getCommentedNode().get();
if (node instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration)node;
BlockStmt blockStmt = methodDeclaration.getBody().get();
System.out.println(blockStmt);
System.out.println(blockStmt.getStatements().toString());
}
}
}
}