1、引入依赖 下载路径 https://download.csdn.net/download/wer754105453/87970837
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.6.16</version>
<scope>compile</scope>
</dependency>
2、抽取代码
引入包:
import com.alibaba.fastjson.JSON;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.utils.ParserCollectionStrategy;
import com.github.javaparser.utils.ProjectRoot;
public class Test{
public static void main(String[] args) throws Exception {
final String filter = ".com.";
Map<String,ClazzDesc> map = new HashMap<String,ClazzDesc>();
List<CompilationUnit> clazzs = clazzParse();
clazzs.forEach(clazz -> {
if(clazz.getPackageDeclaration().isPresent()){
String key = clazz.getPackageDeclaration().get().getName() + "." + clazz.getPrimaryTypeName().get();
map.put(key, new DccFetchDataController().new ClazzDesc());
//设置类路径
map.get(key).setClzzResPath(clazz.getStorage().get().getPath().toString());
//查找类实现的接口、父类、
final NodeList<ClassOrInterfaceType> extendClaszzs = new NodeList<>();
final NodeList<ClassOrInterfaceType> implClaszzs = new NodeList<>();
clazz.getTypes().forEach(type -> {
if (type instanceof com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) {
//设置是否为接口
map.get(key).setInterface(((ClassOrInterfaceDeclaration) type).isInterface());
extendClaszzs.addAll(((ClassOrInterfaceDeclaration) type).getExtendedTypes());
implClaszzs.addAll(((ClassOrInterfaceDeclaration) type).getImplementedTypes());
//else if (type instanceof com.github.javaparser.ast.body.EnumDeclaration)
//else if (type instanceof com.github.javaparser.ast.body.AnnotationDeclaration)
}
//查找导入类
clazz.getImports().forEach(importVal->{
if (importVal.getNameAsString().contains(filter)) {
map.get(key).getImportClazzList().add(importVal.getNameAsString());
extendClaszzs.forEach(ext -> {
if (importVal.getNameAsString().endsWith(ext.getNameAsString())) {
map.get(key).setExtendsList(importVal.getNameAsString());
}
});
implClaszzs.forEach(impt -> {
if (importVal.getNameAsString().endsWith(impt.getNameAsString())) {
map.get(key).getImplFaceList().add(importVal.getNameAsString());
}
});
}
});
});
}
});
//System.out.println(JSON.toJSONString(map));
Set<String> set = exportRefFile("com.xx.clasName",map,new HashSet<>(),true);
System.out.println(JSON.toJSONString(set));
// Runtime runtime = Runtime.getRuntime();
// for (String filePath : set) {
// Process process = runtime.exec("cmd.exe zip d:/2.zip "+ filePath);
// System.out.println(process);
// }
}
static Set<String> exportRefFile(String className, Map<String, ClazzDesc> map, Set<String> set,boolean isUp) {
ClazzDesc clazzDesc = map.get(className);
set.add(clazzDesc.getClzzResPath());
if(isUp){
String extendsClazz = clazzDesc.getExtendsList();
List<String> implFaces = clazzDesc.getImplFaceList();
List<String> importClazzs = clazzDesc.getImportClazzList();
List<String> tmps = new ArrayList<>();
//向上找父类继承,接口实现
if (extendsClazz != null)
tmps.add(extendsClazz);
if (implFaces.size() > 0)
tmps.addAll(implFaces);
if (importClazzs.size() > 0)
tmps.addAll(importClazzs);
tmps.forEach(item -> {
ClazzDesc tmp = map.get(item);
if (tmp != null && set.add(tmp.getClzzResPath())) {
exportRefFile(item, map, set,isUp);
}
});
}
//向下找接口实现类
if(clazzDesc.isInterface()){
map.forEach((key,value)->{
if( ( (ClazzDesc) value).getImplFaceList().contains(className) &&
set.add( ((ClazzDesc) value).getClzzResPath() )){
exportRefFile(key, map, set,true);
exportRefFile(key, map, set,false);
}
});
}
return set;
}
private static List<CompilationUnit> clazzParse() {
Path root = Paths.get("D:/ecp_2380/bb_hb/dev/111/componentcenter");
ProjectRoot projectRoot = new ParserCollectionStrategy().collect(root);
List<CompilationUnit> cuList = new ArrayList<CompilationUnit>();
projectRoot.getSourceRoots().forEach(sourceRoot -> {
try {
// 解析source root
sourceRoot.tryToParse();
} catch (IOException e) {
e.printStackTrace();
}
cuList.addAll(sourceRoot.getCompilationUnits());
});
return cuList;
}
public class ClazzDesc{
String extendsList;
String clzzResPath;
boolean isInterface;
List<String> implFaceList = new ArrayList<String>();
List<String> importClazzList = new ArrayList<String>();
public ClazzDesc(){}
public String getExtendsList() {
return extendsList;
}
public void setExtendsList(String extendsList) {
this.extendsList = extendsList;
}
public List<String> getImplFaceList() {
return implFaceList;
}
public void setImplFaceList(List<String> implFaceList) {
this.implFaceList = implFaceList;
}
public List<String> getImportClazzList() {
return importClazzList;
}
public void setImportClazzList(List<String> importClazzList) {
this.importClazzList = importClazzList;
}
public String getClzzResPath() {
return clzzResPath;
}
public void setClzzResPath(String clzzResPath) {
this.clzzResPath = clzzResPath;
}
public boolean isInterface() {
return isInterface;
}
public void setInterface(boolean isInterface) {
this.isInterface = isInterface;
}
}
}