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 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> 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 * @param <T> the type of results supplied by this supplier.
36 * @since 3.0
37 */
38 public class InstantiateFactory<T> implements Factory<T> {
39
40 /**
41 * Factory method that performs validation.
42 *
43 * @param <T> the type the factory creates
44 * @param classToInstantiate the class to instantiate, not null
45 * @param paramTypes the constructor parameter types, cloned
46 * @param args the constructor arguments, cloned
47 * @return a new instantiate factory
48 * @throws NullPointerException if classToInstantiate is null
49 * @throws IllegalArgumentException if paramTypes does not match args
50 */
51 public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate,
52 final Class<?>[] paramTypes,
53 final Object[] args) {
54 Objects.requireNonNull(classToInstantiate, "classToInstantiate");
55 if (paramTypes == null && args != null
56 || paramTypes != null && args == null
57 || paramTypes != null && args != null && paramTypes.length != args.length) {
58 throw new IllegalArgumentException("Parameter types must match the arguments");
59 }
60
61 if (paramTypes == null || paramTypes.length == 0) {
62 return new InstantiateFactory<>(classToInstantiate);
63 }
64 return new InstantiateFactory<>(classToInstantiate, paramTypes, args);
65 }
66 /** The class to create */
67 private final Class<T> iClassToInstantiate;
68 /** The constructor parameter types */
69 private final Class<?>[] iParamTypes;
70 /** The constructor arguments */
71 private final Object[] iArgs;
72
73 /** The constructor */
74 private transient Constructor<T> iConstructor;
75
76 /**
77 * Constructor that performs no validation.
78 * Use {@code instantiateFactory} if you want that.
79 *
80 * @param classToInstantiate the class to instantiate
81 */
82 public InstantiateFactory(final Class<T> classToInstantiate) {
83 iClassToInstantiate = classToInstantiate;
84 iParamTypes = null;
85 iArgs = null;
86 findConstructor();
87 }
88
89 /**
90 * Constructor that performs no validation.
91 * Use {@code instantiateFactory} if you want that.
92 *
93 * @param classToInstantiate the class to instantiate
94 * @param paramTypes the constructor parameter types, cloned
95 * @param args the constructor arguments, cloned
96 */
97 public InstantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args) {
98 iClassToInstantiate = classToInstantiate;
99 iParamTypes = paramTypes.clone();
100 iArgs = args.clone();
101 findConstructor();
102 }
103
104 /**
105 * Creates an object using the stored constructor.
106 *
107 * @return the new object
108 */
109 @Override
110 public T create() {
111 // needed for post-serialization
112 if (iConstructor == null) {
113 findConstructor();
114 }
115
116 try {
117 return iConstructor.newInstance(iArgs);
118 } catch (final InstantiationException ex) {
119 throw new FunctorException("InstantiateFactory: InstantiationException", ex);
120 } catch (final IllegalAccessException ex) {
121 throw new FunctorException("InstantiateFactory: Constructor must be public", ex);
122 } catch (final InvocationTargetException ex) {
123 throw new FunctorException("InstantiateFactory: Constructor threw an exception", ex);
124 }
125 }
126
127 /**
128 * Find the Constructor for the class specified.
129 */
130 private void findConstructor() {
131 try {
132 iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
133 } catch (final NoSuchMethodException ex) {
134 throw new IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
135 }
136 }
137
138 }