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