Package io.github.classgraph
Class Scanner
- java.lang.Object
-
- io.github.classgraph.Scanner
-
- All Implemented Interfaces:
java.util.concurrent.Callable<ScanResult>
class Scanner extends java.lang.Object implements java.util.concurrent.Callable<ScanResult>
The classpath scanner.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classScanner.ClassfileScannerWorkUnitProcessorWorkUnitProcessor for scanning classfiles.(package private) static classScanner.ClassfileScanWorkUnitUsed to enqueue classfiles for scanning.(package private) static classScanner.ClasspathEntryWorkUnitUsed to enqueue classpath elements for opening.
-
Field Summary
Fields Modifier and Type Field Description private SingletonMap<java.lang.Object,ClasspathElement,java.io.IOException>classpathEntryObjToClasspathEntrySingletonMapA singleton map used to eliminate creation of duplicateClasspathElementobjects, to reduce the chance that resources are scanned twice, by mapping canonicalized Path objects, URLs, etc.private ClasspathFinderclasspathFinderThe classpath finder.private java.util.concurrent.ExecutorServiceexecutorServiceThe executor service.private ClassGraph.FailureHandlerfailureHandlerThe failure handler.private InterruptionCheckerinterruptionCheckerThe interruption checker.private java.util.List<ClasspathElementModule>moduleOrderThe module order.private NestedJarHandlernestedJarHandlerThe nested jar handler.private intnumParallelTasksThe number of parallel tasks.booleanperformScanIf true, performing a scan.private ClassGraph.ScanResultProcessorscanResultProcessorThe scan result processor.private ScanSpecscanSpecThe scan spec.private LogNodetopLevelLogThe toplevel log.
-
Constructor Summary
Constructors Constructor Description Scanner(boolean performScan, ScanSpec scanSpec, java.util.concurrent.ExecutorService executorService, int numParallelTasks, ClassGraph.ScanResultProcessor scanResultProcessor, ClassGraph.FailureHandler failureHandler, ReflectionUtils reflectionUtils, LogNode topLevelLog)The classpath scanner.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ScanResultcall()Determine the unique ordered classpath elements, and run a scan looking for file or classfile matches if necessary.private java.util.List<ClasspathElement>findClasspathOrder(java.util.Set<ClasspathElement> toplevelClasspathElts)Recursively perform a depth-first traversal of child classpath elements, breaking cycles if necessary, to determine the final classpath element order.private static voidfindClasspathOrderRec(ClasspathElement currClasspathElement, java.util.Set<ClasspathElement> visitedClasspathElts, java.util.List<ClasspathElement> order)Recursively perform a depth-first search of jar interdependencies, breaking cycles if necessary, to determine the final classpath element order.private voidfindNestedClasspathElements(java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,ClasspathElement>> classpathElts, LogNode log)Find classpath elements whose path is a prefix of another classpath element, and record the nesting.private voidmaskClassfiles(java.util.List<ClasspathElement> classpathElementOrder, LogNode maskLog)Perform classpath masking of classfiles.private WorkQueue.WorkUnitProcessor<Scanner.ClasspathEntryWorkUnit>newClasspathEntryWorkUnitProcessor(java.util.Set<ClasspathElement> allClasspathEltsOut, java.util.Set<ClasspathElement> toplevelClasspathEltsOut)Create a WorkUnitProcessor for opening traditional classpath entries (which are mapped toClasspathElementDirorClasspathElementZip--is handled separately).private static java.lang.ObjectnormalizeClasspathEntry(java.lang.Object classpathEntryObj)Normalize a classpath entry object so that it is mapped to a canonicalPathobject if possible, falling back to aURLorURIif not possible.private ScanResultopenClasspathElementsThenScan()Open each of the classpath elements, looking for additional child classpath elements that need scanning (e.g.private ScanResultperformScan(java.util.List<ClasspathElement> finalClasspathEltOrder, java.util.List<java.lang.String> finalClasspathEltOrderStrs, ClasspathFinder classpathFinder)Scan the classpath and/or visible modules.private voidpreprocessClasspathElementsByType(java.util.List<ClasspathElement> finalTraditionalClasspathEltOrder, LogNode classpathFinderLog)Find classpath elements whose path is a prefix of another classpath element, and record the nesting.private <W> voidprocessWorkUnits(java.util.Collection<W> workUnits, LogNode log, WorkQueue.WorkUnitProcessor<W> workUnitProcessor)Process work units.
-
-
-
Field Detail
-
scanSpec
private final ScanSpec scanSpec
The scan spec.
-
performScan
public boolean performScan
If true, performing a scan. If false, only fetching the classpath.
-
nestedJarHandler
private final NestedJarHandler nestedJarHandler
The nested jar handler.
-
executorService
private final java.util.concurrent.ExecutorService executorService
The executor service.
-
interruptionChecker
private final InterruptionChecker interruptionChecker
The interruption checker.
-
numParallelTasks
private final int numParallelTasks
The number of parallel tasks.
-
scanResultProcessor
private final ClassGraph.ScanResultProcessor scanResultProcessor
The scan result processor.
-
failureHandler
private final ClassGraph.FailureHandler failureHandler
The failure handler.
-
topLevelLog
private final LogNode topLevelLog
The toplevel log.
-
classpathFinder
private final ClasspathFinder classpathFinder
The classpath finder.
-
moduleOrder
private final java.util.List<ClasspathElementModule> moduleOrder
The module order.
-
classpathEntryObjToClasspathEntrySingletonMap
private final SingletonMap<java.lang.Object,ClasspathElement,java.io.IOException> classpathEntryObjToClasspathEntrySingletonMap
A singleton map used to eliminate creation of duplicateClasspathElementobjects, to reduce the chance that resources are scanned twice, by mapping canonicalized Path objects, URLs, etc. to ClasspathElements.
-
-
Constructor Detail
-
Scanner
Scanner(boolean performScan, ScanSpec scanSpec, java.util.concurrent.ExecutorService executorService, int numParallelTasks, ClassGraph.ScanResultProcessor scanResultProcessor, ClassGraph.FailureHandler failureHandler, ReflectionUtils reflectionUtils, LogNode topLevelLog) throws java.lang.InterruptedExceptionThe classpath scanner. Scanning is started by callingcall()on this object.- Parameters:
performScan- If true, performing a scan. If false, only fetching the classpath.scanSpec- the scan specexecutorService- the executor servicenumParallelTasks- the num parallel tasksscanResultProcessor- the scan result processorfailureHandler- the failure handlertopLevelLog- the log- Throws:
java.lang.InterruptedException- if interrupted
-
-
Method Detail
-
findClasspathOrderRec
private static void findClasspathOrderRec(ClasspathElement currClasspathElement, java.util.Set<ClasspathElement> visitedClasspathElts, java.util.List<ClasspathElement> order)
Recursively perform a depth-first search of jar interdependencies, breaking cycles if necessary, to determine the final classpath element order.- Parameters:
currClasspathElement- the current classpath elementvisitedClasspathElts- visited classpath eltsorder- the classpath element order
-
findClasspathOrder
private java.util.List<ClasspathElement> findClasspathOrder(java.util.Set<ClasspathElement> toplevelClasspathElts)
Recursively perform a depth-first traversal of child classpath elements, breaking cycles if necessary, to determine the final classpath element order. This causes child classpath elements to be inserted in-place in the classpath order, after the parent classpath element that contained them.- Parameters:
toplevelClasspathElts- the toplevel classpath elts, indexed by order within the toplevel classpath- Returns:
- the final classpath order, after depth-first traversal of child classpath elements
-
processWorkUnits
private <W> void processWorkUnits(java.util.Collection<W> workUnits, LogNode log, WorkQueue.WorkUnitProcessor<W> workUnitProcessor) throws java.lang.InterruptedException, java.util.concurrent.ExecutionExceptionProcess work units.- Type Parameters:
W- the work unit type- Parameters:
workUnits- the work unitslog- the log entry text to group work units underworkUnitProcessor- the work unit processor- Throws:
java.lang.InterruptedException- if a worker was interrupted.java.util.concurrent.ExecutionException- If a worker threw an uncaught exception.
-
normalizeClasspathEntry
private static java.lang.Object normalizeClasspathEntry(java.lang.Object classpathEntryObj) throws java.io.IOExceptionNormalize a classpath entry object so that it is mapped to a canonicalPathobject if possible, falling back to aURLorURIif not possible. This is needed to avoid treating "file:///path/to/x.jar" and "/path/to/x.jar" as different classpath elements. Maps URL("jar:file:x.jar!/") to Path("x.jar"), etc.- Parameters:
classpathEntryObj- The classpath entry object.- Returns:
- The normalized classpath entry object.
- Throws:
java.io.IOException
-
newClasspathEntryWorkUnitProcessor
private WorkQueue.WorkUnitProcessor<Scanner.ClasspathEntryWorkUnit> newClasspathEntryWorkUnitProcessor(java.util.Set<ClasspathElement> allClasspathEltsOut, java.util.Set<ClasspathElement> toplevelClasspathEltsOut)
Create a WorkUnitProcessor for opening traditional classpath entries (which are mapped toClasspathElementDirorClasspathElementZip--is handled separately).- Parameters:
allClasspathEltsOut- on exit, the set of all classpath elementstoplevelClasspathEltsOut- on exit, the toplevel classpath elements- Returns:
- the work unit processor
-
findNestedClasspathElements
private void findNestedClasspathElements(java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,ClasspathElement>> classpathElts, LogNode log)
Find classpath elements whose path is a prefix of another classpath element, and record the nesting.- Parameters:
classpathElts- the classpath elementslog- the log
-
preprocessClasspathElementsByType
private void preprocessClasspathElementsByType(java.util.List<ClasspathElement> finalTraditionalClasspathEltOrder, LogNode classpathFinderLog)
Find classpath elements whose path is a prefix of another classpath element, and record the nesting.- Parameters:
finalTraditionalClasspathEltOrder- the final traditional classpath elt orderclasspathFinderLog- the classpath finder log
-
maskClassfiles
private void maskClassfiles(java.util.List<ClasspathElement> classpathElementOrder, LogNode maskLog)
Perform classpath masking of classfiles. If the same relative classfile path occurs multiple times in the classpath, causes the second and subsequent occurrences to be ignored (removed).- Parameters:
classpathElementOrder- the classpath element ordermaskLog- the mask log
-
performScan
private ScanResult performScan(java.util.List<ClasspathElement> finalClasspathEltOrder, java.util.List<java.lang.String> finalClasspathEltOrderStrs, ClasspathFinder classpathFinder) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
Scan the classpath and/or visible modules.- Parameters:
finalClasspathEltOrder- the final classpath elt orderfinalClasspathEltOrderStrs- the final classpath elt order strsclasspathFinder- theClasspathFinder- Returns:
- the scan result
- Throws:
java.lang.InterruptedException- if the scan was interruptedjava.util.concurrent.ExecutionException- if the scan threw an uncaught exception
-
openClasspathElementsThenScan
private ScanResult openClasspathElementsThenScan() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
Open each of the classpath elements, looking for additional child classpath elements that need scanning (e.g.Class-Pathentries in jar manifest files), then perform the scan ifScanSpec#performScanis true, or just get the classpath ifScanSpec#performScanis false.- Returns:
- the scan result
- Throws:
java.lang.InterruptedException- if the scan was interruptedjava.util.concurrent.ExecutionException- if a worker threw an uncaught exception
-
call
public ScanResult call() throws java.lang.InterruptedException, java.util.concurrent.CancellationException, java.util.concurrent.ExecutionException
Determine the unique ordered classpath elements, and run a scan looking for file or classfile matches if necessary.- Specified by:
callin interfacejava.util.concurrent.Callable<ScanResult>- Returns:
- the scan result
- Throws:
java.lang.InterruptedException- if scanning was interruptedjava.util.concurrent.CancellationException- if scanning was cancelledjava.util.concurrent.ExecutionException- if a worker threw an uncaught exception
-
-