    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;
    021import java.util.Objects;
    022
    023import org.apache.commons.collections4.Factory;
    024import org.apache.commons.collections4.FunctorException;
    025
    026/**
    027 * Factory 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 InstantiateFactory<T> implements Factory<T> {
    038
    039    /**
    040     * Factory method that performs validation.
    041     *
    042     * @param <T>  the type the factory creates
    043     * @param classToInstantiate  the class to instantiate, not null
    044     * @param paramTypes  the constructor parameter types, cloned
    045     * @param args  the constructor arguments, cloned
    046     * @return a new instantiate factory
    047     * @throws NullPointerException if classToInstantiate is null
    048     * @throws IllegalArgumentException if paramTypes does not match args
    049     */
    050    public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate,
    051                                                    final Class<?>[] paramTypes,
    052                                                    final Object[] args) {
    053        Objects.requireNonNull(classToInstantiate, "classToInstantiate");
    054        if (paramTypes == null && args != null
    055            || paramTypes != null && args == null
    056            || paramTypes != null && args != null && paramTypes.length != args.length) {
    057            throw new IllegalArgumentException("Parameter types must match the arguments");
    058        }
    059
    060        if (paramTypes == null || paramTypes.length == 0) {
    061            return new InstantiateFactory<>(classToInstantiate);
    062        }
    063        return new InstantiateFactory<>(classToInstantiate, paramTypes, args);
    064    }
    065    /** The class to create */
    066    private final Class<T> iClassToInstantiate;
    067    /** The constructor parameter types */
    068    private final Class<?>[] iParamTypes;
    069    /** The constructor arguments */
    070    private final Object[] iArgs;
    071
    072    /** The constructor */
    073    private transient Constructor<T> iConstructor;
    074
    075    /**
    076     * Constructor that performs no validation.
    077     * Use {@code instantiateFactory} if you want that.
    078     *
    079     * @param classToInstantiate  the class to instantiate
    080     */
    081    public InstantiateFactory(final Class<T> classToInstantiate) {
    082        iClassToInstantiate = classToInstantiate;
    083        iParamTypes = null;
    084        iArgs = null;
    085        findConstructor();
    086    }
    087
    088    /**
    089     * Constructor that performs no validation.
    090     * Use {@code instantiateFactory} if you want that.
    091     *
    092     * @param classToInstantiate  the class to instantiate
    093     * @param paramTypes  the constructor parameter types, cloned
    094     * @param args  the constructor arguments, cloned
    095     */
    096    public InstantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args) {
    097        iClassToInstantiate = classToInstantiate;
    098        iParamTypes = paramTypes.clone();
    099        iArgs = args.clone();
    100        findConstructor();
    101    }
    102
    103    /**
    104     * Creates an object using the stored constructor.
    105     *
    106     * @return the new object
    107     */
    108    @Override
    109    public T create() {
    110        // needed for post-serialization
    111        if (iConstructor == null) {
    112            findConstructor();
    113        }
    114
    115        try {
    116            return iConstructor.newInstance(iArgs);
    117        } catch (final InstantiationException ex) {
    118            throw new FunctorException("InstantiateFactory: InstantiationException", ex);
    119        } catch (final IllegalAccessException ex) {
    120            throw new FunctorException("InstantiateFactory: Constructor must be public", ex);
    121        } catch (final InvocationTargetException ex) {
    122            throw new FunctorException("InstantiateFactory: Constructor threw an exception", ex);
    123        }
    124    }
    125
    126    /**
    127     * Find the Constructor for the class specified.
    128     */
    129    private void findConstructor() {
    130        try {
    131            iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
    132        } catch (final NoSuchMethodException ex) {
    133            throw new IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
    134        }
    135    }
    136
    137}