Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MethodInvokerConfig |
|
| 3.75;3.75 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
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 | package org.kuali.rice.krad.uif.component; | |
17 | ||
18 | import org.apache.commons.lang.StringUtils; | |
19 | import org.springframework.util.MethodInvoker; | |
20 | import org.springframework.util.ReflectionUtils; | |
21 | ||
22 | import java.io.Serializable; | |
23 | import java.lang.reflect.Method; | |
24 | ||
25 | /** | |
26 | * Extends <code>MethodInvoker</code> to add properties for specifying | |
27 | * a method for invocation within the UIF | |
28 | * | |
29 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
30 | */ | |
31 | 0 | public class MethodInvokerConfig extends MethodInvoker implements Serializable { |
32 | ||
33 | private String staticMethod; | |
34 | private Class[] argumentTypes; | |
35 | ||
36 | /** | |
37 | * Override to catch a set staticMethod since super does | |
38 | * not contain a getter | |
39 | * | |
40 | * @param staticMethod - static method to invoke | |
41 | */ | |
42 | @Override | |
43 | public void setStaticMethod(String staticMethod) { | |
44 | 0 | super.setStaticMethod(staticMethod); |
45 | 0 | } |
46 | ||
47 | /** | |
48 | * Declared argument types for the method to be invoked, if not set the types will | |
49 | * be retrieved based on the target class and target name | |
50 | * | |
51 | * @return Class[] method argument types | |
52 | */ | |
53 | public Class[] getArgumentTypes() { | |
54 | 0 | if (argumentTypes == null) { |
55 | 0 | return getMethodArgumentTypes(); |
56 | } | |
57 | ||
58 | 0 | return argumentTypes; |
59 | } | |
60 | ||
61 | /** | |
62 | * Setter for the method argument types that should be invoked | |
63 | * | |
64 | * @param argumentTypes | |
65 | */ | |
66 | public void setArgumentTypes(Class[] argumentTypes) { | |
67 | 0 | this.argumentTypes = argumentTypes; |
68 | 0 | } |
69 | ||
70 | /** | |
71 | * Finds the method on the target class that matches the target name and | |
72 | * returns the declared parameter types | |
73 | * | |
74 | * @return Class[] method parameter types | |
75 | */ | |
76 | protected Class[] getMethodArgumentTypes() { | |
77 | 0 | if (StringUtils.isNotBlank(staticMethod)) { |
78 | 0 | int lastDotIndex = this.staticMethod.lastIndexOf('.'); |
79 | 0 | if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) { |
80 | 0 | throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " + |
81 | "e.g. 'example.MyExampleClass.myExampleMethod'"); | |
82 | } | |
83 | 0 | String className = this.staticMethod.substring(0, lastDotIndex); |
84 | 0 | String methodName = this.staticMethod.substring(lastDotIndex + 1); |
85 | try { | |
86 | 0 | setTargetClass(resolveClassName(className)); |
87 | 0 | } catch (ClassNotFoundException e) { |
88 | 0 | throw new RuntimeException("Unable to get class for name: " + className); |
89 | 0 | } |
90 | 0 | setTargetMethod(methodName); |
91 | } | |
92 | ||
93 | 0 | Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass()); |
94 | 0 | for (Method candidate : candidates) { |
95 | 0 | if (candidate.getName().equals(getTargetMethod())) { |
96 | 0 | return candidate.getParameterTypes(); |
97 | } | |
98 | } | |
99 | ||
100 | 0 | return null; |
101 | } | |
102 | } |