/** * 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(); } }
@Override public ClassPathScanner createScanner(ClassLoader classLoader) { return new Scanner(classLoader); } }
/** * 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); }
/** * 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); } }
public byte[] loadAsBytes() { try { InputStream inputStream = classLoader.getResourceAsStream(location); if (inputStream == null) { throw new ClassPathScanException("Unable to obtain inputstream for resource: " + location); } return FileCopyUtils.copyToByteArray(inputStream); } catch (IOException e) { throw new ClassPathScanException("Unable to load resource: " + location, e); } }
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); }
public AndroidScanner(ClassLoader classLoader) { this.classLoader = (PathClassLoader) classLoader; context = ContextHolder.getContext(); if (context == null) { throw new ClassPathScanException("Unable to create scanner. " + "Within an activity you can fix this with org.avaje.classpath.scanner.android.ContextHolder.setContext(this);"); } }
@SuppressWarnings("NullableProblems") public int compareTo(Location o) { return getDescriptor().compareTo(o.getDescriptor()); }
public URL toStandardJavaUrl(URL url) throws IOException { try { Class<?> vfsClass = Class.forName("org.jboss.virtual.VFS"); Class<?> vfsUtilsClass = Class.forName("org.jboss.virtual.VFSUtils"); Class<?> virtualFileClass = Class.forName("org.jboss.virtual.VirtualFile"); Method getRootMethod = vfsClass.getMethod("getRoot", URL.class); Method getRealURLMethod = vfsUtilsClass.getMethod("getRealURL", virtualFileClass); Object root = getRootMethod.invoke(null, url); return (URL) getRealURLMethod.invoke(null, root); } catch (Exception e) { throw new ClassPathScanException("JBoss VFS v2 call failed", e); } } }
@Override public int hashCode() { return getDescriptor().hashCode(); }
public String loadAsString(String encoding) { try { InputStream inputStream = classLoader.getResourceAsStream(location); if (inputStream == null) { throw new ClassPathScanException("Unable to obtain inputstream for resource: " + location); } Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding)); return FileCopyUtils.copyToString(reader); } catch (IOException e) { throw new ClassPathScanException("Unable to load resource: " + location + " (encoding: " + encoding + ")", e); } }
@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()); }
public String getLocationOnDisk() { URL url = getUrl(); if (url == null) { throw new ClassPathScanException("Unable to location resource on disk: " + location); } try { return new File(URLDecoder.decode(url.getPath(), "UTF-8")).getAbsolutePath(); } catch (UnsupportedEncodingException e) { throw new ClassPathScanException("Unknown encoding: UTF-8", e); } }
/** * Loads this resource as a byte array. * * @return The contents of the resource. */ public byte[] loadAsBytes() { try { InputStream inputStream = new FileInputStream(location); return FileCopyUtils.copyToByteArray(inputStream); } catch (IOException e) { throw new ClassPathScanException("Unable to load filesystem resource: " + location.getPath(), e); } }
/** * Loads this resource as a string. * * @param encoding The encoding to use. * @return The string contents of the resource. */ public String loadAsString(String encoding) { try { InputStream inputStream = new FileInputStream(location); Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding)); return FileCopyUtils.copyToString(reader); } catch (IOException e) { throw new ClassPathScanException("Unable to load filesystem resource: " + location.getPath() + " (encoding: " + encoding + ")", e); } }
@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 byte[] loadAsBytes() { try { return FileCopyUtils.copyToByteArray(assetManager.open(getLocation())); } catch (IOException e) { throw new ClassPathScanException("Unable to load asset: " + getLocation(), e); } }
@Override public String loadAsString(String encoding) { try { return FileCopyUtils.copyToString(new InputStreamReader(assetManager.open(getLocation()), encoding)); } catch (IOException e) { throw new ClassPathScanException("Unable to load asset: " + getLocation(), e); } }