    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.Constructor;
    020import java.lang.reflect.InvocationTargetException;
    021
    022import org.apache.commons.collections4.FunctorException;
    023import org.apache.commons.collections4.Transformer;
    024
    025/**
    026 * Transformer implementation that creates a new object instance by reflection.
    027 * <p>
    028 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
    029 * in order to prevent potential remote code execution exploits. Please refer to
    030 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
    031 * for more details.
    032 * </p>
    033 *
    034 * @param <T> the type of the input and result to the function.
    035 * @since 3.0
    036 */
    037public class InstantiateTransformer<T> implements Transformer<Class<? extends T>, T> {
    038
    039    /** Singleton instance that uses the no arg constructor */
    040    @SuppressWarnings("rawtypes")
    041    private static final Transformer NO_ARG_INSTANCE = new InstantiateTransformer<>();
    042
    043    /**
    044     * Gets a typed no-arg instance.
    045     *
    046     * @param <T>  the type of the objects to be created
    047     * @return Transformer&lt;Class&lt;? extends T&gt;, T&gt;
    048     */
    049    public static <T> Transformer<Class<? extends T>, T> instantiateTransformer() {
    050        return NO_ARG_INSTANCE;
    051    }
    052    /**
    053     * Transformer method that performs validation.
    054     *
    055     * @param <T>  the type of the objects to be created
    056     * @param paramTypes  the constructor parameter types
    057     * @param args  the constructor arguments
    058     * @return an instantiate transformer
    059     * @throws IllegalArgumentException if paramTypes does not match args
    060     */
    061    public static <T> Transformer<Class<? extends T>, T> instantiateTransformer(final Class<?>[] paramTypes,
    062                                                                                final Object[] args) {
    063        if (paramTypes == null && args != null
    064            || paramTypes != null && args == null
    065            || paramTypes != null && args != null && paramTypes.length != args.length) {
    066            throw new IllegalArgumentException("Parameter types must match the arguments");
    067        }
    068
    069        if (paramTypes == null || paramTypes.length == 0) {
    070            return new InstantiateTransformer<>();
    071        }
    072        return new InstantiateTransformer<>(paramTypes, args);
    073    }
    074
    075    /** The constructor parameter types */
    076    private final Class<?>[] iParamTypes;
    077
    078    /** The constructor arguments */
    079    private final Object[] iArgs;
    080
    081    /**
    082     * Constructor for no arg instance.
    083     */
    084    private InstantiateTransformer() {
    085        iParamTypes = null;
    086        iArgs = null;
    087    }
    088
    089    /**
    090     * Constructor that performs no validation.
    091     * Use {@code instantiateTransformer} if you want that.
    092     * <p>
    093     * Note: from 4.0, the input parameters will be cloned
    094     *
    095     * @param paramTypes  the constructor parameter types
    096     * @param args  the constructor arguments
    097     */
    098    public InstantiateTransformer(final Class<?>[] paramTypes, final Object[] args) {
    099        iParamTypes = paramTypes != null ? paramTypes.clone() : null;
    100        iArgs = args != null ? args.clone() : null;
    101    }
    102
    103    /**
    104     * Transforms the input Class object to a result by instantiation.
    105     *
    106     * @param input  the input object to transform
    107     * @return the transformed result
    108     */
    109    @Override
    110    public T transform(final Class<? extends T> input) {
    111        try {
    112            if (input == null) {
    113                throw new FunctorException(
    114                    "InstantiateTransformer: Input object was not an instanceof Class, it was a null object");
    115            }
    116            final Constructor<? extends T> con = input.getConstructor(iParamTypes);
    117            return con.newInstance(iArgs);
    118        } catch (final NoSuchMethodException ex) {
    119            throw new FunctorException("InstantiateTransformer: The constructor must exist and be public ");
    120        } catch (final InstantiationException ex) {
    121            throw new FunctorException("InstantiateTransformer: InstantiationException", ex);
    122        } catch (final IllegalAccessException ex) {
    123            throw new FunctorException("InstantiateTransformer: Constructor must be public", ex);
    124        } catch (final InvocationTargetException ex) {
    125            throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex);
    126        }
    127    }
    128
    129}