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

net_io.utils.FindClassUtils Maven / Gradle / Ivy

The newest version!
package net_io.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class FindClassUtils {
	private static ClassLoader defaultLoader = null;
	
	public static void setDefaultClassLoader(ClassLoader loader) {
		defaultLoader = loader;
	}

	public static ClassLoader getDefaultClassLoader() {
		return defaultLoader;
	}

	public static List getClassInPackage(String pkgName) {
		return getClassInPackage(pkgName, defaultLoader);
	}
	
	public static List getClassInPackage(String pkgName, ClassLoader loader) {
		List ret = new ArrayList();
		try {
//			for(File classPath : CLASS_PATH_ARRAY) {
			for(File classPath : getClassPath(loader)) {
				if(!classPath.exists()) {
					continue;
				}
				ret.addAll(_getClassInPackage(pkgName, classPath));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return ret;	
	}
 
	public static List getClassInPackage(String pkgName, String classPath) {
		try {
			return _getClassInPackage(pkgName, new File(classPath));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private static List _getClassInPackage(String pkgName, File classPath) throws IOException {
		List ret = new ArrayList();
		String searchPath = pkgName.replace('.', '/') + "/";
		if (!classPath.exists()) {
			return ret;
		}
		if (classPath.isDirectory()) {
			ArrayList dirs = new ArrayList();
			ArrayList pkgNames = new ArrayList(); //package名称。与 dirs 一一对应
			dirs.add(new File(classPath, searchPath));
			pkgNames.add(pkgName);
			for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy