Class MethodLookup


  • public final class MethodLookup
    extends Object
    A class to help find methods based on annotations or other method references. These lookups work similar to the lookups used for MethodSource. In addition, it supports multiple sets of supported parameter types. This can be used to validate method references if the parameter types are given, or perform multiple lookups otherwise.

    Instances of this class are not thread safe when configuring them using orParameterTypes(Class...). Once an instance is configured, it's safe to call find(String, ExtensionContext) from different threads concurrently.

    Author:
    Rob Spoor
    Since:
    2.0
    • Method Detail

      • withParameterTypes

        public static MethodLookup withParameterTypes​(Class<?>... parameterTypes)
        Creates a new method lookup instance.
        Parameters:
        parameterTypes - The preferred set of parameter types.
        Returns:
        The created method lookup instance.
      • orParameterTypes

        public MethodLookup orParameterTypes​(Class<?>... parameterTypes)
        Adds another allowed set of parameter types.
        Parameters:
        parameterTypes - The set of parameter types.
        Returns:
        This object.
      • find

        public MethodLookup.Result find​(String methodReference,
                                        ExtensionContext context)
        Tries to find a method. If the method cannot be found, an exception is thrown.

        The method reference can be defined in a number of ways:

        • methodName for a method in the test class itself. The parameter types for the method are those used to create this instance; the first match will be returned.
        • className#methodName for a method in the defined class. The parameter types for the method are those used to create this instance; the first match will be returned.
        • methodName(parameterTypes) for a method in the test class itself. The parameter types are used as-is, but these must match one of the sets of parameter types used to create this instance.
        • className#methodName(parameterTypes) for a method in the defined class. The parameter types are used as-is, but these must match one of the sets of parameter types used to create this instance.
        Parameters:
        methodReference - A reference to the method to find.
        context - The current extension context; never null.
        Returns:
        A result describing the method that was found.
      • findMethod

        public static Method findMethod​(String methodReference,
                                        ExtensionContext context)
        Tries to find a method. If the method cannot be found, an exception is thrown.

        The method reference can be defined in a number of ways:

        • methodName for a method in the test class itself. If multiple methods are found with the given name, an exception is thrown.
        • className#methodName for a method in the defined class. If multiple methods are found with the given name, an exception is thrown.
        • methodName(parameterTypes) for a method in the test class itself. The parameter types are used as-is.
        • className#methodName(parameterTypes) for a method in the defined class. The parameter types are used as-is.
        Parameters:
        methodReference - A reference to the method to find.
        context - The current extension context; never null.
        Returns:
        The method that was found.