org.springframework.util

## 类 MethodInvoker

- java.lang.Object
- - org.springframework.util.MethodInvoker

-     public class MethodInvoker
      extends Object

  Helper class that allows for specifying a method to invoke in a declarative fashion, be it static or non-static.

  Usage: Specify "targetClass"/"targetMethod" or "targetObject"/"targetMethod", optionally specify arguments, prepare the invoker. Afterwards, you may invoke the method any number of times, obtaining the invocation result.

  从以下版本开始:
    19.02.2004
  作者:
    Colin Sampaleanu, Juergen Hoeller
  另请参阅:
    `prepare()`, `invoke()`

- - ### 字段概要

    字段
    限定符和类型 | 字段和说明
    --- | ---
    `protected Class<?>` | `targetClass`

  - ### 构造器概要

    构造器
    构造器和说明
    `MethodInvoker()`

  - ### 方法概要

    所有方法 静态方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `protected Method` | `findMatchingMethod()` Find a matching method with the specified name for the specified arguments.
    `Object[]` | `getArguments()` Return the arguments for the method invocation.
    `Method` | `getPreparedMethod()` Return the prepared Method object that will be invoked.
    `Class<?>` | `getTargetClass()` Return the target class on which to call the target method.
    `String` | `getTargetMethod()` Return the name of the method to be invoked.
    `Object` | `getTargetObject()` Return the target object on which to call the target method.
    `static int` | `getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args)` Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.
    `Object` | `invoke()` Invoke the specified method.
    `boolean` | `isPrepared()` Return whether this invoker has been prepared already, i.e. whether it allows access to `getPreparedMethod()` already.
    `void` | `prepare()` Prepare the specified method.
    `protected Class<?>` | `resolveClassName(String className)` Resolve the given class name into a Class.
    `void` | `setArguments(Object... arguments)` Set arguments for the method invocation.
    `void` | `setStaticMethod(String staticMethod)` Set a fully qualified static method name to invoke, e.g.
    `void` | `setTargetClass(Class<?> targetClass)` Set the target class on which to call the target method.
    `void` | `setTargetMethod(String targetMethod)` Set the name of the method to be invoked.
    `void` | `setTargetObject(Object targetObject)` Set the target object on which to call the target method.

    - ### 从类继承的方法 java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 字段详细资料

    - #### targetClass

          @Nullable
          protected Class<?> targetClass

  - ### 构造器详细资料

    - #### MethodInvoker

          public MethodInvoker()

  - ### 方法详细资料

    - #### setTargetClass

          public void setTargetClass(@Nullable
                                     Class<?> targetClass)

      Set the target class on which to call the target method. Only necessary when the target method is static; else, a target object needs to be specified anyway.
      另请参阅:
        `setTargetObject(java.lang.Object)`, `setTargetMethod(java.lang.String)`

    - #### getTargetClass

          @Nullable
          public Class<?> getTargetClass()

      Return the target class on which to call the target method.

    - #### setTargetObject

          public void setTargetObject(@Nullable
                                      Object targetObject)

      Set the target object on which to call the target method. Only necessary when the target method is not static; else, a target class is sufficient.
      另请参阅:
        `setTargetClass(java.lang.Class<?>)`, `setTargetMethod(java.lang.String)`

    - #### getTargetObject

          @Nullable
          public Object getTargetObject()

      Return the target object on which to call the target method.

    - #### setTargetMethod

          public void setTargetMethod(@Nullable
                                      String targetMethod)

      Set the name of the method to be invoked. Refers to either a static method or a non-static method, depending on a target object being set.
      另请参阅:
        `setTargetClass(java.lang.Class<?>)`, `setTargetObject(java.lang.Object)`

    - #### getTargetMethod

          @Nullable
          public String getTargetMethod()

      Return the name of the method to be invoked.

    - #### setStaticMethod

          public void setStaticMethod(String staticMethod)

      Set a fully qualified static method name to invoke, e.g. "example.MyExampleClass.myExampleMethod". Convenient alternative to specifying targetClass and targetMethod.
      另请参阅:
        `setTargetClass(java.lang.Class<?>)`, `setTargetMethod(java.lang.String)`

    - #### setArguments

          public void setArguments(Object... arguments)

      Set arguments for the method invocation. If this property is not set, or the Object array is of length 0, a method with no arguments is assumed.

    - #### getArguments

          public Object[] getArguments()

      Return the arguments for the method invocation.

    - #### prepare

          public void prepare()
                       throws ClassNotFoundException,
                              NoSuchMethodException

      Prepare the specified method. The method can be invoked any number of times afterwards.
      抛出:
        `ClassNotFoundException`
        `NoSuchMethodException`
      另请参阅:
        `getPreparedMethod()`, `invoke()`

    - #### resolveClassName

          protected Class<?> resolveClassName(String className)
                                       throws ClassNotFoundException

      Resolve the given class name into a Class.

      The default implementations uses `ClassUtils.forName`, using the thread context class loader.

      参数:
        `className` - the class name to resolve
      返回:
        the resolved Class
      抛出:
        `ClassNotFoundException` - if the class name was invalid

    - #### findMatchingMethod

          @Nullable
          protected Method findMatchingMethod()

      Find a matching method with the specified name for the specified arguments.
      返回:
        a matching method, or `null` if none
      另请参阅:
        `getTargetClass()`, `getTargetMethod()`, `getArguments()`

    - #### getPreparedMethod

          public Method getPreparedMethod()
                                   throws IllegalStateException

      Return the prepared Method object that will be invoked.

      Can for example be used to determine the return type.

      返回:
        the prepared Method object (never `null`)
      抛出:
        `IllegalStateException` - if the invoker hasn't been prepared yet
      另请参阅:
        `prepare()`, `invoke()`

    - #### isPrepared

          public boolean isPrepared()

      Return whether this invoker has been prepared already, i.e. whether it allows access to `getPreparedMethod()` already.

    - #### invoke

          @Nullable
          public Object invoke()
                                   throws InvocationTargetException,
                                          IllegalAccessException

      Invoke the specified method.

      The invoker needs to have been prepared before.

      返回:
        the object (possibly null) returned by the method invocation, or `null` if the method has a void return type
      抛出:
        `InvocationTargetException` - if the target method threw an exception
        `IllegalAccessException` - if the target method couldn't be accessed
      另请参阅:
        `prepare()`

    - #### getTypeDifferenceWeight

          public static int getTypeDifferenceWeight(Class<?>[] paramTypes,
                                                    Object[] args)

      Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.

      Determines a weight that represents the class hierarchy difference between types and arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase the result - all direct matches means weight 0. A match between type Object and arg of class Integer would increase the weight by 2, due to the superclass 2 steps up in the hierarchy (i.e. Object) being the last one that still matches the required type Object. Type Number and class Integer would increase the weight by 1 accordingly, due to the superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number. Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a constructor (Number) which would in turn be preferred to a constructor (Object). All argument weights get accumulated.

      Note: This is the algorithm used by MethodInvoker itself and also the algorithm used for constructor and factory method selection in Spring's bean container (in case of lenient constructor resolution which is the default for regular bean definitions).

      参数:
        `paramTypes` - the parameter types to match
        `args` - the arguments to match
      返回:
        the accumulated weight for all arguments