NutzCN Logo
问答 根据被调用方法中传入的对象类型,动态来执行方法报错,Class<?>用不了
发布于 2614天前 作者 qq_7fafbecc 1318 次浏览 复制 上一个帖子 下一个帖子
标签:

service方法:

	@Aop(TransAop.READ_COMMITTED)
	public Object commitDept(NutMap map) throws BusinessException {
		NutMap resultMap = new NutMap();
		int commitNum = 0;
		try{
			commitNum = dictServiceBo.commitPublicDict(Ward.class,map);
		}catch(Exception e){
			throw new BusinessException("提交数据时出错:"+e.getMessage());
		}
		resultMap.setv("commitNum", commitNum);
		return resultMap;
	}

工具类方法:

	public int commitPublicDict(Class<?> classOfDict,NutMap map){
		List<Map> insertList = new ArrayList<Map>();
		List<Map> updateList = new ArrayList<Map>();
		List<Map> deleteList = new ArrayList<Map>();
		insertList = (List)map.get("inserted");
		updateList = (List)map.get("updated");
		deleteList = (List)map.get("deleted");
		
		int insertNum = 0;
		int updateNum = 0;
		int deleteNum = 0;
		//新增操作
		for(int i=0; i<insertList.size(); i++){
			//Dept dept = new Dept();
			classOfDict = Lang.map2Object(insertList.get(i), classOfDict.getClass());
			dao.insert(classOfDict);
			insertNum = insertNum + 1;
		}
		//更新操作
		for(int i=0; i<updateList.size(); i++){
			//Dept dept = new Dept();
			classOfDict = Lang.map2Object(updateList.get(i), classOfDict.getClass());
			dao.update(classOfDict);
			updateNum = updateNum + 1;
		}
		//删除操作
		for(int i=0; i<deleteList.size(); i++){
			//Dept dept = new Dept();
			classOfDict = Lang.map2Object(deleteList.get(i), classOfDict.getClass());
			dao.delete(classOfDict);
			deleteNum = deleteNum + 1;
		}
		return insertNum + updateNum + deleteNum;
	} 

报错信息:

For the reason: 提交数据时出错:Fail to born 'java.lang.Class'
 by args: [] because: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Invoking.invoke(Invoking.java:195)
	at org.nutz.lang.Mirror.invoke(Mirror.java:1100)
	at com.shlx.blood.service.ComServices.ExcuteServices(ComServices.java:144)
	at com.shlx.blood.action.dict.PublicDictAction.commitDept(PublicDictAction.java:121)
	at com.shlx.blood.action.dict.PublicDictAction$$FASTCLASS._invoke(PublicDictAction.java:1)
	at org.nutz.lang.reflect.AbstractFastClass.invoke(AbstractFastClass.java:143)
	at org.nutz.lang.reflect.FastClassFactory.invoke(FastClassFactory.java:67)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.shlx.blood.mvc.SessionCheckProcessor.process(SessionCheckProcessor.java:56)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.shlx.blood.exception.BusinessException: 提交数据时出错:Fail to born 'java.lang.Class'
 by args: [] because: No suitable Constructor or Factory Method!!
	at com.shlx.blood.service.dict.PublicDictService.commitDept(PublicDictService.java:198)
	at com.shlx.blood.service.dict.PublicDictService$$NUTZAOP._aop_invoke(PublicDictService.java:5)
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
	at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at com.shlx.blood.aop.ServiceInterceptor.filter(ServiceInterceptor.java:60)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at com.shlx.blood.service.dict.PublicDictService$$NUTZAOP.commitDept(PublicDictService.java:1)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.nutz.lang.Invoking$DefaultInvoker.invoke(Invoking.java:47)
	at org.nutz.lang.Invoking.invoke(Invoking.java:192)
	... 42 more
5 回复
ComServices.java:144

这是哪一行?

