java.lang.Object
com.github.thought2code.mcp.annotated.reflect.MethodCache

public final class MethodCache extends Object
A cache class that stores metadata and annotations for Java methods.

This class encapsulates information about a Java method, including its name, declaring class, parameters, return type, and MCP-specific annotations. It provides efficient access to method metadata without repeated reflection operations.

The class maintains a thread-safe static cache using ConcurrentHashMap to ensure that each method is cached only once, improving performance when the same method is accessed multiple times.

Supported MCP annotations include:

This class is immutable and thread-safe once constructed.

Author:
codeboyzhou
See Also:
  • Constructor Details

    • MethodCache

      public MethodCache(Method method)
      Creates a new instance of MethodCache with the specified method.

      This constructor extracts all relevant metadata from the method including its name, declaring class, parameters, return type, generic signature, and all MCP-specific annotations. The method is wrapped in an Immutable wrapper for thread safety.

      Note: For creating cached instances, prefer using the of(Method) factory method which utilizes the static cache.

      Parameters:
      method - the method to cache, must not be null
      Throws:
      NullPointerException - if the method is null
      See Also:
  • Method Details

    • of

      public static MethodCache of(Method method)
      Creates or retrieves a cached instance of MethodCache for the specified method.

      This factory method uses a thread-safe cache to ensure that each method is cached only once. If a MethodCache for the given method already exists in the cache, it will be returned; otherwise, a new instance will be created, cached, and returned.

      The caching mechanism significantly improves performance by avoiding repeated reflection operations when the same method is accessed multiple times.

      Parameters:
      method - the method to cache or retrieve from cache, must not be null
      Returns:
      a cached MethodCache instance for the specified method
      See Also:
    • getMethod

      public Method getMethod()
      Returns the method cached by this MethodCache instance.

      The method is retrieved from the Immutable wrapper, ensuring thread-safe access to the underlying method object.

      Returns:
      the method cached by this MethodCache instance
      See Also:
    • getMethodName

      public String getMethodName()
      Returns the name of the cached method.
      Returns:
      the name of the cached method
    • getDeclaringClass

      public Class<?> getDeclaringClass()
      Returns the class that declares the cached method.
      Returns:
      the class that declares the cached method
    • getParameters

      public Parameter[] getParameters()
      Returns a clone of the parameters array of the cached method.

      This method returns a defensive copy of the parameters array to prevent external modification of the internal state.

      Returns:
      a clone of the parameters of the cached method
      See Also:
    • getReturnType

      public Class<?> getReturnType()
      Returns the return type of the cached method.
      Returns:
      the return type of the cached method
    • getMethodSignature

      public String getMethodSignature()
      Returns the generic signature of the cached method.

      The generic signature includes the fully qualified class name, method name, parameter types, and return type with generic type information.

      Returns:
      the generic signature of the cached method
      See Also:
    • getMcpResourceAnnotation

      public McpResource getMcpResourceAnnotation()
      Returns the McpResource annotation on the cached method.

      Returns null if the method is not annotated with @McpResource.

      Returns:
      the McpResource annotation on the cached method, or null if not present
      See Also:
    • getMcpPromptAnnotation

      public McpPrompt getMcpPromptAnnotation()
      Returns the McpPrompt annotation on the cached method.

      Returns null if the method is not annotated with @McpPrompt.

      Returns:
      the McpPrompt annotation on the cached method, or null if not present
      See Also:
    • getMcpToolAnnotation

      public McpTool getMcpToolAnnotation()
      Returns the McpTool annotation on the cached method.

      Returns null if the method is not annotated with @McpTool.

      Returns:
      the McpTool annotation on the cached method, or null if not present
      See Also:
    • getMcpPromptCompletionAnnotation

      public McpPromptCompletion getMcpPromptCompletionAnnotation()
      Returns the McpPromptCompletion annotation on the cached method.

      Returns null if the method is not annotated with @McpPromptCompletion.

      Returns:
      the McpPromptCompletion annotation on the cached method, or null if not present
      See Also:
    • getMcpResourceCompletionAnnotation

      public McpResourceCompletion getMcpResourceCompletionAnnotation()
      Returns the McpResourceCompletion annotation on the cached method.

      Returns null if the method is not annotated with @McpResourceCompletion.

      Returns:
      the McpResourceCompletion annotation on the cached method, or null if not present
      See Also:
    • equals

      public boolean equals(Object obj)
      Indicates whether some other object is "equal to" this one.

      Two MethodCache instances are considered equal if they cache the same method object. The comparison is based on the underlying method reference.

      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare
      Returns:
      true if this object is the same as the obj argument, false otherwise
      See Also:
    • hashCode

      public int hashCode()
      Returns a hash code value for the object.

      The hash code is based on the cached method object, ensuring that equal MethodCache instances have the same hash code.

      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this object
      See Also:
    • toString

      public String toString()
      Returns a string representation of the object.

      The string representation includes the method signature, providing a concise and informative description of the cached method.

      Overrides:
      toString in class Object
      Returns:
      a string representation of the object
      See Also: