package com.cycling74.max; import java.lang.reflect.*; /** * Used to transform a java method into an Executable object. This * simplify the job of always implementing the executable * interface on all your objects since you can define it dynamically * with this class. *

 * 
 * class Myclass  {
 *    public void doit() {
 *		do_something_fun()
 *    }
 * }
 * 
 * ...
 * 
 *      Myclass myclass = new Myclass();
 *      Executable e = new Callback(myclass, "doit");
 *      MaxClock clock = new MaxClock(e);
 * 
 * 

*/ public class Callback implements Executable { private Method method; private String methodName; private Object obj; private Object args[]; /** * Will call method methodName with no argument by using execute() * @param obj the object with the method * @param methodName the name of the method */ public Callback(Object obj, String methodName) { this(obj, methodName, null, null); } /** * Will call method methodName with a int by using execute() * @param obj the object with the method * @param methodName the name of the method * @param i the int value */ public Callback(Object obj, String methodName, int i) { this(obj, methodName, new Object[] { new Integer(i) }, new Class[] { Integer.TYPE }); } /** * Will call method methodName with a float by using execute() * @param obj the object with the method * @param methodName the name of the method * @param f the float value */ public Callback(Object obj, String methodName, float f) { this(obj, methodName, new Object[] { new Float(f) }, new Class[] { Float.TYPE }); } /** * Will call method methodName with a Stringt by using execute() * @param obj the object with the method * @param methodName the name of the method * @param str the string value */ public Callback(Object obj, String methodName, String str) { this(obj, methodName, new Object[] { str }); } /** * Will call method methodName with a boolean by using execute() * @param obj the object with the method * @param methodName the name of the method * @param flag the boolean value */ public Callback(Object obj, String methodName, boolean flag) { this(obj, methodName, new Object[] { flag ? Boolean.TRUE : Boolean.FALSE }); } /** * Will call method methodName with multiple arguments by using execute() * @param obj the object with the method * @param methodName the name of the method * @param params argument to pass to the method */ public Callback(Object obj, String methodName, Object params[]) { this(obj, methodName, params, buildClasses(params)); } /** * Will call method methodName with multiple arguments (typed) by using execute() * @param obj the object with the method * @param methodName the name of the method * @param params argument to pass to the method * @param params_types the type of arguments */ public Callback(Object obj, String methodName, Object params[], Class params_types[]) { try { if ( params == null ) { method = obj.getClass().getDeclaredMethod(methodName, null); } else { method = obj.getClass().getDeclaredMethod(methodName, params_types); } this.obj = obj; this.methodName = methodName; } catch ( NoSuchMethodException e ) { MaxSystem.post("pdj: unable to find method: " + methodName + ", "+ e); } } private static Class[] buildClasses(Object params[]) { Class clz[] = new Class[params.length]; for(int i=0;i