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 * https://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
67 /** The class to create */
68 private final Class<T> iClassToInstantiate;
69
70 /** The constructor parameter types */
71 private final Class<?>[] iParamTypes;
72
73 /** The constructor arguments */
74 private final Object[] iArgs;
75
76 /** The constructor */
77 private transient Constructor<T> iConstructor;
78
79 /**
80 * Constructor that performs no validation.
81 * Use {@code instantiateFactory} if you want that.
82 *
83 * @param classToInstantiate The class to instantiate
84 */
85 public InstantiateFactory(final Class<T> classToInstantiate) {
86 iClassToInstantiate = classToInstantiate;
87 iParamTypes = null;
88 iArgs = null;
89 findConstructor();
90 }
91
92 /**
93 * Constructor that performs no validation.
94 * Use {@code instantiateFactory} if you want that.
95 *
96 * @param classToInstantiate The class to instantiate
97 * @param paramTypes The constructor parameter types, cloned
98 * @param args The constructor arguments, cloned
99 */
100 public InstantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args) {
101 iClassToInstantiate = classToInstantiate;
102 iParamTypes = paramTypes.clone();
103 iArgs = args.clone();
104 findConstructor();
105 }
106
107 /**
108 * Creates an object using the stored constructor.
109 *
110 * @return The new object
111 */
112 @Override
113 public T create() {
114 // needed for post-serialization
115 if (iConstructor == null) {
116 findConstructor();
117 }
118
119 try {
120 return iConstructor.newInstance(iArgs);
121 } catch (final InstantiationException ex) {
122 throw new FunctorException("InstantiateFactory: InstantiationException", ex);
123 } catch (final IllegalAccessException ex) {
124 throw new FunctorException("InstantiateFactory: Constructor must be public", ex);
125 } catch (final InvocationTargetException ex) {
126 throw new FunctorException("InstantiateFactory: Constructor threw an exception", ex);
127 }
128 }
129
130 /**
131 * Find the Constructor for the class specified.
132 */
133 private void findConstructor() {
134 try {
135 iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
136 } catch (final NoSuchMethodException ex) {
137 throw new IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
138 }
139 }
140
141 }