Java中大家对addListener之类的方法必须实现一个Listener接口的类(匿名类或内部类),感觉很麻烦,甚至吵着要Java改语法,实现一级函数,Java 8 的lambda表达式终于解决了这个问题,但是其实不用动语法,我们还有别的办法。最简单的想法,如果Java有函数指针,这个问题就好解决了,不过,虽然Java没有函数指针,却能反射,我们一样可以实现类似函数指针的功能。
ActionHelper用于javafx,其他场景类推
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ActionHelper {
public static EventHandler<ActionEvent> handle(Object obj,String methodName,Object... param){
return new Action(obj,methodName,param);
}
private static class Action implements EventHandler<ActionEvent>{
private Object obj;
private String methodName;
private Class<?>[] clazz;
private Object[] param;
public Action(Object obj,String methodName,Object... param){
this.obj = obj;
if(param.length>0){
clazz = new Class[1+param.length];
clazz[0] = ActionEvent.class;
for(int i=0;i<param.length;i++){
clazz[1+i] = param[i].getClass();
}
this.param = new Object[1+param.length];
for(int i=0;i<param.length;i++){
this.param[1+i] = param[i];
}
}else{
clazz = new Class[1];
clazz[0] = ActionEvent.class;
this.param = new Object[1];
}
this.methodName = methodName;
}
@Override
public void handle(ActionEvent event) {
try {
Method method = obj.getClass().getDeclaredMethod(methodName, clazz);
if(method != null){
method.setAccessible(true);
param[0] = event;
method.invoke(obj, param);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
//以下为使用的代码片段,javafx的右键菜单
ContextMenu ctxMenu = new ContextMenu();
MenuItem mitem = new MenuItem("查看页面");
mitem.setOnAction(ActionHelper.handle(this, "viewPage"));
ctxMenu.getItems().add(mitem);
private void viewPage(ActionEvent event){
//do some thing
}