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;
	}
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值