1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.jemmy.input; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Constructor; 27 import java.lang.reflect.Method; 28 29 /** 30 * 31 * Allows access to classes by reflection. 32 * 33 * @author Alexandre Iline (alexandre.iline@sun.com) 34 */ 35 public class ClassReference { 36 37 private Class<?> cl; 38 private Object instance; 39 40 /** 41 * Constructor. 42 * @param o Object to work with. 43 */ 44 public ClassReference(Object o) { 45 super(); 46 instance = o; 47 cl = o.getClass(); 48 } 49 50 /** 51 * Contructor. 52 * The object created by this constructor can be used 53 * to access static methods and fields only. 54 * 55 * @param className name of class 56 * @exception ClassNotFoundException 57 */ 58 public ClassReference(String className) 59 throws ClassNotFoundException { 60 super(); 61 cl = Class.forName(className); 62 instance = null; 63 } 64 65 /** 66 * Executes class's <code>main(java.lang.String[])</code> method 67 * with a zero-length <code>java.lang.String</code> array 68 * as a parameter. 69 * 70 * @exception NoSuchMethodException 71 * @exception InvocationTargetException 72 */ 73 public void startApplication() 74 throws InvocationTargetException, NoSuchMethodException { 75 String[] params = new String[0]; 76 startApplication(params); 77 } 78 79 /** 80 * Executes class's <code>main(java.lang.String[])</code> method. 81 * 82 * @param params The <code>java.lang.String</code> array to pass 83 * to <code>main(java.lang.String[])</code>. 84 * @exception NoSuchMethodException 85 * @exception InvocationTargetException 86 */ 87 public void startApplication(String[] params) 88 throws InvocationTargetException, NoSuchMethodException { 89 String[] real_params; 90 if (params == null) { 91 real_params = new String[0]; 92 } else { 93 real_params = params; 94 } 95 String[][] methodParams = {real_params}; 96 Class[] classes = {real_params.getClass()}; 97 try { 98 invokeMethod("main", methodParams, classes); 99 } catch (IllegalAccessException e) { 100 e.printStackTrace(); 101 } catch (IllegalStateException e) { 102 e.printStackTrace(); 103 } 104 } 105 106 /** 107 * Locates method by name and parameter types and executes it. 108 * 109 * @param method_name Name of method. 110 * @param params Method parameters. 111 * @param params_classes Method parameters types. 112 * @return the return value from an invocation of the Method.<BR> 113 * If <code>method_name</code> method is void, <code>null</code> is returned.<BR> 114 * If <code>method_name</code> method returns a primitive type, then 115 * return wrapper class instance. 116 * @throws InvocationTargetException when the invoked method throws an exception. 117 * @throws NoSuchMethodException when the method cannot be found. 118 * @throws IllegalAccessException when access to the class or method is lacking. 119 * @throws SecurityException if access to the package or method is denied. 120 */ 121 public Object invokeMethod(String method_name, Object[] params, Class<?>[] params_classes) 122 throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { 123 if (params == null) { 124 params = new Object[0]; 125 } 126 if (params_classes == null) { 127 params_classes = new Class<?>[0]; 128 } 129 Method method = cl.getMethod(method_name, 130 params_classes); 131 return (method.invoke(instance, params)); 132 } 133 134 /** 135 * Locates constructor by parameter types and creates an instance. 136 * 137 * @param params An array of Method parameters. 138 * @param params_classes An array of Method parameter types. 139 * @return a new class instance. 140 * @throws InvocationTargetException when the invoked constructor throws an exception. 141 * @throws NoSuchMethodException when the constructor cannot be found. 142 * @throws IllegalAccessException when access to the class or constructor is lacking. 143 * @throws InstantiationException when the constructor is for an abstract class. 144 * @throws SecurityException if access to the package or constructor is denied. 145 */ 146 public Object newInstance(Object[] params, Class[] params_classes) 147 throws InvocationTargetException, NoSuchMethodException, 148 IllegalAccessException, InstantiationException { 149 if (params == null) { 150 params = new Object[0]; 151 } 152 if (params_classes == null) { 153 params_classes = new Class[0]; 154 } 155 Constructor constructor = cl.getConstructor(params_classes); 156 return (constructor.newInstance(params)); 157 } 158 159 /** 160 * Returns the field value. 161 * @param field_name The name of the field. 162 * @return the field value 163 * @see #setField 164 * @throws NoSuchFieldException when the field cannot be found. 165 * @throws IllegalAccessException when access to the class or constructor is lacking. 166 * @throws SecurityException if access to the package or field is denied. 167 */ 168 public Object getField(String field_name) 169 throws NoSuchFieldException, IllegalAccessException { 170 return (cl.getField(field_name).get(instance)); 171 } 172 173 /** 174 * Change a field's value. 175 * 176 * @param field_name The name of the field. 177 * @param newValue The fields new value. 178 * @see #getField 179 * @throws NoSuchFieldException when the field cannot be found. 180 * @throws IllegalAccessException when access to the class or constructor is lacking. 181 * @throws SecurityException if access to the package or field is denied. 182 */ 183 public void setField(String field_name, Object newValue) 184 throws NoSuchFieldException, IllegalAccessException { 185 cl.getField(field_name).set(instance, newValue); 186 } 187 188 /** 189 * Returns all superclasses. 190 * @return an array of superclasses, starting with the reference class 191 * and ending with <code>java.lang.Object</code>. 192 */ 193 public Class[] getClasses() { 194 Class cls = cl; 195 int count = 0; 196 do { 197 count++; 198 cls = cls.getSuperclass(); 199 } while (cls != null); 200 Class[] result = new Class[count]; 201 cls = cl; 202 for (int i = 0; i < count; i++) { 203 result[i] = cls; 204 cls = cls.getSuperclass(); 205 } 206 return (result); 207 } 208 }