001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.commons.collections.functors;
018    
019    import java.io.Serializable;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    import org.apache.commons.collections.FunctorException;
024    import org.apache.commons.collections.Transformer;
025    
026    /**
027     * Transformer implementation that creates a new object instance by reflection.
028     * 
029     * @since Commons Collections 3.0
030     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
031     *
032     * @author Stephen Colebourne
033     */
034    public class InvokerTransformer implements Transformer, Serializable {
035    
036        /** The serial version */
037        private static final long serialVersionUID = -8653385846894047688L;
038        
039        /** The method name to call */
040        private final String iMethodName;
041        /** The array of reflection parameter types */
042        private final Class[] iParamTypes;
043        /** The array of reflection arguments */
044        private final Object[] iArgs;
045    
046        /**
047         * Gets an instance of this transformer calling a specific method with no arguments.
048         * 
049         * @param methodName  the method name to call
050         * @return an invoker transformer
051         * @since Commons Collections 3.1
052         */
053        public static Transformer getInstance(String methodName) {
054            if (methodName == null) {
055                throw new IllegalArgumentException("The method to invoke must not be null");
056            }
057            return new InvokerTransformer(methodName);
058        }
059    
060        /**
061         * Gets an instance of this transformer calling a specific method with specific values.
062         * 
063         * @param methodName  the method name to call
064         * @param paramTypes  the parameter types of the method
065         * @param args  the arguments to pass to the method
066         * @return an invoker transformer
067         */
068        public static Transformer getInstance(String methodName, Class[] paramTypes, Object[] args) {
069            if (methodName == null) {
070                throw new IllegalArgumentException("The method to invoke must not be null");
071            }
072            if (((paramTypes == null) && (args != null))
073                || ((paramTypes != null) && (args == null))
074                || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
075                throw new IllegalArgumentException("The parameter types must match the arguments");
076            }
077            if (paramTypes == null || paramTypes.length == 0) {
078                return new InvokerTransformer(methodName);
079            } else {
080                paramTypes = (Class[]) paramTypes.clone();
081                args = (Object[]) args.clone();
082                return new InvokerTransformer(methodName, paramTypes, args);
083            }
084        }
085    
086        /**
087         * Constructor for no arg instance.
088         * 
089         * @param methodName  the method to call
090         */
091        private InvokerTransformer(String methodName) {
092            super();
093            iMethodName = methodName;
094            iParamTypes = null;
095            iArgs = null;
096        }
097    
098        /**
099         * Constructor that performs no validation.
100         * Use <code>getInstance</code> if you want that.
101         * 
102         * @param methodName  the method to call
103         * @param paramTypes  the constructor parameter types, not cloned
104         * @param args  the constructor arguments, not cloned
105         */
106        public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
107            super();
108            iMethodName = methodName;
109            iParamTypes = paramTypes;
110            iArgs = args;
111        }
112    
113        /**
114         * Transforms the input to result by invoking a method on the input.
115         * 
116         * @param input  the input object to transform
117         * @return the transformed result, null if null input
118         */
119        public Object transform(Object input) {
120            if (input == null) {
121                return null;
122            }
123            try {
124                Class cls = input.getClass();
125                Method method = cls.getMethod(iMethodName, iParamTypes);
126                return method.invoke(input, iArgs);
127                    
128            } catch (NoSuchMethodException ex) {
129                throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist");
130            } catch (IllegalAccessException ex) {
131                throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed");
132            } catch (InvocationTargetException ex) {
133                throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex);
134            }
135        }
136    
137    }