兼容压缩包
This commit is contained in:
parent
f4989813e5
commit
4bd6d6ded7
@ -1,210 +1,241 @@
|
|||||||
package com.foxinmy.weixin4j.util;
|
package com.foxinmy.weixin4j.util;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
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.util.ArrayList;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Enumeration;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.Enumeration;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.List;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
/**
|
|
||||||
* 对class的获取
|
/**
|
||||||
*
|
* 对class的获取
|
||||||
* @className ClassUtil
|
*
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @className ClassUtil
|
||||||
* @date 2014年10月31日
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @since JDK 1.6
|
* @date 2014年10月31日
|
||||||
* @see
|
* @since JDK 1.6
|
||||||
*/
|
* @see
|
||||||
public final class ClassUtil {
|
*/
|
||||||
private final static String POINT = ".";
|
public final class ClassUtil {
|
||||||
private final static String CLASS = ".class";
|
private final static String POINT = ".";
|
||||||
|
private final static String CLASS = ".class";
|
||||||
/**
|
|
||||||
* 获取某个包下所有的class信息
|
/**
|
||||||
*
|
* 获取某个包下所有的class信息
|
||||||
* @param packageName
|
*
|
||||||
* 包名
|
* @param packageName
|
||||||
* @return
|
* 包名
|
||||||
*/
|
* @return
|
||||||
public static List<Class<?>> getClasses(String packageName) {
|
*/
|
||||||
String packageFileName = packageName.replace(POINT, File.separator);
|
public static List<Class<?>> getClasses(String packageName) {
|
||||||
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
|
URL fullPath = getDefaultClassLoader().getResource(packageName.replace(POINT, File.separator));
|
||||||
if (fullPath == null) {
|
if (fullPath == null) {
|
||||||
fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();
|
fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();
|
||||||
}
|
}
|
||||||
String protocol = fullPath.getProtocol();
|
List<Class<?>> clazz = null;
|
||||||
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
String protocol = fullPath.getProtocol();
|
||||||
try {
|
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
||||||
File dir = new File(fullPath.toURI());
|
try {
|
||||||
return findClassesByFile(dir, packageName);
|
File dir = new File(fullPath.toURI());
|
||||||
} catch (URISyntaxException e) {
|
clazz = findClassesByFile(dir, packageName);
|
||||||
throw new RuntimeException(e);
|
} catch (URISyntaxException e) {
|
||||||
}
|
throw new RuntimeException(e);
|
||||||
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
}
|
||||||
try {
|
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
||||||
return findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
|
try {
|
||||||
} catch (IOException e) {
|
clazz = findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
|
||||||
throw new RuntimeException(e);
|
} catch (IOException e) {
|
||||||
}
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
throw new RuntimeException("the " + packageName + " not found classes.");
|
}
|
||||||
}
|
if (clazz == null || clazz.isEmpty()) {
|
||||||
|
clazz = new ArrayList<>();
|
||||||
/**
|
try {
|
||||||
* 扫描目录下所有的class对象
|
for (URL url : ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs()) {
|
||||||
*
|
File file = new File(url.getFile());
|
||||||
* @param dir
|
if (file.getName().toLowerCase().endsWith("." + ServerToolkits.PROTOCOL_JAR)) {
|
||||||
* 文件目录
|
clazz.addAll(findClassesByJar(new JarFile(file), packageName));
|
||||||
* @param packageName
|
} else {
|
||||||
* 包的全限类名
|
clazz.addAll(findClassesByFile(file, packageName));
|
||||||
* @return
|
}
|
||||||
*/
|
}
|
||||||
private static List<Class<?>> findClassesByFile(File dir, String packageName) {
|
} catch (IOException e) {
|
||||||
List<Class<?>> classes = new ArrayList<Class<?>>();
|
throw new RuntimeException(e);
|
||||||
File[] files = dir.listFiles(new FilenameFilter() {
|
}
|
||||||
@Override
|
}
|
||||||
public boolean accept(File file, String name) {
|
if (clazz == null || clazz.isEmpty()) {
|
||||||
return file.isDirectory() || file.getName().endsWith(CLASS);
|
throw new RuntimeException("the " + packageName + " not found classes.");
|
||||||
}
|
}
|
||||||
});
|
return clazz;
|
||||||
if (files != null) {
|
}
|
||||||
for (File file : files) {
|
|
||||||
if (file.isDirectory()) {
|
/**
|
||||||
classes.addAll(findClassesByFile(file, packageName + POINT + file.getName()));
|
* 扫描目录下所有的class对象
|
||||||
} else {
|
*
|
||||||
try {
|
* @param dir
|
||||||
classes.add(Class.forName(packageName + POINT + file.getName().replace(CLASS, "")));
|
* 文件目录
|
||||||
} catch (ClassNotFoundException e) {
|
* @param packageName
|
||||||
;
|
* 包的全限类名
|
||||||
}
|
* @return
|
||||||
}
|
*/
|
||||||
}
|
private static List<Class<?>> findClassesByFile(File dir, String packageName) {
|
||||||
}
|
List<Class<?>> classes = new ArrayList<Class<?>>();
|
||||||
return classes;
|
File[] files = dir.listFiles(new FilenameFilter() {
|
||||||
}
|
@Override
|
||||||
|
public boolean accept(File file, String name) {
|
||||||
/**
|
return file.isDirectory() || file.getName().endsWith(CLASS);
|
||||||
* 扫描jar包下所有的class对象
|
}
|
||||||
*
|
});
|
||||||
* @param jar
|
if (files != null) {
|
||||||
* jar包对象
|
for (File file : files) {
|
||||||
* @param packageName
|
if (file.isDirectory()) {
|
||||||
* 包的全限类名
|
classes.addAll(findClassesByFile(file, packageName + POINT + file.getName()));
|
||||||
* @return
|
} else {
|
||||||
*/
|
try {
|
||||||
private static List<Class<?>> findClassesByJar(JarFile jar, String packageName) {
|
classes.add(Class.forName(packageName + POINT + file.getName().replace(CLASS, "")));
|
||||||
List<Class<?>> classes = new ArrayList<Class<?>>();
|
} catch (ClassNotFoundException e) {
|
||||||
Enumeration<JarEntry> jarEntries = jar.entries();
|
;
|
||||||
while (jarEntries.hasMoreElements()) {
|
}
|
||||||
JarEntry jarEntry = jarEntries.nextElement();
|
}
|
||||||
if (jarEntry.isDirectory()) {
|
}
|
||||||
continue;
|
}
|
||||||
}
|
return classes;
|
||||||
String className = jarEntry.getName().replace(File.separator, POINT);
|
}
|
||||||
if (!className.startsWith(packageName) || !className.endsWith(CLASS)) {
|
|
||||||
continue;
|
/**
|
||||||
}
|
* 扫描jar包下所有的class对象
|
||||||
try {
|
*
|
||||||
classes.add(Class.forName(className.replace(CLASS, "")));
|
* @param jar
|
||||||
} catch (ClassNotFoundException e) {
|
* jar包对象
|
||||||
;
|
* @param packageName
|
||||||
}
|
* 包的全限类名
|
||||||
}
|
* @return
|
||||||
return classes;
|
*/
|
||||||
}
|
private static List<Class<?>> findClassesByJar(JarFile jar, String packageName) {
|
||||||
|
List<Class<?>> classes = new ArrayList<Class<?>>();
|
||||||
public static Object deepClone(Object obj) {
|
Enumeration<JarEntry> jarEntries = jar.entries();
|
||||||
ByteArrayOutputStream bos = null;
|
String packageFileName = packageName.replace(POINT, File.separator);
|
||||||
ObjectOutputStream oos = null;
|
while (jarEntries.hasMoreElements()) {
|
||||||
ByteArrayInputStream bis = null;
|
JarEntry jarEntry = jarEntries.nextElement();
|
||||||
ObjectInputStream ois = null;
|
if (jarEntry.isDirectory()) {
|
||||||
try {
|
continue;
|
||||||
bos = new ByteArrayOutputStream();
|
}
|
||||||
oos = new ObjectOutputStream(bos);
|
if (!jarEntry.getName().endsWith(CLASS)) {
|
||||||
oos.writeObject(obj);
|
continue;
|
||||||
bis = new ByteArrayInputStream(bos.toByteArray());
|
}
|
||||||
ois = new ObjectInputStream(bis);
|
String className = jarEntry.getName().replace("/", File.separator);
|
||||||
return ois.readObject();
|
if (className.startsWith(packageFileName)) {
|
||||||
} catch (IOException e) {
|
try {
|
||||||
throw new RuntimeException(e);
|
classes.add(Class.forName(className.replace(File.separator, POINT).replace(CLASS, "")));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
;
|
||||||
} finally {
|
}
|
||||||
try {
|
}
|
||||||
if (bos != null) {
|
className = jarEntry.getName().replace(File.separator, POINT);
|
||||||
bos.close();
|
if (className.startsWith(packageFileName)) {
|
||||||
}
|
try {
|
||||||
if (oos != null) {
|
classes.add(Class.forName(className.replace(CLASS, "")));
|
||||||
oos.close();
|
} catch (ClassNotFoundException e) {
|
||||||
}
|
;
|
||||||
if (bis != null) {
|
}
|
||||||
bis.close();
|
}
|
||||||
}
|
}
|
||||||
if (ois != null) {
|
return classes;
|
||||||
ois.close();
|
}
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
public static Object deepClone(Object obj) {
|
||||||
;// ignore
|
ByteArrayOutputStream bos = null;
|
||||||
}
|
ObjectOutputStream oos = null;
|
||||||
}
|
ByteArrayInputStream bis = null;
|
||||||
}
|
ObjectInputStream ois = null;
|
||||||
|
try {
|
||||||
/**
|
bos = new ByteArrayOutputStream();
|
||||||
* 获得泛型类型
|
oos = new ObjectOutputStream(bos);
|
||||||
*
|
oos.writeObject(obj);
|
||||||
* @param object
|
bis = new ByteArrayInputStream(bos.toByteArray());
|
||||||
* @return
|
ois = new ObjectInputStream(bis);
|
||||||
*/
|
return ois.readObject();
|
||||||
public static Class<?> getGenericType(Class<?> clazz) {
|
} catch (IOException e) {
|
||||||
if (clazz == Object.class) {
|
throw new RuntimeException(e);
|
||||||
return null;
|
} catch (ClassNotFoundException e) {
|
||||||
}
|
throw new RuntimeException(e);
|
||||||
Type type = clazz.getGenericSuperclass();
|
} finally {
|
||||||
if (type instanceof ParameterizedType) {
|
try {
|
||||||
ParameterizedType ptype = ((ParameterizedType) type);
|
if (bos != null) {
|
||||||
Type[] args = ptype.getActualTypeArguments();
|
bos.close();
|
||||||
return (Class<?>) args[0];
|
}
|
||||||
}
|
if (oos != null) {
|
||||||
return getGenericType(clazz.getSuperclass());
|
oos.close();
|
||||||
}
|
}
|
||||||
|
if (bis != null) {
|
||||||
public static ClassLoader getDefaultClassLoader() {
|
bis.close();
|
||||||
ClassLoader cl = null;
|
}
|
||||||
try {
|
if (ois != null) {
|
||||||
cl = Thread.currentThread().getContextClassLoader();
|
ois.close();
|
||||||
} catch (Throwable ex) {
|
}
|
||||||
// Cannot access thread context ClassLoader - falling back...
|
} catch (IOException e) {
|
||||||
}
|
;// ignore
|
||||||
if (cl == null) {
|
}
|
||||||
// No thread context class loader -> use class loader of this class.
|
}
|
||||||
cl = ClassUtil.class.getClassLoader();
|
}
|
||||||
if (cl == null) {
|
|
||||||
// getClassLoader() returning null indicates the bootstrap
|
/**
|
||||||
// ClassLoader
|
* 获得泛型类型
|
||||||
try {
|
*
|
||||||
cl = ClassLoader.getSystemClassLoader();
|
* @param object
|
||||||
} catch (Throwable ex) {
|
* @return
|
||||||
// Cannot access system ClassLoader - oh well, maybe the
|
*/
|
||||||
// caller can live with null...
|
public static Class<?> getGenericType(Class<?> clazz) {
|
||||||
}
|
if (clazz == Object.class) {
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
return cl;
|
Type type = clazz.getGenericSuperclass();
|
||||||
}
|
if (type instanceof ParameterizedType) {
|
||||||
|
ParameterizedType ptype = ((ParameterizedType) type);
|
||||||
public static void main(String[] args) {
|
Type[] args = ptype.getActualTypeArguments();
|
||||||
System.err.println(getClasses("com.foxinmy.weixin4j.qy.event"));
|
return (Class<?>) args[0];
|
||||||
}
|
}
|
||||||
}
|
return getGenericType(clazz.getSuperclass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ClassLoader getDefaultClassLoader() {
|
||||||
|
ClassLoader cl = null;
|
||||||
|
try {
|
||||||
|
cl = Thread.currentThread().getContextClassLoader();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
// Cannot access thread context ClassLoader - falling back...
|
||||||
|
}
|
||||||
|
if (cl == null) {
|
||||||
|
// No thread context class loader -> use class loader of this class.
|
||||||
|
cl = ClassUtil.class.getClassLoader();
|
||||||
|
if (cl == null) {
|
||||||
|
// getClassLoader() returning null indicates the bootstrap
|
||||||
|
// ClassLoader
|
||||||
|
try {
|
||||||
|
cl = ClassLoader.getSystemClassLoader();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
// Cannot access system ClassLoader - oh well, maybe the
|
||||||
|
// caller can live with null...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
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