View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.functors;
18  
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.Factory;
24  import org.apache.commons.collections4.FunctorException;
25  
26  /**
27   * Factory implementation that creates a new object instance by reflection.
28   * <p>
29   * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> be serializable anymore
30   * in order to prevent potential remote code execution exploits. Please refer to
31   * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
32   * for more details.
33   * </p>
34   *
35   * @since 3.0
36   */
37  public class InstantiateFactory<T> implements Factory<T> {
38  
39      /**
40       * Factory method that performs validation.
41       *
42       * @param <T>  the type the factory creates
43       * @param classToInstantiate  the class to instantiate, not null
44       * @param paramTypes  the constructor parameter types, cloned
45       * @param args  the constructor arguments, cloned
46       * @return a new instantiate factory
47       * @throws NullPointerException if classToInstantiate is null
48       * @throws IllegalArgumentException if paramTypes does not match args
49       */
50      public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate,
51                                                      final Class<?>[] paramTypes,
52                                                      final Object[] args) {
53          Objects.requireNonNull(classToInstantiate, "classToInstantiate");
54          if (paramTypes == null && args != null
55              || paramTypes != null && args == null
56              || paramTypes != null && args != null && paramTypes.length != args.length) {
57              throw new IllegalArgumentException("Parameter types must match the arguments");
58          }
59  
60          if (paramTypes == null || paramTypes.length == 0) {
61              return new InstantiateFactory<>(classToInstantiate);
62          }
63          return new InstantiateFactory<>(classToInstantiate, paramTypes, args);
64      }
65      /** The class to create */
66      private final Class<T> iClassToInstantiate;
67      /** The constructor parameter types */
68      private final Class<?>[] iParamTypes;
69      /** The constructor arguments */
70      private final Object[] iArgs;
71  
72      /** The constructor */
73      private transient Constructor<T> iConstructor;
74  
75      /**
76       * Constructor that performs no validation.
77       * Use {@code instantiateFactory} if you want that.
78       *
79       * @param classToInstantiate  the class to instantiate
80       */
81      public InstantiateFactory(final Class<T> classToInstantiate) {
82          iClassToInstantiate = classToInstantiate;
83          iParamTypes = null;
84          iArgs = null;
85          findConstructor();
86      }
87  
88      /**
89       * Constructor that performs no validation.
90       * Use {@code instantiateFactory} if you want that.
91       *
92       * @param classToInstantiate  the class to instantiate
93       * @param paramTypes  the constructor parameter types, cloned
94       * @param args  the constructor arguments, cloned
95       */
96      public InstantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args) {
97          iClassToInstantiate = classToInstantiate;
98          iParamTypes = paramTypes.clone();
99          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 }