KickJava   Java API By Example, From Geeks To Geeks.

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


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.FunctorException;
23 import org.apache.commons.collections.Transformer;
24
25 /**
26  * Transformer 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:36:31 $
30  *
31  * @author Stephen Colebourne
32  */

33 public class InstantiateTransformer implements Transformer, Serializable JavaDoc {
34
35     /** The serial version */
36     static final long serialVersionUID = 3786388740793356347L;
37     
38     /** Singleton instance that uses the no arg constructor */
39     public static final Transformer NO_ARG_INSTANCE = new InstantiateTransformer();
40
41     /** The constructor parameter types */
42     private final Class JavaDoc[] iParamTypes;
43     /** The constructor arguments */
44     private final Object JavaDoc[] iArgs;
45
46     /**
47      * Transformer method that performs validation.
48      *
49      * @param paramTypes the constructor parameter types
50      * @param args the constructor arguments
51      * @return an instantiate transformer
52      */

53     public static Transformer getInstance(Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
54         if (((paramTypes == null) && (args != null))
55             || ((paramTypes != null) && (args == null))
56             || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
57             throw new IllegalArgumentException JavaDoc("Parameter types must match the arguments");
58         }
59
60         if (paramTypes == null || paramTypes.length == 0) {
61             return NO_ARG_INSTANCE;
62         } else {
63             paramTypes = (Class JavaDoc[]) paramTypes.clone();
64             args = (Object JavaDoc[]) args.clone();
65         }
66         return new InstantiateTransformer(paramTypes, args);
67     }
68
69     /**
70      * Constructor for no arg instance.
71      */

72     private InstantiateTransformer() {
73         super();
74         iParamTypes = null;
75         iArgs = null;
76     }
77
78     /**
79      * Constructor that performs no validation.
80      * Use <code>getInstance</code> if you want that.
81      *
82      * @param paramTypes the constructor parameter types, not cloned
83      * @param args the constructor arguments, not cloned
84      */

85     public InstantiateTransformer(Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
86         super();
87         iParamTypes = paramTypes;
88         iArgs = args;
89     }
90
91     /**
92      * Transforms the input Class object to a result by instantiation.
93      *
94      * @param input the input object to transform
95      * @return the transformed result
96      */

97     public Object JavaDoc transform(Object JavaDoc input) {
98         try {
99             if (input instanceof Class JavaDoc == false) {
100                 throw new FunctorException(
101                     "InstantiateTransformer: Input object was not an instanceof Class, it was a "
102                         + (input == null ? "null object" : input.getClass().getName()));
103             }
104             Constructor JavaDoc con = ((Class JavaDoc) input).getConstructor(iParamTypes);
105             return con.newInstance(iArgs);
106
107         } catch (NoSuchMethodException JavaDoc ex) {
108             throw new FunctorException("InstantiateTransformer: The constructor must exist and be public ");
109         } catch (InstantiationException JavaDoc ex) {
110             throw new FunctorException("InstantiateTransformer: InstantiationException", ex);
111         } catch (IllegalAccessException JavaDoc ex) {
112             throw new FunctorException("InstantiateTransformer: Constructor must be public", ex);
113         } catch (InvocationTargetException JavaDoc ex) {
114             throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex);
115         }
116     }
117
118 }
119
Popular Tags