防止配置信息反序列化时导致信息丢失,改进为编码后存储。简化存储格式,避免丢失。

This commit is contained in:
makejava 2023-06-16 10:49:34 +08:00
parent 0651597b65
commit eae8b4cff9
2 changed files with 22 additions and 9 deletions

View File

@ -7,12 +7,14 @@ import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiClass; import com.intellij.psi.PsiClass;
import com.sjhy.plugin.dict.GlobalDict; import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.tool.JSON;
import com.sjhy.plugin.tool.ReflectionUtils; import com.sjhy.plugin.tool.ReflectionUtils;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Data; import lombok.Data;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -25,7 +27,7 @@ import java.util.TreeMap;
*/ */
@Data @Data
public class TableInfoSettingsDTO { public class TableInfoSettingsDTO {
private Map<String, TableInfoDTO> tableInfoMap; private Map<String, String> tableInfoMap;
public TableInfoSettingsDTO() { public TableInfoSettingsDTO() {
this.tableInfoMap = new TreeMap<>(); this.tableInfoMap = new TreeMap<>();
@ -71,9 +73,9 @@ public class TableInfoSettingsDTO {
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public TableInfo readTableInfo(PsiClass psiClass) { public TableInfo readTableInfo(PsiClass psiClass) {
String key = generateKey(psiClass); String key = generateKey(psiClass);
TableInfoDTO dto = this.tableInfoMap.get(key); TableInfoDTO dto = decode(this.tableInfoMap.get(key));
dto = new TableInfoDTO(dto, psiClass); dto = new TableInfoDTO(dto, psiClass);
this.tableInfoMap.put(key, dto); this.tableInfoMap.put(key, encode(dto));
return dto.toTableInfo(psiClass); return dto.toTableInfo(psiClass);
} }
@ -86,10 +88,10 @@ public class TableInfoSettingsDTO {
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public TableInfo readTableInfo(DbTable dbTable) { public TableInfo readTableInfo(DbTable dbTable) {
String key = generateKey(dbTable); String key = generateKey(dbTable);
TableInfoDTO dto = this.tableInfoMap.get(key); TableInfoDTO dto = decode(this.tableInfoMap.get(key));
// 表可能新增了字段需要重新合并保存 // 表可能新增了字段需要重新合并保存
dto = new TableInfoDTO(dto, dbTable); dto = new TableInfoDTO(dto, dbTable);
this.tableInfoMap.put(key, dto); this.tableInfoMap.put(key, encode(dto));
return dto.toTableInfo(dbTable); return dto.toTableInfo(dbTable);
} }
@ -112,7 +114,7 @@ public class TableInfoSettingsDTO {
Messages.showInfoMessage(tableInfo.getName() + "表配置信息保存失败", GlobalDict.TITLE_INFO); Messages.showInfoMessage(tableInfo.getName() + "表配置信息保存失败", GlobalDict.TITLE_INFO);
return; return;
} }
this.tableInfoMap.put(key, TableInfoDTO.valueOf(tableInfo)); this.tableInfoMap.put(key, encode(TableInfoDTO.valueOf(tableInfo)));
} }
/** /**
@ -122,7 +124,7 @@ public class TableInfoSettingsDTO {
*/ */
public void resetTableInfo(DbTable dbTable) { public void resetTableInfo(DbTable dbTable) {
String key = generateKey(dbTable); String key = generateKey(dbTable);
this.tableInfoMap.put(key, new TableInfoDTO(null, dbTable)); this.tableInfoMap.put(key, encode(new TableInfoDTO(null, dbTable)));
} }
/** /**
@ -134,4 +136,15 @@ public class TableInfoSettingsDTO {
String key = generateKey(dbTable); String key = generateKey(dbTable);
this.tableInfoMap.remove(key); this.tableInfoMap.remove(key);
} }
private static String encode(TableInfoDTO tableInfo) {
return Base64.getEncoder().encodeToString(JSON.toJson(tableInfo).getBytes());
}
private static TableInfoDTO decode(String base64) {
if (StringUtils.isEmpty(base64)) {
return null;
}
return JSON.parse(new String(Base64.getDecoder().decode(base64)), TableInfoDTO.class);
}
} }

View File

@ -17,7 +17,7 @@ import java.util.Objects;
* @version 1.0.0 * @version 1.0.0
* @date 2021/08/14 15:20 * @date 2021/08/14 15:20
*/ */
@State(name = "EasyCodeTableSetting", storages = @Storage("easyCodeTableSetting.xml")) @State(name = "EasyCodeTableSetting", storages = @Storage("easyCodeTableSettingEncode.xml"))
public class TableInfoSettingsServiceImpl implements TableInfoSettingsService { public class TableInfoSettingsServiceImpl implements TableInfoSettingsService {
private TableInfoSettingsDTO tableInfoSettings = new TableInfoSettingsDTO(); private TableInfoSettingsDTO tableInfoSettings = new TableInfoSettingsDTO();