java实体类的基本数据类型填充默认值的工具类
fillDefault方法
/**
* 将obj对象的8大基本数据类型以及BigDecimal、BigInteger的属性填充默认值
* @param obj
*/
public static final void fillDefault(Object obj) {
if(null == obj) {
return;
}
Class clazz = obj.getClass();
PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
for (PropertyDescriptor pd : pds) {
String prop = pd.getName();
if (null != prop && ID.equals(prop)) {
continue;
}
Class typeClass = getClassType(pd.getPropertyType());
if (Boolean.class.isAssignableFrom(typeClass)) {
Method readMethod = pd.getReadMethod();
Object value = null;
if (readMethod != null) {
if (!Modifier.isPublic(readMethod.getDeclaringClass()
.getModifiers())) {
readMethod.setAccessible(true);
}
try {
value = readMethod.invoke(obj);
} catch (Exception e) {
throw new RuntimeException("Could not read field[" + prop
+ "] in Class[" + clazz.getName() + "]!");
}
}
if (null == value) {
Method writeMethod = pd.getWriteMethod();
if (null != writeMethod) {
if (!Modifier.isPublic(writeMethod.getDeclaringClass()
.getModifiers())) {
writeMethod.setAccessible(true);
}
try {
writeMethod.invoke(obj, Boolean.FALSE);
} catch (Exception e) {
handleInvokeWriteMethodError(obj, writeMethod.getName(),
Boolean.FALSE, e);
}
}
}
} else if (Number.class.isAssignableFrom(typeClass)) {
// 初始化数字类型
Method readMethod = pd.getReadMethod();
Object value = null;
if (readMethod != null) {
if (!Modifier.isPublic(readMethod.getDeclaringClass()
.getModifiers())) {
readMethod.setAccessible(true);
}
try {
value = readMethod.invoke(obj);
} catch (Exception e) {
throw new RuntimeException("Could not read field[" + prop
+ "] in Class[" + clazz.getName() + "]!");
}
}
if (null == value) {
Method writeMethod = pd.getWriteMethod();
if (null != writeMethod) {
if (!Modifier.isPublic(writeMethod.getDeclaringClass()
.getModifiers())) {
writeMethod.setAccessible(true);
}
try {
if(isPrimitive(typeClass)) {
value = invokeStaticMethod(typeClass, "valueOf", "0");
}else {
Constructor constructor = typeClass.getConstructor(String.class);
value = constructor.newInstance("0");
}
writeMethod.invoke(obj, value);
} catch (Exception e) {
handleInvokeWriteMethodError(obj, writeMethod.getName(),
value, e);
}
}
}
}
}
}
最关键的地方是在对Number类型的数据进行初始化,BigDecimal和BigInteger跟其它number类型子类不一样,这两个类没有valueOf方法,只有带字符串的构造方法,因此需要区别对待一下。如下图:
getPropertyDescriptors方法获取实体类的所有PropertyDescriptor
/**
* 获取clazz自省的所有属性描述
*
* @param clazz
* @return
*/
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) {
PropertyDescriptor[] pds = null;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
pds = beanInfo.getPropertyDescriptors();
} catch (IntrospectionException e) {
throw new RuntimeException("Could not read BeanInfo from class '"
+ clazz.getName() + "'! ", e);
}
return pds;
}
getClassType方法返回基本数据类型的类类型
/**
* 返回基本数据类型的类类型
*
* @param clazz
* @return
*/
public static final Class getClassType(Class clazz) {
if (clazz.isPrimitive()) {
if (clazz == Boolean.TYPE) {
return Boolean.class;
} else if (clazz == Byte.TYPE) {
return Byte.class;
} else if (clazz == Character.TYPE) {
return Character.class;
} else if (clazz == Double.TYPE) {
return Double.class;
} else if (clazz == Float.TYPE) {
return Float.class;
} else if (clazz == Integer.TYPE) {
return Integer.class;
} else if (clazz == Long.TYPE) {
return Long.class;
} else if (clazz == Short.TYPE) {
return Short.class;
}
}
return clazz;
}