自定义@NotNull注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @inteface NotNull{
}
public class Conditions{
public static Boolean hasNullOrEmpty(Object... args) {
for (Object arg : args) {
if (arg instanceof String && Strings.isNullOrEmpty(String.valueOf(arg))) {
return true;
}
if (arg == null) {
return true;
}else{
Field[] fields = arg.getClass().getDeclaredFields();
for (Field field : fields) {
NotNull notNull = field.getAnnotation(NotNull.class);
try {
if (notNull != null) {
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
}
if (field.get(arg) == null) {
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(false);
}
return true;
}
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(false);
}
}
} catch (IllegalAccessException e) {
Throwables.propagate(e);
}
}
}
}
return false;
}
public static List<String> getNullableField(Object object) {
List<String> result = new ArrayList<>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
NotNull notNull = field.getAnnotation(NotNull.class);
try {
if (notNull != null) {
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
}
if (field.get(object) == null) {
result.add(field.getName());
}
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(false);
}
}
} catch (IllegalAccessException e) {
Throwables.propagate(e);
}
}
return result;
}
}