Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Jul 15, 2021
2 parents 431916b + c6d36da commit 445559d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 68 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ Toolkit

更新
----
## [v1.1.1](https://github.com/liuzhihang/toolkit/releases/tag/v1.1.3) (2021-06-08)

- fix:空 JsonArray 的 bug

[查看更多历史更新记录](https://github.com/liuzhihang/toolkit/releases)
[查看历史更新记录](https://github.com/liuzhihang/toolkit/releases)

关于我
----
Expand Down
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.toolkit'
version '1.1.3'
version '1.1.4'

JavaVersion.VERSION_11

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

<h4>English introduction</h4>
<ul>
<li>1.1.3
<li>1.1.4
<ol>
<li>fix bug with empty JsonArray</li>
<li>Parameter recursive reference exception repair</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/toolkit/releases">More records</a></li>
</ul>
<h4>中文介绍</h4>
<ul>
<li>1.1.3
<li>1.1.4
<ol>
<li>fix:空 JsonArray 的 bug</li>
<li>参数递归引用异常修复</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/toolkit/releases">更多记录</a></li>
Expand Down
177 changes: 121 additions & 56 deletions src/main/java/com/liuzhihang/toolkit/utils/FieldsPsiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,83 +20,148 @@ public class FieldsPsiUtils {
@NotNull
public static Map<String, Object> getFieldsAndDefaultValue(PsiClass psiClass, PsiType[] genericArr) {

return getFieldsAndDefaultValue(psiClass, genericArr, new LinkedList<>());
}

/**
* @param psiClass
* @param genericArr
* @param qualifiedNameList 根节点到当前节点的链表
* @return
*/
public static Map<String, Object> getFieldsAndDefaultValue(PsiClass psiClass, PsiType[] genericArr, LinkedList<String> qualifiedNameList) {

Map<String, Object> fieldMap = new LinkedHashMap<>();
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();

if (psiClass != null && !psiClass.isEnum() && !psiClass.isInterface() && !psiClass.isAnnotationType()) {
for (PsiField field : psiClass.getAllFields()) {
if (psiClass == null || psiClass.isEnum() || psiClass.isInterface() || psiClass.isAnnotationType()) {
return fieldMap;
}

if (field.getModifierList() != null && field.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
continue;
}
// 设置当前类的类型
qualifiedNameList.add(psiClass.getQualifiedName());


PsiType type = field.getType();
String name = field.getName();
// 判断注解 javax.annotation.Resource org.springframework.beans.factory.annotation.Autowired
PsiAnnotation[] annotations = field.getAnnotations();
if (annotations.length > 0 && containsAnnotation(annotations)) {
fieldMap.put(name, "");
} else if (type instanceof PsiPrimitiveType) {
// 基本类型
fieldMap.put(name, PsiTypesUtil.getDefaultValue(type));
} else {
//reference Type
String fieldTypeName = type.getPresentableText();
// 指定的类型
if (Constants.FIELD_TYPE.containsKey(fieldTypeName)) {
fieldMap.put(name, Constants.FIELD_TYPE.get(fieldTypeName));
} else if (type instanceof PsiArrayType) {
//array type
List<Object> list = new ArrayList<>();
PsiType deepType = type.getDeepComponentType();
String deepTypeName = deepType.getPresentableText();
if (deepType instanceof PsiPrimitiveType) {
list.add(PsiTypesUtil.getDefaultValue(deepType));
} else if (Constants.FIELD_TYPE.containsKey(deepTypeName)) {
list.add(Constants.FIELD_TYPE.get(deepTypeName));
for (PsiField field : psiClass.getAllFields()) {

if (field.getModifierList() != null && field.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
continue;
}

PsiType type = field.getType();
String name = field.getName();
// 判断注解 javax.annotation.Resource org.springframework.beans.factory.annotation.Autowired
PsiAnnotation[] annotations = field.getAnnotations();
if (annotations.length > 0 && containsAnnotation(annotations)) {
fieldMap.put(name, "");
} else if (type instanceof PsiPrimitiveType) {
// 基本类型
fieldMap.put(name, PsiTypesUtil.getDefaultValue(type));
} else {
// 引用类型
String fieldTypeName = type.getPresentableText();
// 指定的类型
if (Constants.FIELD_TYPE.containsKey(fieldTypeName)) {
fieldMap.put(name, Constants.FIELD_TYPE.get(fieldTypeName));
} else if (type instanceof PsiArrayType) {
// 数组类型
List<Object> list = new ArrayList<>();
PsiType deepType = type.getDeepComponentType();
if (deepType instanceof PsiPrimitiveType) {
list.add(PsiTypesUtil.getDefaultValue(deepType));
} else if (Constants.FIELD_TYPE.containsKey(deepType.getPresentableText())) {
list.add(Constants.FIELD_TYPE.get(deepType.getPresentableText()));
} else {
// 参数类型为对象 校验是否递归
PsiClass classInType = PsiUtil.resolveClassInType(deepType);

LinkedList<String> temp = new LinkedList<>(qualifiedNameList);
if (classInType != null && hasContainQualifiedName(temp, classInType.getQualifiedName())) {
list.add("Object for " + classInType.getName());
} else {
list.add(getFieldsAndDefaultValue(PsiUtil.resolveClassInType(deepType), null));
list.add(getFieldsAndDefaultValue(classInType, null, temp));
}
fieldMap.put(name, list);
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION)) {
// List Set or HashSet
List<Object> list = new ArrayList<>();
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
PsiClass iterableClass = PsiUtil.resolveClassInClassTypeOnly(iterableType);
if (iterableClass != null) {
String classTypeName = iterableClass.getName();
if (Constants.FIELD_TYPE.containsKey(classTypeName)) {
list.add(Constants.FIELD_TYPE.get(classTypeName));
}
fieldMap.put(name, list);
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION)) {
// List Set or HashSet
List<Object> list = new ArrayList<>();
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
PsiClass iterableClass = PsiUtil.resolveClassInClassTypeOnly(iterableType);
if (iterableClass != null) {
String classTypeName = iterableClass.getName();
if (Constants.FIELD_TYPE.containsKey(classTypeName)) {
list.add(Constants.FIELD_TYPE.get(classTypeName));
} else {

// 参数类型为对象 校验是否递归
LinkedList<String> temp = new LinkedList<>(qualifiedNameList);
if (hasContainQualifiedName(temp, iterableClass.getQualifiedName())) {
list.add("Object for " + iterableClass.getName());
} else {
list.add(getFieldsAndDefaultValue(iterableClass, null));
list.add(getFieldsAndDefaultValue(iterableClass, null, temp));
}

}
fieldMap.put(name, list);
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
// HashMap or Map
fieldMap.put(name, new HashMap<>(4));
} else if (psiClass.isEnum() || psiClass.isInterface() || psiClass.isAnnotationType()) {
// enum or interface
fieldMap.put(name, "");
}
fieldMap.put(name, list);
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
// HashMap or Map
fieldMap.put(name, new HashMap<>(4));
} else if (psiClass.isEnum() || psiClass.isInterface() || psiClass.isAnnotationType()) {
// enum or interface
fieldMap.put(name, "");
} else {

if (type.getPresentableText().equals("T") && genericArr != null && genericArr.length >= 1) {
// T 泛型
type = genericArr[0];
} else if (type.getPresentableText().equals("K") && genericArr != null && genericArr.length >= 2) {
// K 泛型
type = genericArr[1];
}

if (Constants.FIELD_TYPE.containsKey(type.getPresentableText())) {
fieldMap.put(name, Constants.FIELD_TYPE.get(type.getPresentableText()));
} else {

if (type.getPresentableText().equals("T") && genericArr != null && genericArr.length >= 1) {
// T 泛型
type = genericArr[0];
} else if (type.getPresentableText().equals("K") && genericArr != null && genericArr.length >= 2) {
// K 泛型
type = genericArr[1];
// 参数类型为对象 校验是否递归
PsiClass classInType = PsiUtil.resolveClassInType(type);

LinkedList<String> temp = new LinkedList<>(qualifiedNameList);
if (classInType != null && hasContainQualifiedName(temp, classInType.getQualifiedName())) {
fieldMap.put(name, "Object for " + classInType.getName());
} else {
fieldMap.put(name, getFieldsAndDefaultValue(PsiUtil.resolveClassInType(type), null, temp));
}

fieldMap.put(name, getFieldsAndDefaultValue(PsiUtil.resolveClassInType(type), null));

}


}
}

}
return fieldMap;
}

private static boolean hasContainQualifiedName(LinkedList<String> qualifiedNameList, String qualifiedName) {

if (qualifiedNameList.isEmpty()) {
return false;
}

for (String s : qualifiedNameList) {
if (s.equals(qualifiedName)) {
return true;
}
}

return false;

}


/**
* 是否包含指定的注解
*
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<extensions defaultExtensionNs="com.intellij">
<projectService serviceImplementation="com.liuzhihang.toolkit.config.Settings"/>
<projectConfigurable instance="com.liuzhihang.toolkit.config.SettingsConfigurable"
displayName="Toolkit Settings"
nonDefaultProject="true"/>
<!--<projectConfigurable instance="com.liuzhihang.toolkit.config.SettingsConfigurable"-->
<!-- displayName="Toolkit Settings"-->
<!-- nonDefaultProject="true"/>-->
</extensions>

<actions>
Expand Down

0 comments on commit 445559d

Please sign in to comment.