用于在给定目录和子目录中查找所有类的递归方法。
//package com.nowjava; import java.io.File; /*来自 时 代 J a v a*/ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { public static void main(String[] argv) throws Exception { File directory = new File("Main.java");/*来 自 时代Java公众号 - N o w J a v a . c o m*/ String packageName = "nowjava.com"; System.out.println(findClasses(directory, packageName)); } /** * Recursive method used to find all classes in a given directory and subdirs. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException */ public static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class> classes = new ArrayList<Class>(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; } public static List<Class> findClasses(String path, String packageName) { List<Class> classes = new ArrayList<Class>(); try { if (path.startsWith("file:")) { URL jar = new URL(path); ZipInputStream zip = new ZipInputStream(jar.openStream()); ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { if (entry.getName().endsWith(".class")) { String className = entry.getName() .replaceAll("[$].*", "") .replaceAll("[.]class", "") .replace('/', '.'); if (className.startsWith(packageName)) { classes.add(Class.forName(className)); } } } } File dir = new File(path); if (!dir.exists()) { return classes; } File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findCla