Package io.github.classgraph
Class ClasspathElement
- java.lang.Object
-
- io.github.classgraph.ClasspathElement
-
- All Implemented Interfaces:
java.lang.Comparable<ClasspathElement>
- Direct Known Subclasses:
ClasspathElementDir,ClasspathElementModule,ClasspathElementZip
abstract class ClasspathElement extends java.lang.Object implements java.lang.Comparable<ClasspathElement>
A classpath element (a directory or jarfile on the classpath).
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<Resource>acceptedClassfileResourcesThe list of all classfiles found within this classpath element that were accepted and not rejected.protected java.util.List<Resource>acceptedResourcesResources found within this classpath element that were accepted and not rejected.(package private) java.util.Collection<ClasspathElement>childClasspathElementsThe child classpath elements, keyed by the order of the child classpath element within the Class-Path entry of the manifest file the child classpath element was listed in (or the position of the file within the sorted entries of a lib directory).protected java.lang.ClassLoaderclassLoaderThe classloader that this classpath element was obtained from.(package private) intclasspathElementIdxThe index of the classpath element within the classpath or module path.(package private) intclasspathElementIdxWithinParentThe index of the classpath element within the parent classpath element (e.g.(package private) booleancontainsSpecificallyAcceptedClasspathElementResourcePathTrue if classpath element contains a specifically-accepted resource path.protected java.util.Map<java.io.File,java.lang.Long>fileToLastModifiedThe map from File to last modified timestamp, if scanFiles is true.(package private) java.lang.StringmoduleNameFromModuleDescriptorThe name of the module from themodule-info.classmodule descriptor, if one is present in the root of the classpath element.(package private) java.util.List<java.lang.String>nestedClasspathRootPrefixesIf non-null, contains a list of resolved paths for any classpath element roots nested inside this classpath element.protected java.lang.StringpackageRootPrefixThe package root within the jarfile or Path.protected java.util.concurrent.atomic.AtomicBooleanscannedFlag to ensure classpath element is only scanned once.protected ScanResultscanResultThe ScanResult that the classpath element came from.(package private) ScanSpecscanSpecThe scan spec.(package private) booleanskipClasspathElementTrue if there was an exception when trying to open this classpath element (e.g.
-
Constructor Summary
Constructors Constructor Description ClasspathElement(Scanner.ClasspathEntryWorkUnit workUnit, ScanSpec scanSpec)A classpath element.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddAcceptedResource(Resource resource, ScanSpec.ScanSpecPathMatch parentMatchStatus, boolean isClassfileOnly, LogNode log)Add a resource discovered during the scan.protected booleancheckResourcePathAcceptReject(java.lang.String relativePath, LogNode log)Check relativePath against classpathElementResourcePathAcceptReject.intcompareTo(ClasspathElement other)Sort in increasing order of classpathElementIdxWithinParent.protected voidfinishScanPaths(LogNode log)Called by scanPaths() after scan completion.(package private) abstract java.util.List<java.net.URI>getAllURIs()Get the URI for this classpath element, and the URIs for any automatic nested package prefixes (e.g.(package private) java.lang.ClassLoadergetClassLoader()Get the ClassLoader the classpath element was obtained from.(package private) abstract java.io.FilegetFile()Get the file for this classpath element, or null if this is a module with a "jrt:" URI.(package private) abstract java.lang.StringgetModuleName()Get the name of this classpath element's module, or null if there is no module name.(package private) intgetNumClassfileMatches()Get the number of classfile matches.(package private) abstract ResourcegetResource(java.lang.String relativePath)Get theResourcefor a given relative path.(package private) abstract java.net.URIgetURI()Get the URI for this classpath element.protected LogNodelog(int classpathElementIdx, java.lang.String msg, java.lang.Throwable t, LogNode log)Write entries to log in classpath / module path order.protected LogNodelog(int classpathElementIdx, java.lang.String msg, LogNode log)Write entries to log in classpath / module path order.(package private) voidmaskClassfiles(int classpathIdx, java.util.Set<java.lang.String> classpathRelativePathsFound, LogNode log)Apply relative path masking within this classpath resource -- remove relative paths that were found in an earlier classpath element.(package private) abstract voidopen(WorkQueue<Scanner.ClasspathEntryWorkUnit> workQueue, LogNode log)Determine if this classpath element is valid.(package private) abstract voidscanPaths(LogNode log)Scan paths in the classpath element for accept/reject criteria, creating Resource objects for accepted and non-rejected resources and classfiles.(package private) voidsetScanResult(ScanResult scanResult)Used to set the ScanResult after the scan is complete.
-
-
-
Field Detail
-
classpathElementIdx
int classpathElementIdx
The index of the classpath element within the classpath or module path.
-
nestedClasspathRootPrefixes
java.util.List<java.lang.String> nestedClasspathRootPrefixes
If non-null, contains a list of resolved paths for any classpath element roots nested inside this classpath element. (Scanning should stop at a nested classpath element root, otherwise that subtree will be scanned more than once.) N.B. contains only the nested part of the resolved path (the common prefix is removed). Also includes a trailing '/', since only nested directory classpath elements need to be caught (nested jars do not need to be caught, because we don't scan jars-within-jars unless the inner jar is explicitly listed on the classpath).
-
skipClasspathElement
boolean skipClasspathElement
True if there was an exception when trying to open this classpath element (e.g. a corrupt ZipFile).
-
containsSpecificallyAcceptedClasspathElementResourcePath
boolean containsSpecificallyAcceptedClasspathElementResourcePath
True if classpath element contains a specifically-accepted resource path.
-
classpathElementIdxWithinParent
final int classpathElementIdxWithinParent
The index of the classpath element within the parent classpath element (e.g. for classpath elements added via a Class-Path entry in the manifest). Set to -1 initially in case the same ClasspathElement is present twice in the classpath, as a child of different parent ClasspathElements.
-
childClasspathElements
java.util.Collection<ClasspathElement> childClasspathElements
The child classpath elements, keyed by the order of the child classpath element within the Class-Path entry of the manifest file the child classpath element was listed in (or the position of the file within the sorted entries of a lib directory).
-
acceptedResources
protected final java.util.List<Resource> acceptedResources
Resources found within this classpath element that were accepted and not rejected. (Only written by one thread, so doesn't need to be a concurrent list.)
-
acceptedClassfileResources
protected java.util.List<Resource> acceptedClassfileResources
The list of all classfiles found within this classpath element that were accepted and not rejected. (Only written by one thread, so doesn't need to be a concurrent list.)
-
fileToLastModified
protected final java.util.Map<java.io.File,java.lang.Long> fileToLastModified
The map from File to last modified timestamp, if scanFiles is true.
-
scanned
protected final java.util.concurrent.atomic.AtomicBoolean scanned
Flag to ensure classpath element is only scanned once.
-
classLoader
protected java.lang.ClassLoader classLoader
The classloader that this classpath element was obtained from.
-
packageRootPrefix
protected java.lang.String packageRootPrefix
The package root within the jarfile or Path.
-
moduleNameFromModuleDescriptor
java.lang.String moduleNameFromModuleDescriptor
The name of the module from themodule-info.classmodule descriptor, if one is present in the root of the classpath element.
-
scanSpec
final ScanSpec scanSpec
The scan spec.
-
scanResult
protected ScanResult scanResult
The ScanResult that the classpath element came from.
-
-
Constructor Detail
-
ClasspathElement
ClasspathElement(Scanner.ClasspathEntryWorkUnit workUnit, ScanSpec scanSpec)
A classpath element.- Parameters:
workUnit- the work unitscanSpec- the scan spec
-
-
Method Detail
-
setScanResult
void setScanResult(ScanResult scanResult)
Used to set the ScanResult after the scan is complete.
-
compareTo
public int compareTo(ClasspathElement other)
Sort in increasing order of classpathElementIdxWithinParent.- Specified by:
compareToin interfacejava.lang.Comparable<ClasspathElement>
-
getClassLoader
java.lang.ClassLoader getClassLoader()
Get the ClassLoader the classpath element was obtained from.- Returns:
- the classloader
-
getNumClassfileMatches
int getNumClassfileMatches()
Get the number of classfile matches.- Returns:
- the num classfile matches
-
checkResourcePathAcceptReject
protected boolean checkResourcePathAcceptReject(java.lang.String relativePath, LogNode log)Check relativePath against classpathElementResourcePathAcceptReject.- Parameters:
relativePath- the relative pathlog- the log- Returns:
- true if path should be scanned
-
maskClassfiles
void maskClassfiles(int classpathIdx, java.util.Set<java.lang.String> classpathRelativePathsFound, LogNode log)Apply relative path masking within this classpath resource -- remove relative paths that were found in an earlier classpath element.- Parameters:
classpathIdx- the classpath indexclasspathRelativePathsFound- the classpath relative paths foundlog- the log
-
addAcceptedResource
protected void addAcceptedResource(Resource resource, ScanSpec.ScanSpecPathMatch parentMatchStatus, boolean isClassfileOnly, LogNode log)
Add a resource discovered during the scan.- Parameters:
resource- the resourceparentMatchStatus- the parent match statusisClassfileOnly- if true, only add the resource to the list of classfile resources, not to the list of non-classfile resourceslog- the log
-
finishScanPaths
protected void finishScanPaths(LogNode log)
Called by scanPaths() after scan completion.- Parameters:
log- the log
-
log
protected LogNode log(int classpathElementIdx, java.lang.String msg, LogNode log)
Write entries to log in classpath / module path order.- Parameters:
classpathElementIdx- the classpath element idxmsg- the log messagelog- the log- Returns:
- the new
LogNode
-
log
protected LogNode log(int classpathElementIdx, java.lang.String msg, java.lang.Throwable t, LogNode log)
Write entries to log in classpath / module path order.- Parameters:
classpathElementIdx- the classpath element idxmsg- the log messaget- The exception that was thrownlog- the log- Returns:
- the new
LogNode
-
open
abstract void open(WorkQueue<Scanner.ClasspathEntryWorkUnit> workQueue, LogNode log) throws java.lang.InterruptedException
Determine if this classpath element is valid. If it is not valid, sets skipClasspathElement. ForClasspathElementZip, may also open or extract inner jars, and also causes jarfile manifests to be read to look for Class-Path entries. If nested jars or Class-Path entries are found, they are added to the work queue. This method is only run once per classpath element, from a single thread.- Parameters:
workQueue- the work queuelog- the log- Throws:
java.lang.InterruptedException- if the thread was interrupted while trying to open the classpath element.
-
scanPaths
abstract void scanPaths(LogNode log)
Scan paths in the classpath element for accept/reject criteria, creating Resource objects for accepted and non-rejected resources and classfiles.- Parameters:
log- the log
-
getResource
abstract Resource getResource(java.lang.String relativePath)
Get theResourcefor a given relative path.- Parameters:
relativePath- The relative path of theResourceto return. Path should have already be sanitized by callingFileUtils#sanitizeEntryPath(String, boolean), or by providing a path that is already sanitized (i.e. doesn't start or end with "/", doesn't contain "/../" or "/./", etc.).- Returns:
- The
Resourcefor the given relative path, or null if relativePath does not exist in this classpath element.
-
getURI
abstract java.net.URI getURI()
Get the URI for this classpath element.- Returns:
- the URI for the classpath element.
-
getAllURIs
abstract java.util.List<java.net.URI> getAllURIs()
Get the URI for this classpath element, and the URIs for any automatic nested package prefixes (e.g. "spring-boot.jar/BOOT-INF/classes") within this jarfile.- Returns:
- the URI for the classpath element.
-
getFile
abstract java.io.File getFile()
Get the file for this classpath element, or null if this is a module with a "jrt:" URI.- Returns:
- the file for the classpath element.
-
getModuleName
abstract java.lang.String getModuleName()
Get the name of this classpath element's module, or null if there is no module name.- Returns:
- the module name
-
-