Source Code Cross Referenced for ClassReference.java in  » IDE-Netbeans » jemmy » org » netbeans » jemmy » 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 » IDE Netbeans » jemmy » org.netbeans.jemmy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s): Alexandre Iline.
025:         *
026:         * The Original Software is the Jemmy library.
027:         * The Initial Developer of the Original Software is Alexandre Iline.
028:         * All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         *
041:         *
042:         *
043:         * $Id$ $Revision$ $Date$
044:         *
045:         */
046:
047:        package org.netbeans.jemmy;
048:
049:        import java.lang.reflect.InvocationTargetException;
050:        import java.lang.reflect.Constructor;
051:        import java.lang.reflect.Method;
052:
053:        /**
054:         *
055:         * Allows access to classes by reflection.
056:         *	
057:         * @author Alexandre Iline (alexandre.iline@sun.com)
058:         */
059:
060:        public class ClassReference {
061:
062:            private Class cl;
063:            private Object instance;
064:
065:            /**
066:             * Constructor.
067:             * @param o Object to work with.
068:             */
069:            public ClassReference(Object o) {
070:                super ();
071:                instance = o;
072:                cl = o.getClass();
073:            }
074:
075:            /**
076:             * Contructor.
077:             * The object created by this constructor can be used
078:             * to access static methods and fields only.
079:             * 
080:             * @param	className name of class
081:             * @exception	ClassNotFoundException
082:             */
083:            public ClassReference(String className)
084:                    throws ClassNotFoundException {
085:                super ();
086:                cl = Class.forName(className);
087:                instance = null;
088:            }
089:
090:            /**
091:             * Executes class's <code>main(java.lang.String[])</code> method
092:             * with a zero-length <code>java.lang.String</code> array
093:             * as a parameter.
094:             * 
095:             * @exception	NoSuchMethodException
096:             * @exception	InvocationTargetException
097:             */
098:            public void startApplication() throws InvocationTargetException,
099:                    NoSuchMethodException {
100:                String[] params = new String[0];
101:                startApplication(params);
102:            }
103:
104:            /**
105:             * Executes class's <code>main(java.lang.String[])</code> method.
106:             * 
107:             * @param	params The <code>java.lang.String</code> array to pass
108:             * to <code>main(java.lang.String[])</code>.
109:             * @exception	NoSuchMethodException
110:             * @exception	InvocationTargetException
111:             */
112:            public void startApplication(String[] params)
113:                    throws InvocationTargetException, NoSuchMethodException {
114:                String[] real_params;
115:                if (params == null) {
116:                    real_params = new String[0];
117:                } else {
118:                    real_params = params;
119:                }
120:                String[][] methodParams = { real_params };
121:                Class[] classes = { real_params.getClass() };
122:                try {
123:                    invokeMethod("main", methodParams, classes);
124:                } catch (IllegalAccessException e) {
125:                    e.printStackTrace();
126:                } catch (IllegalStateException e) {
127:                    e.printStackTrace();
128:                }
129:            }
130:
131:            /**
132:             * Locates method by name and parameter types and executes it.
133:             * 
134:             * @param	method_name Name of method.
135:             * @param	params Method parameters.
136:             * @param	params_classes Method parameters types.
137:             * @return	the return value from an invocation of the Method.<BR>
138:             * If <code>method_name</code> method is void, <code>null</code> is returned.<BR>
139:             * If <code>method_name</code> method returns a primitive type, then
140:             * return wrapper class instance.
141:             * @throws	InvocationTargetException when the invoked method throws an exception.
142:             * @throws	NoSuchMethodException when the method cannot be found.
143:             * @throws	IllegalAccessException when access to the class or method is lacking.
144:             * @throws	SecurityException if access to the package or method is denied.
145:             * @exception	IllegalAccessException
146:             * @exception	NoSuchMethodException
147:             * @exception	InvocationTargetException
148:             */
149:            public Object invokeMethod(String method_name, Object[] params,
150:                    Class[] params_classes) throws InvocationTargetException,
151:                    NoSuchMethodException, IllegalAccessException {
152:                if (params == null) {
153:                    params = new Object[0];
154:                }
155:                if (params_classes == null) {
156:                    params_classes = new Class[0];
157:                }
158:                Method method = cl.getMethod(method_name, params_classes);
159:                return (method.invoke(instance, params));
160:            }
161:
162:            /**
163:             * Locates constructor by parameter types and creates an instance.
164:             * 
165:             * @param	params An array of Method parameters.
166:             * @param	params_classes An array of Method parameter types.
167:             * @return	a new class instance.
168:             * @throws	InvocationTargetException when the invoked constructor throws an exception.
169:             * @throws	NoSuchMethodException when the constructor cannot be found.
170:             * @throws	IllegalAccessException when access to the class or constructor is lacking.
171:             * @throws	InstantiationException when the constructor is for an abstract class.
172:             * @throws	SecurityException if access to the package or constructor is denied.
173:             * @exception	IllegalAccessException
174:             * @exception	NoSuchMethodException
175:             * @exception	InstantiationException
176:             * @exception	InvocationTargetException
177:             */
178:            public Object newInstance(Object[] params, Class[] params_classes)
179:                    throws InvocationTargetException, NoSuchMethodException,
180:                    IllegalAccessException, InstantiationException {
181:                if (params == null) {
182:                    params = new Object[0];
183:                }
184:                if (params_classes == null) {
185:                    params_classes = new Class[0];
186:                }
187:                Constructor constructor = cl.getConstructor(params_classes);
188:                return (constructor.newInstance(params));
189:            }
190:
191:            /**
192:             * Returns the field value.
193:             * @param	field_name The name of the field.
194:             * @return	the field value
195:             * @see #setField
196:             * @throws	NoSuchFieldException when the field cannot be found.
197:             * @throws	IllegalAccessException when access to the class or constructor is lacking.
198:             * @throws	SecurityException if access to the package or field is denied.
199:             * @exception	IllegalAccessException
200:             * @exception	NoSuchFieldException
201:             */
202:            public Object getField(String field_name)
203:                    throws NoSuchFieldException, IllegalAccessException {
204:                return (cl.getField(field_name).get(instance));
205:            }
206:
207:            /**
208:             * Change a field's value.
209:             * 
210:             * @param	field_name The name of the field.
211:             * @param	newValue The fields new value.
212:             * @see #getField
213:             * @throws	NoSuchFieldException when the field cannot be found.
214:             * @throws	IllegalAccessException when access to the class or constructor is lacking.
215:             * @throws	SecurityException if access to the package or field is denied.
216:             * @exception	IllegalAccessException
217:             * @exception	NoSuchFieldException
218:             */
219:            public void setField(String field_name, Object newValue)
220:                    throws NoSuchFieldException, IllegalAccessException {
221:                cl.getField(field_name).set(instance, newValue);
222:            }
223:
224:            /**
225:             * Returns all superclasses.
226:             * @return an array of superclasses, starting with the reference class
227:             * and ending with <code>java.lang.Object</code>.
228:             */
229:            public Class[] getClasses() {
230:                Class cls = cl;
231:                int count = 0;
232:                do {
233:                    count++;
234:                    cls = cls.getSuperclass();
235:                } while (cls != null);
236:                Class[] result = new Class[count];
237:                cls = cl;
238:                for (int i = 0; i < count; i++) {
239:                    result[i] = cls;
240:                    cls = cls.getSuperclass();
241:                }
242:                return (result);
243:            }
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.