View Javadoc

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