EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.item.adapter]

COVERAGE SUMMARY FOR SOURCE FILE [HippyMethodInvoker.java]

nameclass, %method, %block, %line, %
HippyMethodInvoker.java100% (1/1)100% (2/2)80%  (127/158)94%  (34/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class HippyMethodInvoker100% (1/1)100% (2/2)80%  (127/158)94%  (34/36)
findMatchingMethod (): Method 100% (1/1)80%  (124/155)94%  (33/35)
HippyMethodInvoker (): void 100% (1/1)100% (3/3)100% (1/1)

1/*
2 * Copyright 2006-2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.batch.item.adapter;
17 
18import java.lang.reflect.Method;
19 
20import org.springframework.util.ClassUtils;
21import org.springframework.util.MethodInvoker;
22import org.springframework.util.ReflectionUtils;
23 
24/**
25 * A {@link MethodInvoker} that is a bit relaxed about its arguments. You can
26 * give it arguments in the wrong order or you can give it too many arguments
27 * and it will try and find a method that matches a subset.
28 * 
29 * @author Dave Syer
30 * 
31 * @since 2.1
32 */
33public class HippyMethodInvoker extends MethodInvoker {
34 
35        @Override
36        protected Method findMatchingMethod() {
37                String targetMethod = getTargetMethod();
38                Object[] arguments = getArguments();
39                int argCount = arguments.length;
40 
41                Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
42                int minTypeDiffWeight = Integer.MAX_VALUE;
43                Method matchingMethod = null;
44 
45                Object[] transformedArguments = null;
46                int transformedArgumentCount = 0;
47 
48                for (int i = 0; i < candidates.length; i++) {
49                        Method candidate = candidates[i];
50                        if (candidate.getName().equals(targetMethod)) {
51                                Class<?>[] paramTypes = candidate.getParameterTypes();
52                                Object[] candidateArguments = new Object[paramTypes.length];
53                                int assignedParameterCount = 0;
54                                boolean assigned = paramTypes.length==0;
55                                for (int j = 0; j < arguments.length; j++) {
56                                        for (int k = 0; k < paramTypes.length; k++) {
57                                                // Pick the first assignable of the right type that
58                                                // matches this slot and hasn't already been filled...
59                                                if (ClassUtils.isAssignableValue(paramTypes[k], arguments[j]) && candidateArguments[k] == null) {
60                                                        candidateArguments[k] = arguments[j];
61                                                        assignedParameterCount++;
62                                                        assigned = true;
63                                                        break;
64                                                }
65                                        }
66                                }
67                                if (assigned && paramTypes.length <= argCount) {
68                                        int typeDiffWeight = getTypeDifferenceWeight(paramTypes, candidateArguments);
69                                        if (typeDiffWeight < minTypeDiffWeight) {
70                                                minTypeDiffWeight = typeDiffWeight;
71                                                matchingMethod = candidate;
72                                                transformedArguments = candidateArguments;
73                                                transformedArgumentCount = assignedParameterCount;
74                                        }
75                                }
76                        }
77                }
78 
79                if (transformedArguments == null) {
80                        throw new IllegalArgumentException("No matching arguments found for method: " + targetMethod);
81                }
82 
83                if (transformedArgumentCount < transformedArguments.length) {
84                        throw new IllegalArgumentException("Only " + transformedArgumentCount + " out of "
85                                        + transformedArguments.length + " arguments could be assigned.");
86                }
87 
88                setArguments(transformedArguments);
89                return matchingMethod;
90 
91        }
92}

[all classes][org.springframework.batch.item.adapter]
EMMA 2.0.5312 (C) Vladimir Roubtsov