1. 程式人生 > >Class掃描工具類

Class掃描工具類

enc creat str odi cto package ces ack col

下面代碼用於掃描項目中的所有類:

/**
 * class掃描工具
 * Created by wangl on 2017/7/6.
 */
public class ScanUtil {

    private static final Set<String> classNames = new HashSet<String>();

    /**
     * 獲取指定包下以及子包中所有的類
     *
     * @param packageName 包名
     * @return 所有的完整類名
     */
    public static Set<String> scan(String packageName) {
        
if(packageName == null){ throw new RuntimeException("The path can not be null."); } String packagePath = packageName.replace(".", "/"); ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { Enumeration<URL> urls = loader.getResources(packagePath);
while(urls.hasMoreElements()){ URL url= urls.nextElement(); if("file".equals(url.getProtocol())){ scanFromDir(url.getPath(), packageName); } if("jar".equals(url.getProtocol())){ JarURLConnection connection
= (JarURLConnection)url.openConnection(); scanFromJar(connection.getJarFile()); } } } catch (IOException e) { throw new RuntimeException("Resolve path error.", e); } return classNames; } /** * 從項目文件獲取某包下所有類 * * @param filePath 文件目錄 * @param packageName 包名 */ private static void scanFromDir(String filePath, String packageName) throws UnsupportedEncodingException{ filePath = URLDecoder.decode(filePath, "utf-8"); packageName = URLDecoder.decode(packageName, "utf-8"); File[] files = new File(filePath).listFiles(); packageName = packageName + "."; for (File childFile : files) { if (childFile.isDirectory()) { scanFromDir(childFile.getPath(), packageName + childFile.getName()); } else { String fileName = childFile.getName(); if (fileName.endsWith(".class")) { if(packageName.charAt(0) == ‘.‘){ packageName = packageName.substring(1, packageName.length()); } String className = packageName + fileName.replace(".class", ""); classNames.add(className); } } } } /** * 掃描jar文件 * @param jarFile */ private static void scanFromJar(JarFile jarFile) { Enumeration<JarEntry> files = jarFile.entries(); while (files.hasMoreElements()) { JarEntry entry = files.nextElement(); if (entry.getName().endsWith(".class")){ String className = entry.getName().replace("/", ".").replace(".class", ""); classNames.add(className); } } } }

Class掃描工具類