为对象赋默认值工具类
为对象赋默认值工具类
需求
返回响应对象时,对字段为null 设置默认值
version 1.0
工具类
import lombok.Data;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Objects;
/**
* 为对象赋默认值工具类
* 描述: 只支持为Long,Integer,Double,String,BigDecimal类型赋默认值
*
* @author lyn
* @date 2022/3/28 15:41
*/
public class DefaultUtil {
public static void setDefaultValue(Object obj) {
if (Objects.isNull(obj)) {
throw new RuntimeException("不能为null");
}
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//对于字段上有注解的赋默认值
if (field.isAnnotationPresent(DefaultValue.class)) {
Class<?> type = field.getType();
// 设置些属性是可以访问的
boolean isStatic = Modifier.isStatic(field.getModifiers());
if (isStatic) {
continue;
}
// 设置些属性是可以访问的
field.setAccessible(true);
// 得到此属性的值
try {
Object val = field.get(obj);
if (Objects.isNull(val)) {
String value = field.getAnnotation(DefaultValue.class).value();
if (type.equals(String.class)) {
field.set(obj, value);
} else if (type.equals(Integer.class)) {
field.set(obj, Integer.parseInt(value));
} else if (type.equals(Double.class)) {
field.set(obj, Double.parseDouble(value));
} else if (type.equals(Long.class)) {
field.set(obj, Long.parseLong(value));
} else if (type.equals(BigDecimal.class)) {
field.set(obj, new BigDecimal(value));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
/**
* 测试实体类
*/
@Data
private static class TestDTO {
@DefaultValue("0")
private Long id;
@DefaultValue("未知")
private String name;
@DefaultValue("0")
private Integer order;
@DefaultValue("0.00")
private BigDecimal price;
@DefaultValue("0.0")
private Double value;
private String message;
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
TestDTO dto = new TestDTO();
DefaultUtil.setDefaultValue(dto);
System.out.println(dto);
// DefaultUtil.TestDTO(id=0, name=未知, order=0, price=0.00, value=0.0, message=null)
}
}
默认值注解
/**
* 默认值注解
* 描述: 请谨慎使用
*
* @author lyn
* @date 2022/3/28 15:11
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DefaultValue {
String value() ;
}
version 1.1
对可以走系统默认值的,不必要加注解
工具类
import lombok.Data;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Objects;
/**
* 为对象赋默认值工具类
* 描述: 只支持为Long,Integer,Double,String,BigDecimal类型赋默认值
*
* @author lyn
* @date 2022/3/28 15:41
*/
public class DefaultUtil {
/**
* @param obj 对象
* @param flag 是否为全部字段设置默认值
*/
private static void setDefaultValue(Object obj, boolean flag) {
if (Objects.isNull(obj)) {
throw new RuntimeException("不能为null");
}
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
Class<?> type = field.getType();
// 设置些属性是可以访问的
boolean isStatic = Modifier.isStatic(field.getModifiers());
if (isStatic) {
continue;
}
// 设置些属性是可以访问的
field.setAccessible(true);
//对于字段上有注解的赋默认值
try {
Object val = field.get(obj);
if (Objects.isNull(val)) {
if (field.isAnnotationPresent(DefaultValue.class)) {
// 得到此属性的值
String value = field.getAnnotation(DefaultValue.class).value();
boolean isIgnore = field.getAnnotation(DefaultValue.class).isIgnore();
if (isIgnore) {
continue;
}
if (type.equals(String.class)) {
field.set(obj, value);
} else if (type.equals(Integer.class)) {
field.set(obj, Integer.parseInt(value));
} else if (type.equals(Double.class)) {
field.set(obj, Double.parseDouble(value));
} else if (type.equals(Long.class)) {
field.set(obj, Long.parseLong(value));
} else if (type.equals(BigDecimal.class)) {
field.set(obj, new BigDecimal(value));
}
} else if (flag) {
if (type.equals(String.class)) {
field.set(obj, "");
} else if (type.equals(Integer.class)) {
field.set(obj, 0);
} else if (type.equals(Double.class)) {
field.set(obj, 0.0);
} else if (type.equals(Long.class)) {
field.set(obj, 0L);
} else if (type.equals(BigDecimal.class)) {
field.set(obj, new BigDecimal("0.00"));
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 为所有为null的字段赋默认值
* 多对数字段赋默认值,没有默认值注解的走系统默认值
* 如果不想设默认值的字段可用@DefaultValue(isIgnore = true)
*
* @param obj 对象
*/
public static void setAllDefaultValues(Object obj) {
setDefaultValue(obj, true);
}
/***
* 为有默认值注解的字段赋值
* 为少数字段赋默认值
* @param obj 对象
*/
public static void setAnnDefaultValues(Object obj) {
setDefaultValue(obj, false);
}
/**
* 测试实体类
*/
@Data
private static class TestDTO {
@DefaultValue(isIgnore = true)
private Long id;
@DefaultValue("未知")
private String name;
@DefaultValue("0")
private Integer order;
@DefaultValue("0.00")
private BigDecimal price;
@DefaultValue("0.0")
private Double value;
private String message;
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
TestDTO dto = new TestDTO();
DefaultUtil.setAnnDefaultValues(dto);
System.out.println(dto);
// DefaultUtil.TestDTO(id=null, name=未知, order=0, price=0.00, value=0.0, message=null)
// DefaultUtil.setAllDefaultValues(dto);
//System.out.println(dto);
//DefaultUtil.TestDTO(id=null, name=未知, order=0, price=0.00, value=0.0, message=)
}
}
注解
/**
* 默认值注解
* 描述: 请谨慎使用
*
* @author lyn
* @date 2022/3/28 15:11
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DefaultValue {
/**
* 默认值
* @return
*/
String value() default "" ;
/**
* 是否忽略
* @return
*/
boolean isIgnore() default false;
}