我想threre可能是一個更簡單的方法來做到這一點,但這個類可能是有用的:
package classFinder;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ClassFinder {
protected static Class<?> getClass(String prefix, String classPath)
throws ClassNotFoundException {
if (!classPath.endsWith(".class") || !classPath.startsWith(prefix)) {
return null;
}
return Class.forName(classPath.substring(prefix.length(),
classPath.length() - ".class".length()).replace('/', '.'));
}
protected static Class<?> getClass(File rootFile, File classFile)
throws ClassNotFoundException {
return getClass(rootFile.getPath() + '/', classFile.getPath());
}
@SuppressWarnings("unchecked")
protected static <T> Set<Class<T>> searchAllSubclassesInDirectory(
File rootFile, File searchFile, Class<?> cls,
boolean abstractClasses) throws ClassFinderException {
Set<Class<T>> result = new HashSet<Class<T>>();
if (searchFile.isDirectory()) {
for (File file : searchFile.listFiles()) {
result.addAll(ClassFinder.<T> searchAllSubclasses(
rootFile.getPath(), file.getPath(), cls,
abstractClasses));
}
return result;
}
String fileName = searchFile.getName();
if (!fileName.endsWith(".class")) {
return result;
}
try {
Class<?> entry = getClass(rootFile, searchFile);
if (entry != null
&& (abstractClasses || !Modifier.isAbstract(entry
.getModifiers()))) {
Class<?> superClass = entry;
while (!((superClass = superClass.getSuperclass()) == null)) {
if (superClass.equals(cls)) {
result.add((Class<T>) entry);
return result;
}
}
}
return result;
} catch (ClassNotFoundException e) {
// e.printStackTrace(); //DEBUG only
return result;
}
}
@SuppressWarnings("unchecked")
protected static <T> Set<Class<T>> searchAllSubclassesInJar(File jar,
Class<?> cls, boolean abstractClasses) {
Set<Class<T>> result = new HashSet<Class<T>>();
try {
JarFile jarFile = new JarFile(jar);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry file = entries.nextElement();
if (file.isDirectory()) {
continue;
}
Class<?> entry = getClass("", file.getName());
if (entry != null
&& (abstractClasses || !Modifier.isAbstract(entry
.getModifiers()))) {
Class<?> superClass = entry;
while (!((superClass = superClass.getSuperclass()) == null)) {
if (superClass.equals(cls)) {
result.add((Class<T>) entry);
}
}
}
}
jarFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
protected static <T> Set<Class<T>> searchAllSubclasses(String rootPath,
String searchPath, Class<?> cls, boolean abstractClasses)
throws ClassFinderException {
if (searchPath.endsWith(".jar")) {
return searchAllSubclassesInJar(new File(searchPath), cls,
abstractClasses);
// return new HashSet<Class<T>>();
} else {
return searchAllSubclassesInDirectory(new File(rootPath), new File(
searchPath), cls, abstractClasses);
}
}
// TODO create public method to search inside a not root directory/package
public static <T> Set<Class<T>> searchAllSubclasses(Class<?> cls,
boolean abstractClasses) throws ClassFinderException {
Set<Class<T>> result = new HashSet<Class<T>>();
String classpath = System.getProperty("java.class.path");
for (String path : classpath
.split(System.getProperty("path.separator"))) {
result.addAll(ClassFinder.<T> searchAllSubclasses(path, path, cls,
abstractClasses));
}
return result;
// return ClassFinder.<T> searchAllSubclasses(ROOT_URL, cls,
// abstractClasses, "");
}
}
如果你有一個標準的Java桌面應用程序可能運行工作。 該類在您的程序目錄樹上搜索給定超類的實現。也適用於jar文件。
然後,您可以初始化靜態字段:
abstract public class Parent {
protected static Set<Parent> children = ClassFinder
.<Parent> searchAllSubclasses(Parent.class, true);
}
的[?你怎麼找到Java中的給定類的子類(
可能重複http://stackoverflow.com/questions/492184/how -do-you-find-a-given-class-in-java) – A4L 2014-11-21 23:20:25