Skip to content

Commit

Permalink
优化泛型数据获取
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Aug 19, 2021
1 parent 4268b6d commit 8934d8c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.liuzhihang'
version '1.1.10'
version '1.1.11'

JavaVersion.VERSION_11

Expand Down
6 changes: 3 additions & 3 deletions parts/changeNotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<h4>English introduction</h4>
<ul>
<li>1.1.10
<li>1.1.11
<ol>
<li>Optimize generic data acquisition</li>
</ol>
Expand All @@ -12,9 +12,9 @@ <h4>English introduction</h4>
</ul>
<h4>中文介绍</h4>
<ul>
<li>1.1.10
<li>1.1.11
<ol>
<li>优化泛型数据获取</li>
<li>优化泛型嵌套数据获取</li>
<li>备注:当前版本使用 swingx-core 依赖生成 TreeTable, 导致插件包占用空间变大, 需要后期调整, 具体可参考:
https://youtrack.jetbrains.com/issue/IDEA-275249 ,如果小伙伴有其他解决方式, 希望可以联系我,或者提 Issue.
</li>
Expand Down
85 changes: 73 additions & 12 deletions src/main/java/com/liuzhihang/doc/view/utils/ParamPsiUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.liuzhihang.doc.view.utils;

import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassReferenceType;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtil;
Expand Down Expand Up @@ -30,11 +31,6 @@ public static void buildBodyParam(PsiField field, Map<String, PsiType> genericsM

PsiType type = field.getType();

// 如果是泛型, 且泛型字段是当前字段, 将当前字段类型替换为泛型类型
if (genericsMap != null && genericsMap.containsKey(type.getPresentableText())) {
type = genericsMap.get(type.getPresentableText());
}

Body body = new Body();
body.setRequired(DocViewUtils.isRequired(field));
body.setName(field.getName());
Expand All @@ -47,6 +43,12 @@ public static void buildBodyParam(PsiField field, Map<String, PsiType> genericsM
if (type instanceof PsiPrimitiveType || FieldTypeConstant.FIELD_TYPE.containsKey(type.getPresentableText())) {
return;
}

// 提前替换字段比如 T -> UserDTO List<T> -> List<UserDTO>
// 如果是泛型, 且泛型字段是当前字段, 将当前字段类型替换为泛型类型, 替换完之后重新设置 body 的 type
type = replaceFieldType(genericsMap, type);
body.setType(type.getPresentableText());

// 剩下都是 PsiClass 类型处理
PsiClass fieldClass = PsiUtil.resolveClassInClassTypeOnly(type);

Expand Down Expand Up @@ -79,7 +81,7 @@ public static void buildBodyParam(PsiField field, Map<String, PsiType> genericsM
}
// 集合参数构建, 集合就一个参数, 泛型 E
fieldGenericsMap = CustomPsiUtils.getGenericsMap((PsiClassType) iterableType);
parentBody = buildFieldGenericsBody("E", childClass, body);
parentBody = buildFieldGenericsBody("element", childClass, body);


} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
Expand Down Expand Up @@ -131,6 +133,68 @@ public static void buildBodyParam(PsiField field, Map<String, PsiType> genericsM

}

/**
* 判断当前字段是否含有泛型, 从泛型映射表中替换字段类型
* <p>
* 替换字段比如 T -> UserDTO List<T> -> List<UserDTO>
*
* @param genericsMap 含有泛型的 Map
* @param type 当前字段的类型
* @return
*/
private static PsiType replaceFieldType(Map<String, PsiType> genericsMap, PsiType type) {

if (genericsMap == null || genericsMap.isEmpty()) {
return type;
}

if (!(type instanceof PsiClassType)) {
return type;
}

if (!(type instanceof PsiClassReferenceType)) {
return type;
}

// 当前字段的类型就是泛型 private T data;
if (genericsMap.containsKey(type.getPresentableText())) {
return genericsMap.get(type.getPresentableText());
}

// 当前字段含有泛型 List<T> -> List<UserDTO>
PsiClass fieldClass = PsiUtil.resolveClassInClassTypeOnly(type);

if (fieldClass == null || !fieldClass.hasTypeParameters()) {
return type;
}

PsiType[] parameters = ((PsiClassType) type).getParameters();

List<PsiType> psiTypeList = new ArrayList<>();
// 替换泛型为实际类型
for (PsiType psiType : parameters) {

if (genericsMap.get(psiType.getPresentableText()) == null) {
psiTypeList.add(psiType);
} else {
psiTypeList.add(genericsMap.get(psiType.getPresentableText()));
}
}

if (!psiTypeList.isEmpty()) {

PsiType[] psiTypes = new PsiType[psiTypeList.size()];

for (int i = 0; i < psiTypeList.size(); i++) {
psiTypes[i] = psiTypeList.get(i);
}

return PsiElementFactory.getInstance(fieldClass.getProject()).createType(fieldClass, psiTypes);
}

return type;
}

@NotNull
private static Body buildFieldGenericsBody(String name, PsiClass GenericsClass, Body parent) {
Body listBody = new Body();
Expand Down Expand Up @@ -201,18 +265,15 @@ public static Map<String, Object> getFieldsAndDefaultValue(PsiClass psiClass, Ma
PsiType type = field.getType();
String name = field.getName();

// 如果是泛型, 且泛型字段是当前字段, 将当前字段类型替换为泛型类型
if (genericMap != null && genericMap.containsKey(type.getPresentableText())) {
type = genericMap.get(type.getPresentableText());
}


if (type instanceof PsiPrimitiveType) {
// 基本类型
fieldMap.put(name, PsiTypesUtil.getDefaultValue(type));
continue;
}

// 如果是泛型, 且泛型字段是当前字段, 将当前字段类型替换为泛型类型
type = replaceFieldType(genericMap, type);

// 引用类型
String fieldTypeName = type.getPresentableText();
// 指定的类型
Expand Down

0 comments on commit 8934d8c

Please sign in to comment.