import com.google.common.collect.ImmutableMap;
import com.lin.MyBean;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
*
* 对象校验工具.
*
* [@author](https://my.oschina.net/arthor) linjinzhi
* [@date](https://my.oschina.net/u/2504391) 2019/5/28
*/
public class BeanCheckUtils {
/**
* 列出对象全部私有域
* [@param](https://my.oschina.net/u/2303379) object 对象
* [@return](https://my.oschina.net/u/556800) 域名称列表
* [@throws](https://my.oschina.net/throws) Exception 异常
*/
public static List<String> listAllProperties(Object object) throws Exception {
List<String> properties = new ArrayList<>();
Class<?> clazz = object.getClass();
// 获取对象的全部域的名称
for (Field field : clazz.getDeclaredFields()) {
// 如果域是 private 型查找 setters/getters方法
if (Modifier.isPrivate(field.getModifiers())) {
// 将大写转换成小写
String name = field.getName();
String upperCaseName = name.substring(0, 1).toUpperCase() + name.substring(1);
// 有getters/setters方法
try {
String simpleType = field.getType().getSimpleName();
// 处理布尔型
if (simpleType.equals("Boolean") || simpleType.equals("boolean")) {
if ((clazz.getDeclaredMethod("is" + upperCaseName) != null)
&& (clazz.getDeclaredMethod("set" + upperCaseName,
field.getType()) != null)) {
properties.add(name);
}
}
// 处理非布尔型
else {
if ((clazz.getDeclaredMethod("get" + upperCaseName) != null)
&& (clazz.getDeclaredMethod("set" + upperCaseName,
field.getType()) != null)) {
properties.add(name);
}
}
} catch (NoSuchMethodException | SecurityException e) {
// 忽略此异常
}
}
}
return properties;
}
/**
* 将值为空的域的名称列出来
* @param object 对象
* @return 值为空的域的名称列表
* @throws Exception 异常
*/
public static List<String> listNullProperties(Object object) throws Exception {
if(object == null) {
throw new IllegalArgumentException("arguments error");
}
List<String> nullValueProperties = new ArrayList<>();
List<String> properties = listAllProperties(object);
Class<?> clazz = object.getClass();
// 收集值为空的域
for (String property : properties) {
Field field = clazz.getDeclaredField(property);
field.setAccessible(true);
Object value = field.get(object);
if(value == null) {
nullValueProperties.add(property);
}
}
return nullValueProperties;
}
/**
* 校验对象指定参数是否为null
* @param bean 对象
* @param notAllowNullProperties 不允许为null的域的名称
* @return boolean
* @throws Exception 异常
*/
public static boolean checkNullProperties(Object bean, String... notAllowNullProperties) throws Exception {
if(bean == null || ArrayUtils.isEmpty(notAllowNullProperties)) {
throw new IllegalArgumentException("arguments error");
}
List<String> nullValueProperties = BeanCheckUtils.listNullProperties(bean);
List<String> notAllowNullPropertyList = Arrays.asList(notAllowNullProperties);
for(String property : notAllowNullPropertyList) {
if(nullValueProperties.contains(property)) {
return false;
}
}
return true;
}
/**
* 使用给定的值校验对象对应的域的值是否符合
* @param bean 对象
* @param propertyValuePairs 需要校验的域的指定的值
* @return boolean
* @throws Exception 异常
*/
public static boolean checkProperties(Object bean, Map<String, Object> propertyValuePairs) throws Exception {
if(bean == null || CollectionUtils.isEmpty(propertyValuePairs)) {
throw new IllegalArgumentException("arguments error");
}
List<String> properties = listAllProperties(bean);
Class<?> clazz = bean.getClass();
for (String property : properties) {
Field field = clazz.getDeclaredField(property);
field.setAccessible(true);
Object value = field.get(bean);
Object expectation = propertyValuePairs.get(property);
// 自定义规则
if(expectation instanceof Validator) {
Validator validator = (Validator) expectation;
boolean isValid = validator.validate(value);
if(!isValid) {
return false;
}
}
// 具体值
else {
if(expectation != null && !expectation.equals(value)) {
return false;
}
}
}
return true;
}
public interface Validator {
boolean validate(Object value);
}
public static void main(String[] args) throws Exception {
MyBean myBean = new MyBean();
myBean.setProperties1("123");
myBean.setProperties2(1);
myBean.setProperties3(true);
System.out.println(BeanCheckUtils.checkNullProperties(myBean, "properties3", "properties1"));
System.out.println(BeanCheckUtils.checkProperties(myBean, ImmutableMap.of("properties1", "123", "properties2", new Validator() {
@Override
public boolean validate(Object value) {
return value instanceof Integer;
}
})));
}
}
转载于:https://my.oschina.net/ChiLin/blog/3062480