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.

  • 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
    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.
    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.
    loadClass(String className)
    Loads a class by its fully qualified name using the holder's class loader.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

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