Package com.google.common.reflect
Class ClassPath
- java.lang.Object
-
- com.google.common.reflect.ClassPath
-
public final class ClassPath extends java.lang.ObjectScans the source of aClassLoaderand finds all loadable classes and resources.Warning: Current limitations:
- Looks only for files and JARs in URLs available from
URLClassLoaderinstances or the system class loader. - Only understands
file:URLs.
In the case of directory classloaders, symlinks are supported but cycles are not traversed. This guarantees discovery of each unique loadable resource. However, not all possible aliases for resources on cyclic paths will be listed.
- Since:
- 14.0
- Looks only for files and JARs in URLs available from
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classClassPath.ClassInfoRepresents a class that can be loaded throughClassPath.ClassInfo.load().(package private) static classClassPath.LocationInfoRepresents a single location (a directory or a jar file) in the class path and is responsible for scanning resources from this location.static classClassPath.ResourceInfoRepresents a class path resource that can be either a class file or any other resource file loadable from the class path.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringCLASS_FILE_NAME_EXTENSIONprivate static SplitterCLASS_PATH_ATTRIBUTE_SEPARATORSeparator for the Class-Path manifest attribute value in jar files.private static java.util.logging.Loggerloggerprivate ImmutableSet<ClassPath.ResourceInfo>resources
-
Constructor Summary
Constructors Modifier Constructor Description privateClassPath(ImmutableSet<ClassPath.ResourceInfo> resources)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ClassPathfrom(java.lang.ClassLoader classloader)Returns aClassPathrepresenting all classes and resources loadable fromclassloaderand its ancestor class loaders.ImmutableSet<ClassPath.ClassInfo>getAllClasses()Returns all classes loadable from the current class path.private static ImmutableList<java.net.URL>getClassLoaderUrls(java.lang.ClassLoader classloader)(package private) static java.lang.StringgetClassName(java.lang.String filename)(package private) static ImmutableMap<java.io.File,java.lang.ClassLoader>getClassPathEntries(java.lang.ClassLoader classloader)(package private) static java.net.URLgetClassPathEntry(java.io.File jarFile, java.lang.String path)Returns the absolute uri of the Class-Path entry value as specified in JAR File Specification.(package private) static ImmutableSet<java.io.File>getClassPathFromManifest(java.io.File jarFile, java.util.jar.Manifest manifest)Returns the class path URIs specified by theClass-Pathmanifest attribute, according to JAR File Specification.ImmutableSet<ClassPath.ResourceInfo>getResources()Returns all resources loadable from the current class path, including the class files of all loadable classes but excluding the "META-INF/MANIFEST.MF" file.ImmutableSet<ClassPath.ClassInfo>getTopLevelClasses()Returns all top level classes loadable from the current class path.ImmutableSet<ClassPath.ClassInfo>getTopLevelClasses(java.lang.String packageName)Returns all top level classes whose package name ispackageName.ImmutableSet<ClassPath.ClassInfo>getTopLevelClassesRecursive(java.lang.String packageName)Returns all top level classes whose package name ispackageNameor starts withpackageNamefollowed by a '.'.(package private) static ImmutableSet<ClassPath.LocationInfo>locationsFrom(java.lang.ClassLoader classloader)Returns all locations thatclassloaderand parent loaders load classes and resources from.(package private) static ImmutableList<java.net.URL>parseJavaClassPath()Returns the URLs in the class path specified by thejava.class.pathsystem property.(package private) static java.io.FiletoFile(java.net.URL url)
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
CLASS_PATH_ATTRIBUTE_SEPARATOR
private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR
Separator for the Class-Path manifest attribute value in jar files.
-
CLASS_FILE_NAME_EXTENSION
private static final java.lang.String CLASS_FILE_NAME_EXTENSION
- See Also:
- Constant Field Values
-
resources
private final ImmutableSet<ClassPath.ResourceInfo> resources
-
-
Constructor Detail
-
ClassPath
private ClassPath(ImmutableSet<ClassPath.ResourceInfo> resources)
-
-
Method Detail
-
from
public static ClassPath from(java.lang.ClassLoader classloader) throws java.io.IOException
Returns aClassPathrepresenting all classes and resources loadable fromclassloaderand its ancestor class loaders.Warning:
ClassPathcan find classes and resources only from:URLClassLoaderinstances'file:URLs- the system class loader. To search the
system class loader even when it is not a
URLClassLoader(as in Java 9),ClassPathsearches the files from thejava.class.pathsystem property.
- Throws:
java.io.IOException- if the attempt to read class path resources (jar files or directories) failed.
-
getResources
public ImmutableSet<ClassPath.ResourceInfo> getResources()
Returns all resources loadable from the current class path, including the class files of all loadable classes but excluding the "META-INF/MANIFEST.MF" file.
-
getAllClasses
public ImmutableSet<ClassPath.ClassInfo> getAllClasses()
Returns all classes loadable from the current class path.- Since:
- 16.0
-
getTopLevelClasses
public ImmutableSet<ClassPath.ClassInfo> getTopLevelClasses()
Returns all top level classes loadable from the current class path. Note that "top-level-ness" is determined heuristically by class name (seeClassPath.ClassInfo.isTopLevel()).
-
getTopLevelClasses
public ImmutableSet<ClassPath.ClassInfo> getTopLevelClasses(java.lang.String packageName)
Returns all top level classes whose package name ispackageName.
-
getTopLevelClassesRecursive
public ImmutableSet<ClassPath.ClassInfo> getTopLevelClassesRecursive(java.lang.String packageName)
Returns all top level classes whose package name ispackageNameor starts withpackageNamefollowed by a '.'.
-
locationsFrom
static ImmutableSet<ClassPath.LocationInfo> locationsFrom(java.lang.ClassLoader classloader)
Returns all locations thatclassloaderand parent loaders load classes and resources from. Callers can scan individual locations selectively or even in parallel.
-
getClassPathFromManifest
static ImmutableSet<java.io.File> getClassPathFromManifest(java.io.File jarFile, java.util.jar.Manifest manifest)
Returns the class path URIs specified by theClass-Pathmanifest attribute, according to JAR File Specification. Ifmanifestis null, it means the jar file has no manifest, and an empty set will be returned.
-
getClassPathEntries
static ImmutableMap<java.io.File,java.lang.ClassLoader> getClassPathEntries(java.lang.ClassLoader classloader)
-
getClassLoaderUrls
private static ImmutableList<java.net.URL> getClassLoaderUrls(java.lang.ClassLoader classloader)
-
parseJavaClassPath
static ImmutableList<java.net.URL> parseJavaClassPath()
Returns the URLs in the class path specified by thejava.class.pathsystem property.
-
getClassPathEntry
static java.net.URL getClassPathEntry(java.io.File jarFile, java.lang.String path) throws java.net.MalformedURLExceptionReturns the absolute uri of the Class-Path entry value as specified in JAR File Specification. Even though the specification only talks about relative urls, absolute urls are actually supported too (for example, in Maven surefire plugin).- Throws:
java.net.MalformedURLException
-
getClassName
static java.lang.String getClassName(java.lang.String filename)
-
toFile
static java.io.File toFile(java.net.URL url)
-
-