1 7 package java.beans; 8 9 import java.lang.reflect.AccessibleObject ; 10 import java.lang.reflect.Array ; 11 import java.lang.reflect.Constructor ; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 14 15 import com.sun.beans.ObjectHandler; 16 import sun.reflect.misc.MethodUtil; 17 18 35 public class Statement { 36 37 private static Object [] emptyArray = new Object []{}; 38 39 static ExceptionListener defaultExceptionListener = new ExceptionListener () { 40 public void exceptionThrown(Exception e) { 41 System.err.println(e); 42 System.err.println("Continuing ..."); 44 } 45 }; 46 47 Object target; 48 String methodName; 49 Object [] arguments; 50 51 60 public Statement(Object target, String methodName, Object [] arguments) { 61 this.target = target; 62 this.methodName = methodName; 63 this.arguments = (arguments == null) ? emptyArray : arguments; 64 } 65 66 71 public Object getTarget() { 72 return target; 73 } 74 75 80 public String getMethodName() { 81 return methodName; 82 } 83 84 89 public Object [] getArguments() { 90 return arguments; 91 } 92 93 122 public void execute() throws Exception { 123 invoke(); 124 } 125 126 Object invoke() throws Exception { 127 Object target = getTarget(); 128 String methodName = getMethodName(); 129 130 if (target == null || methodName == null) { 131 throw new NullPointerException ((target == null ? "target" : 132 "methodName") + " should not be null"); 133 } 134 135 Object [] arguments = getArguments(); 136 if (target == Class .class && methodName.equals("forName")) { 140 return ObjectHandler.classForName((String )arguments[0]); 141 } 142 143 Class [] argClasses = new Class [arguments.length]; 144 for(int i = 0; i < arguments.length; i++) { 145 argClasses[i] = (arguments[i] == null) ? null : arguments[i].getClass(); 146 } 147 148 AccessibleObject m = null; 149 if (target instanceof Class ) { 150 160 if (methodName.equals("new")) { 161 methodName = "newInstance"; 162 } 163 if (methodName.equals("newInstance") && ((Class )target).isArray()) { 165 Object result = Array.newInstance(((Class )target).getComponentType(), arguments.length); 166 for(int i = 0; i < arguments.length; i++) { 167 Array.set(result, i, arguments[i]); 168 } 169 return result; 170 } 171 if (methodName.equals("newInstance") && arguments.length != 0) { 172 if (target == Character .class && arguments.length == 1 && 178 argClasses[0] == String .class) { 179 return new Character (((String )arguments[0]).charAt(0)); 180 } 181 m = ReflectionUtils.getConstructor((Class )target, argClasses); 182 } 183 if (m == null && target != Class .class) { 184 m = ReflectionUtils.getMethod((Class )target, methodName, argClasses); 185 } 186 if (m == null) { 187 m = ReflectionUtils.getMethod(Class .class, methodName, argClasses); 188 } 189 } 190 else { 191 199 if (target.getClass().isArray() && 200 (methodName.equals("set") || methodName.equals("get"))) { 201 int index = ((Integer )arguments[0]).intValue(); 202 if (methodName.equals("get")) { 203 return Array.get(target, index); 204 } 205 else { 206 Array.set(target, index, arguments[1]); 207 return null; 208 } 209 } 210 m = ReflectionUtils.getMethod(target.getClass(), methodName, argClasses); 211 } 212 if (m != null) { 213 try { 214 if (m instanceof Method ) { 215 return MethodUtil.invoke((Method )m, target, arguments); 216 } 217 else { 218 return ((Constructor )m).newInstance(arguments); 219 } 220 } 221 catch (IllegalAccessException iae) { 222 throw new Exception ("Statement cannot invoke: " + 223 methodName + " on " + target.getClass(), 224 iae); 225 } 226 catch (InvocationTargetException ite) { 227 Throwable te = ite.getTargetException(); 228 if (te instanceof Exception ) { 229 throw (Exception )te; 230 } 231 else { 232 throw ite; 233 } 234 } 235 } 236 throw new NoSuchMethodException (toString()); 237 } 238 239 String instanceName(Object instance) { 240 if (instance == null) { 241 return "null"; 242 } else if (instance.getClass() == String .class) { 243 return "\""+(String )instance + "\""; 244 } else { 245 251 return NameGenerator.unqualifiedClassName(instance.getClass()); 252 } 253 } 254 255 258 public String toString() { 259 Object target = getTarget(); 261 String methodName = getMethodName(); 262 Object [] arguments = getArguments(); 263 264 StringBuffer result = new StringBuffer (instanceName(target) + "." + methodName + "("); 265 int n = arguments.length; 266 for(int i = 0; i < n; i++) { 267 result.append(instanceName(arguments[i])); 268 if (i != n -1) { 269 result.append(", "); 270 } 271 } 272 result.append(");"); 273 return result.toString(); 274 } 275 } 276 | Popular Tags |