Class MethodInvoker
This class provides static methods for:
- Creating instances of classes using reflection
- Invoking methods with various parameter configurations
- Handling method invocation results and exceptions
All method invocations are wrapped with proper error handling, and results are encapsulated in
InvocationResult objects for consistent error reporting. The class follows the utility
class pattern with a private constructor to prevent instantiation.
- Author:
- codeboyzhou
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectcreateInstance(Class<?> clazz) Creates a new instance of the specified class using reflection.static InvocationResultinvoke(Object instance, MethodCache methodCache) Invokes the method represented by the specified method cache on the given instance with no parameters.static InvocationResultinvoke(Object instance, MethodCache methodCache, io.modelcontextprotocol.spec.McpSchema.CompleteRequest.CompleteArgument argument) Invokes the method represented by the specified method cache on the given instance with the provided completion argument.static InvocationResultinvoke(Object instance, MethodCache methodCache, List<Object> params) Invokes the method represented by the specified method cache on the given instance with the provided parameters.
-
Method Details
-
createInstance
Creates a new instance of the specified class using reflection.This static method uses reflection to create a new instance of the given class by calling its no-argument constructor. The method requires that the class has a public no-argument constructor.
- Parameters:
clazz- the class to instantiate- Returns:
- a new instance of the specified class
- Throws:
McpServerException- if the instance creation fails due to any reason- See Also:
-
invoke
public static InvocationResult invoke(Object instance, MethodCache methodCache, List<Object> params) Invokes the method represented by the specified method cache on the given instance with the provided parameters.This method uses reflection to invoke the specified method on the target instance, passing the provided parameters. The method handles various return types and null values:
- Void return types: Returns a success message indicating the method completed
- Null return values: Returns a message indicating the method succeeded but returned null
- Non-null return values: Returns the actual result
All exceptions are caught and wrapped in an
InvocationResultwith appropriate error messages. The method signature is logged for debugging purposes when an error occurs.- Parameters:
instance- the instance on which to invoke the methodmethodCache- the method cache containing the method metadataparams- the list of parameters to pass to the method- Returns:
- an InvocationResult containing the method result or error information
- See Also:
-
invoke
Invokes the method represented by the specified method cache on the given instance with no parameters.This is a convenience method that invokes a method with an empty parameter list. It delegates to
invoke(Object, MethodCache, List)with an empty list.- Parameters:
instance- the instance on which to invoke the methodmethodCache- the method cache containing the method metadata- Returns:
- an InvocationResult containing the method result or error information
- See Also:
-
invoke
public static InvocationResult invoke(Object instance, MethodCache methodCache, io.modelcontextprotocol.spec.McpSchema.CompleteRequest.CompleteArgument argument) Invokes the method represented by the specified method cache on the given instance with the provided completion argument.This is a convenience method for invoking methods that take a single
McpSchema.CompleteRequest.CompleteArgumentparameter. It wraps the argument in a list and delegates toinvoke(Object, MethodCache, List).This method is typically used for MCP completion operations where a single completion argument needs to be passed to the method.
- Parameters:
instance- the instance on which to invoke the methodmethodCache- the method cache containing the method metadataargument- the completion argument to pass to the method- Returns:
- an InvocationResult containing the method result or error information
- See Also:
-
invoke(Object, MethodCache, List)McpSchema.CompleteRequest.CompleteArgumentMethodCacheInvocationResult
-