use of top.tangyh.basic.annotation.echo.Echo in project lamp-util by zuihou.
the class EchoService method iterationWrite.
private void iterationWrite(Object obj, Map<LoadKey, Map<Serializable, Object>> typeMap, int depth, String... ignoreFields) {
// 解析方法上的注解,计算出obj对象中所有需要查询的数据
List<Field> fields = ClassManager.getFields(obj.getClass());
for (Field field : fields) {
FieldParam fieldParam = getFieldParam(obj, field, typeMap, innerTypeMap -> write(ReflectUtil.getFieldValue(obj, field), innerTypeMap, depth + 1, ignoreFields), ignoreFields);
if (fieldParam == null) {
continue;
}
Echo inField = fieldParam.getEcho();
Object actualValue = fieldParam.getActualValue();
Object originalValue = fieldParam.getOriginalValue();
String fieldName = fieldParam.getFieldName();
String ref = inField.ref();
LoadKey loadKey = fieldParam.getLoadKey();
Object echoValue = getEchoValue(inField, actualValue, originalValue, loadKey, typeMap);
if (echoValue == null) {
continue;
}
if (echoValue instanceof Map && ((Map<?, ?>) echoValue).isEmpty()) {
continue;
}
// feign 接口序列化 丢失类型
if (echoValue instanceof Map && !Object.class.equals(inField.beanClass())) {
echoValue = JsonUtil.parse(JsonUtil.toJson(echoValue), inField.beanClass());
}
if (StrUtil.isNotEmpty(ref)) {
ReflectUtil.setFieldValue(obj, ref, echoValue);
}
// 将新的值 反射 到指定字段
if (obj instanceof EchoVO) {
EchoVO vo = (EchoVO) obj;
vo.getEchoMap().put(fieldName, echoValue);
} else if (originalValue instanceof RemoteData) {
RemoteData remoteData = (RemoteData) originalValue;
remoteData.setData(echoValue);
} else {
ReflectUtil.setFieldValue(obj, field, echoValue);
}
}
}
use of top.tangyh.basic.annotation.echo.Echo in project lamp-util by zuihou.
the class EchoService method getFieldParam.
/**
* 提取参数
*
* @param obj 当前对象
* @param field 当前字段
* @param typeMap 待查询的集合
* @param consumer 字段为复杂类型时的回调处理
* @param ignoreFields 忽略回显的字段
* @return 字段参数
*/
private FieldParam getFieldParam(Object obj, Field field, Map<LoadKey, Map<Serializable, Object>> typeMap, Consumer<Map<LoadKey, Map<Serializable, Object>>> consumer, String... ignoreFields) {
String key = obj.getClass().getName() + "###" + field.getName();
FieldParam fieldParam;
// 是否排除
if (ArrayUtil.contains(ignoreFields, field.getName())) {
log.debug("已经忽略{}字段的解析", field.getName());
return null;
}
// 类型
if (isNotBaseType(field)) {
consumer.accept(typeMap);
return null;
}
if (CACHE.containsKey(key)) {
fieldParam = CACHE.get(key);
} else {
// 是否标记@Echo注解
Echo echo = field.getDeclaredAnnotation(Echo.class);
LoadKey loadKey = new LoadKey(echo);
fieldParam = FieldParam.builder().echo(echo).loadKey(loadKey).fieldName(field.getName()).build();
CACHE.put(key, fieldParam);
}
field.setAccessible(true);
Object originalValue = ReflectUtil.getFieldValue(obj, field);
if (originalValue == null) {
log.debug("字段[{}]为空,跳过", field.getName());
return null;
}
Serializable actualValue = getActualValue(fieldParam.getEcho(), originalValue);
if (ObjectUtil.isEmpty(actualValue)) {
return null;
}
fieldParam.setOriginalValue(originalValue);
fieldParam.setActualValue(actualValue);
return fieldParam;
}
use of top.tangyh.basic.annotation.echo.Echo in project lamp-util by zuihou.
the class EchoServiceImpl method getFieldParam.
/**
* 提取参数
*
* @param obj 当前对象
* @param field 当前字段
* @param typeMap 待查询的集合
* @param consumer 字段为复杂类型时的回调处理
* @param ignoreFields 忽略回显的字段
* @return 字段参数
*/
private FieldParam getFieldParam(Object obj, Field field, Map<LoadKey, Map<Serializable, Object>> typeMap, Consumer<Map<LoadKey, Map<Serializable, Object>>> consumer, String... ignoreFields) {
String key = obj.getClass().getName() + "###" + field.getName();
FieldParam fieldParam;
// 是否排除
if (ArrayUtil.contains(ignoreFields, field.getName())) {
log.debug("已经忽略{}字段的解析", field.getName());
return null;
}
// 类型
if (isNotBaseType(field)) {
consumer.accept(typeMap);
return null;
}
if (CACHE.containsKey(key)) {
fieldParam = CACHE.get(key);
} else {
// 是否标记@Echo注解
Echo echo = field.getDeclaredAnnotation(Echo.class);
LoadKey loadKey = new LoadKey(echo);
fieldParam = FieldParam.builder().echo(echo).loadKey(loadKey).fieldName(field.getName()).build();
CACHE.put(key, fieldParam);
}
field.setAccessible(true);
Object originalValue = ReflectUtil.getFieldValue(obj, field);
if (originalValue == null) {
log.debug("字段[{}]为空,跳过", field.getName());
return null;
}
Serializable actualValue = getActualValue(fieldParam.getEcho(), originalValue);
if (ObjectUtil.isEmpty(actualValue)) {
return null;
}
fieldParam.setOriginalValue(originalValue);
fieldParam.setActualValue(actualValue);
return fieldParam;
}
use of top.tangyh.basic.annotation.echo.Echo in project lamp-util by zuihou.
the class ClassManager method getFields.
public static List<Field> getFields(Class<?> clazz) {
if (CACHE.containsKey(clazz.getName())) {
return CACHE.get(clazz.getName());
}
Field[] declaredFields = ReflectUtil.getFields(clazz);
int mod;
// 循环遍历所有的属性进行判断
List<Field> fieldList = new ArrayList<>();
for (Field field : declaredFields) {
mod = field.getModifiers();
// 如果是 static, final, volatile, transient 的字段,则直接跳过
if (Modifier.isStatic(mod) || Modifier.isFinal(mod) || Modifier.isVolatile(mod)) {
continue;
}
Echo echo = field.getDeclaredAnnotation(Echo.class);
if (echo == null) {
continue;
}
if (StrUtil.hasEmpty(echo.api())) {
log.warn("类 {} 属性 [{}] api 为空。", clazz.getName(), field.getName());
continue;
}
fieldList.add(field);
}
CACHE.put(clazz.getName(), fieldList);
return fieldList;
}
use of top.tangyh.basic.annotation.echo.Echo in project lamp-util by zuihou.
the class EchoServiceImpl method iterationWrite.
private void iterationWrite(Object obj, Map<LoadKey, Map<Serializable, Object>> typeMap, int depth, String... ignoreFields) {
// 解析方法上的注解,计算出obj对象中所有需要查询的数据
List<Field> fields = ClassManager.getFields(obj.getClass());
for (Field field : fields) {
FieldParam fieldParam = getFieldParam(obj, field, typeMap, innerTypeMap -> write(ReflectUtil.getFieldValue(obj, field), innerTypeMap, depth + 1, ignoreFields), ignoreFields);
if (fieldParam == null) {
continue;
}
Echo inField = fieldParam.getEcho();
Object actualValue = fieldParam.getActualValue();
Object originalValue = fieldParam.getOriginalValue();
String fieldName = fieldParam.getFieldName();
String ref = inField.ref();
LoadKey loadKey = fieldParam.getLoadKey();
Object echoValue = getEchoValue(inField, actualValue, originalValue, loadKey, typeMap);
if (echoValue == null) {
continue;
}
if (echoValue instanceof Map && ((Map<?, ?>) echoValue).isEmpty()) {
continue;
}
// feign 接口序列化 丢失类型
if (echoValue instanceof Map && !Object.class.equals(inField.beanClass())) {
echoValue = JsonUtil.parse(JsonUtil.toJson(echoValue), inField.beanClass());
}
if (StrUtil.isNotEmpty(ref)) {
ReflectUtil.setFieldValue(obj, ref, echoValue);
}
// 将新的值 反射 到指定字段
if (obj instanceof EchoVO) {
EchoVO vo = (EchoVO) obj;
vo.getEchoMap().put(fieldName, echoValue);
} else {
ReflectUtil.setFieldValue(obj, field, echoValue);
}
}
}
Aggregations