View Javadoc

1   /**
2    * Copyright 2005-2012 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  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          super.setStaticMethod(staticMethod);
45      }
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          if (argumentTypes == null) {
55              return getMethodArgumentTypes();
56          }
57  
58          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          this.argumentTypes = argumentTypes;
68      }
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          if (StringUtils.isNotBlank(staticMethod)) {
78              int lastDotIndex = this.staticMethod.lastIndexOf('.');
79              if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
80                  throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " +
81                          "e.g. 'example.MyExampleClass.myExampleMethod'");
82              }
83              String className = this.staticMethod.substring(0, lastDotIndex);
84              String methodName = this.staticMethod.substring(lastDotIndex + 1);
85              try {
86                  setTargetClass(resolveClassName(className));
87              } catch (ClassNotFoundException e) {
88                  throw new RuntimeException("Unable to get class for name: " + className);
89              }
90              setTargetMethod(methodName);
91          }
92  
93          Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
94          for (Method candidate : candidates) {
95              if (candidate.getName().equals(getTargetMethod())) {
96                  return candidate.getParameterTypes();
97              }
98          }
99  
100         return null;
101     }
102 }