Package com.google.common.reflect
Class ClassPath
- java.lang.Object
-
- com.google.common.reflect.ClassPath
-
public final class ClassPath extends java.lang.Object
Scans the source of aClassLoaderand finds all loadable classes and resources.Prefer ClassGraph over
ClassPathWe recommend using ClassGraph instead of
ClassPath. ClassGraph improves uponClassPathin several ways, including addressing many of its limitations. Limitations ofClassPathinclude:- It looks only for files and JARs in URLs available from
URLClassLoaderinstances or the system class loader. This means it does not look for classes in the module path. - It understands only
file:URLs. This means that it does not understandjrt:/URLs, among others. - It does not know how to look for classes when running under an Android VM. (ClassGraph does not support this directly, either, but ClassGraph documents how to perform build-time classpath scanning and make the results available to an Android app.)
- Like all of Guava, it is not tested under Windows. We have gotten a report of a specific bug under Windows.
- It returns only one resource for a given path, even if resources with that path appear in multiple jars or directories.
- It assumes that any class with a
$in its name is a nested class.
ClassPathand symlinksIn 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
- Author:
- Ben Yu
- It 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().static classClassPath.ResourceInfoRepresents a class path resource that can be either a class file or any other resource file loadable from the class path.
-
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.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 '.'.
-
-
-
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 '.'.
-
-