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: }
|