兼容压缩包
This commit is contained in:
parent
f4989813e5
commit
4bd6d6ded7
@ -12,6 +12,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.net.JarURLConnection;
|
import java.net.JarURLConnection;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -39,28 +40,46 @@ public final class ClassUtil {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static List<Class<?>> getClasses(String packageName) {
|
public static List<Class<?>> getClasses(String packageName) {
|
||||||
String packageFileName = packageName.replace(POINT, File.separator);
|
URL fullPath = getDefaultClassLoader().getResource(packageName.replace(POINT, File.separator));
|
||||||
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
|
|
||||||
if (fullPath == null) {
|
if (fullPath == null) {
|
||||||
fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();
|
fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();
|
||||||
}
|
}
|
||||||
|
List<Class<?>> clazz = null;
|
||||||
String protocol = fullPath.getProtocol();
|
String protocol = fullPath.getProtocol();
|
||||||
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
||||||
try {
|
try {
|
||||||
File dir = new File(fullPath.toURI());
|
File dir = new File(fullPath.toURI());
|
||||||
return findClassesByFile(dir, packageName);
|
clazz = findClassesByFile(dir, packageName);
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
||||||
try {
|
try {
|
||||||
return findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
|
clazz = findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (clazz == null || clazz.isEmpty()) {
|
||||||
|
clazz = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
for (URL url : ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs()) {
|
||||||
|
File file = new File(url.getFile());
|
||||||
|
if (file.getName().toLowerCase().endsWith("." + ServerToolkits.PROTOCOL_JAR)) {
|
||||||
|
clazz.addAll(findClassesByJar(new JarFile(file), packageName));
|
||||||
|
} else {
|
||||||
|
clazz.addAll(findClassesByFile(file, packageName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clazz == null || clazz.isEmpty()) {
|
||||||
throw new RuntimeException("the " + packageName + " not found classes.");
|
throw new RuntimeException("the " + packageName + " not found classes.");
|
||||||
}
|
}
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 扫描目录下所有的class对象
|
* 扫描目录下所有的class对象
|
||||||
@ -107,21 +126,32 @@ public final class ClassUtil {
|
|||||||
private static List<Class<?>> findClassesByJar(JarFile jar, String packageName) {
|
private static List<Class<?>> findClassesByJar(JarFile jar, String packageName) {
|
||||||
List<Class<?>> classes = new ArrayList<Class<?>>();
|
List<Class<?>> classes = new ArrayList<Class<?>>();
|
||||||
Enumeration<JarEntry> jarEntries = jar.entries();
|
Enumeration<JarEntry> jarEntries = jar.entries();
|
||||||
|
String packageFileName = packageName.replace(POINT, File.separator);
|
||||||
while (jarEntries.hasMoreElements()) {
|
while (jarEntries.hasMoreElements()) {
|
||||||
JarEntry jarEntry = jarEntries.nextElement();
|
JarEntry jarEntry = jarEntries.nextElement();
|
||||||
if (jarEntry.isDirectory()) {
|
if (jarEntry.isDirectory()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String className = jarEntry.getName().replace(File.separator, POINT);
|
if (!jarEntry.getName().endsWith(CLASS)) {
|
||||||
if (!className.startsWith(packageName) || !className.endsWith(CLASS)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
String className = jarEntry.getName().replace("/", File.separator);
|
||||||
|
if (className.startsWith(packageFileName)) {
|
||||||
|
try {
|
||||||
|
classes.add(Class.forName(className.replace(File.separator, POINT).replace(CLASS, "")));
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
className = jarEntry.getName().replace(File.separator, POINT);
|
||||||
|
if (className.startsWith(packageFileName)) {
|
||||||
try {
|
try {
|
||||||
classes.add(Class.forName(className.replace(CLASS, "")));
|
classes.add(Class.forName(className.replace(CLASS, "")));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,6 +235,7 @@ public final class ClassUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.err.println(getClasses("com.foxinmy.weixin4j.qy.event"));
|
String pkg = args.length > 0 ? args[0] : "com.foxinmy.weixin4j.qy.event";
|
||||||
|
System.err.println(getClasses(pkg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user