• JAVA 判断对象内容是否含有空值


    简单判断对象是否含有NULL值,以及信息描述。


    package
    com.sicdt.sicsign.bill.api.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.alibaba.fastjson.JSONObject; import com.sicdt.sicsign.bill.api.model.CheckResultModel; public class CheckResultMsg { private boolean isSuccess; private String massgage; private List<CheckResultModel> successes = new ArrayList<>();//验证通过 private List<CheckResultModel> fails = new ArrayList<>();//验证失败 public CheckResultMsg() { super(); // TODO Auto-generated constructor stub } /** * <br>描 述: 校验入参对象,是否含有空值 * <br>作 者: shizhenwei * <br>历 史: (版本) 作者 时间 注释 * @param obj * @return * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ public static CheckResultMsg checkObject(Object obj){ CheckResultMsg resultMsg = new CheckResultMsg(); if(null==obj){ resultMsg.setSuccess(true); resultMsg.setMassgage("入参对象为NULL"); return resultMsg; } resultMsg.setSuccess(true); List<CheckResultModel> successes = resultMsg.getSuccesses(); List<CheckResultModel> fails = resultMsg.getSuccesses(); Class<?> clazz = obj.getClass(); Method[] methods = clazz.getDeclaredMethods(); JSONObject jsonMsg = new JSONObject(); for(Method method : methods){ String methodName = method.getName(); if(methodName.startsWith("get")){ Object res = null; try { res = method.invoke(obj); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); resultMsg.setSuccess(false); return resultMsg; } if(null==res){ String fieldName = methodName.substring(3); String fieldMsg = "不允许为NULL"; jsonMsg.put(fieldName, fieldMsg); CheckResultModel model = new CheckResultModel(fieldName, fieldMsg, false); fails.add(model); if(resultMsg.isSuccess()){ resultMsg.setSuccess(false); } }else{ CheckResultModel model = new CheckResultModel(methodName.substring(3), "正确", true); successes.add(model); } } } resultMsg.setMassgage(jsonMsg.toJSONString()); return resultMsg; } /** * <br>描 述: 校验入参对象,指定属性,是否含有空值 * <br>作 者: shizhenwei * <br>历 史: (版本) 作者 时间 注释 * @param obj * @return * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ public static CheckResultMsg checkObject(Object obj, String[] fileds, boolean isCheckFileds){ CheckResultMsg resultMsg = new CheckResultMsg(); if(null==obj){ resultMsg.setSuccess(true); resultMsg.setMassgage("入参对象为NULL"); return resultMsg; } resultMsg.setSuccess(true); List<CheckResultModel> successes = resultMsg.getSuccesses(); List<CheckResultModel> fails = resultMsg.getSuccesses(); Class<?> clazz = obj.getClass(); Method[] methods = clazz.getDeclaredMethods(); JSONObject jsonMsg = new JSONObject(); for(Method method : methods){ String methodName = method.getName(); String fieldName = methodName.substring(3); List<String> filedList = Arrays.asList(fileds); if(filedList.contains(fieldName) == isCheckFileds){ if(methodName.startsWith("get")){ Object res = null; try { res = method.invoke(obj); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); resultMsg.setSuccess(false); return resultMsg; } if(null==res){ String fieldMsg = "不允许为NULL"; jsonMsg.put(fieldName, fieldMsg); CheckResultModel model = new CheckResultModel(fieldName, fieldMsg, false); fails.add(model); if(resultMsg.isSuccess()){ resultMsg.setSuccess(false); } }else{ CheckResultModel model = new CheckResultModel(methodName.substring(3), "正确", true); successes.add(model); } } }else{ continue; } } resultMsg.setMassgage(jsonMsg.toJSONString()); return resultMsg; } public String description(){ String message = ""; if(this.isSuccess){ message += "参数检查正确:"; }else{ message += "参数检查错误:"; } return message+this.massgage; } public boolean isSuccess() { return isSuccess; } public void setSuccess(boolean isSuccess) { this.isSuccess = isSuccess; } public String getMassgage() { return massgage; } public void setMassgage(String massgage) { this.massgage = massgage; } public List<CheckResultModel> getSuccesses() { return successes; } public void setSuccesses(List<CheckResultModel> successes) { this.successes = successes; } public List<CheckResultModel> getFails() { return fails; } public void setFails(List<CheckResultModel> fails) { this.fails = fails; } }
  • 相关阅读:
    Git Bash主题配置
    vuex里mapState,mapGetters使用详解
    Vue基础汇总实践
    Chrome for Mac键盘快捷键!来自Google Chrome官网!
    Vue中ESlint配置文件.eslintrc文件
    axios的get,post方法
    正则表达式识别标记——包括它之间的标记
    关于如何在微信里面让video不全屏播放
    javascript执行机制
    防止表单多次提交方法之二
  • 原文地址:https://www.cnblogs.com/zwcry/p/8385932.html
Copyright © 2020-2023  润新知