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.Factory;
  21. import org.apache.commons.collections.FunctorException;
  22. /**
  23. * Factory 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:47:38 $
  27. *
  28. * @author Stephen Colebourne
  29. */
  30. public class InstantiateFactory implements Factory, Serializable {
  31. /** The serial version */
  32. static final long serialVersionUID = -7732226881069447957L;
  33. /** The class to create */
  34. private final Class iClassToInstantiate;
  35. /** The constructor parameter types */
  36. private final Class[] iParamTypes;
  37. /** The constructor arguments */
  38. private final Object[] iArgs;
  39. /** The constructor */
  40. private transient Constructor iConstructor = null;
  41. /**
  42. * Factory method that performs validation.
  43. *
  44. * @param classToInstantiate the class to instantiate, not null
  45. * @param paramTypes the constructor parameter types
  46. * @param args the constructor arguments
  47. * @return a new instantiate factory
  48. */
  49. public static Factory getInstance(Class classToInstantiate, Class[] paramTypes, Object[] args) {
  50. if (classToInstantiate == null) {
  51. throw new IllegalArgumentException("Class to instantiate must not be null");
  52. }
  53. if (((paramTypes == null) && (args != null))
  54. || ((paramTypes != null) && (args == null))
  55. || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
  56. throw new IllegalArgumentException("Parameter types must match the arguments");
  57. }
  58. if (paramTypes == null || paramTypes.length == 0) {
  59. return new InstantiateFactory(classToInstantiate);
  60. } else {
  61. paramTypes = (Class[]) paramTypes.clone();
  62. args = (Object[]) args.clone();
  63. return new InstantiateFactory(classToInstantiate, paramTypes, args);
  64. }
  65. }
  66. /**
  67. * Constructor that performs no validation.
  68. * Use <code>getInstance</code> if you want that.
  69. *
  70. * @param classToInstantiate the class to instantiate
  71. */
  72. public InstantiateFactory(Class classToInstantiate) {
  73. super();
  74. iClassToInstantiate = classToInstantiate;
  75. iParamTypes = null;
  76. iArgs = null;
  77. findConstructor();
  78. }
  79. /**
  80. * Constructor that performs no validation.
  81. * Use <code>getInstance</code> if you want that.
  82. *
  83. * @param classToInstantiate the class to instantiate
  84. * @param paramTypes the constructor parameter types, not cloned
  85. * @param args the constructor arguments, not cloned
  86. */
  87. public InstantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) {
  88. super();
  89. iClassToInstantiate = classToInstantiate;
  90. iParamTypes = paramTypes;
  91. iArgs = args;
  92. findConstructor();
  93. }
  94. /**
  95. * Find the Constructor for the class specified.
  96. */
  97. private void findConstructor() {
  98. try {
  99. iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
  100. } catch (NoSuchMethodException ex) {
  101. throw new IllegalArgumentException("InstantiateFactory: The constructor must exist and be public ");
  102. }
  103. }
  104. /**
  105. * Creates an object using the stored constructor.
  106. *
  107. * @return the new object
  108. */
  109. public Object create() {
  110. // needed for post-serialization
  111. if (iConstructor == null) {
  112. findConstructor();
  113. }
  114. try {
  115. return iConstructor.newInstance(iArgs);
  116. } catch (InstantiationException ex) {
  117. throw new FunctorException("InstantiateFactory: InstantiationException", ex);
  118. } catch (IllegalAccessException ex) {
  119. throw new FunctorException("InstantiateFactory: Constructor must be public", ex);
  120. } catch (InvocationTargetException ex) {
  121. throw new FunctorException("InstantiateFactory: Constructor threw an exception", ex);
  122. }
  123. }
  124. }