@Test public void testGetClassesWithException() throws ClassNotFoundException, IOException { ClassUtils.getClasses("123"); }
public static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); String p = ""; if (resource.getFile().indexOf("!") >= 0) { p = resource.getFile().substring(0, resource.getFile().indexOf("!")).replaceAll("%20", ""); } else { p = resource.getFile(); } if (p.startsWith("file:/")) p = p.substring(5); if (p.toLowerCase().endsWith(".jar")) { JarFile jarFile = new JarFile(p); Enumeration<JarEntry> enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry entry = (JarEntry) enums.nextElement(); String n = entry.getName(); if (n.endsWith(".class")) { n = n.replaceAll("/", ".").substring(0, n.length() - 6); if (n.startsWith(packageName)) { classes.add(Class.forName(n)); } } } jarFile.close(); } else { dirs.add(new File(p)); } } for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }
ClassUtils { public static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); String p = ""; if (resource.getFile().indexOf("!") >= 0) { p = resource.getFile().substring(0, resource.getFile().indexOf("!")).replaceAll("%20", ""); } else { p = resource.getFile(); } if (p.startsWith("file:/")) p = p.substring(5); if (p.toLowerCase().endsWith(".jar")) { JarFile jarFile = new JarFile(p); Enumeration<JarEntry> enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry entry = (JarEntry) enums.nextElement(); String n = entry.getName(); if (n.endsWith(".class")) { n = n.replaceAll("/", ".").substring(0, n.length() - 6); if (n.startsWith(packageName)) { classes.add(Class.forName(n)); } } } jarFile.close(); } else { dirs.add(new File(p)); } } for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); } }
ClassUtils { public static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); String p = ""; if (resource.getFile().indexOf("!") >= 0) { p = resource.getFile().substring(0, resource.getFile().indexOf("!")).replaceAll("%20", ""); } else { p = resource.getFile(); } if (p.startsWith("file:/")) p = p.substring(5); if (p.toLowerCase().endsWith(".jar")) { JarFile jarFile = new JarFile(p); Enumeration<JarEntry> enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry entry = (JarEntry) enums.nextElement(); String n = entry.getName(); if (n.endsWith(".class")) { n = n.replaceAll("/", ".").substring(0, n.length() - 6); if (n.startsWith(packageName)) { classes.add(Class.forName(n)); } } } jarFile.close(); } else { dirs.add(new File(p)); } } for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }  }
ClassUtils { public static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); String p = ""; if (resource.getFile().indexOf("!") >= 0) { p = resource.getFile().substring(0, resource.getFile().indexOf("!")).replaceAll("%20", ""); } else { p = resource.getFile(); } if (p.startsWith("file:/")) p = p.substring(5); if (p.toLowerCase().endsWith(".jar")) { JarFile jarFile = new JarFile(p); Enumeration<JarEntry> enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry entry = (JarEntry) enums.nextElement(); String n = entry.getName(); if (n.endsWith(".class")) { n = n.replaceAll("/", ".").substring(0, n.length() - 6); if (n.startsWith(packageName)) { classes.add(Class.forName(n)); } } } jarFile.close(); } else { dirs.add(new File(p)); } } for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }  static Class<?>[] getClasses(String packageName); static List<Class<?>> findClasses(File directory, String packageName); static URL[] getResource(String resourceName, Class callingClass); }
ClassUtils { public static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); String p = ""; if (resource.getFile().indexOf("!") >= 0) { p = resource.getFile().substring(0, resource.getFile().indexOf("!")).replaceAll("%20", ""); } else { p = resource.getFile(); } if (p.startsWith("file:/")) p = p.substring(5); if (p.toLowerCase().endsWith(".jar")) { JarFile jarFile = new JarFile(p); Enumeration<JarEntry> enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry entry = (JarEntry) enums.nextElement(); String n = entry.getName(); if (n.endsWith(".class")) { n = n.replaceAll("/", ".").substring(0, n.length() - 6); if (n.startsWith(packageName)) { classes.add(Class.forName(n)); } } } jarFile.close(); } else { dirs.add(new File(p)); } } for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }  static Class<?>[] getClasses(String packageName); static List<Class<?>> findClasses(File directory, String packageName); static URL[] getResource(String resourceName, Class callingClass);  }