/** * Invoke the specified method. * <p>The invoker needs to have been prepared before. * @return the object (possibly null) returned by the method invocation, * or {@code null} if the method has a void return type * @throws InvocationTargetException if the target method threw an exception * @throws IllegalAccessException if the target method couldn't be accessed * @see #prepare */ @Nullable public Object invoke() throws InvocationTargetException, IllegalAccessException { // In the static case, target will simply be {@code null}. Object targetObject = getTargetObject(); Method preparedMethod = getPreparedMethod(); if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) { throw new IllegalArgumentException("Target method must not be non-static without a target"); } ReflectionUtils.makeAccessible(preparedMethod); return preparedMethod.invoke(targetObject, getArguments()); }
Assert.notNull(targetMethod, "Property 'targetMethod' is required"); Object[] arguments = getArguments(); Class<?>[] argTypes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; ++i) {
/** * Invoke the specified method. * <p>The invoker needs to have been prepared before. * @return the object (possibly null) returned by the method invocation, * or {@code null} if the method has a void return type * @throws InvocationTargetException if the target method threw an exception * @throws IllegalAccessException if the target method couldn't be accessed * @see #prepare */ @Nullable public Object invoke() throws InvocationTargetException, IllegalAccessException { // In the static case, target will simply be {@code null}. Object targetObject = getTargetObject(); Method preparedMethod = getPreparedMethod(); if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) { throw new IllegalArgumentException("Target method must not be non-static without a target"); } ReflectionUtils.makeAccessible(preparedMethod); return preparedMethod.invoke(targetObject, getArguments()); }
protected Method findMatchingMethod() { String targetMethod = getTargetMethod(); Object[] arguments = getArguments(); int argCount = arguments.length;
Assert.notNull(targetMethod, "Property 'targetMethod' is required"); Object[] arguments = getArguments(); Class<?>[] argTypes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; ++i) {
protected Method findMatchingMethod() { String targetMethod = getTargetMethod(); Object[] arguments = getArguments(); int argCount = arguments.length;
/** * Invoke the specified method. * <p>The invoker needs to have been prepared before. * @return the object (possibly null) returned by the method invocation, * or <code>null</code> if the method has a void return type * @throws InvocationTargetException if the target method threw an exception * @throws IllegalAccessException if the target method couldn't be accessed * @see #prepare */ public Object invoke() throws InvocationTargetException, IllegalAccessException { // In the static case, target will simply be <code>null</code>. Object targetObject = getTargetObject(); Method preparedMethod = getPreparedMethod(); if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) { throw new IllegalArgumentException("Target method must not be non-static without a target"); } ReflectionUtils.makeAccessible(preparedMethod); return preparedMethod.invoke(targetObject, getArguments()); }
Object[] arguments = getArguments(); Class[] argTypes = new Class[arguments.length]; for (int i = 0; i < arguments.length; ++i) {
/** * Find a matching method with the specified name for the specified arguments. * @return a matching method, or <code>null</code> if none * @see #getTargetClass() * @see #getTargetMethod() * @see #getArguments() */ protected Method findMatchingMethod() { String targetMethod = getTargetMethod(); Object[] arguments = getArguments(); int argCount = arguments.length; Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass()); int minTypeDiffWeight = Integer.MAX_VALUE; Method matchingMethod = null; for (Method candidate : candidates) { if (candidate.getName().equals(targetMethod)) { Class[] paramTypes = candidate.getParameterTypes(); if (paramTypes.length == argCount) { int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments); if (typeDiffWeight < minTypeDiffWeight) { minTypeDiffWeight = typeDiffWeight; matchingMethod = candidate; } } } } return matchingMethod; }
/** * Invoke the specified method. * <p>The invoker needs to have been prepared before. * @return the object (possibly null) returned by the method invocation, * or {@code null} if the method has a void return type * @throws InvocationTargetException if the target method threw an exception * @throws IllegalAccessException if the target method couldn't be accessed * @see #prepare */ @Nullable public Object invoke() throws InvocationTargetException, IllegalAccessException { // In the static case, target will simply be {@code null}. Object targetObject = getTargetObject(); Method preparedMethod = getPreparedMethod(); if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) { throw new IllegalArgumentException("Target method must not be non-static without a target"); } ReflectionUtils.makeAccessible(preparedMethod); return preparedMethod.invoke(targetObject, getArguments()); }
/** * Find a matching method with the specified name for the specified arguments. * @return a matching method, or <code>null</code> if none * @see #getTargetClass() * @see #getTargetMethod() * @see #getArguments() */ protected Method findMatchingMethod() { Method[] candidates = getTargetClass().getMethods(); int argCount = getArguments().length; Method matchingMethod = null; int numberOfMatchingMethods = 0; for (int i = 0; i < candidates.length; i++) { // Check if the inspected method has the correct name and number of parameters. if (candidates[i].getName().equals(getTargetMethod()) && candidates[i].getParameterTypes().length == argCount) { matchingMethod = candidates[i]; numberOfMatchingMethods++; } } // Only return matching method if exactly one found. if (numberOfMatchingMethods == 1) { return matchingMethod; } else { return null; } }
Assert.notNull(targetMethod, "Property 'targetMethod' is required"); Object[] arguments = getArguments(); Class<?>[] argTypes = new Class<?>[arguments.length]; for (int i = 0; i < arguments.length; ++i) {
protected Method findMatchingMethod() { String targetMethod = getTargetMethod(); Object[] arguments = getArguments(); int argCount = arguments.length;