JAVA 判斷物件內容是否含有空值
阿新 • • 發佈:2019-02-14
簡單判斷物件是否含有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; } }