import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.*;
public class LukehutchUtil {
//获取全扫描的Scanner
public static FastClasspathScanner getAllPermissionScanner(String packageName){
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner(packageName);//获取指定包的扫描对象
fastClasspathScanner.enableExternalClasses();//开启外部类扫描
fastClasspathScanner.enableFieldInfo();//开启field info扫描
fastClasspathScanner.enableMethodInfo();//开启method info扫描
fastClasspathScanner.enableFieldAnnotationIndexing();//开启属性的注解扫描
fastClasspathScanner.enableMethodAnnotationIndexing();//开启方法上注解扫描
fastClasspathScanner.ignoreFieldVisibility();//忽略字段可见性
fastClasspathScanner.ignoreMethodVisibility();//忽略方法可见性
return fastClasspathScanner;
}
//获取项目所在文件夹
public static String getProjectAtFolder(){
String projectPath=getProjectPath();
return projectPath.substring(0,projectPath.lastIndexOf("/"));
}
//获取项目路径
public static String getProjectPath(){
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner(getRootPackage());
String rootPath=fastClasspathScanner.getUniqueClasspathElementURLs().get(0).getPath();
rootPath=rootPath.substring(1);
if (!rootPath.contains(".jar")){
rootPath=rootPath.replaceAll("/target/classes/","");
}
return rootPath;
}
//获取classes根路径
public static String getClassesPath(){
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner(getRootPackage());
String rootPath=fastClasspathScanner.getUniqueClasspathElementURLs().get(0).getPath();
rootPath=rootPath.substring(1);
if (!rootPath.contains("jar")){
rootPath=rootPath.substring(0,rootPath.length()-1);
}else {
rootPath=rootPath+"/BOOT-INF/classes";
}
return rootPath;
}
//获取根包路径
public static String getRootPackage() {
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner("");
ScanResult scanResult = fastClasspathScanner.scan();
List<String> classNameList = scanResult.getNamesOfClassesWithAnnotation(SpringBootApplication.class);
for (String className:classNameList){
if (className.contains("Application")){
return className.substring(0,className.lastIndexOf("."));
}
}
return null;
}
//获取指定包下的所有类
public static Set<String> getClassesNameFromPackage(String targetPackage,boolean isLoop){
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner(targetPackage);//获取指定包的扫描对象
ScanResult scanResult=fastClasspathScanner.scan();
List<String> classesName=scanResult.getNamesOfAllClasses();
//过滤代理类
Set<String> filterSet=new TreeSet<>();
for (String className:classesName){
if (!className.contains("$")){
filterSet.add(className);
}
}
//多条件排序
Set<String> sortSet = getSortSet();
if (!isLoop){
int len=targetPackage.split("\\.").length;
for (String className:filterSet){
if (className.split("\\.").length==len+1){
sortSet.add(className);
}
}
}else {
sortSet.addAll(filterSet);
}
return sortSet;
}
//获取指定包下的所有包
public static Set<String> getPackagesNameFromPackage(String targetPackage,boolean isLoop){
Set<String> resultSet=new HashSet<>();
Set<String> classesName=getClassesNameFromPackage(targetPackage,true);
for (String className:classesName){
String packageName=className.substring(0,className.lastIndexOf("."));//截取类路径的包名
resultSet.add(packageName);
}
resultSet.remove(targetPackage);//移除本包
//多条件排序
Set<String> sortSet = getSortSet();
if (!isLoop){
int len=targetPackage.split("\\.").length;
for (String packageName:resultSet){
if (packageName.split("\\.").length==len+1){
sortSet.add(packageName);
}
}
}else {
sortSet.addAll(resultSet);
}
return sortSet;
}
//获取按包层级和字母顺序进行排序的set
private static Set<String> getSortSet(){
Set<String> sortSet = new TreeSet<String>(new Comparator<String>() {//返回值为0,会导致addAll的时候加入不了元素
@Override
public int compare(String o1, String o2) {
int num1=o1.split("\\.").length-o2.split("\\.").length;//比较包层级
int num2=o1.substring(o1.lastIndexOf(".")+1).compareTo(o2.substring(o2.lastIndexOf(".")+1));//比较最后一个包字母顺序
if (num1>0){//包层级越短,越靠前
return 1;
}
if (num1==0&&num2>0){//包层级相同时,最后一个包按字母顺序排
return 1;
}
return -1;
}
});
return sortSet;
}
}
网友评论