因为项目长时间积累接口比较多,需要删除不用接口,这里通过工具类递归遍历目录获取calss进行反射创建,获取接口方法以及相关SpringMvc注解,并进行解析。其他方式有很多,目前需求这个就可满足。
直接放入代码
package cn.crl.report;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* 类工具
*/
public class ClassUtils {
private static final String removeStr = "src.main.java.";
private static final String commonPath = "\\src\\main\\java";
private URL projectPath;
private URLClassLoader classLoader;
private String projectPathStr;
/**
* 映射器注解
*/
private static final List<Class> mapperAnnotationLs = new ArrayList() {
{
add(RequestMapping.class);
add(PostMapping.class);
add(GetMapping.class);
add(PutMapping.class);
add(DeleteMapping.class);
add(PatchMapping.class);
}};
public static void main(String[] args) throws Exception {
ClassUtils classUtils = new ClassUtils("E:\\xmdome\\crl-report-form\\crl-report-form-api");
List<InterfaceDo> arrayList = new ArrayList<>();
classUtils.getAllIntefaces(classUtils.projectPathStr, null, arrayList);
arrayList.forEach(System.out::println);
}
public ClassUtils(String filePath) {
init(filePath);
}
//初始化
private void init(String filePath) {
try {
this.projectPathStr = filePath + commonPath;
File file = new File(projectPathStr);
if (file == null || !file.exists()) {
//创建下
throw new RuntimeException("当前目录不存在");
}
projectPath = file.toURI().toURL();
//加载文件
classLoader = new URLClassLoader(new URL[] {projectPath});
} catch (Exception e) {
System.out.println("文件不存在" + e);
}
}
/**
* @param filePath
* @param packageName
* @return
* @throws Exception
*/
public void getAllIntefaces(String filePath, String