Source Code Cross Referenced for InvokerTransformer.java in  » Library » Apache-common-Collections » org » apache » commons » collections » functors » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common Collections » org.apache.commons.collections.functors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.InvocationTargetException;
020:        import java.lang.reflect.Method;
021:
022:        import org.apache.commons.collections.FunctorException;
023:        import org.apache.commons.collections.Transformer;
024:
025:        /**
026:         * Transformer 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 InvokerTransformer implements  Transformer, Serializable {
034:
035:            /** The serial version */
036:            private static final long serialVersionUID = -8653385846894047688L;
037:
038:            /** The method name to call */
039:            private final String iMethodName;
040:            /** The array of reflection parameter types */
041:            private final Class[] iParamTypes;
042:            /** The array of reflection arguments */
043:            private final Object[] iArgs;
044:
045:            /**
046:             * Gets an instance of this transformer calling a specific method with no arguments.
047:             * 
048:             * @param methodName  the method name to call
049:             * @return an invoker transformer
050:             * @since Commons Collections 3.1
051:             */
052:            public static Transformer getInstance(String methodName) {
053:                if (methodName == null) {
054:                    throw new IllegalArgumentException(
055:                            "The method to invoke must not be null");
056:                }
057:                return new InvokerTransformer(methodName);
058:            }
059:
060:            /**
061:             * Gets an instance of this transformer calling a specific method with specific values.
062:             * 
063:             * @param methodName  the method name to call
064:             * @param paramTypes  the parameter types of the method
065:             * @param args  the arguments to pass to the method
066:             * @return an invoker transformer
067:             */
068:            public static Transformer getInstance(String methodName,
069:                    Class[] paramTypes, Object[] args) {
070:                if (methodName == null) {
071:                    throw new IllegalArgumentException(
072:                            "The method to invoke must not be null");
073:                }
074:                if (((paramTypes == null) && (args != null))
075:                        || ((paramTypes != null) && (args == null))
076:                        || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
077:                    throw new IllegalArgumentException(
078:                            "The parameter types must match the arguments");
079:                }
080:                if (paramTypes == null || paramTypes.length == 0) {
081:                    return new InvokerTransformer(methodName);
082:                } else {
083:                    paramTypes = (Class[]) paramTypes.clone();
084:                    args = (Object[]) args.clone();
085:                    return new InvokerTransformer(methodName, paramTypes, args);
086:                }
087:            }
088:
089:            /**
090:             * Constructor for no arg instance.
091:             * 
092:             * @param methodName  the method to call
093:             */
094:            private InvokerTransformer(String methodName) {
095:                super ();
096:                iMethodName = methodName;
097:                iParamTypes = null;
098:                iArgs = null;
099:            }
100:
101:            /**
102:             * Constructor that performs no validation.
103:             * Use <code>getInstance</code> if you want that.
104:             * 
105:             * @param methodName  the method to call
106:             * @param paramTypes  the constructor parameter types, not cloned
107:             * @param args  the constructor arguments, not cloned
108:             */
109:            public InvokerTransformer(String methodName, Class[] paramTypes,
110:                    Object[] args) {
111:                super ();
112:                iMethodName = methodName;
113:                iParamTypes = paramTypes;
114:                iArgs = args;
115:            }
116:
117:            /**
118:             * Transforms the input to result by invoking a method on the input.
119:             * 
120:             * @param input  the input object to transform
121:             * @return the transformed result, null if null input
122:             */
123:            public Object transform(Object input) {
124:                if (input == null) {
125:                    return null;
126:                }
127:                try {
128:                    Class cls = input.getClass();
129:                    Method method = cls.getMethod(iMethodName, iParamTypes);
130:                    return method.invoke(input, iArgs);
131:
132:                } catch (NoSuchMethodException ex) {
133:                    throw new FunctorException(
134:                            "InvokerTransformer: The method '" + iMethodName
135:                                    + "' on '" + input.getClass()
136:                                    + "' does not exist");
137:                } catch (IllegalAccessException ex) {
138:                    throw new FunctorException(
139:                            "InvokerTransformer: The method '" + iMethodName
140:                                    + "' on '" + input.getClass()
141:                                    + "' cannot be accessed");
142:                } catch (InvocationTargetException ex) {
143:                    throw new FunctorException(
144:                            "InvokerTransformer: The method '" + iMethodName
145:                                    + "' on '" + input.getClass()
146:                                    + "' threw an exception", ex);
147:                }
148:            }
149:
150:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.