Class JavaMethodTool

All Implemented Interfaces:
PropertyChangeSource, Rebindable

public class JavaMethodTool extends AbstractTool<JavaMethodToolParameter, JavaMethodToolCall>
A model-agnostic, stateful representation of a single Java method tool. This is a self-contained, executable unit that encapsulates both the definition of the tool and the logic required to invoke it via reflection.
Author:
anahata-gemini-pro-2.5
  • Field Details

    • javaMethodSignature

      @NonNull private final @NonNull String javaMethodSignature
      The full Java method signature.
    • method

      private transient Method method
      The underlying Java method that this tool represents. Transient as Method is not serializable.
    • wrappedResponseJsonSchema

      private String wrappedResponseJsonSchema
      The fully wrapped JSON schema (including JavaMethodToolResponse structure).
    • rawResponseJsonSchema

      private String rawResponseJsonSchema
      The raw JSON schema of the method's return type only.
  • Constructor Details

    • JavaMethodTool

      public JavaMethodTool(JavaObjectToolkit toolkit, Method method, AgiTool toolAnnotation) throws Exception
      The definitive, intelligent constructor for creating a JavaMethodTool from a reflection Method. This constructor encapsulates all the logic for parsing annotations and generating schemas.
      Parameters:
      toolkit - The parent toolkit.
      method - The reflection Method to parse.
      toolAnnotation - The pre-fetched @AgiTool annotation.
      Throws:
      Exception - if schema generation fails.
  • Method Details

    • initAttributes

      private final void initAttributes(AgiTool toolAnnotation)
      Initializes the maxDepth and description attributes.
      Parameters:
      toolAnnotation - The source @AgiTool annotation containing metadata.
    • refreshSchema

      public void refreshSchema() throws Exception
      Refreshes the cached schemas for this tool.
      Throws:
      Exception - if schema generation fails.
    • getResponseJsonSchema

      public String getResponseJsonSchema()
      Dynamically returns either the wrapped or raw schema based on the wrapResponseSchemas flag in the ToolManager.
      Returns:
      The JSON schema string.
    • getToolkitInstance

      public Object getToolkitInstance()
      Retrieves the instance of the toolkit class from the parent toolkit.
      Returns:
      The toolkit instance.
    • postActivate

      public void postActivate() throws Exception
      Performs post-restoration logic, warming up the reflection method and parameters. This is called during the session binding phase to ensure the tool is ready before the UI or AI processing begins.
      Throws:
      Exception - if method restoration or parameter initialization fails.
    • initParameters

      private void initParameters() throws Exception
      Initializes and populates the parameter list for this tool.
      Throws:
      Exception - if parameter mapping or schema generation fails.
    • getDescription

      public String getDescription()
      Returns a rich, formatted description string containing the base description, current permission status, and effective max depth details.
      Returns:
      The formatted tool description.
    • buildMethodSignature

      public static String buildMethodSignature(Method m)
      Builds a human-readable string representation of a Java method's signature.
      Parameters:
      m - The method to inspect.
      Returns:
      The formatted method signature string.
    • createCall

      public JavaMethodToolCall createCall(AbstractModelMessage modelMessage, String id, Map<String,Object> jsonArgs)
      Description copied from class: AbstractTool
      Factory method to create a tool-specific call object from raw model data.
      Specified by:
      createCall in class AbstractTool<JavaMethodToolParameter, JavaMethodToolCall>
      Parameters:
      modelMessage - the model message the call will belong to.
      id - The call ID.
      jsonArgs - The raw arguments from the model.
      Returns:
      A new tool call instance.
    • getResponseType

      public Type getResponseType()
      Description copied from class: AbstractTool
      Template method hook for subclasses to provide their specific Response type.
      Specified by:
      getResponseType in class AbstractTool<JavaMethodToolParameter, JavaMethodToolCall>
      Returns:
      The reflection Type of the corresponding AbstractToolResponse subclass.