Class Scanner

  • All Implemented Interfaces:
    java.util.concurrent.Callable<ScanResult>

    class Scanner
    extends java.lang.Object
    implements java.util.concurrent.Callable<ScanResult>
    The classpath scanner.
    • 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.
      • topLevelLog

        private final LogNode topLevelLog
        The toplevel log.
      • classpathFinder

        private final ClasspathFinder classpathFinder
        The classpath finder.
      • classpathEntryObjToClasspathEntrySingletonMap

        private final SingletonMap<java.lang.Object,​ClasspathElement,​java.io.IOException> classpathEntryObjToClasspathEntrySingletonMap
        A singleton map used to eliminate creation of duplicate ClasspathElement objects, 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.InterruptedException
        The classpath scanner. Scanning is started by calling call() on this object.
        Parameters:
        performScan - If true, performing a scan. If false, only fetching the classpath.
        scanSpec - the scan spec
        executorService - the executor service
        numParallelTasks - the num parallel tasks
        scanResultProcessor - the scan result processor
        failureHandler - the failure handler
        topLevelLog - 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 element
        visitedClasspathElts - visited classpath elts
        order - 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.ExecutionException
        Process work units.
        Type Parameters:
        W - the work unit type
        Parameters:
        workUnits - the work units
        log - the log entry text to group work units under
        workUnitProcessor - 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.IOException
        Normalize a classpath entry object so that it is mapped to a canonical Path object if possible, falling back to a URL or URI if 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
      • 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 elements
        log - 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 order
        classpathFinderLog - 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 order
        maskLog - 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 order
        finalClasspathEltOrderStrs - the final classpath elt order strs
        classpathFinder - the ClasspathFinder
        Returns:
        the scan result
        Throws:
        java.lang.InterruptedException - if the scan was interrupted
        java.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-Path entries in jar manifest files), then perform the scan if ScanSpec#performScan is true, or just get the classpath if ScanSpec#performScan is false.
        Returns:
        the scan result
        Throws:
        java.lang.InterruptedException - if the scan was interrupted
        java.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:
        call in interface java.util.concurrent.Callable<ScanResult>
        Returns:
        the scan result
        Throws:
        java.lang.InterruptedException - if scanning was interrupted
        java.util.concurrent.CancellationException - if scanning was cancelled
        java.util.concurrent.ExecutionException - if a worker threw an uncaught exception