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
     
    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
     
    private void
    addJarEntries(File artifact, List<String> classList)
     
    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)
     
    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
     
    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
     
    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
     

    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
    • 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.
    • 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 path 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
      Throws:
      IOException
    • addJarEntries

      private void addJarEntries(File artifact, List<String> classList) throws IOException
      Throws:
      IOException
    • addDirectoryEntries

      private void addDirectoryEntries(String path, List<String> classList) throws IOException
      Throws:
      IOException
    • processClassEntry

      private void processClassEntry(String path, String id, String name)
    • isSupportedClassEntry

      private boolean isSupportedClassEntry(String name)
    • toClassName

      private String toClassName(String 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
    • 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