feat:增加默认值的写入

This commit is contained in:
wangyu 2021-09-30 17:24:20 +08:00
parent 818f2e36ac
commit 76e8d1441d
2 changed files with 42 additions and 3 deletions

View File

@ -27,6 +27,20 @@ public class ReflectionUtils {
private static final String CGLIB_CLASS_SEPARATOR = "$$"; private static final String CGLIB_CLASS_SEPARATOR = "$$";
/**
* 实例化对象
*
* @param clazz
* @return 结果
*/
public static Object instantiate(Class<?> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return new RuntimeException("对象实例化失败!" + e.getMessage());
}
}
/** /**
* 调用Getter方法. * 调用Getter方法.
* 支持多级对象名.对象名.方法 * 支持多级对象名.对象名.方法

View File

@ -29,6 +29,7 @@ import java.beans.PropertyDescriptor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.sql.Ref;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -71,6 +72,9 @@ public class BeanProperty {
// 属性 // 属性
private Map<String, Object> props; private Map<String, Object> props;
// 视图选项
private Map<String, Object> options;
// 类型为object时拥有子表单 // 类型为object时拥有子表单
private List<BeanProperty> children; private List<BeanProperty> children;
@ -94,7 +98,7 @@ public class BeanProperty {
* @param descriptor 解释器 * @param descriptor 解释器
* @return 结果 * @return 结果
*/ */
public static BeanProperty form(PropertyDescriptor descriptor, Class<?> beanClass) { public static BeanProperty form(PropertyDescriptor descriptor, Class<?> beanClass, Object instance) {
// 组装属性 // 组装属性
BeanProperty property = new BeanProperty(); BeanProperty property = new BeanProperty();
property.setName(descriptor.getName()); property.setName(descriptor.getName());
@ -269,6 +273,11 @@ public class BeanProperty {
} }
} }
} }
// 写入默认值
Object value = ReflectionUtils.getFieldValue(instance, property.name);
if (null != value) {
property.option("initialValue", property.name);
}
return property; return property;
} }
@ -278,10 +287,11 @@ public class BeanProperty {
* @return 结果 * @return 结果
*/ */
public static List<BeanProperty> from(Class<?> clazz) { public static List<BeanProperty> from(Class<?> clazz) {
Object instance = ReflectionUtils.instantiate(clazz);
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz); PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz);
List<BeanProperty> properties = Arrays.stream(descriptors) List<BeanProperty> properties = Arrays.stream(descriptors)
.filter(descriptor -> !"class".equals(descriptor.getName())) .filter(descriptor -> !"class".equals(descriptor.getName()))
.map(descriptor -> BeanProperty.form(descriptor, clazz)) .map(descriptor -> BeanProperty.form(descriptor, clazz, instance))
.filter(property -> !property.isReadonly()) .filter(property -> !property.isReadonly())
.collect(Collectors.toList()); .collect(Collectors.toList());
// 这里进行一些修复和初始化 // 这里进行一些修复和初始化
@ -448,6 +458,21 @@ public class BeanProperty {
return this; return this;
} }
/**
* 设置当前对象的额外属性
*
* @param key
* @param value
* @return 结果
*/
public BeanProperty option(String key, Object value) {
if (null == options) {
options = new HashMap<>();
}
options.put(key, value);
return this;
}
private boolean isAttachment(Class<?> clazz) { private boolean isAttachment(Class<?> clazz) {
return "Attachment".equals(clazz.getSimpleName()); return "Attachment".equals(clazz.getSimpleName());
} }