package springcloud_producer_8001;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.FileAlreadyExistsException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class AppTest {
private Set<String> classResult = new HashSet<String>();
private Set<String> methodResult = new HashSet<String>();
private List<String> searchPackages;
public AppTest(List<String> searchPackages) {
if(searchPackages == null || searchPackages.isEmpty()) {
throw new IllegalArgumentException("searchPackages不能为空");
}
this.searchPackages = searchPackages;
}
public void parseAllUsedImportClassAndMethods(String path) throws Exception {
File file = new File(path);
this.getAllUsedImportClassAndMethods(file);
this.getMethodParams();
}
private void getAllUsedImportClassAndMethods(File f) throws Exception {
if(f.isDirectory()) {
File[] listFiles = f.listFiles();
for(File file :listFiles) {
this.getAllUsedImportClassAndMethods(file);
}
}else {
if(f.getName().endsWith(".java")) {
this.getFileText(f);
}
}
}
private void getFileText(File file) throws Exception{
FileReader reader = new FileReader(file);
char[] buf = new char[1024];
StringBuilder sb = new StringBuilder();
while(reader.read(buf ) != -1) {
sb.append(buf);
}
Set<String> importClass = this.getImport(sb.toString());
this.getMethods(sb.toString(), importClass);
reader.close();
}
private Set<String> getImport(String fileText) {
StringBuilder sb = new StringBuilder();
sb.append("import +?(");
for(String packageStr :this.searchPackages) {
sb.append(packageStr).append(".*?").append("|");
}
if(sb.charAt(sb.length() - 1) == '|') {
sb.deleteCharAt(sb.length() - 1);
}
sb.append(") *;");
Pattern p = Pattern.compile(sb.toString());
Matcher m = p.matcher(fileText);
Set<String> thisResult = new HashSet<String>();
while(m.find()) {
for(int i = 1; i <= m.groupCount(); i++) {
String group = m.group(i);
classResult.add(group);
thisResult.add(group);
}
}
return thisResult;
}
private void getMethods(String fileText,Set<String> importClass) {
for(String clasz: importClass) {
String simpleClass = clasz.substring(clasz.lastIndexOf(".")+1);
Pattern p = Pattern.compile("private +"+simpleClass+" +(.*?);");
Matcher m = p.matcher(fileText);
if(m.find()) {
String fieldName = m.group(1);
Pattern p2 = Pattern.compile(fieldName+"\\.([a-zA-Z]+?)\\(.*?\\)");
Matcher m2 = p2.matcher(fileText);
while(m2.find()) {
for(int i = 1; i <= m2.groupCount(); i++) {
String group = m2.group(i);
methodResult.add(clasz + "." + group);
}
}
}
}
for(String clasz: importClass) {
String simpleClass = clasz.substring(clasz.lastIndexOf(".")+1);
Pattern p2 = Pattern.compile(simpleClass+"\\.([a-zA-Z]+?)\\(.*?\\)");
Matcher m2 = p2.matcher(fileText);
while(m2.find()) {
for(int i = 1; i <= m2.groupCount(); i++) {
String group = m2.group(i);
methodResult.add(clasz + "." + group);
}
}
}
}
@SuppressWarnings("rawtypes")
private void getMethodParams() {
this.methodResult = this.methodResult.stream().sorted().map((methodStr)->{
StringBuilder sb = new StringBuilder(methodStr);
sb.append("(");
int pointLastIndex = methodStr.lastIndexOf(".");
String methodName = methodStr.substring(pointLastIndex+1);
String className = methodStr.substring(0,pointLastIndex);
try {
Class<?> clasz = Class.forName(className);
Method[] declaredMethods = clasz.getDeclaredMethods();
for(Method method: declaredMethods) {
if(method.getName().equals(methodName)) {
Class<?>[] parameterTypes = method.getParameterTypes();
for(Class p: parameterTypes) {
sb.append(p.getSimpleName()).append(" ,");
}
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return methodStr;
}
if(sb.charAt(sb.length()-1) == ',') {
sb.deleteCharAt(sb.length()-1);
}
sb.append(")");
return sb.toString();
}).collect(Collectors.toSet());
}
public void export(String path) throws Exception {
File file = new File(path);
if(file.exists()) {
throw new FileAlreadyExistsException(path);
}
file.getParentFile().mkdirs();
FileWriter fw = new FileWriter(file);
fw.write("引用的类如下:\n");
for(String clasz: this.classResult) {
fw.write(clasz);
fw.write("\n");
}
fw.write("\n使用的引用类的方法如下:\n");
for(String method: this.methodResult) {
fw.write(method);
fw.write("\n");
}
fw.flush();
fw.close();
}
public void export(OutputStream out) throws Exception {
out.write("引用的类如下:\n".getBytes(Charset.forName("utf-8")));
for(String clasz: this.classResult) {
out.write(clasz.getBytes(Charset.forName("utf-8")));
out.write("\n".getBytes(Charset.forName("utf-8")));
}
out.write("\n使用的引用类的方法如下:\n".getBytes(Charset.forName("utf-8")));
for(String method: this.methodResult) {
out.write(method.getBytes(Charset.forName("utf-8")));
out.write("\n".getBytes(Charset.forName("utf-8")));
}
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
AppTest test = new AppTest(Arrays.asList("springcloud_producer.service"));
test.parseAllUsedImportClassAndMethods("D:\\workspace\\java_web\\springcloud-parent\\springcloud_producer\\src\\main\\java\\springcloud_producer");
test.export(System.out);
test.export("C:\\Users\\HongCheng\\Desktop\\2.txt");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律