KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > InstantiateFactory


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
18 import java.io.Serializable JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21
22 import org.apache.commons.collections.Factory;
23 import org.apache.commons.collections.FunctorException;
24
25 /**
26  * Factory implementation that creates a new object instance by reflection.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.6 $ $Date: 2004/05/16 11:47:38 $
30  *
31  * @author Stephen Colebourne
32  */

33 public class InstantiateFactory implements Factory, Serializable JavaDoc {
34
35     /** The serial version */
36     static final long serialVersionUID = -7732226881069447957L;
37
38     /** The class to create */
39     private final Class JavaDoc iClassToInstantiate;
40     /** The constructor parameter types */
41     private final Class JavaDoc[] iParamTypes;
42     /** The constructor arguments */
43     private final Object JavaDoc[] iArgs;
44     /** The constructor */
45     private transient Constructor JavaDoc iConstructor = null;
46
47     /**
48      * Factory method that performs validation.
49      *
50      * @param classToInstantiate the class to instantiate, not null
51      * @param paramTypes the constructor parameter types
52      * @param args the constructor arguments
53      * @return a new instantiate factory
54      */

55     public static Factory getInstance(Class JavaDoc classToInstantiate, Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
56         if (classToInstantiate == null) {
57             throw new IllegalArgumentException JavaDoc("Class to instantiate must not be null");
58         }
59         if (((paramTypes == null) && (args != null))
60             || ((paramTypes != null) && (args == null))
61             || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
62             throw new IllegalArgumentException JavaDoc("Parameter types must match the arguments");
63         }
64
65         if (paramTypes == null || paramTypes.length == 0) {
66             return new InstantiateFactory(classToInstantiate);
67         } else {
68             paramTypes = (Class JavaDoc[]) paramTypes.clone();
69             args = (Object JavaDoc[]) args.clone();
70             return new InstantiateFactory(classToInstantiate, paramTypes, args);
71         }
72     }
73
74     /**
75      * Constructor that performs no validation.
76      * Use <code>getInstance</code> if you want that.
77      *
78      * @param classToInstantiate the class to instantiate
79      */

80     public InstantiateFactory(Class JavaDoc classToInstantiate) {
81         super();
82         iClassToInstantiate = classToInstantiate;
83         iParamTypes = null;
84         iArgs = null;
85         findConstructor();
86     }
87
88     /**
89      * Constructor that performs no validation.
90      * Use <code>getInstance</code> if you want that.
91      *
92      * @param classToInstantiate the class to instantiate
93      * @param paramTypes the constructor parameter types, not cloned
94      * @param args the constructor arguments, not cloned
95      */

96     public InstantiateFactory(Class JavaDoc classToInstantiate, Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
97         super();
98         iClassToInstantiate = classToInstantiate;
99         iParamTypes = paramTypes;
100         iArgs = args;
101         findConstructor();
102     }
103
104     /**
105      * Find the Constructor for the class specified.
106      */

107     private void findConstructor() {
108         try {
109             iConstructor = iClassToInstantiate.getConstructor(iParamTypes);
110
111         } catch (NoSuchMethodException JavaDoc ex) {
112             throw new IllegalArgumentException JavaDoc("InstantiateFactory: The constructor must exist and be public ");
113         }
114     }
115
116     /**
117      * Creates an object using the stored constructor.
118      *
119      * @return the new object
120      */

121     public Object JavaDoc create() {
122         // needed for post-serialization
123
if (iConstructor == null) {
124             findConstructor();
125         }
126
127         try {
128             return iConstructor.newInstance(iArgs);
129
130         } catch (InstantiationException JavaDoc ex) {
131             throw new FunctorException("InstantiateFactory: InstantiationException", ex);
132         } catch (IllegalAccessException JavaDoc ex) {
133             throw new FunctorException("InstantiateFactory: Constructor must be public", ex);
134         } catch (InvocationTargetException JavaDoc ex) {
135             throw new FunctorException("InstantiateFactory: Constructor threw an exception", ex);
136         }
137     }
138     
139 }
140
Popular Tags