1. 扫描类
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.cnp.andromeda.common.util.StringUtil;
/**
* @Author
* @Description 包扫描器
* @CopyRight
*/
public class ClassScanner{
private Map> classes = new HashMap>();
private FilenameFilter javaClassFilter; // 类文件过滤器,只扫描一级类
private final String CLASS_FILE_SUFFIX = ".class"; // Java字节码文件后缀
private String packPrefix; // 包路径根路劲
public ClassScanner(){
javaClassFilter = new FilenameFilter(){
@Override
public boolean accept(File dir, String name){
// 排除内部内
return !name.contains("$");
}
};
}
/**
* @Title: scanning
* @Description 扫描指定包(本地)
* @param basePath 包所在的根路径
* @param packagePath 目标包路径
* @return Integer 被扫描到的类的数量
* @throws ClassNotFoundException
*/
public Integer scanning(String basePath, String packagePath) throws ClassNotFoundException{
packPrefix = basePath;
String packTmp = packagePath.replace('.', '/');
File dir = new File(basePath, packTmp);
// 不是文件夹
if(dir.isDirectory()){
scan0(dir);
}
return classes.size();
}
/**