perf: 翻译防止递归导致的栈溢出

This commit is contained in:
2024-07-26 14:57:52 +08:00
parent f34c24698d
commit 4c5b1e99a2
@@ -151,69 +151,79 @@ public class TranslationAspect {
return result; return result;
} }
private final Set<Object> processedObjects = new HashSet<>();
private Object translateObject(Object record, List<SysDictItemCore> listDict) { private Object translateObject(Object record, List<SysDictItemCore> listDict) {
ObjectMapper mapper = new ObjectMapper(); if (record == null || processedObjects.contains(record)) {
String json;
try {
json = mapper.writeValueAsString(record);
} catch (JsonProcessingException e) {
log.error("json解析失败" + e.getMessage(), e);
return record; return record;
} }
JSONObject item; processedObjects.add(record);
try { try {
item = JSONObject.parseObject(json); ObjectMapper mapper = new ObjectMapper();
} catch (Exception e) { String json;
log.error("json解析失败" + e.getMessage(), e); try {
return record; json = mapper.writeValueAsString(record);
} } catch (JsonProcessingException e) {
log.error("json解析失败" + e.getMessage(), e);
return record;
}
JSONObject item;
try {
item = JSONObject.parseObject(json);
} catch (Exception e) {
log.error("json解析失败" + e.getMessage(), e);
return record;
}
for (Field field : oConvertUtils.getAllFields(record)) { for (Field field : oConvertUtils.getAllFields(record)) {
field.setAccessible(true); field.setAccessible(true);
// 如果字段是复杂对象类型或者是List,进行递归翻译 if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) {
if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) { Object nestedRecord;
Object nestedRecord; try {
try { nestedRecord = field.get(record);
nestedRecord = field.get(record); } catch (IllegalAccessException e) {
} catch (IllegalAccessException e) { continue;
continue; }
}
if (nestedRecord != null) { if (nestedRecord != null) {
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName(); String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName();
if (List.class.isAssignableFrom(field.getType())) { if (List.class.isAssignableFrom(field.getType())) {
List<?> objList = (List<?>) nestedRecord; List<?> objList = (List<?>) nestedRecord;
List<Object> translatedList = new ArrayList<>(); List<Object> translatedList = new ArrayList<>();
for (Object obj : objList) { for (Object obj : objList) {
translatedList.add(translateObject(obj, listDict)); translatedList.add(translateObject(obj, listDict));
}
item.put(jsonFieldName, translatedList);
} else {
Object translatedNestedRecord = translateObject(nestedRecord, listDict);
item.put(jsonFieldName, translatedNestedRecord);
} }
item.put(jsonFieldName, translatedList);
} else {
Object translatedNestedRecord = translateObject(nestedRecord, listDict);
item.put(jsonFieldName, translatedNestedRecord);
} }
} }
if (field.getAnnotation(Dict.class) != null) {
String code = field.getAnnotation(Dict.class).dicCode();
String text = field.getAnnotation(Dict.class).dicText();
String table = field.getAnnotation(Dict.class).dictTable();
String key = String.valueOf(item.get(field.getName()));
String textValue = translateDictValue(code, text, table, key, listDict);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
}
if (Objects.equals(field.getType().getName(), "java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
}
} }
if (field.getAnnotation(Dict.class) != null) { return item;
String code = field.getAnnotation(Dict.class).dicCode(); } finally {
String text = field.getAnnotation(Dict.class).dicText(); processedObjects.remove(record);
String table = field.getAnnotation(Dict.class).dictTable();
String key = String.valueOf(item.get(field.getName()));
String textValue = translateDictValue(code, text, table, key, listDict);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
}
if (Objects.equals(field.getType().getName(), "java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
}
} }
return item;
} }
private boolean isComplexObject(Class<?> type) { private boolean isComplexObject(Class<?> type) {