/** * 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(); } } } } }
/** * 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(); } } } } }
/** * 自验证方法 * * @param parameterType 参数类型 * @throws MapperException 异常 */ public boolean validate(Class parameterType) throws MapperException { super.validate(parameterType); MapperException mapperException = null; if (getIfContent() == null) { throw new MapperException( ExceptionCommonConstant.IF_TAG_EXPLAIN_ERROR, String.format(ERROR_MSG, getContents())); } if (!getIfContent().trim().endsWith(SymbolConstant.SYMBOL_COMMA)) { mapperException = new MapperException( ExceptionCommonConstant.INSERT_END_WITH_COMMA, String.format(ERROR_MSG, getIfContent())); } if (!StringUtil.containBrace(getIfContent())) { // 不包含赋值标记,表示赋值为固定值,不做验证 return true; } List<String> propertyNames = StringUtil.extractBrace(getIfContent()); String propertyName = propertyNames.get(0); if (propertyName.contains(SqlHelperConstant.JDBC_TYPE_TAG)) { propertyName = propertyName.substring(0, propertyName.indexOf(SqlHelperConstant.JDBC_TYPE_TAG)); } boolean result = ReflectHelper.haveGetMethod(propertyName, parameterType); if (!result) { if (mapperException != null) { mapperException.appendMessage(ExceptionCommonConstant.COLUMN_NOT_EXIST); } else { mapperException = new MapperException( ExceptionCommonConstant.COLUMN_NOT_EXIST, String.format(ERROR_MSG, getIfContent())); } } if (mapperException != null) { throw mapperException; } else { return true; } }