feat: 提供诸多简化配置,更轻松实现功能
This commit is contained in:
parent
2722a86949
commit
4909c17f90
@ -0,0 +1,15 @@
|
|||||||
|
package com.flyfish.framework.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序用
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface Order {
|
||||||
|
|
||||||
|
int value() default 0;
|
||||||
|
}
|
@ -21,4 +21,12 @@ public @interface PropertyGroup {
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
String code();
|
String code();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否默认
|
||||||
|
* 指定为true,默认将未指定分组的项归到该类
|
||||||
|
*
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
boolean initial() default false;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.flyfish.framework.beans.meta;
|
package com.flyfish.framework.beans.meta;
|
||||||
|
|
||||||
|
import com.flyfish.framework.annotations.PropertyGroup;
|
||||||
import com.flyfish.framework.annotations.PropertyGroups;
|
import com.flyfish.framework.annotations.PropertyGroups;
|
||||||
import com.flyfish.framework.bean.Result;
|
import com.flyfish.framework.bean.Result;
|
||||||
import com.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
|
import com.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
|
||||||
@ -90,12 +91,21 @@ public class BeanController {
|
|||||||
} else {
|
} else {
|
||||||
info.setCode(StringFormats.camel2Line(ClassUtils.getShortClassName(clazz)));
|
info.setCode(StringFormats.camel2Line(ClassUtils.getShortClassName(clazz)));
|
||||||
}
|
}
|
||||||
|
// 解析属性
|
||||||
|
info.setProperties(BeanProperty.from(clazz));
|
||||||
|
// 设置分组信息
|
||||||
if (clazz.isAnnotationPresent(PropertyGroups.class)) {
|
if (clazz.isAnnotationPresent(PropertyGroups.class)) {
|
||||||
PropertyGroups groups = clazz.getAnnotation(PropertyGroups.class);
|
PropertyGroups groups = clazz.getAnnotation(PropertyGroups.class);
|
||||||
info.setGroups(Arrays.stream(groups.value()).map(group -> new BeanPropertyGroup(group.name(), group.code()))
|
info.setGroups(Arrays.stream(groups.value()).map(group -> new BeanPropertyGroup(group.name(), group.code()))
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
|
// 尝试补全分组
|
||||||
|
Arrays.stream(groups.value()).filter(PropertyGroup::initial).findFirst()
|
||||||
|
.ifPresent(initial -> info.getProperties()
|
||||||
|
.stream()
|
||||||
|
.filter(property -> StringUtils.isBlank(property.getGroup()))
|
||||||
|
.forEach(property -> property.setGroup(initial.code()))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
info.setProperties(BeanProperty.from(clazz));
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import com.flyfish.framework.utils.ReflectionUtils;
|
|||||||
import com.flyfish.framework.utils.StringFormats;
|
import com.flyfish.framework.utils.StringFormats;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.apache.commons.collections4.ListUtils;
|
import org.apache.commons.collections4.ListUtils;
|
||||||
import org.apache.commons.lang3.ClassUtils;
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -90,7 +91,13 @@ public class BeanProperty {
|
|||||||
property.setTitle(props.inherited() ? parentName + props.title() : props.title());
|
property.setTitle(props.inherited() ? parentName + props.title() : props.title());
|
||||||
property.setDescription(props.description());
|
property.setDescription(props.description());
|
||||||
property.setReadonly(props.readonly());
|
property.setReadonly(props.readonly());
|
||||||
|
property.setGroup(props.group());
|
||||||
|
Order order = AnnotationUtils.findAnnotation(field, Order.class);
|
||||||
|
if (null != order) {
|
||||||
|
property.setOrder(order.value());
|
||||||
|
} else {
|
||||||
property.setOrder(props.order());
|
property.setOrder(props.order());
|
||||||
|
}
|
||||||
} else if (strict) {
|
} else if (strict) {
|
||||||
property.setReadonly(true);
|
property.setReadonly(true);
|
||||||
return property;
|
return property;
|
||||||
@ -201,7 +208,9 @@ public class BeanProperty {
|
|||||||
.map(descriptor -> BeanProperty.form(descriptor, clazz))
|
.map(descriptor -> BeanProperty.form(descriptor, clazz))
|
||||||
.filter(property -> !property.isReadonly())
|
.filter(property -> !property.isReadonly())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
// 这里进行一些修复和初始化
|
||||||
List<BeanProperty> result = ListUtils.union(parseExtras(clazz, properties), properties);
|
List<BeanProperty> result = ListUtils.union(parseExtras(clazz, properties), properties);
|
||||||
|
// 排序
|
||||||
result.sort(Comparator.comparingInt(a -> a.order));
|
result.sort(Comparator.comparingInt(a -> a.order));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -241,6 +250,9 @@ public class BeanProperty {
|
|||||||
if (prop.order() != 0) {
|
if (prop.order() != 0) {
|
||||||
property.order = prop.order();
|
property.order = prop.order();
|
||||||
}
|
}
|
||||||
|
if (StringUtils.isNotBlank(prop.group())) {
|
||||||
|
property.group = prop.group();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
BeanProperty property = new BeanProperty();
|
BeanProperty property = new BeanProperty();
|
||||||
property.setType(BeanPropertyType.STRING);
|
property.setType(BeanPropertyType.STRING);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user