Class ClassFunctionalTools

java.lang.Object
org.machanism.machai.gw.maven.tools.ClassFunctionalTools
All Implemented Interfaces:
org.machanism.machai.ai.tools.FunctionTools

public class ClassFunctionalTools extends Object implements org.machanism.machai.ai.tools.FunctionTools
Provides function-tool integrations for discovering Java classes and reading reflective metadata from the classpath of one or more Maven projects.

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 Details

    • FT_NOT_SUPPORTED_FOR_PROJECT_MSG

      private static final String 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

      private static final String CLASS_NAME_PROPERTY
      JSON property used for a class's fully qualified name.
      See Also:
    • MODIFIERS_PROPERTY

      private static final String MODIFIERS_PROPERTY
      JSON property used for Java modifier text.
      See Also:
    • MAX_ALLOWED_CLASS_RESULTS

      private static final int MAX_ALLOWED_CLASS_RESULTS
      Maximum number of class names returned by a single search.
      See Also:
    • classInfoProjectMap

      private Map<File,ClassInfoHolder> 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 - if project is null
    • ClassFunctionalTools

      public ClassFunctionalTools()
      Creates an empty instance. Projects must be registered later through scanProjectClasses(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 - if project is null
    • findClass

      public List<String> findClass(String className, File projectDir)
      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 name
      projectDir - 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 inspect
      projectDir - 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 loader
      IllegalArgumentException - if no project is registered for projectDir
    • 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 source
      className - fully qualified class name requested by the caller
      clazz - resolved class to describe
      Returns:
      structured class metadata
    • addSuperclass

      private void addSuperclass(Map<String,Object> info, Class<?> clazz)
      Adds the class's direct superclass, when one exists.
      Parameters:
      info - destination metadata map
      clazz - inspected class
    • addInterfaces

      private void addInterfaces(Map<String,Object> info, Class<?> clazz)
      Adds names of interfaces directly implemented by the class. Inherited interfaces are not included.
      Parameters:
      info - destination metadata map
      clazz - inspected class
    • addFields

      private void addFields(Map<String,Object> info, Class<?> clazz)
      Adds metadata for declared fields that are not private.
      Parameters:
      info - destination metadata map
      clazz - inspected class
    • addConstructors

      private void addConstructors(Map<String,Object> info, Class<?> clazz)
      Adds metadata for all declared constructors, including private constructors.
      Parameters:
      info - destination metadata map
      clazz - inspected class
    • addMethods

      private void addMethods(Map<String,Object> info, Class<?> clazz)
      Adds metadata for declared methods that are not private.
      Parameters:
      info - destination metadata map
      clazz - inspected class
    • addAnnotations

      private void addAnnotations(Map<String,Object> info, Class<?> clazz)
      Adds string representations of annotations declared directly on the class.
      Parameters:
      info - destination metadata map
      clazz - 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 map
      classInfoHolder - class metadata source
      className - fully qualified class name
    • toParameterTypes

      private List<String> toParameterTypes(Class<?>[] parameterTypes)
      Converts reflective parameter types into fully qualified type names.
      Parameters:
      parameterTypes - parameter types to convert
      Returns:
      fully qualified parameter type names
    • forEachNonPrivate

      private <T extends Member> void forEachNonPrivate(T[] members, Consumer<T> consumer)
      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 inspect
      consumer - action applied to each visible member