001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.component;
017
018import org.apache.commons.lang.StringUtils;
019import org.springframework.util.MethodInvoker;
020import org.springframework.util.ReflectionUtils;
021
022import java.io.Serializable;
023import java.lang.reflect.Method;
024
025/**
026 * Extends <code>MethodInvoker</code> to add properties for specifying
027 * a method for invocation within the UIF
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class MethodInvokerConfig extends MethodInvoker implements Serializable {
032
033    private String staticMethod;
034    private Class[] argumentTypes;
035
036    /**
037     * Override to catch a set staticMethod since super does
038     * not contain a getter
039     *
040     * @param staticMethod - static method to invoke
041     */
042    @Override
043    public void setStaticMethod(String staticMethod) {
044        super.setStaticMethod(staticMethod);
045    }
046
047    /**
048     * Declared argument types for the method to be invoked, if not set the types will
049     * be retrieved based on the target class and target name
050     *
051     * @return Class[] method argument types
052     */
053    public Class[] getArgumentTypes() {
054        if (argumentTypes == null) {
055            return getMethodArgumentTypes();
056        }
057
058        return argumentTypes;
059    }
060
061    /**
062     * Setter for the method argument types that should be invoked
063     *
064     * @param argumentTypes
065     */
066    public void setArgumentTypes(Class[] argumentTypes) {
067        this.argumentTypes = argumentTypes;
068    }
069
070    /**
071     * Finds the method on the target class that matches the target name and
072     * returns the declared parameter types
073     *
074     * @return Class[] method parameter types
075     */
076    protected Class[] getMethodArgumentTypes() {
077        if (StringUtils.isNotBlank(staticMethod)) {
078            int lastDotIndex = this.staticMethod.lastIndexOf('.');
079            if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
080                throw new IllegalArgumentException("staticMethod must be a fully qualified class plus method name: " +
081                        "e.g. 'example.MyExampleClass.myExampleMethod'");
082            }
083            String className = this.staticMethod.substring(0, lastDotIndex);
084            String methodName = this.staticMethod.substring(lastDotIndex + 1);
085            try {
086                setTargetClass(resolveClassName(className));
087            } catch (ClassNotFoundException e) {
088                throw new RuntimeException("Unable to get class for name: " + className);
089            }
090            setTargetMethod(methodName);
091        }
092
093        Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
094        for (Method candidate : candidates) {
095            if (candidate.getName().equals(getTargetMethod())) {
096                return candidate.getParameterTypes();
097            }
098        }
099
100        return null;
101    }
102}