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  
22  import org.apache.commons.collections4.FunctorException;
23  import org.apache.commons.collections4.Transformer;
24  
25  /**
26   * Transformer implementation that creates a new object instance by reflection.
27   * <p>
28   * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> be serializable anymore
29   * in order to prevent potential remote code execution exploits. Please refer to
30   * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
31   * for more details.
32   * </p>
33   *
34   * @since 3.0
35   */
36  public class InstantiateTransformer<T> implements Transformer<Class<? extends T>, T> {
37  
38      /** Singleton instance that uses the no arg constructor */
39      @SuppressWarnings("rawtypes")
40      private static final Transformer NO_ARG_INSTANCE = new InstantiateTransformer<>();
41  
42      /**
43       * Gets a typed no-arg instance.
44       *
45       * @param <T>  the type of the objects to be created
46       * @return Transformer&lt;Class&lt;? extends T&gt;, T&gt;
47       */
48      public static <T> Transformer<Class<? extends T>, T> instantiateTransformer() {
49          return NO_ARG_INSTANCE;
50      }
51      /**
52       * Transformer method that performs validation.
53       *
54       * @param <T>  the type of the objects to be created
55       * @param paramTypes  the constructor parameter types
56       * @param args  the constructor arguments
57       * @return an instantiate transformer
58       * @throws IllegalArgumentException if paramTypes does not match args
59       */
60      public static <T> Transformer<Class<? extends T>, T> instantiateTransformer(final Class<?>[] paramTypes,
61                                                                                  final Object[] args) {
62          if (paramTypes == null && args != null
63              || paramTypes != null && args == null
64              || paramTypes != null && args != null && paramTypes.length != args.length) {
65              throw new IllegalArgumentException("Parameter types must match the arguments");
66          }
67  
68          if (paramTypes == null || paramTypes.length == 0) {
69              return new InstantiateTransformer<>();
70          }
71          return new InstantiateTransformer<>(paramTypes, args);
72      }
73  
74      /** The constructor parameter types */
75      private final Class<?>[] iParamTypes;
76  
77      /** The constructor arguments */
78      private final Object[] iArgs;
79  
80      /**
81       * Constructor for no arg instance.
82       */
83      private InstantiateTransformer() {
84          iParamTypes = null;
85          iArgs = null;
86      }
87  
88      /**
89       * Constructor that performs no validation.
90       * Use {@code instantiateTransformer} if you want that.
91       * <p>
92       * Note: from 4.0, the input parameters will be cloned
93       *
94       * @param paramTypes  the constructor parameter types
95       * @param args  the constructor arguments
96       */
97      public InstantiateTransformer(final Class<?>[] paramTypes, final Object[] args) {
98          iParamTypes = paramTypes != null ? paramTypes.clone() : null;
99          iArgs = args != null ? args.clone() : null;
100     }
101 
102     /**
103      * Transforms the input Class object to a result by instantiation.
104      *
105      * @param input  the input object to transform
106      * @return the transformed result
107      */
108     @Override
109     public T transform(final Class<? extends T> input) {
110         try {
111             if (input == null) {
112                 throw new FunctorException(
113                     "InstantiateTransformer: Input object was not an instanceof Class, it was a null object");
114             }
115             final Constructor<? extends T> con = input.getConstructor(iParamTypes);
116             return con.newInstance(iArgs);
117         } catch (final NoSuchMethodException ex) {
118             throw new FunctorException("InstantiateTransformer: The constructor must exist and be public ");
119         } catch (final InstantiationException ex) {
120             throw new FunctorException("InstantiateTransformer: InstantiationException", ex);
121         } catch (final IllegalAccessException ex) {
122             throw new FunctorException("InstantiateTransformer: Constructor must be public", ex);
123         } catch (final InvocationTargetException ex) {
124             throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex);
125         }
126     }
127 
128 }