想法是:
业务类中,向方法传入的可以是A.class、B.class、C.class ...
工具类中,根据Class的类型,动态的实例化出a,b,c,
再dao.insert(a或b或c)

	public Object ExcuteServices(String _function_no, NutMap _params) {
		log.info("FunctionId : " + _function_no + " params:" + _params.toString());
		//获取这个功能号的配置
		List<SysFunction> sysfunctions = dao.query(SysFunction.class, Cnd.where("functionid", "=", _function_no));
		if(sysfunctions == null || sysfunctions.isEmpty()) {
			return GetSystemExcepitonResponse(String.format("找不到功能编号为%s的方法!", _function_no));
		}
		SysFunction sysfunction = sysfunctions.get(0);
		if(Strings.isEmpty(sysfunction.getFunctionId()) || Strings.isEmpty(sysfunction.getServiceClass()) || Strings.isEmpty(sysfunction.getServiceMethod())) {
			return GetSystemExcepitonResponse(String.format("功能编号%s的配置错误!", _function_no));
		}
		
		//把功能号写入request中,以便后面的AOP拦截器,能够做权限判断。
		//Mvcs.getReq().setAttribute("functionId", _function_no);
		
		//获得命名空间
		//String _nameSpace = Strings.endsWithChar(sysfunction.getServiceClass(), '.') ? sysfunction.getServiceClass() : sysfunction.getServiceClass() + ".";
		//获得服务名称
		String services_name = sysfunction.getServiceClass();
		//通过反射判断这个类是否存在
		//反射之中的所有泛型都定义为?,返回值都是Object
		Class<?> services_class;
		try {
			services_class = Class.forName(services_name);
		}
		catch (ClassNotFoundException e) {
			return GetSystemExcepitonResponse(String.format("%s服务类没有找到!", services_name));
		}
		//判断这个类是否在IOC容器中存在
		Object obj;
		try {
			obj = Mvcs.getIoc().get(services_class);
		} catch (IocException e) {
			return GetSystemExcepitonResponse(String.format("%s服务类在IOC容器中没有找到,请注意类是否为IOC管理!", services_name));
		}
		//通过反射调用方法
		Mirror mirror = Mirror.me(obj.getClass());
		//获得所有方法--判断是否存在
		boolean hasMethod = false;
		Method[] methods = mirror.getMethods();
		for (Method method : methods) {
			String methodName = method.getName();
			if(methodName.toUpperCase().equals(sysfunction.getServiceMethod().toUpperCase())) {
				hasMethod = true;
				break;
			}
		}
		if(!hasMethod) {
			return GetSystemExcepitonResponse(String.format("%s服务类的%s方法没有找到!", services_name, sysfunction.getServiceMethod()));
		}
		//判断权限
		User user = (User)Mvcs.getHttpSession().getAttribute(Globals.SESSION_USER_CODE);
		if(!isHaveRight(user,sysfunction)){
			return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
		}
		//TODO 检查参数配置
		try {
			if(sysfunction.getNoCodeResponse() != null && "1".equals(sysfunction.getNoCodeResponse())){
				//返回没有code的json形式
				return mirror.invoke(obj, sysfunction.getServiceMethod(), _params);
			}else{
				return this.GetOkResponse(mirror.invoke(obj, sysfunction.getServiceMethod(), _params));
			}
		}catch (Exception ex) {
			log.debug(ex.getMessage(), ex);
			if (ex.getCause() instanceof BusinessException) {
				BusinessException businessException = (BusinessException)ex.getCause();
				return this.GetBusinessExceptionResponse(businessException.getMessage());
			} else if(ex.getCause() instanceof SystemException) {
				SystemException systemException = (SystemException)ex.getCause();
				return this.GetSystemExcepitonResponse(systemException.getMessage());
			} else {
				return this.GetSystemExcepitonResponse(ex.getMessage());
			}
		}
	}
				return this.GetOkResponse(mirror.invoke(obj, sysfunction.getServiceMethod(), _params));

报错的时候, sysfunction.getServiceMethod() 和 _params 的值 分别是什么, 预期会调用哪个方法?

添加回复
请先登陆
回到顶部