@Test public void test_getClassesFromJar_withSubpackage() throws IOException { final String packageString = "ro.pippo.controller.util.data"; mockClassUtils(packageString, true); Collection<Class<?>> classes = ClassUtils.getClasses(packageString); assertEquals(5, classes.size()); }
public static Collection<Class<?>> getClasses(String... packageNames) { Set<Class<?>> classes = new HashSet<>(); for (String packageName : packageNames) { final String packagePath = packageName.replace('.', '/'); final String packagePrefix = packageName + '.'; List<URL> packageUrls = getResources(packagePath); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { log.debug("Scanning jar '{}' for classes", packageUrl); classes.addAll(getClassesFromJar(packageUrl, packagePrefix)); } else { log.debug("Scanning filesystem '{}' for classes (protocol = {})", packageUrl, packageUrl.getProtocol()); classes.addAll(getClassesFromFileSystem(packageUrl, packagePrefix)); } } } return Collections.unmodifiableCollection(classes); }
ClassUtils { public static Collection<Class<?>> getClasses(String... packageNames) { Set<Class<?>> classes = new HashSet<>(); for (String packageName : packageNames) { final String packagePath = packageName.replace('.', '/'); final String packagePrefix = packageName + '.'; List<URL> packageUrls = getResources(packagePath); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { log.debug("Scanning jar '{}' for classes", packageUrl); classes.addAll(getClassesFromJar(packageUrl, packagePrefix)); } else { log.debug("Scanning filesystem '{}' for classes (protocol = {})", packageUrl, packageUrl.getProtocol()); classes.addAll(getClassesFromFileSystem(packageUrl, packagePrefix)); } } } return Collections.unmodifiableCollection(classes); } }
ClassUtils { public static Collection<Class<?>> getClasses(String... packageNames) { Set<Class<?>> classes = new HashSet<>(); for (String packageName : packageNames) { final String packagePath = packageName.replace('.', '/'); final String packagePrefix = packageName + '.'; List<URL> packageUrls = getResources(packagePath); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { log.debug("Scanning jar '{}' for classes", packageUrl); classes.addAll(getClassesFromJar(packageUrl, packagePrefix)); } else { log.debug("Scanning filesystem '{}' for classes (protocol = {})", packageUrl, packageUrl.getProtocol()); classes.addAll(getClassesFromFileSystem(packageUrl, packagePrefix)); } } } return Collections.unmodifiableCollection(classes); }  }
ClassUtils { public static Collection<Class<?>> getClasses(String... packageNames) { Set<Class<?>> classes = new HashSet<>(); for (String packageName : packageNames) { final String packagePath = packageName.replace('.', '/'); final String packagePrefix = packageName + '.'; List<URL> packageUrls = getResources(packagePath); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { log.debug("Scanning jar '{}' for classes", packageUrl); classes.addAll(getClassesFromJar(packageUrl, packagePrefix)); } else { log.debug("Scanning filesystem '{}' for classes (protocol = {})", packageUrl, packageUrl.getProtocol()); classes.addAll(getClassesFromFileSystem(packageUrl, packagePrefix)); } } } return Collections.unmodifiableCollection(classes); }  @SuppressWarnings("unchecked") static Class<T> getClass(String className); static Collection<Class<?>> getClasses(String... packageNames); static Collection<Class<? extends T>> getSubTypesOf(Class<T> type, String... packageNames); static Collection<Class<?>> getAnnotatedClasses(Class<? extends Annotation> annotationClass, String... packageNames); static T getAnnotation(Method method, Class<T> annotationClass); static T getAnnotation(Class<?> objectClass, Class<T> annotationClass); static List<Method> getDeclaredMethods(Class<?> clazz); static List<T> collectNestedAnnotation(Method method, Class<T> annotationClass); static List<T> collectNestedAnnotation(Class<?> objectClass, Class<T> annotationClass); @SuppressWarnings("unchecked") static T executeDeclaredMethod(Object o, String methodName, Object... args); static List<URL> getResources(String name); static boolean isAssignable(Object value, Class<?> type); }
ClassUtils { public static Collection<Class<?>> getClasses(String... packageNames) { Set<Class<?>> classes = new HashSet<>(); for (String packageName : packageNames) { final String packagePath = packageName.replace('.', '/'); final String packagePrefix = packageName + '.'; List<URL> packageUrls = getResources(packagePath); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { log.debug("Scanning jar '{}' for classes", packageUrl); classes.addAll(getClassesFromJar(packageUrl, packagePrefix)); } else { log.debug("Scanning filesystem '{}' for classes (protocol = {})", packageUrl, packageUrl.getProtocol()); classes.addAll(getClassesFromFileSystem(packageUrl, packagePrefix)); } } } return Collections.unmodifiableCollection(classes); }  @SuppressWarnings("unchecked") static Class<T> getClass(String className); static Collection<Class<?>> getClasses(String... packageNames); static Collection<Class<? extends T>> getSubTypesOf(Class<T> type, String... packageNames); static Collection<Class<?>> getAnnotatedClasses(Class<? extends Annotation> annotationClass, String... packageNames); static T getAnnotation(Method method, Class<T> annotationClass); static T getAnnotation(Class<?> objectClass, Class<T> annotationClass); static List<Method> getDeclaredMethods(Class<?> clazz); static List<T> collectNestedAnnotation(Method method, Class<T> annotationClass); static List<T> collectNestedAnnotation(Class<?> objectClass, Class<T> annotationClass); @SuppressWarnings("unchecked") static T executeDeclaredMethod(Object o, String methodName, Object... args); static List<URL> getResources(String name); static boolean isAssignable(Object value, Class<?> type);  }