Java Code Examples for cn.hutool.core.util.ReflectUtil#invoke()
The following examples show how to use
cn.hutool.core.util.ReflectUtil#invoke() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionRequest.java From openAGV with Apache License 2.0 | 6 votes |
public static String callServiceMethod(ServiceRequestDto serviceRequestDto) { Object service = BeanHelper.duang().getBean(serviceRequestDto.getServiceClass()); if (ToolsKit.isEmpty(service)) { throw new RobotException("根据["+serviceRequestDto.getServiceClass().getName()+"]没有找到实例对象,请检查!"); } try { Object resultObj = ReflectUtil.invoke(service, serviceRequestDto.getMethodName(), serviceRequestDto.getParam()); String result = ""; if (resultObj instanceof String) { result = resultObj.toString(); } else if (resultObj instanceof IProtocol) { result = ((IProtocol) resultObj).getParams(); } return result; } catch (Exception e) { throw new RobotException(e.getMessage(), e); } }
Example 2
Source File: TaskHandler.java From openAGV with Apache License 2.0 | 5 votes |
/*** * 执行任务处理 * @param target 方法名 * @param request 请求对象 * @param response 返回对象 * @throws Exception */ public IResponse doHandler(String target, IRequest request, IResponse response) throws Exception { String deviceId = request.getProtocol().getDeviceId(); try { Route route = RouteHelper.duang().getRoutes().get(deviceId); if (null == route) { return emptyRouteOrMehtod(deviceId, target, request, (BaseResponse) response); } Method method = route.getMethodMap().get(target.toLowerCase()); // 如果Service里没有实现该指令对应的方法,则执行公用的duang方法,直接返回响应协议,防止抛出异常 if (ToolsKit.isEmpty(method)) { return emptyRouteOrMehtod(deviceId, target, request, (BaseResponse) response); } Object resultObj = ReflectUtil.invoke(route.getServiceObj(), method, request, response); // 如果是同一个请求响应单元并且rawContent值为空,则写入响应对象 if (response.isResponseTo(request) && ToolsKit.isEmpty(response.getRawContent()) && ToolsKit.isNotEmpty(resultObj)) { response.write(resultObj); } // 如果响应对象不为空,但没有设置响应内容,则抛出异常 if (ToolsKit.isNotEmpty(resultObj) && ToolsKit.isEmpty(response.getRawContent())) { throw new RobotException(ExceptionEnums.RESPONSE_RAW_NULL); } } catch (Exception e) { if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; Throwable t = ite.getTargetException();// 获取目标异常 throw new RobotException(t.getMessage(), t); } else { throw new RobotException(e.getMessage(), e); } } return response; }
Example 3
Source File: HutoolController.java From mall-learning with Apache License 2.0 | 5 votes |
@ApiOperation("ReflectUtil使用:Java反射工具类") @GetMapping("/reflectUtil") public CommonResult reflectUtil() { //获取某个类的所有方法 Method[] methods = ReflectUtil.getMethods(PmsBrand.class); //获取某个类的指定方法 Method method = ReflectUtil.getMethod(PmsBrand.class, "getId"); //使用反射来创建对象 PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class); //反射执行对象的方法 ReflectUtil.invoke(pmsBrand, "setId", 1); return CommonResult.success(null, "操作成功"); }
Example 4
Source File: CallServiceController.java From Aooms with Apache License 2.0 | 5 votes |
private void invokeMethod(String serviceBeanName,String method,ServiceConfiguration serviceConfiguration,HttpServletRequest request){ Object bean = SpringUtils.getApplicationContext().getBean(serviceBeanName); boolean isPost = ("POST".equalsIgnoreCase(request.getMethod())); boolean isExcludeMethod = serviceConfiguration.isExcludeMethod(method); if(isExcludeMethod){ throw AoomsExceptions.create("Service Bean [" + serviceBeanName + "] Method [" + method + "] is ExcludeMethod"); } boolean isPostMethod = serviceConfiguration.isPostMethod(method); if(isPostMethod){ if(isPost){ ReflectUtil.invoke(bean,method); }else{ throw AoomsExceptions.create("Service Bean [" + serviceBeanName + "] Method [" + method + "] is Post Method"); } }else{ List<String> methods = serviceConfiguration.getMethods(); if(methods.size() > 0){ boolean containsMethod = serviceConfiguration.containsMethod(method); if(containsMethod){ ReflectUtil.invoke(bean,method); }else{ throw AoomsExceptions.create("Service Bean [" + serviceBeanName + "] Method [" + method + "] is not register"); } }else{ ReflectUtil.invoke(bean,method); } } }
Example 5
Source File: ClassUtil.java From albedo with GNU Lesser General Public License v3.0 | 5 votes |
/** * 调用Getter方法. 支持多级,如:对象名.对象名.方法 */ public static Object invokeGetter(Object obj, String propertyName) { if (obj instanceof Map) { return ((Map) obj).get(propertyName); } Object object = obj; for (String name : StringUtil.split(propertyName, StringUtil.DOT)) { object = ReflectUtil.invoke(object, GETTER_PREFIX + StringUtil.upperFirst(name)); } return object; }
Example 6
Source File: ClassUtil.java From albedo with GNU Lesser General Public License v3.0 | 5 votes |
/** * 调用Setter方法, 仅匹配方法名。 支持多级,如:对象名.对象名.方法 */ public static void invokeSetter(Object obj, String propertyName, Object value) { Object object = obj; String[] names = StringUtil.split(propertyName, StringUtil.DOT); for (int i = 0; i < names.length; i++) { if (i < names.length - 1) { object = ReflectUtil.invoke(object, GETTER_PREFIX + StringUtil.upperFirst(names[i]), value); } else { ReflectUtil.invoke(object, SETTER_PREFIX + StringUtil.upperFirst(names[i]), value); } } }
Example 7
Source File: JvmUtil.java From Jpom with MIT License | 4 votes |
public static String importFrom(int pid) { if (importFrom == null) { throw new JpomRuntimeException("jdk 环境不正常,没有找到:ConnectorAddressLink"); } return ReflectUtil.invoke(null, importFrom, pid); }