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 = "$$";
/**
* 实例化对象
*
* @param clazz
* @return 结果
*/
public static Object instantiate(Class<?> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return new RuntimeException("对象实例化失败!" + e.getMessage());
}
}
/**
* 调用Getter方法.
* 支持多级对象名.对象名.方法

View File

@ -29,6 +29,7 @@ import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Ref;
import java.util.*;
import java.util.stream.Collectors;
@ -71,6 +72,9 @@ public class BeanProperty {
// 属性
private Map<String, Object> props;
// 视图选项
private Map<String, Object> options;
// 类型为object时拥有子表单
private List<BeanProperty> children;
@ -94,7 +98,7 @@ public class BeanProperty {
* @param descriptor 解释器
* @return 结果
*/
public static BeanProperty form(PropertyDescriptor descriptor, Class<?> beanClass) {
public static BeanProperty form(PropertyDescriptor descriptor, Class<?> beanClass, Object instance) {
// 组装属性
BeanProperty property = new BeanProperty();
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;
}
@ -278,10 +287,11 @@ public class BeanProperty {
* @return 结果
*/
public static List<BeanProperty> from(Class<?> clazz) {
Object instance = ReflectionUtils.instantiate(clazz);
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz);
List<BeanProperty> properties = Arrays.stream(descriptors)
.filter(descriptor -> !"class".equals(descriptor.getName()))
.map(descriptor -> BeanProperty.form(descriptor, clazz))
.map(descriptor -> BeanProperty.form(descriptor, clazz, instance))
.filter(property -> !property.isReadonly())
.collect(Collectors.toList());
// 这里进行一些修复和初始化
@ -411,7 +421,7 @@ public class BeanProperty {
// 处理值生成策略此处可清除原策略
if (ArrayUtils.isNotEmpty(prop.generated())) {
property.extra.put(BeanProps.GENERATED, MergedAnnotations.from(prop.generated()).get(Generation.class).asMap());
if (StringUtils.isBlank((String)property.extra.get(BeanProps.COMPONENT))) {
if (StringUtils.isBlank((String) property.extra.get(BeanProps.COMPONENT))) {
property.extra.put(BeanProps.COMPONENT, "input-hidden");
}
} else {
@ -448,6 +458,21 @@ public class BeanProperty {
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) {
return "Attachment".equals(clazz.getSimpleName());
}