Class MethodCache
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:
McpResource- Marks methods as MCP resourcesMcpPrompt- Marks methods as MCP promptsMcpTool- Marks methods as MCP toolsMcpPromptCompletion- Marks methods as MCP prompt completion handlersMcpResourceCompletion- Marks methods as MCP resource completion handlers
This class is immutable and thread-safe once constructed.
- Author:
- codeboyzhou
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMethodCache(Method method) Creates a new instance ofMethodCachewith the specified method. -
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whether some other object is "equal to" this one.Class<?>Returns the class that declares the cached method.Returns theMcpPromptannotation on the cached method.Returns theMcpPromptCompletionannotation on the cached method.Returns theMcpResourceannotation on the cached method.Returns theMcpResourceCompletionannotation on the cached method.Returns theMcpToolannotation on the cached method.Returns the method cached by thisMethodCacheinstance.Returns the name of the cached method.Returns the generic signature of the cached method.Returns a clone of the parameters array of the cached method.Class<?>Returns the return type of the cached method.inthashCode()Returns a hash code value for the object.static MethodCacheCreates or retrieves a cached instance ofMethodCachefor the specified method.toString()Returns a string representation of the object.
-
Constructor Details
-
MethodCache
Creates a new instance ofMethodCachewith 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
Immutablewrapper 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
Creates or retrieves a cached instance ofMethodCachefor the specified method.This factory method uses a thread-safe cache to ensure that each method is cached only once. If a
MethodCachefor 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
Returns the method cached by thisMethodCacheinstance.The method is retrieved from the
Immutablewrapper, ensuring thread-safe access to the underlying method object.- Returns:
- the method cached by this
MethodCacheinstance - See Also:
-
getMethodName
Returns the name of the cached method.- Returns:
- the name of the cached method
-
getDeclaringClass
Returns the class that declares the cached method.- Returns:
- the class that declares the cached method
-
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
Returns the return type of the cached method.- Returns:
- the return type of the cached method
-
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
Returns theMcpResourceannotation on the cached method.Returns null if the method is not annotated with
@McpResource.- Returns:
- the
McpResourceannotation on the cached method, or null if not present - See Also:
-
getMcpPromptAnnotation
Returns theMcpPromptannotation on the cached method.Returns null if the method is not annotated with
@McpPrompt. -
getMcpToolAnnotation
Returns theMcpToolannotation on the cached method.Returns null if the method is not annotated with
@McpTool. -
getMcpPromptCompletionAnnotation
Returns theMcpPromptCompletionannotation on the cached method.Returns null if the method is not annotated with
@McpPromptCompletion.- Returns:
- the
McpPromptCompletionannotation on the cached method, or null if not present - See Also:
-
getMcpResourceCompletionAnnotation
Returns theMcpResourceCompletionannotation on the cached method.Returns null if the method is not annotated with
@McpResourceCompletion.- Returns:
- the
McpResourceCompletionannotation on the cached method, or null if not present - See Also:
-
equals
Indicates whether some other object is "equal to" this one.Two
MethodCacheinstances are considered equal if they cache the same method object. The comparison is based on the underlying method reference. -
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
MethodCacheinstances have the same hash code. -
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.
-