Here you can find the source of getClasses(String pkgname, boolean flat)
Parameter | Description |
---|---|
packageName | The base package |
flat | scan only one package level, do not dive into subdirectories |
Parameter | Description |
---|---|
ClassNotFoundException | an exception |
IOException | an exception |
protected static Class<?>[] getClasses(String pkgname, boolean flat)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class Main { /**/*from ww w . ja va 2 s . c o m*/ * Scans all classes accessible from the context class loader which belong * to the given package and subpackages. * * @param packageName The base package * @param flat scan only one package level, do not dive into subdirectories * @return The classes * @throws ClassNotFoundException * @throws IOException */ protected static Class<?>[] getClasses(String pkgname, boolean flat) { List<File> dirs = new ArrayList<File>(); List<Class<?>> classes = new ArrayList<Class<?>>(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = pkgname.replace('.', '/'); Enumeration<URL> resources = null; try { resources = classLoader.getResources(path); } catch (IOException e) { throw new RuntimeException("Cannot read path " + path, e); } while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } for (File directory : dirs) { try { classes.addAll(findClasses(directory, pkgname, flat)); } catch (ClassNotFoundException e) { throw new RuntimeException("Class not found", e); } } return classes.toArray(new Class<?>[classes.size()]); } /** * Recursive method used to find all classes in a given directory and * subdirectories. * * @param dir base directory * @param pkgname package name for classes found inside the base directory * @param flat scan only one package level, do not dive into subdirectories * @return Classes list * @throws ClassNotFoundException */ private static List<Class<?>> findClasses(File dir, String pkgname, boolean flat) throws ClassNotFoundException { List<Class<?>> classes = new ArrayList<Class<?>>(); if (!dir.exists()) { return classes; } File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory() && !flat) { classes.addAll(findClasses(file, pkgname + "." + file.getName(), flat)); } else if (file.getName().endsWith(".class")) { classes.add( Class.forName(pkgname + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; } }