/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @return The classes found under the class loader * * @throws IOException if a problem is encountered loading the resources from the package * @throws ClassNotFoundException if we're not able to find classes for the package in the directory */ public List<Class<?>> getClasses() throws IOException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); TreeSet<File> dirs = new TreeSet<>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } List<Class<?>> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes; }
/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @return The classes found under the class loader * * @throws IOException if a problem is encountered loading the resources from the package * @throws ClassNotFoundException if we're not able to find classes for the package in the directory */ public List<Class<?>> getClasses() throws IOException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); TreeSet<File> dirs = new TreeSet<>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } List<Class<?>> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes; }
/** * Recursive method used to find all classes in a given directory and subdirectories. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException if a class in the directory cannot be loaded */ private List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class<?>> classes = new ArrayList<>(); if (!directory.exists()) { return classes; } Iterable<File> files = new TreeSet<>(Arrays.asList(directory.listFiles())); for (File file : files) { String name = file.getName(); if (file.isDirectory()) { assert !name.contains("."); // Extend the package and recurse classes.addAll(findClasses(file, packageName + "." + name)); } else if (name.endsWith(".class")) { // Grab just the class name, stripping the .class extension name = packageName + '.' + name.substring(0, name.length() - 6); classes.add(Class.forName(name)); } } return classes; }
/** * Recursive method used to find all classes in a given directory and subdirectories. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException if a class in the directory cannot be loaded */ private List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class<?>> classes = new ArrayList<>(); if (!directory.exists()) { return classes; } Iterable<File> files = new TreeSet<>(Arrays.asList(directory.listFiles())); for (File file : files) { String name = file.getName(); if (file.isDirectory()) { assert !name.contains("."); // Extend the package and recurse classes.addAll(findClasses(file, packageName + "." + name)); } else if (name.endsWith(".class")) { // Grab just the class name, stripping the .class extension name = packageName + '.' + name.substring(0, name.length() - 6); classes.add(Class.forName(name)); } } return classes; }