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