Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit d22ed6e

Browse files
authored
Merge pull request #805 from shalousun/master
fix: Generic replacement error causing parsing bug.
2 parents 4d28eff + 84358a8 commit d22ed6e

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/main/java/com/ly/doc/utils/DocUtil.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,18 +1066,41 @@ public static List<ApiDocDict> buildDictionary(ApiConfig config, JavaProjectBuil
10661066
*/
10671067
public static String formatFieldTypeGicName(Map<String, String> genericMap, String[] globGicName, String fieldGicName) {
10681068
String gicName = "";
1069+
String fieldGicNameCopy = fieldGicName;
10691070
String[] gNameArr = DocClassUtil.getSimpleGicName(fieldGicName);
10701071
for (String g : gNameArr) {
10711072
if (g.length() == 1) {
10721073
if (Objects.nonNull(genericMap.get(g))) {
10731074
gicName = genericMap.get(g);
10741075
}
10751076
if (StringUtil.isNotEmpty(gicName)) {
1076-
fieldGicName = fieldGicName.replace(g, gicName);
1077+
fieldGicNameCopy = replaceGenericParameter(fieldGicName, g, gicName);
10771078
}
10781079
}
10791080
}
1080-
return fieldGicName;
1081+
return fieldGicNameCopy;
1082+
}
1083+
1084+
/**
1085+
* Replaces the specified generic parameter in a string with a given type,
1086+
* supporting multi-level generics.
1087+
*
1088+
* @param baseString The base string
1089+
* @param originalGenericParameter The generic parameter to be replaced, like "T"
1090+
* @param replacementType The type to replace the original parameter with, like "User"
1091+
* @return The modified string
1092+
*/
1093+
public static String replaceGenericParameter(String baseString, String originalGenericParameter, String replacementType) {
1094+
StringBuilder result = new StringBuilder(baseString);
1095+
String searchPattern = "<" + originalGenericParameter + ">";
1096+
int index = 0;
1097+
while ((index = result.indexOf(searchPattern, index)) != -1) {
1098+
// Replace the specified generic parameter with the replacement type
1099+
result.replace(index, index + searchPattern.length(), "<" + replacementType + ">");
1100+
// Update the index to continue searching for the next occurrence
1101+
index += replacementType.length() + 2; // +2 for '<' and '>' characters
1102+
}
1103+
return result.toString();
10811104
}
10821105

10831106
public static String handleConstants(Map<String, String> constantsMap, String value) {

src/test/java/com/ly/doc/util/DocUtilTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,13 @@ public void testSplitPathBySlash() {
7171
}
7272
}
7373
}
74+
75+
@Test
76+
public void testReplaceGenericParameter(){
77+
String base = "com.Test<List<T>>";
78+
String originalGeneric = "T";
79+
String replacement = "User";
80+
String result = DocUtil.replaceGenericParameter(base, originalGeneric, replacement);
81+
System.out.println(result); // Output: com.Test<List<Use
82+
}
7483
}

0 commit comments

Comments
 (0)