Class ClassFunctionalTools
- All Implemented Interfaces:
org.machanism.machai.ai.tools.FunctionTools
Instances maintain a cache of ClassInfoHolder objects keyed by Maven
project base directory, allowing tool invocations to resolve classes relative
to the current working project. The class exposes tools for locating classes
by simple-name regular expression and for describing a resolved class's
declared members and source-location metadata.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringJSON property used for a class's fully qualified name.private Map<File, ClassInfoHolder> Holds class metadata by Maven project base directory.private static final StringMessage returned when a tool call is made for a project that has not been scanned and registered in this instance.private static final intMaximum number of class names returned by a single search.private static final StringJSON property used for Java modifier text. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty instance.ClassFunctionalTools(org.apache.maven.project.MavenProject project) Creates a new instance backed by the supplied Maven project's classpath. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddAnnotations(Map<String, Object> info, Class<?> clazz) Adds string representations of annotations declared directly on the class.private voidaddConstructors(Map<String, Object> info, Class<?> clazz) Adds metadata for all declared constructors, including private constructors.private voidAdds metadata for declared fields that are not private.private voidaddInterfaces(Map<String, Object> info, Class<?> clazz) Adds names of interfaces directly implemented by the class.private voidaddLocationMetadata(Map<String, Object> info, ClassInfoHolder classInfoHolder, String className) Adds classpath, artifact, and project-source location metadata.private voidaddMethods(Map<String, Object> info, Class<?> clazz) Adds metadata for declared methods that are not private.private voidaddSuperclass(Map<String, Object> info, Class<?> clazz) Adds the class's direct superclass, when one exists.Finds fully qualified class names whose simple names match the supplied regular expression.private <T extends Member>
voidforEachNonPrivate(T[] members, Consumer<T> consumer) Invokes a consumer for every member that is not private.getClassInfo(String className, File projectDir) Returns reflective information for the specified class.populateClassInfo(ClassInfoHolder classInfoHolder, String className, Class<?> clazz) Builds the structured metadata response for a resolved class.voidscanProjectClasses(org.apache.maven.project.MavenProject project) Registers a project by scanning and caching class metadata for its base directory.toParameterTypes(Class<?>[] parameterTypes) Converts reflective parameter types into fully qualified type names.
-
Field Details
-
FT_NOT_SUPPORTED_FOR_PROJECT_MSG
Message returned when a tool call is made for a project that has not been scanned and registered in this instance.- See Also:
-
CLASS_NAME_PROPERTY
JSON property used for a class's fully qualified name.- See Also:
-
MODIFIERS_PROPERTY
JSON property used for Java modifier text.- See Also:
-
MAX_ALLOWED_CLASS_RESULTS
private static final int MAX_ALLOWED_CLASS_RESULTSMaximum number of class names returned by a single search.- See Also:
-
classInfoProjectMap
Holds class metadata by Maven project base directory.
-
-
Constructor Details
-
ClassFunctionalTools
public ClassFunctionalTools(org.apache.maven.project.MavenProject project) Creates a new instance backed by the supplied Maven project's classpath.- Parameters:
project- the Maven project used to resolve classpath and output directories- Throws:
NullPointerException- ifprojectisnull
-
ClassFunctionalTools
public ClassFunctionalTools()Creates an empty instance. Projects must be registered later throughscanProjectClasses(MavenProject)before tool invocations can resolve class information.
-
-
Method Details
-
scanProjectClasses
public void scanProjectClasses(org.apache.maven.project.MavenProject project) Registers a project by scanning and caching class metadata for its base directory. Registering the same base directory again replaces its cached metadata with a newly scanned holder.- Parameters:
project- the Maven project to register- Throws:
NullPointerException- ifprojectisnull
-
findClass
Finds fully qualified class names whose simple names match the supplied regular expression.The search is performed against the classes visible when the project was registered. A result set larger than ten entries is rejected so callers can refine broad expressions before requesting metadata.
- Parameters:
className- regular expression matched against each class's simple nameprojectDir- base directory of a project previously registered with this instance- Returns:
- fully qualified names of matching classes, in the order supplied by the class metadata holder
- Throws:
IllegalArgumentException- if the project is not registered, no class matches, the pattern is invalid, or more than ten classes match
-
getClassInfo
public Map<String,Object> getClassInfo(String className, File projectDir) throws ClassNotFoundException Returns reflective information for the specified class.The generated output may include modifiers, superclass, interfaces, non-private fields, constructors, non-private methods, annotations, class path, and dependency artifact coordinates.
- Parameters:
className- fully qualified class name to inspectprojectDir- the Maven project base directory used to resolve class metadata- Returns:
- a map describing the requested class, including its declared non-private fields and methods and available location metadata
- Throws:
ClassNotFoundException- if the requested class is not visible from the registered project's class loaderIllegalArgumentException- if no project is registered forprojectDir
-
populateClassInfo
private Map<String,Object> populateClassInfo(ClassInfoHolder classInfoHolder, String className, Class<?> clazz) Builds the structured metadata response for a resolved class. Member data is limited to declarations on the class itself; inherited members are not included.- Parameters:
classInfoHolder- class metadata sourceclassName- fully qualified class name requested by the callerclazz- resolved class to describe- Returns:
- structured class metadata
-
addSuperclass
Adds the class's direct superclass, when one exists.- Parameters:
info- destination metadata mapclazz- inspected class
-
addInterfaces
Adds names of interfaces directly implemented by the class. Inherited interfaces are not included.- Parameters:
info- destination metadata mapclazz- inspected class
-
addFields
Adds metadata for declared fields that are not private.- Parameters:
info- destination metadata mapclazz- inspected class
-
addConstructors
Adds metadata for all declared constructors, including private constructors.- Parameters:
info- destination metadata mapclazz- inspected class
-
addMethods
Adds metadata for declared methods that are not private.- Parameters:
info- destination metadata mapclazz- inspected class
-
addAnnotations
Adds string representations of annotations declared directly on the class.- Parameters:
info- destination metadata mapclazz- inspected class
-
addLocationMetadata
private void addLocationMetadata(Map<String, Object> info, ClassInfoHolder classInfoHolder, String className) Adds classpath, artifact, and project-source location metadata. Artifact and source-path properties are omitted when the metadata holder cannot resolve them.- Parameters:
info- destination metadata mapclassInfoHolder- class metadata sourceclassName- fully qualified class name
-
toParameterTypes
Converts reflective parameter types into fully qualified type names.- Parameters:
parameterTypes- parameter types to convert- Returns:
- fully qualified parameter type names
-
forEachNonPrivate
Invokes a consumer for every member that is not private. Package-private, protected, and public members are included.- Type Parameters:
T- reflective member type- Parameters:
members- members to inspectconsumer- action applied to each visible member
-