****更新1(2012)****
好的,我终于得到了清理下面的代码段。我坚持到它自己的github项目,甚至添加测试。
****更新2(2016)****
对于更强大和功能丰富的类路径扫描器,请参见https://github.com/lukehutch/fast-classpath-scanner/wiki.我建议首先阅读我的代码片段以获得高水平的理解,然后使用lukehutch的工具用于生产目的。
****原帖(2010)****
严格地说,不可能在包中列出类。这是因为包实际上只是一个命名空间(例如com.epicapplications.foo.bar),并且类路径中的任何jar文件都可能将类添加到包中。更糟糕的是,类加载器将根据需要加载类,并且类路径的一部分可能位于网络连接的另一端。
可以解决更多限制性的问题。例如JAR文件中的所有类,或JAR文件在特定包中定义的所有类。这是更常见的情况。
不幸的是,没有任何框架代码使这个任务容易。您必须以类似于ClassLoader寻找类定义的方式扫描文件系统。
网上有很多样本,用于普通老目录中的类文件。我们大多数人现在使用JAR文件。
要使事情与JAR文件一起工作,请尝试此操作…
private static ArrayList> getClassesForPackage(Package pkg) {
String pkgname = pkg.getName();
ArrayList> classes = new ArrayList>();
// Get a File object for the package
File directory = null;
String fullPath;
String relPath = pkgname.replace('.', '/');
System.out.println("ClassDiscovery: Package: " + pkgname + " becomes Path:" + relPath);
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
System.out.println("ClassDiscovery: Resource = " + resource);
if (resource == null) {
throw new RuntimeException("No resource for " + relPath);
}
fullPath = resource.getFile();
System.out.println("ClassDiscovery: FullPath = " + resource);
try {
directory = new File(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...", e);
} catch (IllegalArgumentException e) {
directory = null;
}
System.out.println("ClassDiscovery: Directory = " + directory);
if (directory != null && directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
// we are only interested in .class files
if (files[i].endsWith(".class")) {
// removes the .class extension
String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
System.out.println("ClassDiscovery: className = " + className);
try {
classes.add(Class.forName(className));
}
catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
}
else {
try {
String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
JarFile jarFile = new JarFile(jarPath);
Enumeration entries = jarFile.entries();
while(entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if(entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
System.out.println("ClassDiscovery: JarEntry: " + entryName);
String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
System.out.println("ClassDiscovery: className = " + className);
try {
classes.add(Class.forName(className));
}
catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
} catch (IOException e) {
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
}
}
return classes;
}