因为包多的话,遍历查找会损耗性能。 所以我想只加载某个包,然后查找类。 请问可以实现吗? 最好能给一些示例代码。 谢谢。
2012-07-11 13:06
网友采纳
private static List<Class> getClassesByPackageName(String packageName) throws IOException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread() .getContextClassLoader(); String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes; } private 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 void main(String[] args) { try { List<Class> list=getClassesByPackageName("com.test"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getName()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
///2016.3.15以下是我调试通过:
package test; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; public class TestClass { public static void main(String[] args) { try { ArrayList<Class> list=getClassesByPackageName("test"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getName()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static ArrayList<Class> getClassesByPackageName(String packageName) throws IOException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread() .getContextClassLoader(); String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); ArrayList<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes; } private static ArrayList<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { ArrayList<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; } }