public final class ScanResult extends Object implements Closeable, AutoCloseable
Modifier and Type | Method and Description |
---|---|
long |
classpathContentsLastModifiedTime()
Find the maximum last-modified timestamp of any accepted file/directory/jarfile encountered during the scan.
|
boolean |
classpathContentsModifiedSinceScan()
Determine whether the classpath contents have been modified since the last scan.
|
void |
close()
Free any temporary files created by extracting jars or files from within jars.
|
static void |
closeAll()
Close all
ScanResult instances that have not yet been closed. |
static ScanResult |
fromJSON(String json)
Deserialize a ScanResult from previously-serialized JSON.
|
ClassInfoList |
getAllAnnotations()
Get all annotation classes found during the scan.
|
ClassInfoList |
getAllClasses()
Get all classes, interfaces and annotations found during the scan.
|
Map<String,ClassInfo> |
getAllClassesAsMap()
Get a map from class name to
ClassInfo object for all classes, interfaces and annotations found
during the scan. |
ClassInfoList |
getAllEnums()
Get all
Enum classes found during the scan. |
ClassInfoList |
getAllInterfaces()
Get all interface classes found during the scan (not including annotations, which are also technically
interfaces).
|
ClassInfoList |
getAllInterfacesAndAnnotations()
Get all interface or annotation classes found during the scan.
|
ClassInfoList |
getAllRecords()
Get all
record classes found during the scan (JDK 14+). |
ResourceList |
getAllResources()
Get the list of all resources.
|
Map<String,ResourceList> |
getAllResourcesAsMap()
Get a map from resource path to
Resource for all resources (including classfiles and non-classfiles)
found in accepted packages. |
ClassInfoList |
getAllStandardClasses()
Get all standard (non-interface/non-annotation) classes found during the scan.
|
ClassInfoList |
getAnnotationsOnClass(String className)
Get annotations on the named class.
|
Map<ClassInfo,ClassInfoList> |
getClassDependencyMap()
Get a map from the
ClassInfo object for each accepted class to a list of the classes referenced by
that class (i.e. |
ClassInfoList |
getClassesImplementing(Class<?> interfaceClass)
Get all classes that implement (or have superclasses that implement) the interface (or one of its
subinterfaces).
|
ClassInfoList |
getClassesImplementing(String interfaceName)
Get all classes that implement (or have superclasses that implement) the named interface (or one of its
subinterfaces).
|
ClassInfoList |
getClassesWithAnnotation(Class<? extends Annotation> annotation)
Get classes with the class annotation or meta-annotation.
|
ClassInfoList |
getClassesWithAnnotation(String annotationName)
Get classes with the named class annotation or meta-annotation.
|
ClassInfoList |
getClassesWithFieldAnnotation(Class<? extends Annotation> fieldAnnotation)
Get classes that have a field with an annotation of the named type.
|
ClassInfoList |
getClassesWithFieldAnnotation(String fieldAnnotationName)
Get classes that have a field with an annotation of the named type.
|
ClassInfoList |
getClassesWithMethodAnnotation(Class<? extends Annotation> methodAnnotation)
Get classes that have a method with an annotation of the named type.
|
ClassInfoList |
getClassesWithMethodAnnotation(String methodAnnotationName)
Get classes that have a method with an annotation of the named type.
|
ClassInfoList |
getClassesWithMethodParameterAnnotation(Class<? extends Annotation> methodParameterAnnotation)
Get classes that have a method with a parameter that is annotated with an annotation of the named type.
|
ClassInfoList |
getClassesWithMethodParameterAnnotation(String methodParameterAnnotationName)
Get classes that have a method with a parameter that is annotated with an annotation of the named type.
|
ClassInfo |
getClassInfo(String className)
Get the
ClassInfo object for the named class, or null if no class of the requested name was found in
an accepted/non-rejected package during the scan. |
String |
getClasspath()
Returns all unique directories or zip/jarfiles on the classpath, in classloader resolution order, as a
classpath string, delineated with the standard path separator character.
|
List<File> |
getClasspathFiles()
Returns the list of File objects for unique classpath elements (directories or jarfiles), in classloader
resolution order.
|
List<URI> |
getClasspathURIs()
Returns an ordered list of unique classpath element and module URIs.
|
List<URL> |
getClasspathURLs()
Returns an ordered list of unique classpath element and module URLs.
|
ClassInfoList |
getInterfaces(Class<?> classRef)
Get all interfaces implemented by the class or by one of its superclasses, if the given class is a standard
class, or the superinterfaces extended by this interface, if it is an interface.
|
ClassInfoList |
getInterfaces(String className)
Get all interfaces implemented by the named class or by one of its superclasses, if the named class is a
standard class, or the superinterfaces extended by this interface, if it is an interface.
|
ModuleInfoList |
getModuleInfo()
Get all modules found during the scan.
|
ModuleInfo |
getModuleInfo(String moduleName)
Get the
ModuleInfo object for the named module, or null if no module of the requested name was found
during the scan. |
ModulePathInfo |
getModulePathInfo()
Get the module path info provided on the commandline with
--module-path , --add-modules ,
--patch-module , --add-exports , --add-opens , and --add-reads , and also the
Add-Exports and Add-Opens entries from jarfile manifest files encountered during scanning. |
List<ModuleRef> |
getModules()
Get
ModuleRef references for all visible modules. |
PackageInfoList |
getPackageInfo()
Get all packages found during the scan.
|
PackageInfo |
getPackageInfo(String packageName)
Get the
PackageInfo object for the named package, or null if no package of the requested name was
found during the scan. |
ResourceList |
getResourcesMatchingPattern(Pattern pattern)
Get the list of all resources found in accepted packages that have a path matching the requested regex
pattern.
|
ResourceList |
getResourcesMatchingWildcard(String wildcardString)
Get the list of all resources found in accepted packages that have a path matching the requested wildcard
string.
|
ResourceList |
getResourcesWithExtension(String extension)
Get the list of all resources found in accepted packages that have the requested filename extension.
|
ResourceList |
getResourcesWithLeafName(String leafName)
Get the list of all resources found in accepted packages that have the requested leafname.
|
ResourceList |
getResourcesWithPath(String resourcePath)
Get the list of all resources found in accepted packages that have the given path, relative to the package
root of the classpath element.
|
ResourceList |
getResourcesWithPathIgnoringAccept(String resourcePath)
Get the list of all resources found in any classpath element, whether in accepted packages or not (as long
as the resource is not rejected), that have the given path, relative to the package root of the classpath
element.
|
ResourceList |
getResourcesWithPathIgnoringWhitelist(String resourcePath)
Deprecated.
Use
getResourcesWithPathIgnoringAccept(String) instead. |
Map<ClassInfo,ClassInfoList> |
getReverseClassDependencyMap()
Get the reverse class dependency map, i.e.
|
ClassInfoList |
getSubclasses(Class<?> superclass)
Get all subclasses of the superclass.
|
ClassInfoList |
getSubclasses(String superclassName)
Get all subclasses of the named superclass.
|
ClassInfoList |
getSuperclasses(Class<?> subclass)
Get superclasses of the subclass.
|
ClassInfoList |
getSuperclasses(String subclassName)
Get superclasses of the named subclass.
|
boolean |
isObtainedFromDeserialization()
Checks if this
ScanResult was obtained from JSON by deserialization, by calling
fromJSON(String) . |
Class<?> |
loadClass(String className,
boolean returnNullIfClassNotFound)
Load a class given a class name.
|
<T> Class<T> |
loadClass(String className,
Class<T> superclassOrInterfaceType,
boolean returnNullIfClassNotFound)
Load a class given a class name.
|
String |
toJSON()
Serialize a ScanResult to minified (un-indented) JSON.
|
String |
toJSON(int indentWidth)
Serialize a ScanResult to JSON.
|
public List<File> getClasspathFiles()
public String getClasspath()
public List<URI> getClasspathURIs()
public List<URL> getClasspathURLs()
URL
does not support the jrt:
URI
scheme.public List<ModuleRef> getModules()
ModuleRef
references for all visible modules.ModuleRef
references for all visible modules.public ModulePathInfo getModulePathInfo()
--module-path
, --add-modules
,
--patch-module
, --add-exports
, --add-opens
, and --add-reads
, and also the
Add-Exports
and Add-Opens
entries from jarfile manifest files encountered during scanning.
Note that the returned ModulePathInfo
object does not include classpath entries from the traditional
classpath or system modules. Use getModules()
to get all visible modules, including anonymous,
automatic and system modules.
ModulePathInfo
.public ResourceList getAllResources()
public Map<String,ResourceList> getAllResourcesAsMap()
Resource
for all resources (including classfiles and non-classfiles)
found in accepted packages.Resource
for all resources (including classfiles and
non-classfiles) found in accepted packages.public ResourceList getResourcesWithPath(String resourcePath)
resourcePath
- A complete resource path, relative to the classpath entry package root.public ResourceList getResourcesWithPathIgnoringAccept(String resourcePath)
resourcePath
- A complete resource path, relative to the classpath entry package root.@Deprecated public ResourceList getResourcesWithPathIgnoringWhitelist(String resourcePath)
getResourcesWithPathIgnoringAccept(String)
instead.getResourcesWithPathIgnoringAccept(String)
instead.resourcePath
- A complete resource path, relative to the classpath entry package root.public ResourceList getResourcesWithLeafName(String leafName)
leafName
- A resource leaf filename.public ResourceList getResourcesWithExtension(String extension)
extension
- A filename extension, e.g. "xml" to match all resources ending in ".xml".public ResourceList getResourcesMatchingPattern(Pattern pattern)
getResourcesMatchingWildcard(String)
.pattern
- A pattern to match Resource
paths with.public ResourceList getResourcesMatchingWildcard(String wildcardString)
The wildcard string may contain:
The wildcard string is translated in a simplistic way into a regex. If you need more complex pattern
matching, use a regex directly, via getResourcesMatchingPattern(Pattern)
.
wildcardString
- A wildcard (glob) pattern to match Resource
paths with.public ModuleInfo getModuleInfo(String moduleName)
ModuleInfo
object for the named module, or null if no module of the requested name was found
during the scan.moduleName
- The module name.ModuleInfo
object for the named module, or null if the module was not found.public ModuleInfoList getModuleInfo()
public PackageInfo getPackageInfo(String packageName)
PackageInfo
object for the named package, or null if no package of the requested name was
found during the scan.packageName
- The package name.PackageInfo
object for the named package, or null if the package was not found.public PackageInfoList getPackageInfo()
public Map<ClassInfo,ClassInfoList> getClassDependencyMap()
ClassInfo
object for each accepted class to a list of the classes referenced by
that class (i.e. returns a map from dependents to dependencies). Note that you need to call
ClassGraph.enableInterClassDependencies()
before ClassGraph.scan()
for this method to work.
You should also call ClassGraph.enableExternalClasses()
before ClassGraph.scan()
if you want
non-accepted classes to appear in the result. See also getReverseClassDependencyMap()
, which inverts
the map.ClassInfo
object for each accepted class to a list of the classes referenced by
that class (i.e. returns a map from dependents to dependencies). Each map value is the result of
calling ClassInfo.getClassDependencies()
on the corresponding key.public Map<ClassInfo,ClassInfoList> getReverseClassDependencyMap()
ClassInfo
object for each dependency class
(accepted or not) to a list of the accepted classes that referenced that class as a dependency (i.e. returns
a map from dependencies to dependents). Note that you need to call
ClassGraph.enableInterClassDependencies()
before ClassGraph.scan()
for this method to work.
You should also call ClassGraph.enableExternalClasses()
before ClassGraph.scan()
if you want
non-accepted classes to appear in the result. See also getClassDependencyMap()
.ClassInfo
object for each dependency class (accepted or not) to a list of the
accepted classes that referenced that class as a dependency (i.e. returns a map from dependencies to
dependents).public ClassInfo getClassInfo(String className)
ClassInfo
object for the named class, or null if no class of the requested name was found in
an accepted/non-rejected package during the scan.className
- The class name.ClassInfo
object for the named class, or null if the class was not found.public ClassInfoList getAllClasses()
public ClassInfoList getAllEnums()
Enum
classes found during the scan.Enum
classes found during the scan, or the empty list if none.public ClassInfoList getAllRecords()
record
classes found during the scan (JDK 14+).record
classes found during the scan, or the empty list if none.public Map<String,ClassInfo> getAllClassesAsMap()
ClassInfo
object for all classes, interfaces and annotations found
during the scan.ClassInfo
object for all classes, interfaces and annotations found
during the scan.public ClassInfoList getAllStandardClasses()
public ClassInfoList getSubclasses(Class<?> superclass)
superclass
- The superclass.public ClassInfoList getSubclasses(String superclassName)
superclassName
- The name of the superclass.public ClassInfoList getSuperclasses(String subclassName)
subclassName
- The name of the subclass.public ClassInfoList getSuperclasses(Class<?> subclass)
subclass
- The subclass.public ClassInfoList getClassesWithMethodAnnotation(Class<? extends Annotation> methodAnnotation)
methodAnnotation
- the method annotation.public ClassInfoList getClassesWithMethodAnnotation(String methodAnnotationName)
methodAnnotationName
- the name of the method annotation.public ClassInfoList getClassesWithMethodParameterAnnotation(Class<? extends Annotation> methodParameterAnnotation)
methodParameterAnnotation
- the method parameter annotation.public ClassInfoList getClassesWithMethodParameterAnnotation(String methodParameterAnnotationName)
methodParameterAnnotationName
- the name of the method parameter annotation.public ClassInfoList getClassesWithFieldAnnotation(Class<? extends Annotation> fieldAnnotation)
fieldAnnotation
- the field annotation.public ClassInfoList getClassesWithFieldAnnotation(String fieldAnnotationName)
fieldAnnotationName
- the name of the field annotation.public ClassInfoList getAllInterfaces()
getAllInterfacesAndAnnotations()
.public ClassInfoList getInterfaces(String className)
className
- The class name.public ClassInfoList getInterfaces(Class<?> classRef)
classRef
- The class.public ClassInfoList getClassesImplementing(Class<?> interfaceClass)
interfaceClass
- The interface class.public ClassInfoList getClassesImplementing(String interfaceName)
interfaceName
- The interface name.public ClassInfoList getAllAnnotations()
getAllInterfacesAndAnnotations()
.public ClassInfoList getAllInterfacesAndAnnotations()
public ClassInfoList getClassesWithAnnotation(Class<? extends Annotation> annotation)
annotation
- The class annotation or meta-annotation.public ClassInfoList getClassesWithAnnotation(String annotationName)
annotationName
- The name of the class annotation or meta-annotation.public ClassInfoList getAnnotationsOnClass(String className)
getClassInfo(String)
to get the ClassInfo
object for the named class, then if the
ClassInfo
object is non-null, call ClassInfo.getAnnotationInfo()
to get detailed annotation
info.className
- The name of the class.public boolean classpathContentsModifiedSinceScan()
public long classpathContentsLastModifiedTime()
This method cannot in general tell if classpath has changed (or modules have been added or removed) if it is run twice during the same runtime session.
public Class<?> loadClass(String className, boolean returnNullIfClassNotFound) throws IllegalArgumentException
Enable verbose scanning to see details of any exceptions thrown during classloading, even if ignoreExceptions is false.
className
- the class to load.returnNullIfClassNotFound
- If true, null is returned if there was an exception during classloading, otherwise
IllegalArgumentException is thrown if a class could not be loaded.IllegalArgumentException
- if ignoreExceptions is false, IllegalArgumentException is thrown if there were problems loading
or initializing the class. (Note that class initialization on load is disabled by default, you
can enable it with ClassGraph#initializeLoadedClasses(true)
.) Otherwise exceptions are
suppressed, and null is returned if any of these problems occurs.public <T> Class<T> loadClass(String className, Class<T> superclassOrInterfaceType, boolean returnNullIfClassNotFound) throws IllegalArgumentException
Enable verbose scanning to see details of any exceptions thrown during classloading, even if ignoreExceptions is false.
T
- the superclass or interface type.className
- the class to load.superclassOrInterfaceType
- The class type to cast the result to.returnNullIfClassNotFound
- If true, null is returned if there was an exception during classloading, otherwise
IllegalArgumentException is thrown if a class could not be loaded.IllegalArgumentException
- if ignoreExceptions is false, IllegalArgumentException is thrown if there were problems loading
the class, initializing the class, or casting it to the requested type. (Note that class
initialization on load is disabled by default, you can enable it with
ClassGraph#initializeLoadedClasses(true)
.) Otherwise exceptions are suppressed, and null
is returned if any of these problems occurs.public static ScanResult fromJSON(String json)
json
- The JSON string for the serialized ScanResult
.ScanResult
.public String toJSON(int indentWidth)
indentWidth
- If greater than 0, JSON will be formatted (indented), otherwise it will be minified (un-indented).ScanResult
, serialized as a JSON string.public String toJSON()
ScanResult
, serialized as a JSON string.public boolean isObtainedFromDeserialization()
ScanResult
was obtained from JSON by deserialization, by calling
fromJSON(String)
.ScanResult
was obtained from JSON by deserialization.public void close()
ScanResult
.close
in interface Closeable
close
in interface AutoCloseable
public static void closeAll()
ScanResult
instances that have not yet been closed. Note that this will close all open
ScanResult
instances for any class that uses the classloader that the ScanResult
class is
cached in -- so if you call this method, you need to ensure that the lifecycle of the classloader matches the
lifecycle of your application, or that two concurrent applications don't share the same classloader,
otherwise one application might close another application's ScanResult
instances while they are still
in use.Copyright © 2021. All rights reserved.