import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.PackageDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import java.io.FileInputStream; public class Test { public static void main(String[] args) throws Exception{ FileInputStream in = new FileInputStream("/proj/javaparse/src/main/java/modd/dd/dda/Course.java"); // parse the file CompilationUnit cu = JavaParser.parse(in); // prints the resulting compilation unit to default system output // System.out.println(cu.toString()); cu.accept(new MethodVisitor(), null); } private static class MethodVisitor extends VoidVisitorAdapter<Void> { @Override public void visit(MethodDeclaration n, Void arg) { /* here you can access the attributes of the method. this method will be called for all methods in this CompilationUnit, including inner class methods */ System.out.println("method:"+n.getName()); super.visit(n, arg); } @Override public void visit(ClassOrInterfaceDeclaration n, Void arg) { System.out.println("class:"+n.getName()); System.out.println("extends:"+n.getExtendedTypes()); System.out.println("implements:"+n.getImplementedTypes()); super.visit(n, arg); } @Override public void visit(PackageDeclaration n, Void arg) { System.out.println("package:"+n.getName()); super.visit(n, arg); } } }
//只有parser <dependency> <groupId>com.github.javaparser</groupId> <artifactId>javaparser-core</artifactId> <version>3.6.5</version> </dependency> //或者 //symbolsolver 和 parser <dependency> <groupId>com.github.javaparser</groupId> <artifactId>javaparser-symbol-solver-core</artifactId> <version>3.6.5</version> </dependency>
标签:github,java,javaparser,com,System,arg,import,数等,javaParse From: https://www.cnblogs.com/chenchengzi/p/17648661.html