类 MethodInvoker

  • 直接已知子类:
    ArgumentConvertingMethodInvoker

    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()
    • 方法详细资料

      • getTargetClass

        public Class<?> getTargetClass()
        Return the target class on which to call the target method.
      • getTargetObject

        public Object getTargetObject()
        Return the target object on which to call the target method.
      • 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.
      • 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
      • isPrepared

        public boolean isPrepared()
        Return whether this invoker has been prepared already, i.e. whether it allows access to getPreparedMethod() already.
      • 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