    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 */
    017package org.apache.commons.collections4.functors;
    018
    019import java.lang.reflect.InvocationTargetException;
    020import java.lang.reflect.Method;
    021import java.util.Objects;
    022
    023import org.apache.commons.collections4.FunctorException;
    024import org.apache.commons.collections4.Transformer;
    025
    026/**
    027 * Transformer implementation that creates a new object instance by reflection.
    028 * <p>
    029 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
    030 * in order to prevent potential remote code execution exploits. Please refer to
    031 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
    032 * for more details.
    033 * </p>
    034 *
    035 * @param <T> the type of the input to the function.
    036 * @param <R> the type of the result of the function.
    037 * @since 3.0
    038 */
    039public class InvokerTransformer<T, R> implements Transformer<T, R> {
    040
    041    /**
    042     * Gets an instance of this transformer calling a specific method with no arguments.
    043     *
    044     * @param <I>  the input type
    045     * @param <O>  the output type
    046     * @param methodName  the method name to call
    047     * @return an invoker transformer
    048     * @throws NullPointerException if methodName is null
    049     * @since 3.1
    050     */
    051    public static <I, O> Transformer<I, O> invokerTransformer(final String methodName) {
    052        return new InvokerTransformer<>(Objects.requireNonNull(methodName, "methodName"));
    053    }
    054    /**
    055     * Gets an instance of this transformer calling a specific method with specific values.
    056     *
    057     * @param <I>  the input type
    058     * @param <O>  the output type
    059     * @param methodName  the method name to call
    060     * @param paramTypes  the parameter types of the method
    061     * @param args  the arguments to pass to the method
    062     * @return an invoker transformer
    063     * @throws NullPointerException if methodName is null
    064     * @throws IllegalArgumentException if paramTypes does not match args
    065     */
    066    public static <I, O> Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
    067                                                              final Object[] args) {
    068        Objects.requireNonNull(methodName, "methodName");
    069        if (paramTypes == null && args != null
    070            || paramTypes != null && args == null
    071            || paramTypes != null && args != null && paramTypes.length != args.length) {
    072            throw new IllegalArgumentException("The parameter types must match the arguments");
    073        }
    074        if (paramTypes == null || paramTypes.length == 0) {
    075            return new InvokerTransformer<>(methodName);
    076        }
    077        return new InvokerTransformer<>(methodName, paramTypes, args);
    078    }
    079    /** The method name to call */
    080    private final String iMethodName;
    081
    082    /** The array of reflection parameter types */
    083    private final Class<?>[] iParamTypes;
    084
    085    /** The array of reflection arguments */
    086    private final Object[] iArgs;
    087
    088    /**
    089     * Constructor for no arg instance.
    090     *
    091     * @param methodName  the method to call
    092     */
    093    private InvokerTransformer(final String methodName) {
    094        iMethodName = methodName;
    095        iParamTypes = null;
    096        iArgs = null;
    097    }
    098
    099    /**
    100     * Constructor that performs no validation.
    101     * Use {@code invokerTransformer} if you want that.
    102     * <p>
    103     * Note: from 4.0, the input parameters will be cloned
    104     *
    105     * @param methodName  the method to call
    106     * @param paramTypes  the constructor parameter types
    107     * @param args  the constructor arguments
    108     */
    109    public InvokerTransformer(final String methodName, final Class<?>[] paramTypes, final Object[] args) {
    110        iMethodName = methodName;
    111        iParamTypes = paramTypes != null ? paramTypes.clone() : null;
    112        iArgs = args != null ? args.clone() : null;
    113    }
    114
    115    /**
    116     * Transforms the input to result by invoking a method on the input.
    117     *
    118     * @param input  the input object to transform
    119     * @return the transformed result, null if null input
    120     */
    121    @Override
    122    @SuppressWarnings("unchecked")
    123    public R transform(final Object input) {
    124        if (input == null) {
    125            return null;
    126        }
    127        try {
    128            final Class<?> cls = input.getClass();
    129            final Method method = cls.getMethod(iMethodName, iParamTypes);
    130            return (R) method.invoke(input, iArgs);
    131        } catch (final NoSuchMethodException ex) {
    132            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    133                                       input.getClass() + "' does not exist");
    134        } catch (final IllegalAccessException ex) {
    135            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    136                                       input.getClass() + "' cannot be accessed");
    137        } catch (final InvocationTargetException ex) {
    138            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    139                                       input.getClass() + "' threw an exception", ex);
    140        }
    141    }
    142
    143}