/** * Scans the classpath for classes under the specified package matching the given predicate. * * @param location The package in the classpath to start scanning. Subpackages are also scanned. * @param predicate The predicate used to match scanned classes. * @return The classes found matching the predicate */ @Override public List<Class<?>> scanForClasses(String location, ClassFilter predicate) { return scanForClasses(new Location(location), predicate); }
@Override public String toString() { return getDescriptor(); } }
/** * Creates a new location. * * @param descriptor The location descriptor. */ public Location(String descriptor) { String normalizedDescriptor = descriptor.trim().replace("\\", "/"); if (normalizedDescriptor.contains(":")) { prefix = normalizedDescriptor.substring(0, normalizedDescriptor.indexOf(":") + 1); path = normalizedDescriptor.substring(normalizedDescriptor.indexOf(":") + 1); } else { prefix = CLASSPATH_PREFIX; path = normalizedDescriptor; } if (isClassPath()) { path = path.replace(".", "/"); if (path.startsWith("/")) { path = path.substring(1); } } else { if (!isFileSystem()) { throw new ClassPathScanException("Unknown prefix, should be either filesystem: or classpath: " + normalizedDescriptor); } } if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } }
Enumeration<URL> urls = classLoader.getResources(location.toString()); if (!urls.hasMoreElements()) { LOG.warn("Unable to resolve location " + location); 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 this location for resources matching the given predicate. * <p> * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine * how to scan. If no prefix is used then classpath scan is the default. * </p> * * @param location The location to start searching. Subdirectories are also searched. * @param predicate The predicate used to match resource names. * @return The resources that were found. */ public List<Resource> scanForResources(Location location, ResourceFilter predicate) { if (location.isFileSystem()) { return fileSystemScanner.scanForResources(location, predicate); } return resourceAndClassScanner.scanForResources(location, predicate); }
/** * 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); } }
@SuppressWarnings("NullableProblems") public int compareTo(Location o) { return getDescriptor().compareTo(o.getDescriptor()); }
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); } } }
/** * Scans this location for resources matching the given predicate. * <p> * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine * how to scan. If no prefix is used then classpath scan is the default. * </p> * * @param location The location to start searching. Subdirectories are also searched. * @param predicate The predicate used to match resource names. * @return The resources that were found. */ @Override public List<Resource> scanForResources(String location, ResourceFilter predicate) { return scanForResources(new Location(location), predicate); }
@Override public int hashCode() { return getDescriptor().hashCode(); }
/** * 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 boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Location location = (Location) o; return getDescriptor().equals(location.getDescriptor()); }