java反射灵异事件,求大师相助
本帖最后由 jeking217 于 2012-02-13 15:37:19 编辑 最近发现一个很灵异的事件,事情是这样的:我使用的是structI,写了一个BaseAction,所有的其他所有的action都继承这个BaseAction类,在BaseAction的execute方法中采用反射的方法,根据传入的参数,执行子类的不同方法。
类图结构如下:
BaseAction的execute方法代码如下:
@Override
@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
session = request.getSession();
String thisAction = request.getParameter("act");
if (Tool.isEmpty(thisAction)) {
return mapping.findForward("error");
}
this.actionForward = null;
success = true;
try {
this.request = request;
this.response = response;
this.form = (BaseForm) form;
if (!Tool.isEmpty(thisAction)) {
Class ctrlClass = this.getClass();
Method ctrlMethod = null;
ctrlMethod = ctrlClass.getMethod(thisAction);
ctrlMethod.invoke(this);
}
} catch (Exception e) {
System.out.println("方法没找到:"+thisAction);
success = false;
}
if (!success)
forward = "error";
if (actionForward == null) {
return mapping.findForward(forward);
} else {
return actionForward;
}
}