1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.component;
17
18 import java.io.Serializable;
19 import java.lang.reflect.Method;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.rice.core.api.exception.RiceRuntimeException;
23 import org.kuali.rice.krad.datadictionary.Copyable;
24 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
25 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
26 import org.springframework.util.MethodInvoker;
27 import org.springframework.util.ReflectionUtils;
28
29
30
31
32
33
34
35 @BeanTag(name = "methodInvokerConfig-bean", parent = "Uif-MethodInvokerConfig")
36 public class MethodInvokerConfig extends MethodInvoker implements Serializable, Copyable {
37 private static final long serialVersionUID = 6626790175367500081L;
38
39 private String staticMethod;
40 private Class[] argumentTypes;
41
42
43
44
45 @Override
46 public void prepare() throws ClassNotFoundException, NoSuchMethodException {
47 if ((getTargetObject() == null) && (getTargetClass() != null)) {
48 try {
49 setTargetObject(getTargetClass().newInstance());
50 } catch (Exception e) {
51 throw new RiceRuntimeException("Unable to create new intance of target class", e);
52 }
53 }
54
55 super.prepare();
56 }
57
58
59
60
61
62
63
64
65 @BeanTagAttribute(name="staticMethod")
66 public String getStaticMethod() {
67 return staticMethod;
68 }
69
70
71
72
73
74
75
76 @Override
77 public void setStaticMethod(String staticMethod) {
78 super.setStaticMethod(staticMethod);
79 this.staticMethod = staticMethod;
80 }
81
82
83
84
85
86
87
88 @BeanTagAttribute(name="argumentTypes",type= BeanTagAttribute.AttributeType.LISTBEAN)
89 public Class[] getArgumentTypes() {
90 if (argumentTypes == null) {
91 return getMethodArgumentTypes();
92 }
93
94 return argumentTypes;
95 }
96
97
98
99
100
101
102 public void setArgumentTypes(Class[] argumentTypes) {
103 this.argumentTypes = argumentTypes;
104 }
105
106
107
108
109
110
111
112 protected Class[] getMethodArgumentTypes() {
113 if (StringUtils.isNotBlank(staticMethod)) {
114 int lastDotIndex = this.staticMethod.lastIndexOf('.');
115 if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
116 throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " +
117 "e.g. 'example.MyExampleClass.myExampleMethod'");
118 }
119 String className = this.staticMethod.substring(0, lastDotIndex);
120 String methodName = this.staticMethod.substring(lastDotIndex + 1);
121 try {
122 setTargetClass(resolveClassName(className));
123 } catch (ClassNotFoundException e) {
124 throw new RuntimeException("Unable to get class for name: " + className);
125 }
126 setTargetMethod(methodName);
127 }
128
129 Method matchingCandidate = findMatchingMethod();
130 if (matchingCandidate != null) {
131 return matchingCandidate.getParameterTypes();
132 }
133
134 Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
135 for (Method candidate : candidates) {
136 if (candidate.getName().equals(getTargetMethod())) {
137 return candidate.getParameterTypes();
138 }
139 }
140
141 return null;
142 }
143
144
145
146
147 @Override
148 public MethodInvokerConfig clone() throws CloneNotSupportedException {
149 return (MethodInvokerConfig) super.clone();
150 }
151
152 }