import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.pingan.cgi.common.annotation.ValidateAnnotation;
import com.pingan.cgi.common.enums.RegexType;
import com.pingan.cgi.common.exception.CGIException;
/**
* @description: (参数校验)
* @date 2018年05月04日 下午14:10
*
*/
public class ValidateUtil {
private static Log logger = LogFactory.getLog(ValidateUtil.class);
public ValidateUtil() {
super();
}
// 解析的入口
public static boolean valid(Object object, List<String> exclusive)
throws Exception {
boolean validResult = true; // 默认true
if (null != object) {
// 获取object的类型
Class<? extends Object> clazz = object.getClass();
// 获取该类型声明的成员
Field[] fields = clazz.getDeclaredFields();
// 遍历属性
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
// 对于private私有化的成员变量,通过setAccessible来修改器访问权限
field.setAccessible(true);
validate(field, object);
//只要出现一次false即全部为false:不完整
validResult = validResult && validateFull(field, object, exclusive);
// 重新设置会私有权限
field.setAccessible(false);
}
} else {
throw new CGIException("待效验的对象不能为空");
}
return validResult;
}
// 解析字段完整性
public static String checkFullDetail(Object object, List<String> exclusive)
throws Exception {
if (null != object) {
// 获取object的类型
Class<? extends Object> clazz = object.getClass();
// 获取该类型声明的成员
Field[] fields = clazz.getDeclaredFields();
// 遍历属性
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
// 对于private私有化的成员变量,通过setAccessible来修改器访问权限
field.setAccessible(true);
if (null != exclusive&&exclusive.contains(field.getName())) {
// 未排除
logger.info("validateFull : " + field.getName());
continue;
}
String describe = checkFullDetail(field, object);
// 重新设置会私有权限
field.setAccessible(false);
if(!StringUtil.isBlank(describe)){
return describe;
}
}
} else {
throw new CGIException("待效验的对象不能为空");
}
return null;
}
private static String checkFullDetail(Field field, Object object) throws IllegalAccessException {
String description = null;
Object value = null;
// 获取对象的成员的注解信息
ValidateAnnotation va = field.getAnnotation(ValidateAnnotation.class);
if (va != null) {
value = field.get(object);
description = va.description().equals("") ? field.getName() : va.description();
if (!va.nullable()) {
if (value == null || StringUtil.isBlank(value.toString())) {
return description;
}
}
}
return null;
}
/**
* 效验完整性
*
* @param field
* @param model
* @param exclusive
* @return
*/
private static boolean validateFull(Field field, Object model,
List<String> exclusive) {
try {
String name = field.getName(); // 获取属性的名字
String upperName = name.substring(0, 1).toUpperCase()
+ name.substring(1); // 将属性的首字符大写,方便构造get,set方法
String type = field.getGenericType().toString(); // 获取属性的类型
if (type.equals("class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
Method m = model.getClass().getMethod("get" + upperName);
String value = (String) m.invoke(model); // 调用getter方法获取属性值
if (StringUtils.isBlank(value)) {
if (null != exclusive) {
// 未排除
if (!exclusive.contains(name)) {
System.out.print("validateFull : " + name);
logger.info("validateFull : " + name);
return false;
}
} else {
return false;
}
}
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("get" + upperName);
Integer value = (Integer) m.invoke(model);
if (value == null) {
if (null != exclusive) {
if (!exclusive.contains(name)) {
return false;
}
} else {
return false;
}
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = model.getClass().getMethod("get" + upperName);
Boolean value = (Boolean) m.invoke(model);
if (value == null) {
if (null != exclusive) {
if (!exclusive.contains(name)) {
return false;
}
} else {
return false;
}
}
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("get" + upperName);
Date value = (Date) m.invoke(model);
if (value == null) {
if (null != exclusive) {
if (!exclusive.contains(name)) {
return false;
}
} else {
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* @param field
* @param object
* @throws Exception
*/
private static void validate(Field field, Object object) throws Exception {
String description = null;
Object value = null;
// 获取对象的成员的注解信息
ValidateAnnotation va = field.getAnnotation(ValidateAnnotation.class);
value = field.get(object);
if (null != va) {
description = va.description().equals("") ? field.getName() : va.description();
if (!va.nullable()) {
if (value == null || StringUtil.isBlank(value.toString())) {
throw new CGIException(description + "不能为空");
}
}
if (null != value) {
if (value.toString().length() > va.maxLength() && va.maxLength() != 0) {
throw new CGIException(description + "长度不能超过" + va.maxLength());
}
if (value.toString().length() < va.minLength() && va.minLength() != 0) {
throw new CGIException(description + "长度不能小于" + va.minLength());
}
}
if (va.regexType() != RegexType.NONE) {
switch (va.regexType()) {
case NONE:
break;
case SPECIALCHAR:
if (RegexUtil.hasSpecialChar(value.toString())) {
throw new CGIException(description + "不能含有特殊字符");
}
break;
case CHINESE:
if (RegexUtil.isChinese2(value.toString())) {
throw new CGIException(description + "不能含有中文字符");
}
break;
case EMAIL:
if (!RegexUtil.isEmail(value.toString())) {
throw new CGIException(description + "邮箱格式不正确");
}
break;
case IP:
if (!RegexUtil.isIp(value.toString())) {
throw new CGIException(description + "地址格式不正确");
}
break;
case NUMBER:
if (!RegexUtil.isNumber(value.toString())) {
throw new CGIException(description + "不是数字");
}
break;
case PHONENUMBER:
if (!RegexUtil.isPhoneNumber(value.toString())) {
throw new CGIException(description + "不是电话号码");
}
break;
case YESORNO:
if (null != value) {
if (StringUtils.isNotEmpty(value.toString())) {
if (!"Y".equals(value.toString())
&& !"N".equals(value.toString())) {
throw new CGIException(description + "只能是Y或N");
}
}
}
break;
case IDNUM:
if (!RegexUtil.isPersonIDCode(value.toString())) {
throw new CGIException(description + "身份证格式不合法");
}
break;
default:
break;
}
}
if (!va.regexExpression().equals("")) {
if (value.toString().matches(va.regexExpression())) {
throw new CGIException(description + "格式不正确");
}
}
}
}
}