    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 * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> 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 * @since 3.0
    036 */
    037public class InvokerTransformer<I, O> implements Transformer<I, O> {
    038
    039    /**
    040     * Gets an instance of this transformer calling a specific method with no arguments.
    041     *
    042     * @param <I>  the input type
    043     * @param <O>  the output type
    044     * @param methodName  the method name to call
    045     * @return an invoker transformer
    046     * @throws NullPointerException if methodName is null
    047     * @since 3.1
    048     */
    049    public static <I, O> Transformer<I, O> invokerTransformer(final String methodName) {
    050        return new InvokerTransformer<>(Objects.requireNonNull(methodName, "methodName"));
    051    }
    052    /**
    053     * Gets an instance of this transformer calling a specific method with specific values.
    054     *
    055     * @param <I>  the input type
    056     * @param <O>  the output type
    057     * @param methodName  the method name to call
    058     * @param paramTypes  the parameter types of the method
    059     * @param args  the arguments to pass to the method
    060     * @return an invoker transformer
    061     * @throws NullPointerException if methodName is null
    062     * @throws IllegalArgumentException if paramTypes does not match args
    063     */
    064    public static <I, O> Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
    065                                                              final Object[] args) {
    066        Objects.requireNonNull(methodName, "methodName");
    067        if (paramTypes == null && args != null
    068            || paramTypes != null && args == null
    069            || paramTypes != null && args != null && paramTypes.length != args.length) {
    070            throw new IllegalArgumentException("The parameter types must match the arguments");
    071        }
    072        if (paramTypes == null || paramTypes.length == 0) {
    073            return new InvokerTransformer<>(methodName);
    074        }
    075        return new InvokerTransformer<>(methodName, paramTypes, args);
    076    }
    077    /** The method name to call */
    078    private final String iMethodName;
    079
    080    /** The array of reflection parameter types */
    081    private final Class<?>[] iParamTypes;
    082
    083    /** The array of reflection arguments */
    084    private final Object[] iArgs;
    085
    086    /**
    087     * Constructor for no arg instance.
    088     *
    089     * @param methodName  the method to call
    090     */
    091    private InvokerTransformer(final String methodName) {
    092        iMethodName = methodName;
    093        iParamTypes = null;
    094        iArgs = null;
    095    }
    096
    097    /**
    098     * Constructor that performs no validation.
    099     * Use {@code invokerTransformer} if you want that.
    100     * <p>
    101     * Note: from 4.0, the input parameters will be cloned
    102     *
    103     * @param methodName  the method to call
    104     * @param paramTypes  the constructor parameter types
    105     * @param args  the constructor arguments
    106     */
    107    public InvokerTransformer(final String methodName, final Class<?>[] paramTypes, final Object[] args) {
    108        iMethodName = methodName;
    109        iParamTypes = paramTypes != null ? paramTypes.clone() : null;
    110        iArgs = args != null ? args.clone() : null;
    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    @Override
    120    @SuppressWarnings("unchecked")
    121    public O transform(final Object input) {
    122        if (input == null) {
    123            return null;
    124        }
    125        try {
    126            final Class<?> cls = input.getClass();
    127            final Method method = cls.getMethod(iMethodName, iParamTypes);
    128            return (O) method.invoke(input, iArgs);
    129        } catch (final NoSuchMethodException ex) {
    130            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    131                                       input.getClass() + "' does not exist");
    132        } catch (final IllegalAccessException ex) {
    133            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    134                                       input.getClass() + "' cannot be accessed");
    135        } catch (final InvocationTargetException ex) {
    136            throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" +
    137                                       input.getClass() + "' threw an exception", ex);
    138        }
    139    }
    140
    141}