Class AuxClasspathLoader

java.lang.Object
net.sourceforge.pmd.util.AuxClasspathLoader
All Implemented Interfaces:
AutoCloseable

@Experimental public class AuxClasspathLoader extends Object implements AutoCloseable
This class allows to load resources from a given classpath. Unlike a real classloader like URLClassLoader, the Jar files on the classpath are not opened with JarFile and their signature is not verified which saves memory. The Jar files are opened directly with ZipFile.

This classpath loader also supports loading platform classes (e.g. java.lang) from the jrt-fs filesystem. All zip files and the jrt-fs are kept open, until this classpath loader is closed.

To create a new instance, use create(String). Because verifying the classpath and opening the platform classloader via the jrt-fs filesystem is expensive, it supports a simple caching mechanism. See enableReuse(int) and disableReuse().

To load resources, use findResource(String).

This class is usually not used directly for calling PMD but rather by language implementations such as Java. The auxClasspath should be configured via PMDConfiguration.setAuxClasspath(String)

Experimental Status:
Replacement for ClasspathClassLoader.
Since:
7.27.0
  • Method Details

    • create

      public static AuxClasspathLoader create(String rawAuxClasspath)
    • enableReuse

      @Experimental public static void enableReuse(int count)
      Enables caching of AuxClasspathLoader instances. This is useful for unit tests or IDE plugins, when PMD is executed multiple times within one JVM instance.
      Experimental Status:
      Parameters:
      count - Maximum number of instances to be cached. The oldest instances are evicted first.
      Since:
      7.27.0
      See Also:
    • disableReuse

      @Experimental public static void disableReuse()
      If enableReuse(int) is used, then this method can be used to explicitly close the cached auxiliary classpath classloaders.

      Call this only, when it is known, that PMD is not currently executing (e.g. PmdAnalysis is finished).

      Experimental Status:
      Since:
      7.27.0
    • findResource

      public @Nullable InputStream findResource(String name)
      Finds the first resource with the given name (e.g. package/A.class) in the jars of the auxClasspath. If there is no match in the jar files, then the Java Runtime Image is searched via the given "jrt-fs.jar". This allows to find e.g. java/lang/Object.class. If no resource is found, null is returned.

      The name is expected to be a valid, relative path name within a jar file without a leading slash, e.g. package/A.class.

      In order to load module-info.class files, which are all at the root of the jar files, these can be referenced by prefixing the module name, e.g. full.module.name/module-info.class or java.base/module-info.class.

      Note: Multi-Release Jars are not handled in a special way. This method does not automatically search for versioned resources.

      Parameters:
      name - Name of the resource to load, e.g. package/A.class
      Returns:
      an open InputStream or null if the resource is not found.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException