/** * Finds the resources names present at this location and below on the classpath starting with this prefix and * ending with this suffix. */ private Set<String> findResourceNames(Location location, ResourceFilter predicate) throws IOException { Set<String> resourceNames = new TreeSet<String>(); List<URL> locationsUrls = getLocationUrlsForPath(location); for (URL locationUrl : locationsUrls) { LOG.debug("scanning URL: {}", locationUrl.toExternalForm()); UrlResolver urlResolver = createUrlResolver(locationUrl.getProtocol()); URL resolvedUrl = urlResolver.toStandardJavaUrl(locationUrl); String protocol = resolvedUrl.getProtocol(); ClassPathLocationScanner classPathLocationScanner = createLocationScanner(protocol); if (classPathLocationScanner == null) { String scanRoot = UrlUtils.toFilePath(resolvedUrl); LOG.warn("Unable to scan location: {} (unsupported protocol: {})", scanRoot, protocol); } else { Set<String> names = resourceNameCache.get(classPathLocationScanner).get(resolvedUrl); if (names == null) { names = classPathLocationScanner.findResourceNames(location.getPath(), resolvedUrl); resourceNameCache.get(classPathLocationScanner).put(resolvedUrl, names); } resourceNames.addAll(names); } } return filterResourceNames(resourceNames, predicate); }
@Override public List<Resource> scanForResources(Location path, ResourceFilter predicate) { try { List<Resource> resources = new ArrayList<Resource>(); Set<String> resourceNames = findResourceNames(path, predicate); for (String resourceName : resourceNames) { resources.add(new ClassPathResource(resourceName, classLoader)); LOG.trace("... found resource: {}", resourceName); } return resources; } catch (IOException e) { throw new ClassPathScanException(e); } }
@Override public List<Class<?>> scanForClasses(Location location, ClassFilter predicate) { try { List<Class<?>> classes = new ArrayList<Class<?>>(); Set<String> resourceNames = findResourceNames(location, FilterResource.bySuffix(".class")); LOG.debug("scanning for classes at {} found {} resources to check", location, resourceNames.size()); for (String resourceName : resourceNames) { String className = toClassName(resourceName); try { Class<?> clazz = classLoader.loadClass(className); if (predicate.isMatch(clazz)) { classes.add(clazz); LOG.trace("... matched class: {} ", className); } } catch (NoClassDefFoundError err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } catch (ClassNotFoundException err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } } return classes; } catch (IOException e) { throw new ClassPathScanException(e); } }
public Scanner(ClassLoader classLoader) { if (EnvironmentDetection.isAndroid()) { resourceAndClassScanner = new AndroidScanner(classLoader); } else { resourceAndClassScanner = new ClassPathScanner(classLoader); } }