EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.support]

COVERAGE SUMMARY FOR SOURCE FILE [SimpleMethodInvoker.java]

nameclass, %method, %block, %line, %
SimpleMethodInvoker.java100% (1/1)83%  (5/6)76%  (175/229)69%  (31.9/46)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleMethodInvoker100% (1/1)83%  (5/6)76%  (175/229)69%  (31.9/46)
hashCode (): int 0%   (0/1)0%   (0/20)0%   (0/4)
extractTarget (Object, Method): Object 100% (1/1)15%  (5/34)20%  (2/10)
equals (Object): boolean 100% (1/1)83%  (24/29)66%  (3.9/6)
SimpleMethodInvoker (Object, Method): void 100% (1/1)100% (15/15)100% (6/6)
SimpleMethodInvoker (Object, String, Class []): void 100% (1/1)100% (54/54)100% (9/9)
invokeMethod (Object []): Object 100% (1/1)100% (77/77)100% (11/11)

1/*
2 * Copyright 2002-2008 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 */
16 
17/*
18 * Copyright 2002-2008 the original author or authors.
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 *      http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32package org.springframework.batch.support;
33 
34import java.lang.reflect.Method;
35import java.util.Arrays;
36 
37import org.springframework.aop.framework.Advised;
38import org.springframework.util.Assert;
39import org.springframework.util.ClassUtils;
40 
41/**
42 * Simple implementation of the {@link MethodInvoker} interface that invokes a
43 * method on an object. If the method has no arguments, but arguments are
44 * provided, they are ignored and the method is invoked anyway. If there are
45 * more arguments than there are provided, then an exception is thrown.
46 * 
47 * @author Lucas Ward
48 * @since 2.0
49 */
50public class SimpleMethodInvoker implements MethodInvoker {
51 
52        private final Object object;
53 
54        private Method method;
55 
56        public SimpleMethodInvoker(Object object, Method method) {
57                Assert.notNull(object, "Object to invoke must not be null");
58                Assert.notNull(method, "Method to invoke must not be null");
59                this.method = method;
60                this.object = object;
61        }
62 
63        public SimpleMethodInvoker(Object object, String methodName, Class<?>... paramTypes) {
64                Assert.notNull(object, "Object to invoke must not be null");
65                this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes);
66                if (this.method == null) {
67                        // try with no params
68                        this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, new Class[] {});
69                }
70                if (this.method == null) {
71                        throw new IllegalArgumentException("No methods found for name: [" + methodName + "] in class: ["
72                                        + object.getClass() + "] with arguments of type: [" + Arrays.toString(paramTypes) + "]");
73                }
74                this.object = object;
75        }
76 
77        /*
78         * (non-Javadoc)
79         * 
80         * @see
81         * org.springframework.batch.core.configuration.util.MethodInvoker#invokeMethod
82         * (java.lang.Object[])
83         */
84        public Object invokeMethod(Object... args) {
85 
86                Class<?>[] parameterTypes = method.getParameterTypes();
87                Object[] invokeArgs;
88                if (parameterTypes.length == 0) {
89                        invokeArgs = new Object[] {};
90                }
91                else if (parameterTypes.length != args.length) {
92                        throw new IllegalArgumentException("Wrong number of arguments, expected no more than: ["
93                                        + parameterTypes.length + "]");
94                }
95                else {
96                        invokeArgs = args;
97                }
98 
99                method.setAccessible(true);
100 
101                try {
102                        // Extract the target from an Advised as late as possible
103                        // in case it contains a lazy initialization
104                        Object target = extractTarget(object, method);
105                        return method.invoke(target, invokeArgs);
106                }
107                catch (Exception e) {
108                        throw new IllegalArgumentException("Unable to invoke method: [" + method + "] on object: [" + object
109                                        + "] with arguments: [" + Arrays.toString(args) + "]", e);
110                }
111        }
112 
113        private Object extractTarget(Object target, Method method) {
114                if (target instanceof Advised) {
115                        Object source;
116                        try {
117                                source = ((Advised) target).getTargetSource().getTarget();
118                        }
119                        catch (Exception e) {
120                                throw new IllegalStateException("Could not extract target from proxy", e);
121                        }
122                        if (source instanceof Advised) {
123                                source = extractTarget(source, method);
124                        }
125                        if (method.getDeclaringClass().isAssignableFrom(source.getClass())) {
126                                target = source;
127                        }
128                }
129                return target;
130        }
131 
132        @Override
133        public boolean equals(Object obj) {
134                if (!(obj instanceof SimpleMethodInvoker)) {
135                        return false;
136                }
137 
138                if (obj == this) {
139                        return true;
140                }
141                SimpleMethodInvoker rhs = (SimpleMethodInvoker) obj;
142                return (rhs.method.equals(this.method)) && (rhs.object.equals(this.object));
143        }
144 
145        @Override
146        public int hashCode() {
147                int result = 25;
148                result = 31 * result + object.hashCode();
149                result = 31 * result + method.hashCode();
150                return result;
151        }
152}

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