Skip to content

Commit 1aab64b

Browse files
committed
sync(common): 同步 common 模块
1 parent 3db7027 commit 1aab64b

6 files changed

Lines changed: 147 additions & 55 deletions

File tree

src/main/java/cn/gudqs7/plugins/common/enums/PluginSettingEnum.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public enum PluginSettingEnum {
6767
* 给URL设置一个前缀, 设置后, IP 失效; 此前缀不包含最后的 /
6868
*/
6969
DEFAULT_URL_PREFIX("default.url", PluginSettingTypeEnum.STRING),
70+
/**
71+
* 给桌面路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
72+
*/
73+
DEFAULT_DESKTOP_PATH("default.path.desktop", PluginSettingTypeEnum.STRING),
74+
/**
75+
* 给下载路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
76+
*/
77+
DEFAULT_DOWNLOAD_PATH("default.path.download", PluginSettingTypeEnum.STRING),
78+
/**
79+
* 给项目路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
80+
*/
81+
DEFAULT_PROJECT_PATH("default.path.project", PluginSettingTypeEnum.STRING),
7082
/**
7183
* 若设置为 true, 则生成数据时不生成随机数据(主要用于作者测试)
7284
*/

src/main/java/cn/gudqs7/plugins/common/resolver/structure/StructureAndCommentResolver.java

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import org.apache.commons.collections.CollectionUtils;
1818
import org.jetbrains.annotations.NotNull;
1919

20+
import java.util.HashSet;
2021
import java.util.List;
2122
import java.util.Objects;
23+
import java.util.Set;
2224
import java.util.concurrent.ConcurrentHashMap;
2325

2426
/**
@@ -41,7 +43,7 @@ public StructureAndCommentInfo resolveFromClass(PsiClassReferenceType psiClassRe
4143
if (psiClassReferenceType == null) {
4244
return null;
4345
}
44-
StructureAndCommentInfo root = resolveFromClass0(psiClassReferenceType, "", 1);
46+
StructureAndCommentInfo root = resolveFromClass0(null, psiClassReferenceType, "", 1);
4547
psiClassCache.clear();
4648
earlyCache.clear();
4749
return root;
@@ -134,7 +136,7 @@ public StructureAndCommentInfo resolveFromReturnVal(PsiTypeElement returnTypeEle
134136
return structureAndCommentInfo;
135137
}
136138

137-
private StructureAndCommentInfo resolveFromClass0(PsiClassReferenceType psiClassReferenceType, String fieldPrefix, int level) {
139+
private StructureAndCommentInfo resolveFromClass0(StructureAndCommentInfo parent, PsiClassReferenceType psiClassReferenceType, String fieldPrefix, int level) {
138140
if (psiClassReferenceType == null) {
139141
return null;
140142
}
@@ -146,14 +148,14 @@ private StructureAndCommentInfo resolveFromClass0(PsiClassReferenceType psiClass
146148
psiClass = replacePsiClassIfFromJar(psiClass);
147149
String qualifiedName = psiClass.getQualifiedName();
148150
PsiTypeUtil.resolvePsiClassParameter(psiClassReferenceType);
149-
if (qualifiedName != null) {
150-
if (psiClassCache.containsKey(qualifiedName)) {
151-
return null;
152-
}
153-
if (earlyCache.containsKey(qualifiedName)) {
154-
return null;
155-
}
156-
}
151+
// if (qualifiedName != null) {
152+
// if (psiClassCache.containsKey(qualifiedName)) {
153+
// return null;
154+
// }
155+
// if (earlyCache.containsKey(qualifiedName)) {
156+
// return null;
157+
// }
158+
// }
157159
String clazzTypeName = psiClass.getName();
158160
if (BaseTypeUtil.isBaseTypeOrObject(psiClass)) {
159161
return null;
@@ -165,6 +167,7 @@ private StructureAndCommentInfo resolveFromClass0(PsiClassReferenceType psiClass
165167
return null;
166168
}
167169
StructureAndCommentInfo root = new StructureAndCommentInfo();
170+
root.setParent(parent);
168171
root.setFieldType(clazzTypeName);
169172
root.setOriginalFieldType(clazzTypeName);
170173
root.setFieldTypeCode(FieldType.POJO.getType());
@@ -241,18 +244,12 @@ private StructureAndCommentInfo resolveByPsiType(StructureAndCommentInfo parent,
241244
if (parentPsiClass == null) {
242245
ExceptionUtil.handleSyntaxError(parentPsiClassReferenceType.getCanonicalText());
243246
}
244-
String qualifiedName = parentPsiClass.getQualifiedName();
245-
String canonicalText = psiFieldType.getCanonicalText();
246-
if (Objects.equals(qualifiedName, canonicalText)) {
247-
// 自己依赖自己
248-
return null;
249-
}
250247
}
251-
return resolveByPsiType0(FieldType.BASE.getType(), fieldName, psiFieldType, commentInfo, "%s", fieldPrefix, level);
248+
return resolveByPsiType0(parent, FieldType.BASE.getType(), fieldName, psiFieldType, commentInfo, "%s", fieldPrefix, level);
252249
}
253250

254251
@SuppressWarnings("AlibabaMethodTooLong")
255-
private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fieldName, PsiType psiFieldType, CommentInfo commentInfo, String typeNameFormat, String fieldPrefix, int level) {
252+
private StructureAndCommentInfo resolveByPsiType0(StructureAndCommentInfo parent, int fieldTypeCode, String fieldName, PsiType psiFieldType, CommentInfo commentInfo, String typeNameFormat, String fieldPrefix, int level) {
256253
boolean hidden = commentInfo.isHidden(false);
257254
fieldName = commentInfo.getName(fieldName);
258255
hidden = handleHidden(fieldName, psiFieldType, hidden);
@@ -283,7 +280,7 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
283280
PsiArrayType psiArrayType = (PsiArrayType) psiFieldType;
284281
PsiType componentType = psiArrayType.getComponentType();
285282
String newTypeNameFormat = String.format(typeNameFormat, "%s[]");
286-
return resolveByPsiType0(FieldType.ARRAY.getType(), fieldName, componentType, commentInfo, newTypeNameFormat, fieldPrefix, level);
283+
return resolveByPsiType0(parent, FieldType.ARRAY.getType(), fieldName, componentType, commentInfo, newTypeNameFormat, fieldPrefix, level);
287284
}
288285

289286
boolean isReferenceType = psiFieldType instanceof PsiClassReferenceType;
@@ -315,7 +312,7 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
315312
PsiType realPsiType = PsiTypeUtil.getRealPsiType(psiFieldType, project, null);
316313
if (realPsiType != null) {
317314
String newTypeNameFormat = String.format(typeNameFormat, "%s");
318-
return resolveByPsiType0(fieldTypeCode, fieldName, realPsiType, commentInfo, newTypeNameFormat, fieldPrefix, level);
315+
return resolveByPsiType0(parent, fieldTypeCode, fieldName, realPsiType, commentInfo, newTypeNameFormat, fieldPrefix, level);
319316
}
320317

321318
// 枚举
@@ -325,19 +322,19 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
325322

326323
// List
327324
if (PsiTypeUtil.isPsiTypeFromList(psiFieldType, project)) {
328-
return getStructureAndCommentInfoByCollection(fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
325+
return getStructureAndCommentInfoByCollection(parent, fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
329326
structureAndCommentInfo, parameters, "List<%s>", FieldType.LIST);
330327
}
331328

332329
// Set
333330
if (PsiTypeUtil.isPsiTypeFromSet(psiFieldType, project)) {
334-
return getStructureAndCommentInfoByCollection(fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
331+
return getStructureAndCommentInfoByCollection(parent, fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
335332
structureAndCommentInfo, parameters, "Set<%s>", FieldType.SET);
336333
}
337334

338335
// Collection 放在后面判断, 优先级低一些
339336
if (PsiTypeUtil.isPsiTypeFromCollection(psiFieldType, project)) {
340-
return getStructureAndCommentInfoByCollection(fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
337+
return getStructureAndCommentInfoByCollection(parent, fieldName, commentInfo, typeNameFormat, fieldPrefix, level,
341338
structureAndCommentInfo, parameters, "Collection<%s>", FieldType.COLLECTION);
342339
}
343340

@@ -358,7 +355,7 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
358355
}
359356
keyTypeName = keyType.getPresentableText();
360357
String newTypeNameFormat = String.format(typeNameFormat, "Map<" + keyTypeName + ", %s>");
361-
return resolveByPsiType0(FieldType.MAP.getType(), fieldName, valueType, commentInfo, newTypeNameFormat, fieldPrefix, level);
358+
return resolveByPsiType0(parent, FieldType.MAP.getType(), fieldName, valueType, commentInfo, newTypeNameFormat, fieldPrefix, level);
362359
} else {
363360
return structureAndCommentInfo;
364361
}
@@ -371,7 +368,15 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
371368
if (MapKeyConstant.FIELD_PREFIX_INIT.equals(fieldPrefix)) {
372369
fieldPrefix0 = "";
373370
}
374-
StructureAndCommentInfo structureAndCommentInfoChild = resolveFromClass0(psiClassReferenceType, fieldPrefix0, level + 1);
371+
372+
boolean hasRecursion = checkRecursion(parent);
373+
if (hasRecursion) {
374+
return null;
375+
}
376+
377+
structureAndCommentInfo.setPsiClass(resolveClass);
378+
structureAndCommentInfo.setParent(parent);
379+
StructureAndCommentInfo structureAndCommentInfoChild = resolveFromClass0(structureAndCommentInfo, psiClassReferenceType, fieldPrefix0, level + 1);
375380
if (structureAndCommentInfoChild != null && structureAndCommentInfoChild.getChildren().size() > 0) {
376381
structureAndCommentInfo.setLeaf(false);
377382
structureAndCommentInfo.copyChild(structureAndCommentInfoChild.getChildren());
@@ -388,27 +393,49 @@ private StructureAndCommentInfo resolveByPsiType0(int fieldTypeCode, String fiel
388393
return structureAndCommentInfo;
389394
}
390395

391-
private boolean handleHidden(String fieldName, PsiType psiFieldType, boolean oldVal) {
392-
if (FieldJumpUtil.isFieldNameNeedJump(fieldName)) {
393-
return true;
394-
}
395-
String typeQname = psiFieldType.getCanonicalText();
396-
if (FieldJumpUtil.isFieldTypeNeedJump(typeQname)) {
397-
return true;
396+
private boolean checkRecursion(StructureAndCommentInfo parent) {
397+
if (parent == null) {
398+
return false;
398399
}
399-
return oldVal;
400+
Set<String> uniqueNodeSet = new HashSet<>(32);
401+
StructureAndCommentInfo current = parent;
402+
do {
403+
if (current.getFieldTypeCode().equals(FieldType.POJO.getType())) {
404+
PsiClass psiClass = current.getPsiClass();
405+
if (psiClass != null) {
406+
String qualifiedName = psiClass.getQualifiedName();
407+
if (uniqueNodeSet.contains(qualifiedName)) {
408+
return true;
409+
}
410+
uniqueNodeSet.add(qualifiedName);
411+
}
412+
}
413+
current = current.getParent();
414+
} while (current != null);
415+
return false;
400416
}
401417

402-
private StructureAndCommentInfo getStructureAndCommentInfoByCollection(String fieldName, CommentInfo commentInfo, String typeNameFormat, String fieldPrefix, int level, StructureAndCommentInfo structureAndCommentInfo, PsiType[] parameters, String format, FieldType fieldType) {
418+
private StructureAndCommentInfo getStructureAndCommentInfoByCollection(StructureAndCommentInfo parent, String fieldName, CommentInfo commentInfo, String typeNameFormat, String fieldPrefix, int level, StructureAndCommentInfo structureAndCommentInfo, PsiType[] parameters, String format, FieldType fieldType) {
403419
if (parameters.length > 0) {
404420
PsiType elementType = parameters[0];
405421
String newTypeNameFormat = String.format(typeNameFormat, format);
406-
return resolveByPsiType0(fieldType.getType(), fieldName, elementType, commentInfo, newTypeNameFormat, fieldPrefix, level);
422+
return resolveByPsiType0(parent, fieldType.getType(), fieldName, elementType, commentInfo, newTypeNameFormat, fieldPrefix, level);
407423
} else {
408424
return structureAndCommentInfo;
409425
}
410426
}
411427

428+
private boolean handleHidden(String fieldName, PsiType psiFieldType, boolean oldVal) {
429+
if (FieldJumpUtil.isFieldNameNeedJump(fieldName)) {
430+
return true;
431+
}
432+
String typeQname = psiFieldType.getCanonicalText();
433+
if (FieldJumpUtil.isFieldTypeNeedJump(typeQname)) {
434+
return true;
435+
}
436+
return oldVal;
437+
}
438+
412439
private boolean checkHiddenRequest(String fieldPrefix, String fieldName) {
413440
String fieldKey = fieldPrefix + fieldName;
414441
List<String> hiddenKeys = ResolverContextHolder.getData(ResolverContextHolder.HIDDEN_KEYS);

src/main/java/cn/gudqs7/plugins/common/util/PluginSettingHelper.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public class PluginSettingHelper {
2525
private static final String CONFIG_FILE_PATH = "docer-config.properties";
2626
private static final Map<String, String> CONFIG = new ConcurrentHashMap<>(16);
2727

28-
private static VirtualFile configFile;
28+
/**
29+
* 防止不同项目复用同一个缓存, 需按项目路径来分隔
30+
*/
31+
private static final Map<String, VirtualFile> configFileMap = new ConcurrentHashMap<>(32);
2932

3033
/**
3134
* 将配置保存到缓存
@@ -205,14 +208,16 @@ public static void initConfig(Project project, VirtualFile currentVirtualFile) {
205208
*/
206209
@SneakyThrows
207210
public static Map<String, String> getConfigFromFile(Project project, VirtualFile currentVirtualFile) {
211+
String projectBasePath = project.getBasePath();
212+
VirtualFile configFile = configFileMap.get(projectBasePath);
208213
if (configFile != null && configFile.exists()) {
209-
return toMap();
214+
return toMap(projectBasePath);
210215
}
211-
String defaultConfigPath = project.getBasePath() + File.separator + CONFIG_FILE_PATH;
216+
String defaultConfigPath = projectBasePath + File.separator + CONFIG_FILE_PATH;
212217
VirtualFile virtualFileByDefault = LocalFileSystem.getInstance().findFileByPath(defaultConfigPath);
213218
if (virtualFileByDefault != null) {
214-
configFile = virtualFileByDefault;
215-
return toMap();
219+
configFileMap.put(projectBasePath, virtualFileByDefault);
220+
return toMap(projectBasePath);
216221
}
217222
PsiFile[] filesByName = FilenameIndex.getFilesByName(project, CONFIG_FILE_PATH, GlobalSearchScope.projectScope(project));
218223
if (filesByName.length > 0) {
@@ -225,20 +230,21 @@ public static Map<String, String> getConfigFromFile(Project project, VirtualFile
225230
String projectBasePath1 = getProjectBasePath(path);
226231
String projectBasePath2 = getProjectBasePath(configFilePath);
227232
if (projectBasePath1.equals(projectBasePath2)) {
228-
configFile = virtualFile;
229-
return toMap();
233+
configFileMap.put(projectBasePath, virtualFile);
234+
return toMap(projectBasePath);
230235
}
231236
if (back == null) {
232237
back = virtualFile;
233238
}
234239
}
235-
configFile = back;
236-
return toMap();
240+
configFileMap.put(projectBasePath, back);
241+
return toMap(projectBasePath);
237242
}
238243
return null;
239244
}
240245

241-
private static Map<String, String> toMap() throws IOException {
246+
private static Map<String, String> toMap(String projectBasePath) throws IOException {
247+
VirtualFile configFile = configFileMap.get(projectBasePath);
242248
Properties properties = new Properties();
243249
properties.load(configFile.getInputStream());
244250
Map<String, String> map = new HashMap<>(8);

src/main/java/cn/gudqs7/plugins/common/util/jetbrain/ExceptionUtil.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.gudqs7.plugins.common.base.error.CanIgnoreException;
44
import com.intellij.openapi.progress.ProcessCanceledException;
5+
import com.intellij.openapi.project.IndexNotReadyException;
56
import lombok.Lombok;
67

78
import java.io.PrintWriter;
@@ -24,7 +25,7 @@ public static void logException(Throwable throwable, String addition) {
2425
}
2526

2627
public static void handleException(Throwable throwable) {
27-
if (throwable instanceof ProcessCanceledException) {
28+
if (needIgnoredIdeaException(throwable)) {
2829
throw Lombok.sneakyThrow(throwable);
2930
}
3031
if (throwable instanceof CanIgnoreException) {
@@ -38,6 +39,11 @@ public static void handleException(Throwable throwable) {
3839
}
3940
}
4041

42+
private static boolean needIgnoredIdeaException(Throwable throwable) {
43+
return throwable instanceof ProcessCanceledException
44+
|| throwable instanceof IndexNotReadyException;
45+
}
46+
4147
public static void handleSyntaxError(String code) throws RuntimeException {
4248
throw new CanIgnoreException("您的代码可能存在语法错误, 无法为您生成代码, 参考信息: " + code);
4349
}

src/main/java/cn/gudqs7/plugins/common/util/structure/PsiMethodUtil.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,25 @@ public static boolean checkClassHasValidSetter(PsiClass psiClass) {
4343
/**
4444
* 返回类的所有 getter
4545
*
46-
* @param psiClass 类
46+
* @param psiClass 类
47+
* @param noSuperClass 是否不包含父类的数据
4748
* @return 类的所有 getter
4849
*/
4950
@NotNull
50-
public static List<PsiMethod> getGetterMethod(PsiClass psiClass) {
51-
return getMethodByPrefix(psiClass, GET_METHOD_PREFIX, IS_METHOD_PREFIX);
51+
public static List<PsiMethod> getGetterMethod(PsiClass psiClass, boolean noSuperClass) {
52+
return getMethodByPrefix(psiClass, noSuperClass, GET_METHOD_PREFIX, IS_METHOD_PREFIX);
5253
}
5354

5455
/**
5556
* 返回类的所有 setter
5657
*
57-
* @param psiClass 类
58+
* @param psiClass 类
59+
* @param noSuperClass 是否不包含父类的数据
5860
* @return 类的所有 setter
5961
*/
6062
@NotNull
61-
public static List<PsiMethod> getSetterMethod(PsiClass psiClass) {
62-
return getMethodByPrefix(psiClass, SET_METHOD_PREFIX);
63+
public static List<PsiMethod> getSetterMethod(PsiClass psiClass, boolean noSuperClass) {
64+
return getMethodByPrefix(psiClass, noSuperClass, SET_METHOD_PREFIX);
6365
}
6466

6567
/**
@@ -139,14 +141,19 @@ private static boolean isValidMethod(@NotNull PsiMethod method, String... prefix
139141
/**
140142
* 返回类的指定前缀的方法
141143
*
142-
* @param psiClass 类
143-
* @param prefixArray 指定前缀集合
144+
* @param psiClass 类
145+
* @param noSuperClass 是否不包含父类的数据
146+
* @param prefixArray 指定前缀集合
144147
* @return 方法列表
145148
*/
146149
@NotNull
147-
private static List<PsiMethod> getMethodByPrefix(PsiClass psiClass, String... prefixArray) {
150+
private static List<PsiMethod> getMethodByPrefix(PsiClass psiClass, boolean noSuperClass, String... prefixArray) {
148151
List<PsiMethod> methodList = new ArrayList<>();
149152
Set<String> methodNameSet = new HashSet<>();
153+
if (noSuperClass) {
154+
addMethodToList(methodList, methodNameSet, psiClass, prefixArray);
155+
return methodList;
156+
}
150157
while (PsiClassUtil.isNotSystemClass(psiClass)) {
151158
addMethodToList(methodList, methodNameSet, psiClass, prefixArray);
152159
psiClass = psiClass.getSuperClass();

0 commit comments

Comments
 (0)