Commit 32582570 by s_guodong

优化自定义vo赋值 和 校验

parent 18b36fb0
......@@ -69,12 +69,17 @@ public class ReflectUtil {
declaredField.set(baseVo, o);
} else {
// 对象类型
Object typeO = type.newInstance();
String s = setValue2RequestVo(o, f.getType(), typeO, type);
if (!StringUtils.isBlank(s)) {
return s;
if (type == f.getType()) {
// 相同的class 直接赋值
declaredField.set(baseVo, o);
} else {
Object typeO = type.newInstance();
String s = setValue2RequestVo(o, f.getType(), typeO, type);
if (!StringUtils.isBlank(s)) {
return s;
}
declaredField.set(baseVo, typeO);
}
declaredField.set(baseVo, typeO);
}
} catch (Exception e) {
// Class<?> superclass = baseVoClass.getSuperclass();
......
......@@ -31,6 +31,9 @@ public class ValidatorUtil {
CHECK_ERR_MSG.remove();
return "参数校验错误[" + s + "]";
}
if (obj == null) {
return "";
}
Class<?> aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields();
for (Field f : fields) {
......@@ -41,133 +44,165 @@ public class ValidatorUtil {
o = f.get(obj);
} catch (IllegalAccessException e) {
}
if (f.isAnnotationPresent(Length.class)) {
Length length = f.getAnnotation(Length.class);
int max = length.max();
String message = length.message();
if (o != null) {
if (type.isAssignableFrom(String.class)) {
int min = length.min();
String stringValue = (String) o;
if (stringValue.length() > max || stringValue.length() < min) {
return "参数校验错误[" + message + "]";
}
} else if (type.isAssignableFrom(BigDecimal.class)) {
String bigDecimal = ((BigDecimal) o).toPlainString();
if (bigDecimal.length() > max) {
return "参数校验错误[" + message + "]";
}
if (f.isAnnotationPresent(DecimalLength.class)) {
DecimalLength annotation = f.getAnnotation(DecimalLength.class);
int i = bigDecimal.indexOf(".");
if (i > -1) {
String substring = bigDecimal.substring(i + 1);
if (substring.length() > annotation.value()) {
return "参数校验错误[" + annotation.message() + "]";
}
String integerString = bigDecimal.substring(0, i);
if (integerString.length() > annotation.zsvalue() && annotation.zsvalue() > 0) {
return "参数校验错误[" + annotation.zsmessage() + "]";
}
} else {
if (annotation.zsvalue() > 0 && bigDecimal.length() > annotation.zsvalue()) {
return "参数校验错误[" + annotation.zsmessage() + "]";
// 集合
if (type.isAssignableFrom(List.class)) {
List list = (List) o;
for (int i = 0; i < list.size(); i++) {
Object e = list.get(i);
String check = check(e);
if (StringUtils.isNotBlank(check)) {
return check;
}
}
} else if (isPrimate(type)) {
// 基本数据类型
if (f.isAnnotationPresent(Length.class)) {
Length length = f.getAnnotation(Length.class);
int max = length.max();
String message = length.message();
if (o != null) {
if (type.isAssignableFrom(String.class)) {
int min = length.min();
String stringValue = (String) o;
if (stringValue.length() > max || stringValue.length() < min) {
return "参数校验错误[" + message + "]";
}
} else if (type.isAssignableFrom(BigDecimal.class)) {
String bigDecimal = ((BigDecimal) o).toPlainString();
if (bigDecimal.length() > max) {
return "参数校验错误[" + message + "]";
}
if (f.isAnnotationPresent(DecimalLength.class)) {
DecimalLength annotation = f.getAnnotation(DecimalLength.class);
int i = bigDecimal.indexOf(".");
if (i > -1) {
String substring = bigDecimal.substring(i + 1);
if (substring.length() > annotation.value()) {
return "参数校验错误[" + annotation.message() + "]";
}
String integerString = bigDecimal.substring(0, i);
if (integerString.length() > annotation.zsvalue() && annotation.zsvalue() > 0) {
return "参数校验错误[" + annotation.zsmessage() + "]";
}
} else {
if (annotation.zsvalue() > 0 && bigDecimal.length() > annotation.zsvalue()) {
return "参数校验错误[" + annotation.zsmessage() + "]";
}
}
}
}
} else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
int min = length.min();
String stringValue = String.valueOf(o);
if (stringValue.length() > max || stringValue.length() < min) {
return "参数校验错误[" + message + "]";
} else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
int min = length.min();
String stringValue = String.valueOf(o);
if (stringValue.length() > max || stringValue.length() < min) {
return "参数校验错误[" + message + "]";
}
}
}
}
}
if (f.isAnnotationPresent(NotEmpty.class) && type.isAssignableFrom(String.class)) {
NotEmpty annotation = f.getAnnotation(NotEmpty.class);
if (o == null || StringUtils.isBlank((String) o)) {
return "参数校验错误[" + annotation.message() + "]";
}
}
if (f.isAnnotationPresent(NotNull.class) && !type.isAssignableFrom(String.class)) {
NotNull annotation = f.getAnnotation(NotNull.class);
if (o == null) {
return "参数校验错误[" + annotation.message() + "]";
}
}
if (f.isAnnotationPresent(Need.class)) {
Need annotation = f.getAnnotation(Need.class);
String flg = annotation.flg();
// 拦截0, 0.00的情况
if ("1".equals(flg)) {
if (type.isAssignableFrom(BigDecimal.class) && o != null) {
BigDecimal bigDecimal = (BigDecimal) o;
if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
return "参数校验错误[" + annotation.message() + "]";
}
if (f.isAnnotationPresent(NotEmpty.class) && type.isAssignableFrom(String.class)) {
NotEmpty annotation = f.getAnnotation(NotEmpty.class);
if (o == null || StringUtils.isBlank((String) o)) {
return "参数校验错误[" + annotation.message() + "]";
}
}
if (o == null) {
return "参数校验错误[" + annotation.message() + "]";
}
if (type.isAssignableFrom(String.class) && StringUtils.isBlank((String) o)) {
return "参数校验错误[" + annotation.message() + "]";
}
if (ReflectUtil.isPrimate(type)) {
} else if ("java.util.List".equals(type.getName())) {
List list = (List) o;
if (list.size() == 0) {
if (f.isAnnotationPresent(NotNull.class) && !type.isAssignableFrom(String.class)) {
NotNull annotation = f.getAnnotation(NotNull.class);
if (o == null) {
return "参数校验错误[" + annotation.message() + "]";
}
// 获取list的泛型
Class tType = ReflectUtil.getTType(f);
if (!ReflectUtil.isPrimate(tType)) {
for (Object data : list) {
String s = check(data);
if (!StringUtils.isBlank(s)) {
return s;
}
if (f.isAnnotationPresent(Need.class)) {
Need annotation = f.getAnnotation(Need.class);
String flg = annotation.flg();
// 拦截0, 0.00的情况
if ("1".equals(flg)) {
if (type.isAssignableFrom(BigDecimal.class) && o != null) {
BigDecimal bigDecimal = (BigDecimal) o;
if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
return "参数校验错误[" + annotation.message() + "]";
}
}
}
} else {
String s = check(o);
if (!StringUtils.isBlank(s)) {
return s;
if (o == null) {
return "参数校验错误[" + annotation.message() + "]";
}
if (type.isAssignableFrom(String.class) && StringUtils.isBlank((String) o)) {
return "参数校验错误[" + annotation.message() + "]";
}
if (ReflectUtil.isPrimate(type)) {
} else if ("java.util.List".equals(type.getName())) {
List list = (List) o;
if (list.size() == 0) {
return "参数校验错误[" + annotation.message() + "]";
}
// 获取list的泛型
Class tType = ReflectUtil.getTType(f);
if (!ReflectUtil.isPrimate(tType)) {
for (Object data : list) {
String s = check(data);
if (!StringUtils.isBlank(s)) {
return s;
}
}
}
} else {
String s = check(o);
if (!StringUtils.isBlank(s)) {
return s;
}
}
}
}
if (f.isAnnotationPresent(ListValue.class) && o != null && StringUtils.isNotBlank((String.valueOf(o)).trim())) {
ListValue annotation = f.getAnnotation(ListValue.class);
int index = -1;
if (type.isAssignableFrom(String.class)) {
String[] valueList = annotation.strValue();
Arrays.sort(valueList);
index = Arrays.binarySearch(valueList, o);
} else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
Integer v = (Integer) o;
int[] valueList = annotation.value();
Arrays.sort(valueList);
index = Arrays.binarySearch(valueList, v);
if (f.isAnnotationPresent(ListValue.class) && o != null && StringUtils.isNotBlank((String.valueOf(o)).trim())) {
ListValue annotation = f.getAnnotation(ListValue.class);
int index = -1;
if (type.isAssignableFrom(String.class)) {
String[] valueList = annotation.strValue();
Arrays.sort(valueList);
index = Arrays.binarySearch(valueList, o);
} else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
Integer v = (Integer) o;
int[] valueList = annotation.value();
Arrays.sort(valueList);
index = Arrays.binarySearch(valueList, v);
}
if (index < 0) {
return "参数校验错误[" + annotation.message() + "]";
}
}
if (index < 0) {
return "参数校验错误[" + annotation.message() + "]";
if (f.isAnnotationPresent(JsonFormat.class) && o != null) {
Date date = (Date) o;
JsonFormat annotation = f.getAnnotation(JsonFormat.class);
String format = DateUtils.format(date, annotation.pattern());
if (annotation.pattern().length() != format.length()) {
return "参数校验错误[时间格式不正确]";
}
}
}
if (f.isAnnotationPresent(JsonFormat.class) && o != null) {
Date date = (Date) o;
JsonFormat annotation = f.getAnnotation(JsonFormat.class);
String format = DateUtils.format(date, annotation.pattern());
if (annotation.pattern().length() != format.length()) {
return "参数校验错误[时间格式不正确]";
} else if (type.getName().contains("com")) {
// 对象类型
String check = check(o);
if (StringUtils.isNotBlank(check)) {
return check;
}
}
}
return "";
}
private static boolean isPrimate(Class aclass) {
return aclass == String.class
|| aclass == BigDecimal.class
|| aclass == Date.class
|| aclass == Integer.TYPE
|| aclass == Double.TYPE
|| aclass == Float.TYPE
|| aclass == Long.TYPE
|| aclass.isPrimitive();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment