public abstract class ClasspathHelper
extends java.lang.Object
Constructor and Description |
---|
ClasspathHelper() |
Modifier and Type | Method and Description |
---|---|
static java.lang.ClassLoader[] |
classLoaders(java.lang.ClassLoader... classLoaders)
Returns an array of class Loaders initialized from the specified array.
|
static java.lang.String |
cleanPath(java.net.URL url)
Cleans the URL.
|
static java.lang.ClassLoader |
contextClassLoader()
Gets the current thread context class loader.
|
private static java.util.Collection<java.net.URL> |
distinctUrls(java.util.Collection<java.net.URL> urls) |
static java.net.URL |
forClass(java.lang.Class<?> aClass,
java.lang.ClassLoader... classLoaders)
Returns the URL that contains a
Class . |
static java.util.Collection<java.net.URL> |
forClassLoader()
Returns a distinct collection of URLs based on URLs derived from class loaders.
|
static java.util.Collection<java.net.URL> |
forClassLoader(java.lang.ClassLoader... classLoaders)
Returns a distinct collection of URLs based on URLs derived from class loaders.
|
static java.util.Collection<java.net.URL> |
forJavaClassPath()
Returns a distinct collection of URLs based on the
java.class.path system property. |
static java.util.Collection<java.net.URL> |
forManifest()
Returns a distinct collection of URLs based on URLs derived from class loaders expanded with Manifest information.
|
static java.util.Collection<java.net.URL> |
forManifest(java.lang.Iterable<java.net.URL> urls)
Returns a distinct collection of URLs by expanding the specified URLs with Manifest information.
|
static java.util.Collection<java.net.URL> |
forManifest(java.net.URL url)
Returns a distinct collection of URLs from a single URL based on the Manifest information.
|
static java.util.Collection<java.net.URL> |
forPackage(java.lang.String name,
java.lang.ClassLoader... classLoaders)
Returns a distinct collection of URLs based on a package name.
|
static java.util.Collection<java.net.URL> |
forResource(java.lang.String resourceName,
java.lang.ClassLoader... classLoaders)
Returns a distinct collection of URLs based on a resource.
|
static java.net.URL |
forWebInfClasses(javax.servlet.ServletContext servletContext)
Returns the URL of the
WEB-INF/classes folder. |
static java.util.Collection<java.net.URL> |
forWebInfLib(javax.servlet.ServletContext servletContext)
Returns a distinct collection of URLs based on the
WEB-INF/lib folder. |
private static java.lang.String |
resourceName(java.lang.String name) |
static java.lang.ClassLoader |
staticClassLoader()
Gets the class loader of this library.
|
(package private) static java.net.URL |
tryToGetValidUrl(java.lang.String workingDir,
java.lang.String path,
java.lang.String filename) |
public static java.lang.ClassLoader contextClassLoader()
Thread.currentThread().getContextClassLoader()
.public static java.lang.ClassLoader staticClassLoader()
Reflections.class.getClassLoader()
.public static java.lang.ClassLoader[] classLoaders(java.lang.ClassLoader... classLoaders)
If the input is null or empty, it defaults to both contextClassLoader()
and staticClassLoader()
public static java.util.Collection<java.net.URL> forPackage(java.lang.String name, java.lang.ClassLoader... classLoaders)
This searches for the package name as a resource, using ClassLoader.getResources(String)
.
For example, forPackage(org.reflections)
effectively returns URLs from the
classpath containing packages starting with org.reflections
.
If the optional ClassLoader
s are not specified, then both contextClassLoader()
and staticClassLoader()
are used for ClassLoader.getResources(String)
.
The returned URLs retainsthe order of the given classLoaders
.
public static java.util.Collection<java.net.URL> forResource(java.lang.String resourceName, java.lang.ClassLoader... classLoaders)
This searches for the resource name, using ClassLoader.getResources(String)
.
For example, forResource(test.properties)
effectively returns URLs from the
classpath containing files of that name.
If the optional ClassLoader
s are not specified, then both contextClassLoader()
and staticClassLoader()
are used for ClassLoader.getResources(String)
.
The returned URLs retains the order of the given classLoaders
.
public static java.net.URL forClass(java.lang.Class<?> aClass, java.lang.ClassLoader... classLoaders)
Class
.
This searches for the class using ClassLoader.getResource(String)
.
If the optional ClassLoader
s are not specified, then both contextClassLoader()
and staticClassLoader()
are used for ClassLoader.getResources(String)
.
public static java.util.Collection<java.net.URL> forClassLoader()
This finds the URLs using URLClassLoader.getURLs()
using both
contextClassLoader()
and staticClassLoader()
.
The returned URLs retains the order of the given classLoaders
.
public static java.util.Collection<java.net.URL> forClassLoader(java.lang.ClassLoader... classLoaders)
This finds the URLs using URLClassLoader.getURLs()
using the specified
class loader, searching up the parent hierarchy.
If the optional ClassLoader
s are not specified, then both contextClassLoader()
and staticClassLoader()
are used for ClassLoader.getResources(String)
.
The returned URLs retains the order of the given classLoaders
.
public static java.util.Collection<java.net.URL> forJavaClassPath()
java.class.path
system property.
This finds the URLs using the java.class.path
system property.
The returned collection of URLs retains the classpath order.
public static java.util.Collection<java.net.URL> forWebInfLib(javax.servlet.ServletContext servletContext)
WEB-INF/lib
folder.
This finds the URLs using the ServletContext
.
The returned URLs retains the order of the given classLoaders
.
public static java.net.URL forWebInfClasses(javax.servlet.ServletContext servletContext)
WEB-INF/classes
folder.
This finds the URLs using the ServletContext
.
public static java.util.Collection<java.net.URL> forManifest()
The MANIFEST.MF
file can contain a Class-Path
entry that defines
additional jar files to be included on the classpath. This method finds the jar files
using the contextClassLoader()
and staticClassLoader()
, before
searching for any additional manifest classpaths.
public static java.util.Collection<java.net.URL> forManifest(java.net.URL url)
The MANIFEST.MF
file can contain a Class-Path
entry that defines additional
jar files to be included on the classpath. This method takes a single URL, tries to
resolve it as a jar file, and if so, adds any additional manifest classpaths.
The returned collection of URLs will always contain the input URL.
public static java.util.Collection<java.net.URL> forManifest(java.lang.Iterable<java.net.URL> urls)
The MANIFEST.MF
file can contain a Class-Path
entry that defines additional
jar files to be included on the classpath. This method takes each URL in turn, tries to
resolve it as a jar file, and if so, adds any additional manifest classpaths.
The returned collection of URLs will always contain all the input URLs.
The returned URLs retains the input order.
static java.net.URL tryToGetValidUrl(java.lang.String workingDir, java.lang.String path, java.lang.String filename)
public static java.lang.String cleanPath(java.net.URL url)
url
- the URL to clean, not nullprivate static java.lang.String resourceName(java.lang.String name)
private static java.util.Collection<java.net.URL> distinctUrls(java.util.Collection<java.net.URL> urls)