KickJava   Java API By Example, From Geeks To Geeks.

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


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.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method 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.7 $ $Date: 2004/05/26 21:44:05 $
30  *
31  * @author Stephen Colebourne
32  */

33 public class InvokerTransformer implements Transformer, Serializable JavaDoc {
34
35     /** The serial version */
36     static final long serialVersionUID = -8653385846894047688L;
37     
38     /** The method name to call */
39     private final String JavaDoc iMethodName;
40     /** The array of reflection parameter types */
41     private final Class JavaDoc[] iParamTypes;
42     /** The array of reflection arguments */
43     private final Object JavaDoc[] iArgs;
44
45     /**
46      * Gets an instance of this transformer calling a specific method with no arguments.
47      *
48      * @param methodName the method name to call
49      * @return an invoker transformer
50      * @since Commons Collections 3.1
51      */

52     public static Transformer getInstance(String JavaDoc methodName) {
53         if (methodName == null) {
54             throw new IllegalArgumentException JavaDoc("The method to invoke must not be null");
55         }
56         return new InvokerTransformer(methodName);
57     }
58
59     /**
60      * Gets an instance of this transformer calling a specific method with specific values.
61      *
62      * @param methodName the method name to call
63      * @param paramTypes the parameter types of the method
64      * @param args the arguments to pass to the method
65      * @return an invoker transformer
66      */

67     public static Transformer getInstance(String JavaDoc methodName, Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
68         if (methodName == null) {
69             throw new IllegalArgumentException JavaDoc("The method to invoke must not be null");
70         }
71         if (((paramTypes == null) && (args != null))
72             || ((paramTypes != null) && (args == null))
73             || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
74             throw new IllegalArgumentException JavaDoc("The parameter types must match the arguments");
75         }
76         if (paramTypes == null || paramTypes.length == 0) {
77             return new InvokerTransformer(methodName);
78         } else {
79             paramTypes = (Class JavaDoc[]) paramTypes.clone();
80             args = (Object JavaDoc[]) args.clone();
81             return new InvokerTransformer(methodName, paramTypes, args);
82         }
83     }
84
85     /**
86      * Constructor for no arg instance.
87      *
88      * @param methodName the method to call
89      */

90     private InvokerTransformer(String JavaDoc methodName) {
91         super();
92         iMethodName = methodName;
93         iParamTypes = null;
94         iArgs = null;
95     }
96
97     /**
98      * Constructor that performs no validation.
99      * Use <code>getInstance</code> if you want that.
100      *
101      * @param methodName the method to call
102      * @param paramTypes the constructor parameter types, not cloned
103      * @param args the constructor arguments, not cloned
104      */

105     public InvokerTransformer(String JavaDoc methodName, Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
106         super();
107         iMethodName = methodName;
108         iParamTypes = paramTypes;
109         iArgs = args;
110     }
111
112     /**
113      * Transforms the input to result by invoking a method on the input.
114      *
115      * @param input the input object to transform
116      * @return the transformed result, null if null input
117      */

118     public Object JavaDoc transform(Object JavaDoc input) {
119         if (input == null) {
120             return null;
121         }
122         try {
123             Class JavaDoc cls = input.getClass();
124             Method JavaDoc method = cls.getMethod(iMethodName, iParamTypes);
125             return method.invoke(input, iArgs);
126                 
127         } catch (NoSuchMethodException JavaDoc ex) {
128             throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist");
129         } catch (IllegalAccessException JavaDoc ex) {
130             throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed");
131         } catch (InvocationTargetException JavaDoc ex) {
132             throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex);
133         }
134     }
135
136 }
137
Popular Tags