001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.functors;
017:
018: import java.io.Serializable;
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.InvocationTargetException;
021:
022: import org.apache.commons.collections.Factory;
023: import org.apache.commons.collections.FunctorException;
024:
025: /**
026: * Factory implementation that creates a new object instance by reflection.
027: *
028: * @since Commons Collections 3.0
029: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
030: *
031: * @author Stephen Colebourne
032: */
033: public class InstantiateFactory implements Factory, Serializable {
034:
035: /** The serial version */
036: private static final long serialVersionUID = -7732226881069447957L;
037:
038: /** The class to create */
039: private final Class iClassToInstantiate;
040: /** The constructor parameter types */
041: private final Class[] iParamTypes;
042: /** The constructor arguments */
043: private final Object[] iArgs;
044: /** The constructor */
045: private transient Constructor iConstructor = null;
046:
047: /**
048: * Factory method that performs validation.
049: *
050: * @param classToInstantiate the class to instantiate, not null
051: * @param paramTypes the constructor parameter types
052: * @param args the constructor arguments
053: * @return a new instantiate factory
054: */
055: public static Factory getInstance(Class classToInstantiate,
056: Class[] paramTypes, Object[] args) {
057: if (classToInstantiate == null) {
058: throw new IllegalArgumentException(
059: "Class to instantiate must not be null");
060: }
061: if (((paramTypes == null) && (args != null))
062: || ((paramTypes != null) && (args == null))
063: || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
064: throw new IllegalArgumentException(
065: "Parameter types must match the arguments");
066: }
067:
068: if (paramTypes == null || paramTypes.length == 0) {
069: return new InstantiateFactory(classToInstantiate);
070: } else {
071: paramTypes = (Class[]) paramTypes.clone();
072: args = (Object[]) args.clone();
073: return new InstantiateFactory(classToInstantiate,
074: paramTypes, args);
075: }
076: }
077:
078: /**
079: * Constructor that performs no validation.
080: * Use <code>getInstance</code> if you want that.
081: *
082: * @param classToInstantiate the class to instantiate
083: */
084: public InstantiateFactory(Class classToInstantiate) {
085: super ();
086: iClassToInstantiate = classToInstantiate;
087: iParamTypes = null;
088: iArgs = null;
089: findConstructor();
090: }
091:
092: /**
093: * Constructor that performs no validation.
094: * Use <code>getInstance</code> if you want that.
095: *
096: * @param classToInstantiate the class to instantiate
097: * @param paramTypes the constructor parameter types, not cloned
098: * @param args the constructor arguments, not cloned
099: */
100: public InstantiateFactory(Class classToInstantiate,
101: Class[] paramTypes, Object[] args) {
102: super ();
103: iClassToInstantiate = classToInstantiate;
104: iParamTypes = paramTypes;
105: iArgs = args;
106: findConstructor();
107: }
108:
109: /**
110: * Find the Constructor for the class specified.
111: */
112: private void findConstructor() {
113: try {
114: iConstructor = iClassToInstantiate
115: .getConstructor(iParamTypes);
116:
117: } catch (NoSuchMethodException ex) {
118: throw new IllegalArgumentException(
119: "InstantiateFactory: The constructor must exist and be public ");
120: }
121: }
122:
123: /**
124: * Creates an object using the stored constructor.
125: *
126: * @return the new object
127: */
128: public Object create() {
129: // needed for post-serialization
130: if (iConstructor == null) {
131: findConstructor();
132: }
133:
134: try {
135: return iConstructor.newInstance(iArgs);
136:
137: } catch (InstantiationException ex) {
138: throw new FunctorException(
139: "InstantiateFactory: InstantiationException", ex);
140: } catch (IllegalAccessException ex) {
141: throw new FunctorException(
142: "InstantiateFactory: Constructor must be public",
143: ex);
144: } catch (InvocationTargetException ex) {
145: throw new FunctorException(
146: "InstantiateFactory: Constructor threw an exception",
147: ex);
148: }
149: }
150:
151: }
|