获取包下的类名

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @ClassName PackageUtil
 * @Description (获取包下的类名)
 * @author wwj
 * @Date 2017年5月3日 下午4:50:09
 * @version 1.0.0.0
 */
public class PackageUtil { 
 
    public static void main(String[] args) throws Exception { 
        String packageName = "com.pf.task.slave.todo.download.bjwt.dao"; 
        List<String> classNames = getClassName(packageName, false); 
        if (classNames != null) { 
            for (String className : classNames) { 
                System.out.println(className); 
            } 
        } 
    } 
 
    /**
     * 获取某包下(包括该包的所有子包)所有类
     * @param packageName 包名
     * @return 类的完整名称
     * @throws Exception 
     */ 
    public static List<String> getClassName(String packageName) throws Exception { 
        return getClassName(packageName, true); 
    } 
 
    /**
     * 获取某包下所有类
     * @param packageName 包名
     * @param childPackage 是否遍历子包
     * @return 类的完整名称
     * @throws Exception 
     */ 
    public static List<String> getClassName(String packageName, boolean childPackage) throws Exception { 
        List<String> fileNames = null; 
        ClassLoader loader = Thread.currentThread().getContextClassLoader(); 
        String packagePath = packageName.replace(".", "/"); 
        URL url = loader.getResource(packagePath); 
        if (url != null) { 
            String type = url.getProtocol(); 
            if ("file".equals(type)) { 
                fileNames = getClassNameByFile(url.getPath(), null, childPackage); 
            } else if (("jar").equals(type)) { 
                fileNames = getClassNameByJar(url.getPath(), childPackage); 
            } 
        } else { 
            fileNames = getClassNameByJars(((URLClassLoader) loader).getURLs(), packagePath, childPackage); 
        } 
        return fileNames; 
    } 
 
    /**
     * 从项目文件获取某包下所有类
     * @param filePath 文件路径
     * @param className 类名集合
     * @param childPackage 是否遍历子包
     * @return 类的完整名称
     */ 
    private static List<String> getClassNameByFile(String filePath, List<String> className, boolean childPackage) { 
        List<String> myClassName = new ArrayList<String>(); 
        File file = new File(filePath); 
        File[] childFiles = file.listFiles(); 
        for (File childFile : childFiles) { 
            if (childFile.isDirectory()) { 
                if (childPackage) { 
                    myClassName.addAll(getClassNameByFile(childFile.getPath(), myClassName, childPackage)); 
                } 
            } else { 
                String childFilePath = childFile.getPath(); 
                if (childFilePath.endsWith(".class")) { 
                    childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf(".")); 
                    childFilePath = childFilePath.replace("\\", "."); 
                    myClassName.add(childFilePath); 
                } 
            } 
        } 
 
        return myClassName; 
    } 
 
    /**
     * 从jar获取某包下所有类
     * @param jarPath jar文件路径
     * @param childPackage 是否遍历子包
     * @return 类的完整名称
     * @throws Exception 
     */ 
    private static List<String> getClassNameByJar(String jarPath, boolean childPackage) throws Exception { 
        List<String> myClassName = new ArrayList<String>(); 
        String[] jarInfo = jarPath.split("!"); 
        String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/")); 
        String packagePath = jarInfo[1].substring(1);
        JarFile jarFile = null;
        try { 
            jarFile = new JarFile(jarFilePath); 
            Enumeration<JarEntry> entrys = jarFile.entries(); 
            while (entrys.hasMoreElements()) { 
                JarEntry jarEntry = entrys.nextElement(); 
                String entryName = jarEntry.getName(); 
                if (entryName.endsWith(".class")) { 
                    if (childPackage) { 
                        if (entryName.startsWith(packagePath)) { 
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf(".")); 
                            myClassName.add(entryName); 
                        } 
                    } else { 
                        int index = entryName.lastIndexOf("/"); 
                        String myPackagePath; 
                        if (index != -1) { 
                            myPackagePath = entryName.substring(0, index); 
                        } else { 
                            myPackagePath = entryName; 
                        } 
                        if (myPackagePath.equals(packagePath)) { 
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf(".")); 
                            myClassName.add(entryName); 
                        } 
                    } 
                } 
            } 
        } catch (Exception e) { 
            throw e; 
        }finally{
            if(null != jarFile){
                jarFile.close();
            }
        } 
        return myClassName; 
    } 
 
    /**
     * 从所有jar中搜索该包,并获取该包下所有类
     * @param urls URL集合
     * @param packagePath 包路径
     * @param childPackage 是否遍历子包
     * @return 类的完整名称
     * @throws Exception 
     */ 
    private static List<String> getClassNameByJars(URL[] urls, String packagePath, boolean childPackage) throws Exception { 
        List<String> myClassName = new ArrayList<String>(); 
        if (urls != null) { 
            for (int i = 0; i < urls.length; i++) { 
                URL url = urls[i]; 
                String urlPath = url.getPath(); 
                // 不必搜索classes文件夹 
                if (urlPath.endsWith("classes/")) { 
                    continue; 
                } 
                String jarPath = urlPath + "!/" + packagePath; 
                myClassName.addAll(getClassNameByJar(jarPath, childPackage)); 
            } 
        } 
        return myClassName; 
    } 
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值