依赖
<dependencies>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
代码
package com.xxx.fsq;
import org.apache.commons.io.FileUtils;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.eclipse.jdt.core.dom.AST.JLS8;
public class JdtTest {
public static void main(String[] args) {
ASTParser parser = ASTParser.newParser(AST.JLS8); //设置Java语言规范版本
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setCompilerOptions(null);
parser.setResolveBindings(true);
Map<String, String> compilerOptions = JavaCore.getOptions();
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); //设置Java语言版本
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
parser.setCompilerOptions(compilerOptions); //设置编译选项
String src = null;
try {
src = FileUtils.readFileToString(new File("XXXController.java"), "UTF-8"); //要解析的文件
} catch (Exception e) {
e.printStackTrace();
}
parser.setSource(src.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null); //下个断点可以看看cu的types成员就是整个语法树
System.out.println(cu);
//get url
String classUrl = null;
String methodUrl = null;
String className = null;
String methodName = null;
List types = cu.types();
for (Object type : types) {
TypeDeclaration typeDeclaration = (TypeDeclaration) type;
className = typeDeclaration.getName().getIdentifier();
List<Expression> expressionsClass = getExpressions(typeDeclaration);
if (expressionsClass == null) {
System.out.println("x");
}
for (Expression expression : expressionsClass) {
classUrl = expression.toString().replace("\"", "");
}
MethodDeclaration[] methods = typeDeclaration.getMethods();
for (MethodDeclaration method : methods) {
methodName = method.getName().getIdentifier();
List<Expression> expressionsMethod = getExpressions(method);
if (expressionsMethod == null) {
continue;
}
for (Expression expression : expressionsMethod) {
methodUrl = expression.toString().replace("\"", "");
System.out.println(className + "\t" + methodName + "\t" + classUrl + methodUrl);
}
}
}
}
public static List<Expression> getExpressions(BodyDeclaration bodyDeclaration) {
List methodModifiers = bodyDeclaration.modifiers();
for (Object methodModifier : methodModifiers) {
if (methodModifier instanceof SingleMemberAnnotation) {
SingleMemberAnnotation modifier = (SingleMemberAnnotation) methodModifier;
String typeName = modifier.getTypeName().getFullyQualifiedName();
if (!typeName.equals("RequestMapping")) {
continue;
}
Expression v = modifier.getValue();
ArrayList<Expression> list = new ArrayList<Expression>();
list.add(v);
return list;
} else if (methodModifier instanceof NormalAnnotation) {
NormalAnnotation modifier = (NormalAnnotation) methodModifier;
String typeName = modifier.getTypeName().getFullyQualifiedName();
if (!typeName.equals("RequestMapping")) {
continue;
}
List values = modifier.values();
for (Object value : values) {
MemberValuePair memberValuePair = (MemberValuePair) value;
SimpleName simapleName = memberValuePair.getName();
String name = simapleName.getIdentifier();
if (!name.equals("value")) {
continue;
}
Expression v = memberValuePair.getValue();
if (v instanceof ArrayInitializer) {
ArrayInitializer ev = (ArrayInitializer) v;
List expressions = ev.expressions();
return expressions;
} else if (v instanceof Expression) {
ArrayList<Expression> list = new ArrayList<Expression>();
list.add(v);
return list;
}
}
}
}
return null;
}
}