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 CLASS_PATH_ARRAY = getClassPath();
public static List getClassPath(ClassLoader loader) throws IOException {
List ret = new ArrayList();
HashMap existPaths = new HashMap();
String delim = ":";
boolean isWindowsOS = false;
if (System.getProperty("os.name").indexOf("Windows") != -1) {
isWindowsOS = true;
delim = ";";
}
if(NetLog.LOG_LEVEL <= NetLog.RECORD_ALL) {
NetLog.logDebug("[FindClass] current OS: "+System.getProperty("os.name"));
}
for (String pro : CLASS_PATH_PROP) {
String classPathConfig = System.getProperty(pro);
if (classPathConfig == null) {
continue;
}
String[] paths = classPathConfig.split(delim);
for (String path : paths) {
path = path.trim();
if (path.length() == 0) {
continue;
}
if(NetLog.LOG_LEVEL <= NetLog.RECORD_ALL) {
NetLog.logDebug("[FindClass] auto search classpath: "+path);
}
File file = new File(path);
String absPath = file.getAbsolutePath();
if(existPaths.containsKey(absPath) == false) {
ret.add(file);
existPaths.put(absPath, true);
}
}
}
if(loader != null) {
Enumeration eurls = loader.getResources("");
while (eurls.hasMoreElements()) {
URL url = eurls.nextElement();
String path = url.getPath();
if(path == null || path.length() < 2) {
continue;
}
if(NetLog.LOG_LEVEL <= NetLog.RECORD_ALL) {
NetLog.logDebug("[FindClass] auto search by ClassLoader: "+path);
}
if(isWindowsOS) {
if(path.startsWith("/")) {
path = path.substring(1);
} else if(path.startsWith("file:/") && path.endsWith("!/")) {
path = path.substring(6, path.length()-2);
}
} else {
if(path.startsWith("file:") && path.endsWith("!/")) {
path = path.substring(5, path.length()-2);
}
}
File file = new File(path);
String absPath = file.getAbsolutePath();
if(existPaths.containsKey(absPath) == false) {
ret.add(file);
existPaths.put(absPath, true);
}
}
}
return ret;
}
}