|
| 1 | +package jwf.debugport.commands; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | + |
| 6 | +import bsh.CallStack; |
| 7 | +import bsh.Interpreter; |
| 8 | +import jwf.debugport.annotations.Command; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + */ |
| 13 | +@Command |
| 14 | +public class call { |
| 15 | + /* |
| 16 | + * In order to support a varargs-like behavior when the command is called with no third |
| 17 | + * parameter, we have to supply several methods.. beanshell doesn't really support varargs well. |
| 18 | + */ |
| 19 | + public static Object invoke( |
| 20 | + Interpreter interpreter, |
| 21 | + CallStack callStack, |
| 22 | + Object object, |
| 23 | + String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 24 | + return invokeInner(object, methodName, null); |
| 25 | + } |
| 26 | + |
| 27 | + public static Object invoke( |
| 28 | + Interpreter interpreter, |
| 29 | + CallStack callStack, |
| 30 | + Object object, |
| 31 | + String methodName, |
| 32 | + Object p1) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 33 | + return invokeInner(object, methodName, p1); |
| 34 | + } |
| 35 | + |
| 36 | + public static Object invoke( |
| 37 | + Interpreter interpreter, |
| 38 | + CallStack callStack, |
| 39 | + Object object, |
| 40 | + String methodName, |
| 41 | + Object p1, |
| 42 | + Object p2) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 43 | + return invokeInner(object, methodName, p1, p2); |
| 44 | + } |
| 45 | + |
| 46 | + public static Object invoke( |
| 47 | + Interpreter interpreter, |
| 48 | + CallStack callStack, |
| 49 | + Object object, |
| 50 | + String methodName, |
| 51 | + Object p1, |
| 52 | + Object p2, |
| 53 | + Object p3) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 54 | + return invokeInner(object, methodName, p1, p2, p3); |
| 55 | + } |
| 56 | + |
| 57 | + public static Object invoke( |
| 58 | + Interpreter interpreter, |
| 59 | + CallStack callStack, |
| 60 | + Object object, |
| 61 | + String methodName, |
| 62 | + Object p1, |
| 63 | + Object p2, |
| 64 | + Object p3, |
| 65 | + Object p4) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 66 | + return invokeInner(object, methodName, p1, p2, p3, p4); |
| 67 | + } |
| 68 | + |
| 69 | + public static Object invoke( |
| 70 | + Interpreter interpreter, |
| 71 | + CallStack callStack, |
| 72 | + Object object, |
| 73 | + String methodName, |
| 74 | + Object p1, |
| 75 | + Object p2, |
| 76 | + Object p3, |
| 77 | + Object p4, |
| 78 | + Object p5) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 79 | + return invokeInner(object, methodName, p1, p2, p3, p4, p5); |
| 80 | + } |
| 81 | + |
| 82 | + public static Object invoke( |
| 83 | + Interpreter interpreter, |
| 84 | + CallStack callStack, |
| 85 | + Object object, |
| 86 | + String methodName, |
| 87 | + Object p1, |
| 88 | + Object p2, |
| 89 | + Object p3, |
| 90 | + Object p4, |
| 91 | + Object p5, |
| 92 | + Object p6) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 93 | + return invokeInner(object, methodName, p1, p2, p3, p4, p5, p6); |
| 94 | + } |
| 95 | + |
| 96 | + @Command.Help("Call a method, regardless of access modifiers, on the provided object.") |
| 97 | + public static Object invoke( |
| 98 | + Interpreter interpreter, |
| 99 | + CallStack callStack, |
| 100 | + @Command.ParamName("obj") Object object, |
| 101 | + @Command.ParamName("method") String methodName, |
| 102 | + @Command.ParamName("params") Object... params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 103 | + return invokeInner(object, methodName, params); |
| 104 | + } |
| 105 | + |
| 106 | + private static Object invokeInner( |
| 107 | + Object object, |
| 108 | + String methodName, |
| 109 | + Object... params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 110 | + Method method; |
| 111 | + Class<?>[] paramClasses = new Class<?>[0]; |
| 112 | + if (params != null) { |
| 113 | + paramClasses = new Class<?>[params.length]; |
| 114 | + for (int i = 0; i < params.length; i++) { |
| 115 | + paramClasses[i] = params[i].getClass(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + method = object.getClass().getDeclaredMethod(methodName, paramClasses); |
| 121 | + } catch (NoSuchMethodException e) { |
| 122 | + // try a method on a parent of the object's class |
| 123 | + method = object.getClass().getMethod(methodName, paramClasses); |
| 124 | + } |
| 125 | + method.setAccessible(true); |
| 126 | + return method.invoke(object, params); |
| 127 | + } |
| 128 | +} |
0 commit comments