Enumeration<URL> urls = classLoader.getResources(location.getPath()); if (!urls.hasMoreElements()) { LOG.warn("Unable to resolve location " + location);
public List<Resource> scanForResources(Location location, ResourceFilter predicate) { try { List<Resource> resources = new ArrayList<Resource>(); String path = location.getPath(); for (String asset : context.getAssets().list(path)) { if (predicate.isMatch(asset)) { resources.add(new AndroidResource(context.getAssets(), path, asset)); } } return resources; } catch (IOException e) { throw new ClassPathScanException(e); } }
/** * Scans the FileSystem for resources under the specified location, starting with the specified prefix and ending with * the specified suffix. * * @param location The location in the filesystem to start searching. Subdirectories are also searched. * @param predicate The predicate used to match resources. * @return The resources that were found. */ public List<Resource> scanForResources(Location location, ResourceFilter predicate) { String path = location.getPath(); File dir = new File(path); if (!dir.isDirectory() || !dir.canRead()) { LOG.warn("Unable to resolve location filesystem:" + path); return Collections.emptyList(); } List<Resource> resources = new ArrayList<Resource>(); try { Set<String> resourceNames = findResourceNames(path, predicate); for (String resourceName : resourceNames) { resources.add(new FileSystemResource(resourceName)); LOG.debug("Found filesystem resource: " + resourceName); } return resources; } catch (IOException e) { throw new ClassPathScanException(e); } }
public List<Class<?>> scanForClasses(Location location, ClassFilter predicate) { try { String pkg = location.getPath().replace("/", "."); List<Class<?>> classes = new ArrayList<Class<?>>(); DexFile dex = new DexFile(context.getApplicationInfo().sourceDir); Enumeration<String> entries = dex.entries(); while (entries.hasMoreElements()) { String className = entries.nextElement(); if (className.startsWith(pkg)) { Class<?> clazz = classLoader.loadClass(className); if (predicate.isMatch(clazz)) { classes.add(clazz); LOG.trace("... found class: {}", className); } } } return classes; } catch (IOException e) { throw new ClassPathScanException(e); } catch (ClassNotFoundException e) { throw new ClassPathScanException(e); } } }
/** * 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); }