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 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); } }
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); } }
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); } } }
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);"); } }
/** * 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 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); } }
/** * 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 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); } }
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); } } }
@Override public byte[] loadAsBytes() { try { return FileCopyUtils.copyToByteArray(assetManager.open(getLocation())); } catch (IOException e) { throw new ClassPathScanException("Unable to load asset: " + getLocation(), 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); } }
@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); } }