perf: 翻译防止递归导致的栈溢出
This commit is contained in:
+59
-49
@@ -151,69 +151,79 @@ public class TranslationAspect {
|
||||
return result;
|
||||
}
|
||||
|
||||
private final Set<Object> processedObjects = new HashSet<>();
|
||||
|
||||
private Object translateObject(Object record, List<SysDictItemCore> listDict) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json;
|
||||
try {
|
||||
json = mapper.writeValueAsString(record);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("json解析失败" + e.getMessage(), e);
|
||||
if (record == null || processedObjects.contains(record)) {
|
||||
return record;
|
||||
}
|
||||
JSONObject item;
|
||||
processedObjects.add(record);
|
||||
|
||||
try {
|
||||
item = JSONObject.parseObject(json);
|
||||
} catch (Exception e) {
|
||||
log.error("json解析失败" + e.getMessage(), e);
|
||||
return record;
|
||||
}
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json;
|
||||
try {
|
||||
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)) {
|
||||
field.setAccessible(true);
|
||||
for (Field field : oConvertUtils.getAllFields(record)) {
|
||||
field.setAccessible(true);
|
||||
|
||||
// 如果字段是复杂对象类型或者是List,进行递归翻译
|
||||
if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) {
|
||||
Object nestedRecord;
|
||||
try {
|
||||
nestedRecord = field.get(record);
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) {
|
||||
Object nestedRecord;
|
||||
try {
|
||||
nestedRecord = field.get(record);
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nestedRecord != null) {
|
||||
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
|
||||
String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName();
|
||||
if (nestedRecord != null) {
|
||||
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
|
||||
String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName();
|
||||
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
List<?> objList = (List<?>) nestedRecord;
|
||||
List<Object> translatedList = new ArrayList<>();
|
||||
for (Object obj : objList) {
|
||||
translatedList.add(translateObject(obj, listDict));
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
List<?> objList = (List<?>) nestedRecord;
|
||||
List<Object> translatedList = new ArrayList<>();
|
||||
for (Object obj : objList) {
|
||||
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) {
|
||||
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()))));
|
||||
}
|
||||
return item;
|
||||
} finally {
|
||||
processedObjects.remove(record);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private boolean isComplexObject(Class<?> type) {
|
||||
|
||||
Reference in New Issue
Block a user