当前位置:编程学习 > JAVA >>

Java遍历包中所有类-终续

[java]
package com.itkt.mtravel.hotel.util; 
 
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; 
 
public class PackageUtil { 
 
    public static void main(String[] args) throws Exception { 
        String packageName = "com.wang.vo.request.hotel"; 
        // List<String> classNames = getClassName(packageName); 
        List<String> classNames = getClassName(packageName, false); 
        if (classNames != null) { 
            for (String className : classNames) { 
                System.out.println(className); 
            } 
        } 
    } 
 
    /**
     * 获取某包下(包括该包的所有子包)所有类
     * @param packageName 包名
     * @return 类的完整名称
     */ 
    public static List<String> getClassName(String packageName) { 
        return getClassName(packageName, true); 
    } 
 
    /**
     * 获取某包下所有类
     * @param packageName 包名
     * @param childPackage 是否遍历子包
     * @return 类的完整名称
     */ 
    public static List<String> getClassName(String packageName, boolean childPackage) { 
        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 (type.equals("file")) { 
                fileNames = getClassNameByFile(url.getPath(), null, childPackage); 
            } else if (type.equals("jar")) { 
                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 类的完整名称
     */ 
    private static List<String> getClassNameByJar(String jarPath, boolean childPackage) { 
        List<String> myClassName = new ArrayList<String>(); 
        String[] jarInfo = jarPath.split("!"); 
        String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/")); 
        String packagePath = jarInfo[1].substring(1); 
        try { 
            JarFile jarFile = new JarFile(jarFilePath); 
            Enum
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,