/** * Check condition node property exist. * * @author wangyi8 * @taskId * @param node the node * @param clz the clz * @param errMsg the err msg */ private void checkConditionNodePropertyExist(ConditionNode node, Class clz, String errMsg) { if (StringUtil.containBrace(node.getValue())) { if (clz == null || ArrayUtil.contains(Config.IGNORE_PARAMETER_TYPES, clz)) { return; } else { List<String> propertyNames = StringUtil.extractBrace(node.getValue()); for (String propertyName : propertyNames) { if (node.getConditionType().equals("in") && (propertyName.equals("item") || propertyName.startsWith("item."))) { // foreach 条件在foreach标签中判断,这里不判断 return; } try { ReflectHelper.haveGetMethod(propertyName, clz); } catch (MapperException e) { e.setDescription( errMsg + String.format(MessageConstant.EXPRESS_MSG, node.toString()) + SymbolConstant.SYMBOL_BLANK + e.getDescription()); e.printException(); } } } } }
/** * 忽略的属性验证 * * @author wangyi8 * @taskId * @param conditionNode 条件 * @param propertyModel 属性 */ private boolean ignoreProperty(ConditionNode conditionNode, PropertyModel propertyModel) { conditionNode.rebuild(); // in条件查询中,出现单独的#{item}不进行验证 if (conditionNode.getConditionType().equals(SqlConstant.IN) && propertyModel.getPropertyName().equals(MyBatisTagConstant.ITEM)) { return true; } return false; }
/** * Validate update property exist. * * @author wangyi8 * @taskId * @param item the item */ private void validateUpdatePropertyExist(UpdateMappedStatementItem item) { Class clz = item.getParameterType(); if (ClassUtil.ignorePropertyCheck(clz)) { // String clzName = clz == null ? SymbolConstant.SYMBOL_NULL : clz.getName(); /*new MapperException(ExceptionCommonConstant.IGNORE_PARAMETER_TYPES, item.getInfoMessage() + SymbolConstant.SYMBOL_COLON + String.format(MessageConstant.EXPRESS_MSG, clzName)).printException();*/ return; } List<UpdateIfSetNode> setNodeList = item.getSetNodeList(); if (setNodeList != null) { for (int i = 0; i < setNodeList.size(); i++) { UpdateIfSetNode node = setNodeList.get(i); // 验证字段和属性是否存在 List<String> propertyNames = StringUtil.extractBrace(node.getIfContent()); for (int k = 0; k < propertyNames.size(); k++) { PropertyModel propertyModel = new PropertyModel(propertyNames.get(k)); // 验证属性是否存在 try { ReflectHelper.haveGetMethod(propertyModel.getPropertyName(), clz); } catch (MapperException e) { new MapperException( ExceptionCommonConstant.GET_METHOD_NOT_EXIST, item.getInfoMessage() + SymbolConstant.SYMBOL_COLON + String.format(MessageConstant.EXPRESS_MSG, node.getIfContent())) .printException(); } } } } List<SelectElement> selectElements = item.getWhereConditions(); for (int i = 0; i < selectElements.size(); i++) { if (selectElements.get(i) instanceof ConditionNode) { ConditionNode conditionNode = (ConditionNode) selectElements.get(i); List<String> propertyList = StringUtil.extractBrace(conditionNode.toString()); for (String propertyName : propertyList) { PropertyModel propertyModel = new PropertyModel(propertyName); if (ignoreProperty(conditionNode, propertyModel)) { continue; } try { ReflectHelper.haveGetMethod(propertyModel.getPropertyName(), clz); } catch (MapperException e) { new MapperException( ExceptionCommonConstant.GET_METHOD_NOT_EXIST, item.getInfoMessage() + SymbolConstant.SYMBOL_COLON + String.format(MessageConstant.EXPRESS_MSG, conditionNode.toString())) .printException(); } } } } }