feat: 增加bean读取器
This commit is contained in:
parent
80b71ffa08
commit
81c27d6856
@ -0,0 +1,40 @@
|
||||
package com.flyfish.framework.beans.meta;
|
||||
|
||||
import com.flyfish.framework.bean.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* bean控制器,可以通过类直接返回bean信息
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("beans")
|
||||
@Slf4j
|
||||
public class BeanController {
|
||||
|
||||
/**
|
||||
* 通过类标识返回bean信息
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("")
|
||||
public Result<List<BeanProperty>> beanInfo(String className) {
|
||||
try {
|
||||
return Result.ok(Arrays.stream(BeanUtils.getPropertyDescriptors(Class.forName(className)))
|
||||
.map(BeanProperty::form)
|
||||
.collect(Collectors.toList()));
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.flyfish.framework.beans.meta;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* bean的属性描述
|
||||
* 对应js的类型
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@Data
|
||||
public class BeanProperty {
|
||||
|
||||
// 属性名称
|
||||
private String name;
|
||||
|
||||
// bean属性的类型,js类型
|
||||
private BeanPropertyType type;
|
||||
|
||||
// 类型为object时,拥有子表单
|
||||
private List<BeanProperty> children;
|
||||
|
||||
/**
|
||||
* 来自属性解释器构造
|
||||
* 支持对象嵌套
|
||||
*
|
||||
* @param descriptor 解释器
|
||||
* @return 结果
|
||||
*/
|
||||
public static BeanProperty form(PropertyDescriptor descriptor) {
|
||||
BeanProperty property = new BeanProperty();
|
||||
property.setName(descriptor.getName());
|
||||
property.setType(BeanPropertyType.of(descriptor.getPropertyType()));
|
||||
if (property.getType() == BeanPropertyType.OBJECT) {
|
||||
List<BeanProperty> children = Arrays.stream(BeanUtils.getPropertyDescriptors(descriptor.getPropertyType()))
|
||||
.map(BeanProperty::form)
|
||||
.collect(Collectors.toList());
|
||||
property.setChildren(children);
|
||||
}
|
||||
return property;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.flyfish.framework.beans.meta;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* bean属性的类型
|
||||
* 包含java类型到js的映射
|
||||
* 我们通过类型自动解析bean的缺省控件,并自动映射
|
||||
* @author wangyu
|
||||
*/
|
||||
@Getter
|
||||
public enum BeanPropertyType {
|
||||
|
||||
STRING("String", CharSequence.class),
|
||||
NUMBER("Number", Number.class),
|
||||
BOOLEAN("Boolean", Boolean.class),
|
||||
DATE("Date", Date.class),
|
||||
ENUM("Enum", Enum.class),
|
||||
LIST("Array", Collection.class),
|
||||
OBJECT("Object", Object.class);
|
||||
|
||||
BeanPropertyType(String name, Class<?> ...classes) {
|
||||
this.name = name;
|
||||
this.classes = Arrays.asList(classes);
|
||||
}
|
||||
|
||||
private final String name;
|
||||
|
||||
private final List<Class<?>> classes;
|
||||
|
||||
public static BeanPropertyType of(Class<?> clazz) {
|
||||
for (BeanPropertyType type : values()) {
|
||||
if (type.classes.stream().anyMatch(item -> item.isAssignableFrom(clazz))) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user