From 76e8d1441d5b92836b70f689a6bd43100e662651 Mon Sep 17 00:00:00 2001 From: wangyu <727842003@qq.com> Date: Thu, 30 Sep 2021 17:24:20 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=E7=9A=84=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/utils/ReflectionUtils.java | 14 +++++++++ .../framework/beans/meta/BeanProperty.java | 31 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/flyfish-common/src/main/java/com/flyfish/framework/utils/ReflectionUtils.java b/flyfish-common/src/main/java/com/flyfish/framework/utils/ReflectionUtils.java index 1a08cb8..113b7f6 100644 --- a/flyfish-common/src/main/java/com/flyfish/framework/utils/ReflectionUtils.java +++ b/flyfish-common/src/main/java/com/flyfish/framework/utils/ReflectionUtils.java @@ -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方法. * 支持多级,如:对象名.对象名.方法 diff --git a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java index c58dfff..4e70f7e 100644 --- a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java +++ b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java @@ -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 props; + // 视图选项 + private Map options; + // 类型为object时,拥有子表单 private List 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 from(Class clazz) { + Object instance = ReflectionUtils.instantiate(clazz); PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz); List 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()); }