ValidatorUtil.java
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.framework.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.commons.lang3.StringUtils;
import com.framework.annotation.NotNull;
/**
* 自定义bean 验证
* @author 61004
*
*/
public class ValidatorUtil {
/**
* 验证非空
* @param obj
* @return
*/
@SuppressWarnings("all")
public static boolean notNull(Object obj) {
boolean result = true;
Class cls = obj.getClass();
NotNull notNullValidator = null;
try {
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
notNullValidator = field.getAnnotation(NotNull.class);
if (notNullValidator != null) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cls);
Method getMethod = pd.getReadMethod();// 获得get方法
if(getMethod.invoke(obj) == null) {
return false;
}else{
String fieldVal = getMethod.invoke(obj).toString();
if (StringUtils.isBlank(fieldVal)) {
result = false;
return result;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}