1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.functors;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Constructor;
  19. import java.lang.reflect.InvocationTargetException;
  20. import org.apache.commons.collections.FunctorException;
  21. import org.apache.commons.collections.Transformer;
  22. /**
  23. * Transformer implementation that creates a new object instance by reflection.
  24. *
  25. * @since Commons Collections 3.0
  26. * @version $Revision: 1.6 $ $Date: 2004/05/16 11:36:31 $
  27. *
  28. * @author Stephen Colebourne
  29. */
  30. public class InstantiateTransformer implements Transformer, Serializable {
  31. /** The serial version */
  32. static final long serialVersionUID = 3786388740793356347L;
  33. /** Singleton instance that uses the no arg constructor */
  34. public static final Transformer NO_ARG_INSTANCE = new InstantiateTransformer();
  35. /** The constructor parameter types */
  36. private final Class[] iParamTypes;
  37. /** The constructor arguments */
  38. private final Object[] iArgs;
  39. /**
  40. * Transformer method that performs validation.
  41. *
  42. * @param paramTypes the constructor parameter types
  43. * @param args the constructor arguments
  44. * @return an instantiate transformer
  45. */
  46. public static Transformer getInstance(Class[] paramTypes, Object[] args) {
  47. if (((paramTypes == null) && (args != null))
  48. || ((paramTypes != null) && (args == null))
  49. || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
  50. throw new IllegalArgumentException("Parameter types must match the arguments");
  51. }
  52. if (paramTypes == null || paramTypes.length == 0) {
  53. return NO_ARG_INSTANCE;
  54. } else {
  55. paramTypes = (Class[]) paramTypes.clone();
  56. args = (Object[]) args.clone();
  57. }
  58. return new InstantiateTransformer(paramTypes, args);
  59. }
  60. /**
  61. * Constructor for no arg instance.
  62. */
  63. private InstantiateTransformer() {
  64. super();
  65. iParamTypes = null;
  66. iArgs = null;
  67. }
  68. /**
  69. * Constructor that performs no validation.
  70. * Use <code>getInstance</code> if you want that.
  71. *
  72. * @param paramTypes the constructor parameter types, not cloned
  73. * @param args the constructor arguments, not cloned
  74. */
  75. public InstantiateTransformer(Class[] paramTypes, Object[] args) {
  76. super();
  77. iParamTypes = paramTypes;
  78. iArgs = args;
  79. }
  80. /**
  81. * Transforms the input Class object to a result by instantiation.
  82. *
  83. * @param input the input object to transform
  84. * @return the transformed result
  85. */
  86. public Object transform(Object input) {
  87. try {
  88. if (input instanceof Class == false) {
  89. throw new FunctorException(
  90. "InstantiateTransformer: Input object was not an instanceof Class, it was a "
  91. + (input == null ? "null object" : input.getClass().getName()));
  92. }
  93. Constructor con = ((Class) input).getConstructor(iParamTypes);
  94. return con.newInstance(iArgs);
  95. } catch (NoSuchMethodException ex) {
  96. throw new FunctorException("InstantiateTransformer: The constructor must exist and be public ");
  97. } catch (InstantiationException ex) {
  98. throw new FunctorException("InstantiateTransformer: InstantiationException", ex);
  99. } catch (IllegalAccessException ex) {
  100. throw new FunctorException("InstantiateTransformer: Constructor must be public", ex);
  101. } catch (InvocationTargetException ex) {
  102. throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex);
  103. }
  104. }
  105. }