public static Response call(Object obj,Request req){
Class cls = obj.getClass();
Response res=new Response();
try{
//method根据请求的方法,和参数类型断定是调用了那个方法
Method method=cls.getDeclaredMethod(req.getMethod(),req.getParamTypes());
//调用方法,传入具体要执行的类或对象,执行这个对象或类里面的 method 方法,得到的返回值
Object object = method.invok(obj,req.getparam());
res.setValue(obj);
return res;
}catch(InvocationTargetException e){
//方法执行异常
Exception ex = (Exception) e.getTargetException();
res.setException(ex);
return res;
}catch(Exception e){
e.printStackTrace();
res.setException(e);
return res;
}
//测试
public static void main(String[] args){
Request req = new Request("charAt",new Class[]{int.class},new Object[]{2});
Response res = Utils.call("addfdcsfdd", req);
System.out.println(res.getValue()); //得到 d
///
ExamService examServ = new ExamServiceImpl();
Config config = new Config("client.properties");
EntityContext entityContext = new EntityContext(config);
examServ.setEntityContext(entityContext);
//精确找到请求的 login 方法
req = new Request("login",new Class[]{int.class,String.class},new Object[]{1001,"1234"});
//发出req请求,执行examServ接口的login方法
res = Utils.call(examServ,req);
System.out.println(res.getValue()); //得到 id号为1001的名字
}
}