/** return urls that are in the current class path. * attempts to load the jar manifest, if any, and adds to the result any dependencies it finds. */ public static Set<URL> forManifest() { return forManifest(forClassLoader()); }
/** returns the url that contains the given class, using {@link ClassLoader#getResource(String)} * <p>if optional {@link ClassLoader}s are not specified, then either {@link #getContextClassLoader()} or {@link #getStaticClassLoader()} are used for {@link ClassLoader#getResources(String)} * */ public static URL forClass(Class<?> aClass, ClassLoader... classLoaders) { final ClassLoader[] loaders = classLoaders(classLoaders); final String resourceName = aClass.getName().replace(".", "/") + ".class"; for (ClassLoader classLoader : loaders) { try { final URL url = classLoader.getResource(resourceName); if (url != null) { final String normalizedUrl = url.toExternalForm().substring(0, url.toExternalForm().lastIndexOf(aClass.getPackage().getName().replace(".", "/"))); return new URL(normalizedUrl); } } catch (MalformedURLException e) { e.printStackTrace(); } } return null; }
ContraintScanner() { super(new ConfigurationBuilder() .setUrls(ClasspathHelper.forClassLoader()) .setScanners( new FieldAnnotationsScanner(), new TypeAnnotationsScanner())); scan(); } }
addUrls(ClasspathHelper.forPackage((String) urlHint)); prefixes.add((String) urlHint); addUrls(ClasspathHelper.forClass((Class) urlHint)); prefixes.add(((Class) urlHint).getPackage().getName());
/** * collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter * and de-serializes them using the serializer configured in the configuration * <p/> * it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), * so that relevant urls could be found much faster */ public Reflections collect(final String packagePrefix, final Predicate<String> resourceNameFilter, final Serializer serializer) { for (final Vfs.File file : Vfs.findFiles(ClasspathHelper.forPackage(packagePrefix), packagePrefix, resourceNameFilter)) { InputStream inputStream = null; try { inputStream = file.openInputStream(); merge(serializer.read(inputStream)); log.info("Reflections collected metadata from " + file + " using serializer " + serializer.getClass().getName()); } catch (IOException e) { throw new ReflectionsException("could not merge " + file, e); } finally { Utils.close(inputStream); } } return this; }
/** get the urls that are specified in the manifest of the given urls. * attempts to load the jar manifest, if any, and adds to the result any dependencies it finds. */ public static Set<URL> forManifest(final Iterable<URL> urls) { Set<URL> result = Sets.newHashSet(); // determine if any of the URLs are JARs, and get any dependencies for (URL url : urls) { result.addAll(forManifest(url)); } return result; }
/** get the urls that are specified in the manifest of the given url for a jar file. * attempts to load the jar manifest, if any, and adds to the result any dependencies it finds. */ public static Set<URL> forManifest(final URL url) { final Set<URL> result = Sets.newHashSet(); result.add(url); try { final String part = Vfs.normalizePath(url); File jarFile = new File(part); JarFile myJar = new JarFile(part); URL validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), part); if (validUrl != null) { result.add(validUrl); } final Manifest manifest = myJar.getManifest(); if (manifest != null) { final String classPath = manifest.getMainAttributes().getValue(new Attributes.Name("Class-Path")); if (classPath != null) { for (String jar : classPath.split(" ")) { validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), jar); if (validUrl != null) { result.add(validUrl); } } } } } catch (IOException e) { // don't do anything, we're going on the assumption it is a jar, which could be wrong } return result; }
/** * a convenient constructor for scanning within a package prefix * <p>this actually create a {@link Configuration} with: * <br> - urls that contain resources with name {@code prefix} * <br> - acceptsInput where name starts with the given {@code prefix} * <br> - scanners set to the given {@code scanners}, otherwise defaults to {@link TypeAnnotationsScanner} and {@link SubTypesScanner}. * <br> - scanner results filter is set to accept results matching given {@code prefix} */ public Reflections(final String prefix, final Scanner... scanners) { this(new ConfigurationBuilder() { { final Predicate<String> filter = new FilterBuilder.Include(FilterBuilder.prefix(prefix)); setUrls(ClasspathHelper.forPackage(prefix)); filterInputsBy(filter); if (scanners != null && scanners.length != 0) { for (Scanner scanner : scanners) { scanner.filterResultsBy(Predicates.<String>and(filter, scanner.getResultFilter())); } setScanners(scanners); } else { setScanners( new TypeAnnotationsScanner().filterResultsBy(filter), new SubTypesScanner().filterResultsBy(filter)); } } }); }
/** returns urls with resources of package starting with given name, using {@link ClassLoader#getResources(String)} * <p>that is, forPackage("org.reflections") effectively returns urls from classpath with packages starting with {@code org.reflections} * <p>if optional {@link ClassLoader}s are not specified, then both {@link #getContextClassLoader()} and {@link #getStaticClassLoader()} are used for {@link ClassLoader#getResources(String)} */ public static Set<URL> forPackage(String name, ClassLoader... classLoaders) { final Set<URL> result = Sets.newHashSet(); final ClassLoader[] loaders = classLoaders(classLoaders); final String resourceName = name.replace(".", "/"); for (ClassLoader classLoader : loaders) { try { final Enumeration<URL> urls = classLoader.getResources(resourceName); while (urls.hasMoreElements()) { final URL url = urls.nextElement(); final URL normalizedUrl = new URL(url.toExternalForm().substring(0, url.toExternalForm().lastIndexOf(resourceName))); result.add(normalizedUrl); } } catch (IOException e) { e.printStackTrace(); } } return result; }
/** returns urls using {@link java.net.URLClassLoader#getURLs()} up the classloader parent hierarchy * <p>if optional {@link ClassLoader}s are not specified, then both {@link #getContextClassLoader()} and {@link #getStaticClassLoader()} are used for {@link ClassLoader#getResources(String)} * */ public static Set<URL> forClassLoader(ClassLoader... classLoaders) { final Set<URL> result = Sets.newHashSet(); final ClassLoader[] loaders = classLoaders(classLoaders); for (ClassLoader classLoader : loaders) { while (classLoader != null) { if (classLoader instanceof URLClassLoader) { URL[] urls = ((URLClassLoader) classLoader).getURLs(); if (urls != null) { for (URL url : urls) { try { String urlString = url.toExternalForm(); String decodedUrlString = URLDecoder.decode(urlString.replaceAll("\\+", "%2b"), "UTF-8"); result.add(new URL(decodedUrlString)); } catch (IOException ioe) { throw new RuntimeException("Failed to scan configuration Url's", ioe); } } } } classLoader = classLoader.getParent(); } } return result; }
/** tries to resolve a java type name to a Class * <p>if optional {@link ClassLoader}s are not specified, then both {@link org.jboss.errai.reflections.util.ClasspathHelper#getContextClassLoader()} and {@link org.jboss.errai.reflections.util.ClasspathHelper#getStaticClassLoader()} are used * */ public static Class<?> forName(String typeName, ClassLoader... classLoaders) { if (primitiveNames.contains(typeName)) { return primitiveTypes.get(primitiveNames.indexOf(typeName)); } else { String type; if (typeName.contains("[")) { int i = typeName.indexOf("["); type = typeName.substring(0, i); String array = typeName.substring(i).replace("]", ""); if (primitiveNames.contains(type)) { type = primitiveDescriptors.get(primitiveNames.indexOf(type)); } else { type = "L" + type + ";"; } type = array + type; } else { type = typeName; } for (ClassLoader classLoader : ClasspathHelper.classLoaders(classLoaders)) { try { return Class.forName(type, false, classLoader); } catch (ClassNotFoundException e) { /*continue*/ } } throw new IllegalArgumentException("Unable to load class \"" + type + "\""); } }