Class MethodHandleFinder
java.lang.Object
madkit.reflection.MethodHandleFinder
This class provides a way to find a
MethodHandle
from a given Class
and
a method name. It is especially useful when the method signature is unknown and the
method is private or inherited. The MethodHandle
is cached in order to avoid
the cost of the reflection API.- Since:
- MaDKit 6.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandle
findMethodHandle
(Class<?> agentClass, String methodName, Class<?>... paramTypes) Returns aMethodHandle
according to amethodName
and a given agentClass.static MethodHandle
findMethodHandleFromArgs
(Class<?> agentClass, String methodName, Object... args) Returns aMethodHandle
according to amethodName
, a given agentClass and a sample of the arguments which could be passed to it.
-
Method Details
-
findMethodHandleFromArgs
public static MethodHandle findMethodHandleFromArgs(Class<?> agentClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException Returns aMethodHandle
according to amethodName
, a given agentClass and a sample of the arguments which could be passed to it. The purpose of this method is restricted to a limited number of use cases sincefindMethodHandle(Class, String, Class...)
should be preferred if the exact signature of the searched method is known. A typical use case of this method is when the only information available is the arguments which are passed, for instance when overriding theActivator.execute(Object...)
method and the like inActivator
subclasses. This also works for private and inherited methods.- Parameters:
agentClass
- the class from which the search has to be donemethodName
- the name of the methodargs
- a sample of the args which can be passed to the method- Returns:
- the found
MethodHandle
- Throws:
NoSuchMethodException
- if a matching method cannot be foundIllegalAccessException
- if the method cannot be accessed, for instance if targeted the module is not opened to the MaDKit madkit.base module
-
findMethodHandle
public static MethodHandle findMethodHandle(Class<?> agentClass, String methodName, Class<?>... paramTypes) throws NoSuchMethodException Returns aMethodHandle
according to amethodName
and a given agentClass. This method should be preferred the exact signature of the searched method is known.- Parameters:
agentClass
- the class from which the search has to be donemethodName
- the name of the methodparamTypes
- the types of the arguments of the method as an array ofClass
- Returns:
- the found
MethodHandle
- Throws:
NoSuchMethodException
- if a matching method cannot be found
-