List of utility methods to do Class Find
List | getAllClasses(ClassLoader cl, String packageName) get All Classes packageName = packageName.replace('.', '/'); List<String> classes = new ArrayList<>(); URL[] urls = ((URLClassLoader) cl).getURLs(); for (URL url : urls) { File jar = new File(url.getFile()); if (jar.isDirectory()) { File subdir = new File(jar, packageName); if (!subdir.exists()) ... |
List | getAllClasses(String packageName) get All Classes ArrayList<Class> classes = new ArrayList<Class>(); File directory = null; try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) throw new ClassNotFoundException("Can't get class loader."); String path = '/' + packageName.replace('.', '/'); URL resource = classLoader.getResource(path); ... |
Class[] | getAllClassesFromPackage(final String packageName) Load all classes from a package. ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; 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())); ... |
Collection | getAllClassesIn(String... packageNames) Returns a collection of all classes present in the packages with the given packageNames. Collection<Class<?>> allClasses = new ArrayList<Class<?>>(); for (String packageName : packageNames) { allClasses.addAll(getAllClassesIn(packageName)); return allClasses; |
List | getAllClassesInPackage(String packageName) get All Classes In Package ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<String> dirs = new ArrayList<String>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(URLDecoder.decode(resource.getFile(), "UTF-8")); TreeSet<String> classes = new TreeSet<String>(); for (String directory : dirs) { classes.addAll(findClasses(directory, packageName)); ArrayList<Class<?>> classList = new ArrayList<Class<?>>(); for (String clazz : classes) { classList.add(Class.forName(clazz)); return classList; |
Class[] | getClasses(Class clazz) get Classes if (clazz == null) { return new Class[0]; return getClasses(getPackageName(clazz)); |
List | getClasses(Class> cls) get Classes String pk = cls.getPackage().getName(); String path = pk.replace('.', '/'); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); URL url = classloader.getResource(path); return getClasses(new File(url.getFile()), pk); |
List | getClasses(ClassLoader cl, String pack) get Classes String dottedPackage = pack.replaceAll("[/]", "."); List<Class> classes = new ArrayList<Class>(); URL upackage = cl.getResource(pack); DataInputStream dis = new DataInputStream((InputStream) upackage.getContent()); String line = null; while ((line = dis.readLine()) != null) { if (line.endsWith(".class")) { try { ... |
Set | getClasses(final String packageName) get Classes final ClassLoader loader = Thread.currentThread().getContextClassLoader(); return getClasses(loader, packageName); |
List | getClasses(final String packageName, final Class extends Annotation> annotation) Return all classes below the package root that have the specified annotation. final List<Class<?>> classes = getClasses(packageName); final List<Class<?>> annotatedClasses = new LinkedList<Class<?>>(); for (Class<?> clazz : classes) { if (clazz.isAnnotationPresent(annotation)) { annotatedClasses.add(clazz); return annotatedClasses; ... |