从https://javaparser.org/下载Java解析器
您必须编写一些代码。此代码将调用解析器…它将返回一个CompilationUnit:
InputStream in = null; CompilationUnit cu = null; try { in = new SEDInputStream(filename); cu = JavaParser.parse(in); } catch(ParseException x) { // handle parse exceptions here. } finally { in.close(); } return cu;注意:SEDInputStream是输入流的子类。您可以根据需要使用FileInputStream。
您必须创建一个访客。您的访问者会很轻松,因为您仅对方法感兴趣:
public class MethodVisitor extends VoidVisitorAdapter { public void visit(MethodDeclaration n, Object arg) { // extract method information here. // put in to hashmap } }要调用访客,请执行以下操作:
MethodVisitor visitor = new MethodVisitor(); visitor.visit(cu, null);



