Class ClassInfoHolder

java.lang.Object
org.machanism.machai.gw.maven.tools.ClassInfoHolder

public class ClassInfoHolder extends Object
Holds class discovery and lookup data for a single Maven project.

This class builds a dedicated URLClassLoader from the project's compile classpath together with its main and test output directories. It then scans visible classes and records the physical location and artifact coordinates for public and protected classes that can be loaded from that classpath.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Map<String,String>
    Maps fully qualified class names to Maven artifact coordinates in the form groupId:artifactId:version when the class comes from a dependency.
    private com.google.common.collect.ImmutableSet<com.google.common.reflect.ClassPath.ClassInfo>
    All classes visible from the constructed project class loader.
    Class loader built from the Maven project classpath and output directories.
    private Map<String,String>
    Maps fully qualified class names to the filesystem path or jar path from which they were resolved.
    private static final org.slf4j.Logger
    Logger used to report skipped or unloadable class-path entries.
    private org.apache.maven.project.MavenProject
    Maven project used as the source of classpath, output directory, and dependency metadata.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClassInfoHolder(org.apache.maven.project.MavenProject project)
    Creates a holder for the supplied Maven project and eagerly initializes class discovery metadata.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Recursively adds compiled-class entries found beneath a directory.
    private void
    addJarEntries(File artifact, List<String> classList)
    Adds all entries from a jar artifact to the supplied collection.
    List<com.google.common.reflect.ClassPath.ClassInfo>
    findClasses(String className)
    Finds classes whose simple names match the supplied regular expression.
    getArtifactId(String className)
    Returns the Maven artifact coordinates associated with the supplied fully qualified class name.
    private List<String>
    getClassEntries(String path, File artifact)
    Reads potential class entries from a directory or jar artifact.
    getClassPath(String className)
    Returns the originating path for the supplied fully qualified class name.
    getSourcePath(String className)
    Returns the source file path for the supplied fully qualified class name.
    private void
    Lazily initializes class discovery state on first access.
    private boolean
    Determines whether an archive or directory entry represents a supported Java class.
    loadClass(String className)
    Loads a class by its fully qualified name using the holder's class loader.
    private void
    loadClassList(org.apache.maven.project.MavenProject project)
    Builds the project class loader and captures all classes visible from it.
    private void
    loadClassLocations(org.apache.maven.project.MavenProject project)
    Scans the project's output directory and resolved dependency artifacts to map classes back to their origin.
    private void
    Loads and records a class entry when it is visible and has an eligible access level.
    void
    Scans a directory of compiled classes or a jar file and records eligible classes that can be loaded by this holder's class loader.
    private void
    scanClassesByPath(Set<org.apache.maven.artifact.Artifact> artifacts)
    Scans each resolved dependency artifact for loadable classes.
    private String
    Converts a compiled-class entry path to a fully qualified class name.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
      Logger used to report skipped or unloadable class-path entries.
    • classes

      private com.google.common.collect.ImmutableSet<com.google.common.reflect.ClassPath.ClassInfo> classes
      All classes visible from the constructed project class loader.
    • classPathMap

      private Map<String,String> classPathMap
      Maps fully qualified class names to the filesystem path or jar path from which they were resolved.
    • classLoader

      private URLClassLoader classLoader
      Class loader built from the Maven project classpath and output directories.
    • artifactMap

      private Map<String,String> artifactMap
      Maps fully qualified class names to Maven artifact coordinates in the form groupId:artifactId:version when the class comes from a dependency.
    • project

      private org.apache.maven.project.MavenProject project
      Maven project used as the source of classpath, output directory, and dependency metadata.
  • Constructor Details

    • ClassInfoHolder

      public ClassInfoHolder(org.apache.maven.project.MavenProject project)
      Creates a holder for the supplied Maven project and eagerly initializes class discovery metadata.
      Parameters:
      project - the Maven project whose classpath and artifacts are inspected
  • Method Details

    • init

      private void init()
      Lazily initializes class discovery state on first access.
      Throws:
      IllegalArgumentException - if the project classpath or class-location metadata cannot be initialized
    • loadClassList

      private void loadClassList(org.apache.maven.project.MavenProject project)
      Builds the project class loader and captures all classes visible from it.
      Parameters:
      project - the Maven project providing compile classpath and build output directories
      Throws:
      IllegalArgumentException - if the class loader or class path cannot be initialized
    • loadClassLocations

      private void loadClassLocations(org.apache.maven.project.MavenProject project)
      Scans the project's output directory and resolved dependency artifacts to map classes back to their origin.
      Parameters:
      project - the Maven project whose output directory and dependencies are scanned
      Throws:
      IllegalArgumentException - if class location scanning fails
    • scanClassesByPath

      private void scanClassesByPath(Set<org.apache.maven.artifact.Artifact> artifacts) throws IOException
      Scans each resolved dependency artifact for loadable classes.
      Parameters:
      artifacts - the Maven artifacts to inspect
      Throws:
      IOException - if reading an artifact fails
    • scanClassesByPath

      public void scanClassesByPath(String path, String id) throws IOException
      Scans a directory of compiled classes or a jar file and records eligible classes that can be loaded by this holder's class loader.

      Only public and protected classes are added to the internal path and artifact maps. Missing paths are ignored.

      Parameters:
      path - the directory or jar file to scan
      id - the dependency coordinates associated with the path, or null for project output classes
      Throws:
      IOException - if the path cannot be read
    • getClassEntries

      private List<String> getClassEntries(String path, File artifact) throws IOException
      Reads potential class entries from a directory or jar artifact.
      Parameters:
      path - path to scan
      artifact - file representation of path
      Returns:
      candidate class-entry paths
      Throws:
      IOException - if the directory or jar cannot be read
    • addJarEntries

      private void addJarEntries(File artifact, List<String> classList) throws IOException
      Adds all entries from a jar artifact to the supplied collection.
      Parameters:
      artifact - jar file to read
      classList - destination collection
      Throws:
      IOException - if the jar cannot be read
    • addDirectoryEntries

      private void addDirectoryEntries(String path, List<String> classList) throws IOException
      Recursively adds compiled-class entries found beneath a directory.
      Parameters:
      path - directory to scan
      classList - destination collection
      Throws:
      IOException - if the directory cannot be traversed
    • processClassEntry

      private void processClassEntry(String path, String id, String name)
      Loads and records a class entry when it is visible and has an eligible access level.
      Parameters:
      path - classpath location containing the entry
      id - artifact coordinates, or null for project output
      name - class-entry path
    • isSupportedClassEntry

      private boolean isSupportedClassEntry(String name)
      Determines whether an archive or directory entry represents a supported Java class.
      Parameters:
      name - entry path
      Returns:
      true for a non-module class entry outside META-INF
    • toClassName

      private String toClassName(String name)
      Converts a compiled-class entry path to a fully qualified class name.
      Parameters:
      name - class-entry path
      Returns:
      fully qualified class name
    • findClasses

      public List<com.google.common.reflect.ClassPath.ClassInfo> findClasses(String className)
      Finds classes whose simple names match the supplied regular expression.
      Parameters:
      className - the regular expression applied to simple class names
      Returns:
      matching class metadata entries
      Throws:
      PatternSyntaxException - if className is not a valid regular expression
    • loadClass

      public Class<?> loadClass(String className) throws ClassNotFoundException
      Loads a class by its fully qualified name using the holder's class loader.
      Parameters:
      className - the fully qualified class name
      Returns:
      the resolved class
      Throws:
      ClassNotFoundException - if the class cannot be loaded
    • getClassPath

      public String getClassPath(String className)
      Returns the originating path for the supplied fully qualified class name.
      Parameters:
      className - the fully qualified class name
      Returns:
      the directory or jar path for the class, or null if unknown
    • getSourcePath

      public String getSourcePath(String className)
      Returns the source file path for the supplied fully qualified class name.

      The method searches the Maven project's compile source roots for a matching Java source file. For nested classes, the top-level declaring class source file is returned.

      Parameters:
      className - the fully qualified class name
      Returns:
      the source file path, or null if no source file exists in the project source roots
    • getArtifactId

      public String getArtifactId(String className)
      Returns the Maven artifact coordinates associated with the supplied fully qualified class name.
      Parameters:
      className - the fully qualified class name
      Returns:
      artifact coordinates in groupId:artifactId:version form, or null when the class does not originate from a dependency