Class ClasspathElement

java.lang.Object
io.github.classgraph.ClasspathElement
All Implemented Interfaces:
Comparable<ClasspathElement>
Direct Known Subclasses:
ClasspathElementDir, ClasspathElementModule, ClasspathElementZip

abstract class ClasspathElement extends Object implements Comparable<ClasspathElement>
A classpath element (a directory or jarfile on the classpath).
  • Field Details

    • classpathElementIdx

      int classpathElementIdx
      The index of the classpath element within the classpath or module path.
    • nestedClasspathRootPrefixes

      List<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

      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 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 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 Map<File,Long> fileToLastModified
      The map from File to last modified timestamp, if scanFiles is true.
    • scanned

      protected final AtomicBoolean scanned
      Flag to ensure classpath element is only scanned once.
    • classLoader

      protected ClassLoader classLoader
      The classloader that this classpath element was obtained from.
    • packageRootPrefix

      protected String packageRootPrefix
      The package root within the jarfile or Path.
    • moduleNameFromModuleDescriptor

      String moduleNameFromModuleDescriptor
      The name of the module from the module-info.class module descriptor, if one is present in the root of the classpath element.
    • scanSpec

      final ScanSpec scanSpec
      The scan spec.
  • Constructor Details

  • Method Details

    • compareTo

      public int compareTo(ClasspathElement other)
      Sort in increasing order of classpathElementIdxWithinParent.
      Specified by:
      compareTo in interface Comparable<ClasspathElement>
    • getClassLoader

      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(String relativePath, LogNode log)
      Check relativePath against classpathElementResourcePathAcceptReject.
      Parameters:
      relativePath - the relative path
      log - the log
      Returns:
      true if path should be scanned
    • maskClassfiles

      void maskClassfiles(int classpathIdx, Set<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 index
      classpathRelativePathsFound - the classpath relative paths found
      log - 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 resource
      parentMatchStatus - the parent match status
      isClassfileOnly - if true, only add the resource to the list of classfile resources, not to the list of non-classfile resources
      log - the log
    • finishScanPaths

      protected void finishScanPaths(LogNode log)
      Called by scanPaths() after scan completion.
      Parameters:
      log - the log
    • log

      protected LogNode log(int classpathElementIdx, String msg, LogNode log)
      Write entries to log in classpath / module path order.
      Parameters:
      classpathElementIdx - the classpath element idx
      msg - the log message
      log - the log
      Returns:
      the new LogNode
    • log

      protected LogNode log(int classpathElementIdx, String msg, Throwable t, LogNode log)
      Write entries to log in classpath / module path order.
      Parameters:
      classpathElementIdx - the classpath element idx
      msg - the log message
      t - The exception that was thrown
      log - the log
      Returns:
      the new LogNode
    • open

      abstract void open(WorkQueue<Scanner.ClasspathEntryWorkUnit> workQueue, LogNode log) throws InterruptedException
      Determine if this classpath element is valid. If it is not valid, sets skipClasspathElement. For ClasspathElementZip, 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 queue
      log - the log
      Throws:
      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(String relativePath)
      Get the Resource for a given relative path.
      Parameters:
      relativePath - The relative path of the Resource to return. Path should have already be sanitized by calling
      invalid reference
      FileUtils#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 Resource for the given relative path, or null if relativePath does not exist in this classpath element.
    • getURI

      abstract URI getURI()
      Get the URI for this classpath element.
      Returns:
      the URI for the classpath element.
    • getAllURIs

      abstract List<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 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 String getModuleName()
      Get the name of this classpath element's module, or null if there is no module name.
      Returns:
      the module name