Class ClassInfoHolder
java.lang.Object
org.machanism.machai.gw.maven.tools.ClassInfoHolder
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
FieldsModifier and TypeFieldDescriptionMaps fully qualified class names to Maven artifact coordinates in the formgroupId:artifactId:versionwhen 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.private URLClassLoaderClass loader built from the Maven project classpath and output directories.Maps fully qualified class names to the filesystem path or jar path from which they were resolved.private static final org.slf4j.Loggerprivate org.apache.maven.project.MavenProjectMaven project used as the source of classpath, output directory, and dependency metadata. -
Constructor Summary
ConstructorsConstructorDescriptionClassInfoHolder(org.apache.maven.project.MavenProject project) Creates a holder for the supplied Maven project and eagerly initializes class discovery metadata. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddDirectoryEntries(String path, List<String> classList) private voidaddJarEntries(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.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 voidinit()Lazily initializes class discovery state on first access.private booleanisSupportedClassEntry(String name) Class<?> Loads a class by its fully qualified name using the holder's class loader.private voidloadClassList(org.apache.maven.project.MavenProject project) Builds the project class loader and captures all classes visible from it.private voidloadClassLocations(org.apache.maven.project.MavenProject project) Scans the project's output directory and resolved dependency artifacts to map classes back to their origin.private voidprocessClassEntry(String path, String id, String name) voidscanClassesByPath(String path, String id) 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 voidscanClassesByPath(Set<org.apache.maven.artifact.Artifact> artifacts) Scans each resolved dependency artifact for loadable classes.private StringtoClassName(String name)
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
classes
private com.google.common.collect.ImmutableSet<com.google.common.reflect.ClassPath.ClassInfo> classesAll classes visible from the constructed project class loader. -
classPathMap
Maps fully qualified class names to the filesystem path or jar path from which they were resolved. -
classLoader
Class loader built from the Maven project classpath and output directories. -
artifactMap
Maps fully qualified class names to Maven artifact coordinates in the formgroupId:artifactId:versionwhen the class comes from a dependency. -
project
private org.apache.maven.project.MavenProject projectMaven 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
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 scanid- the dependency coordinates associated with the path, ornullfor project output classes- Throws:
IOException- if the path cannot be read
-
getClassEntries
- Throws:
IOException
-
addJarEntries
- Throws:
IOException
-
addDirectoryEntries
- Throws:
IOException
-
processClassEntry
-
isSupportedClassEntry
-
toClassName
-
findClasses
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
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
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
nullif unknown
-
getSourcePath
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
nullif no source file exists in the project source roots
-
getArtifactId
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:versionform, ornullwhen the class does not originate from a dependency
-