@Target({ElementType.FIELD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotAllowEmpty {
String msg() default "不能为空";
String code() default "07";
}
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
@Slf4j
public class ValidateUtil {
@SafeVarargs
public static <T> void isNotEmpty(T... t) {
for (T objSource : t) {
if (!isObjcetType(objSource.getClass())){
Assert.fail(objSource + "不是一个对象");
}
try {
Class<?> clazz = objSource.getClass();
Field[] fields = clazz.getDeclaredFields();
classIsNotEmpty(clazz, objSource, fields);
fieldsIsNotEmpty(objSource, clazz, fields);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
log.error(e.getMessage(), e);
}
}
}
private static <T> void classIsNotEmpty(Class<?> clazz, T obj, Field[] fields) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
NotAllowEmpty objAnnotation = clazz.getAnnotation(NotAllowEmpty.class);
if(objAnnotation != null){
for (Field f : fields){
f.setAccessible(true);
Class<?> fieldType = f.getType();
String fieldName = f.getName();
Object fieldValue = f.get(obj);
judgeFieldsType(obj, clazz, objAnnotation, fieldType, fieldName,fieldValue);
}
}
}
private static <T> void fieldsIsNotEmpty(T obj, Class<?> clazz, Field[] fields) throws IllegalAccessException,, NoSuchMethodException, InvocationTargetException {
for (Field f : fields) {
f.setAccessible(true);
Class<?> fieldType = f.getType();
String fieldName = f.getName();
Object fieldValue = f.get(obj);
if (isObjcetType(fieldType)){
Object invoke = getGetMethod(obj, clazz, fieldName);
if (invoke != null){
isNotEmpty(invoke);
}
}
NotAllowEmpty fieldAnnotation = f.getAnnotation(NotAllowEmpty.class);
if (fieldAnnotation != null) {
judgeFieldsType(obj, clazz, fieldAnnotation, fieldType, fieldName, fieldValue);
}
}
}
private static <T> void judgeFieldsType(T obj, Class<?> clazz, NotAllowEmpty annotation, Class<?> fieldType, String fieldName, Object fieldValue) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if(isStringType(fieldType)){
if (StringUtils.isEmpty(fieldValue)){
throw new MyException(annotation.code(), fieldName + "属性"+ annotation.msg());
}
}else if(isObjcetType(fieldType)){
if (fieldValue == null){
throw new MyException(annotation.code(), fieldName + "对象属性"+ annotation.msg());
}
}else if (isCollectionType(fieldType)){
Object invoke = getGetMethod(obj, clazz, fieldName);
boolean flag = CollectionUtils.isEmpty((Collection<?>) invoke);
if (flag){
throw new MyException(annotation.code(), fieldName + "集合属性" + annotation.msg());
}
}else if (isArrayType(fieldType)){
Object invoke = getGetMethod(obj, clazz, fieldName);
if (invoke == null || Array.getLength(invoke) == 0){
throw new MyException(annotation.code(), fieldName + "数组属性" + annotation.msg());
}
}
}
private static <T> Object getGetMethod(T obj, Class<?> clazz, String fieldName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
StringBuilder sb = new StringBuilder();
sb.append("get");
sb.append(fieldName.substring(0, 1).toUpperCase());
sb.append(fieldName.substring(1));
Method method = clazz.getDeclaredMethod(sb.toString());
method.setAccessible(true);
return method.invoke(obj);
}
private static boolean isStringType(Class<?> clazz){
return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
}
private static boolean isCollectionType(Class<?> clazz){
return Collection.class.isAssignableFrom(clazz);
}
private static boolean isArrayType(Class<?> clazz){
return clazz.isArray();
}
private static boolean isObjcetType(Class<?> clazz){
return clazz.getClassLoader() != null;
}
}