All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.stylesmile.tool.ClassUtils Maven / Gradle / Ivy

package io.github.stylesmile.tool;

import java.io.File;
import java.net.URL;
import java.util.Enumeration;

/**
 * @author Stylesmile
 */
public class ClassUtils {
    // getClassName("top.lingkang.demohibernate.entity")
    public static Class[] getClassByPackage(String packageName) {
        try {
            Enumeration resources = ClassUtils.class.getClassLoader().getResources(packageName.replaceAll("\\.", "/"));
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                String[] file = new File(url.getFile()).list();
                Class[] classList = new Class[file.length];
                for (int i = 0; i < file.length; i++) {
                    classList[i] = Class.forName(packageName + "." + file[i].replaceAll("\\.class", ""));
                }
                return classList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy