public class ValidateUtils {
public static <T> List<String> validate(T obj, List<String> options){
List<String> string = new ArrayList<>();
for(String o:options){
try {
Field f = obj.getClass().getDeclaredField(o);
f.setAccessible(true);
Annotation[] annotations = f.getAnnotations();
System.out.println(Arrays.asList(annotations));
if(f.get(obj) == null){
string.add(o + "不能为空");
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return string;
}
}
List<String> list = ValidateUtils.validate(device, Arrays.asList("code","deviceTypeId","customerId","projectId"));
System.out.println(list);