Class MethodHandleFinder

java.lang.Object
madkit.reflection.MethodHandleFinder

public class MethodHandleFinder extends Object
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 Details

    • findMethodHandleFromArgs

      public static MethodHandle findMethodHandleFromArgs(Class<?> agentClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException
      Returns a MethodHandle according to a methodName, 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 since findMethodHandle(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 the Activator.execute(Object...) method and the like in Activator subclasses. This also works for private and inherited methods.
      Parameters:
      agentClass - the class from which the search has to be done
      methodName - the name of the method
      args - a sample of the args which can be passed to the method
      Returns:
      the found MethodHandle
      Throws:
      NoSuchMethodException - if a matching method cannot be found
      IllegalAccessException - 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 a MethodHandle according to a methodName 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 done
      methodName - the name of the method
      paramTypes - the types of the arguments of the method as an array of Class
      Returns:
      the found MethodHandle
      Throws:
      NoSuchMethodException - if a matching method cannot be found