站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题[求解]报被反射类里的方法名是什么错误?

[求解]报被反射类里的方法名是什么错误?

摘要: [求解]报被反射类里的方法名是什么错误?


大家帮帮我吧?我找了好久了,不知道是什么错误,分不够再加

==================== Reflect class:
package Reflect;

import java.lang.reflect.Method;

import Constance.Constance;
import Debug.Debug;

public class Reflect {

private Reflect() {
}

// new class function
private static Class getClass(String strClassName) throws Exception {
try {
return Class.forName(strClassName);
} catch (ClassNotFoundException e) {
Debug.PrintlnAndThrowException(Constance.OUT_ERROR
+ " Reflect>getClass>"
+ e.getMessage());
}
return null;
}
// new instance function
private static Object newInstance(Class cls) throws Exception{
try {
return cls.newInstance();
} catch (Exception e) {
Debug.PrintlnAndThrowException(Constance.OUT_ERROR
+ " Reflect>newInstance>"
+ e.getMessage());
}
return null;
}

// return method function
private static Method getMethod(Class cls,String method,Class[] list) throws Exception{
try {
return cls.getMethod(method,list);
} catch (SecurityException e) {
Debug.PrintlnAndThrowException(Constance.OUT_ERROR
+ " Reflect>getMethod>"
+ e.getMessage());
}
return null;
}

// do Reflect
public synchronized static Object invoke(String classname,Object[] args,String method) throws Exception{

// create list class
Class[] list = new Class[args.length];
for (int i = 0; i < args.length; i++) {
list[i] = args[i].getClass();
}

// create object
Class cls = Reflect.getClass(classname);
Object obj = Reflect.newInstance(cls);
Method mod = Reflect.getMethod(cls, method, list);

try {
mod.invoke(obj, args);
} catch (IllegalArgumentException e) {
Debug.PrintlnAndThrowException(Constance.OUT_ERROR
+ " Reflect>invoke>"
+ e.getMessage());
}

Debug.Println(Constance.OUT_INFORMATION
+ obj.toString());

return obj;

}
}



================= ReflectAction class:
package Reflect;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import Action.IAction;
import Constance.Constance;
import Debug.Debug;

public class ReflectAction {

public synchronized static String goForward(String classname,ActionForm form,HttpServletRequest request){

Object[] args = new Object[2];
args[0] = form;
args[1] = request;

IAction action = null;

try {
action =
(IAction) Reflect.invoke(
classname,args,Constance.STRUTS_ACTION_GOFORWARD);
} catch (Exception e) {
Debug.Println(
Constance.OUT_ERROR
+ " ReflectAcionClassError:"
+ e.getMessage());


return "error";
}



return action.getForward();


}

}



=================== 调用类 Login class:
package Action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import Constance.Constance;
import DTO.AddressListClass;
import com.sms.struts.form.LoginForm;

public class Login extends AbsAction {

private LoginForm loginform;
private HttpServletRequest request;

public Login(){
}

public void goForward(ActionForm form,HttpServletRequest request){
this.loginform = (LoginForm)form;
this.request = request;

AddressListClass login = null;
try {
login = new AddressListClass(this.loginform.getIntername());
login.setInterName(this.loginform.getIntername());
login.Fill();
} catch (Exception e) {
System.out.println(e.getMessage());
this.gotoError();
return;
}

if (login.getPhone().equals(loginform.getPhonenumber())){
//登录成功
//添加session值
this.request.getSession().setAttribute(
Constance.TABLE_ADDRESSLIST_INTERNAME,login.getInterName());
this.gotoQueryNote();
return;
}
this.gotoError();
return;
}
}



现在我在 struts的Action里这样调用
return mapping.findForward(ReflectAction.goForward(
Constance.STRUTS_ACTION_LOGIN,form,request));


报错:
ReflectAcionClassError:Action.Login.goForward(com.sms.struts.form.LoginForm, org.apache.coyote.tomcat5.CoyoteRequestFacade)

谢谢高手们了!~


你的设计有问题,你获取method的时候,不应该是根据每个参数(Object[] args)的实际类型(.getClass())来作为Method的参数类型,而应该是方法定义时候的类型,即使那些实际类型是方法定义的参数类型的子类型也不行。举个例子来讲
java.util.Date.before(java.util.Date): boolean 方法
java.util.Date d = new java.sql.Date(System.currentTimeMillis());
Class clazz = Date.class;
Method method = clazz.getMethod("before", new Class[] {java.util.Date.class});
System.out.println(method.invoke(new java.util.Date(), new Object[] {
d
}));



java.util.Date d = new java.sql.Date(System.currentTimeMillis());
Class clazz = Date.class;
Method method = clazz.getMethod("before", new Class[] {d.getClass()});
System.out.println(method.invoke(new java.util.Date(), new Object[] {
d
}));

前者,在靠反射动态找Method的时候,“静态”指定了参数类型java.util.Date.class,因而成功,而后者在运行期,“动态”指定了参数的实际类型java.sql.Date.class,虽然sql date也是util date,但是仍然抱错
java.lang.NoSuchMethodException: java.util.Date.before(java.sql.Date)
at java.lang.Class.getMethod(Class.java:1581)



原来是这样啊!~

那,有没有解决办法?我的意思是说,我想做这样一个设计,用其它的办法实现?


问题解决了!~

设计上加了个通用参数
谢谢高手


↑返回目录
前一篇: 有没有比JUnit更好的单元测试工具啊?
后一篇: 问一个Double类里的isNaN(double v)方法问题