Skip to content

Commit 76c83c4

Browse files
committed
feat:解决方法重载导致的function的名称重复的问题
1 parent 112783d commit 76c83c4

7 files changed

Lines changed: 164 additions & 2 deletions

File tree

src/main/java/org/github/azirzsk/fcp/invoke/FunctionCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private Object[] parseArguments(JSONObject argumentJson, ParametersEntity parame
6262
Collection<PropertyEntity> property = propertiesMap.values();
6363
// 根据方法中参数顺序排序
6464
List<PropertyEntity> sortedList = property.stream()
65-
.sorted(Comparator.comparingInt(PropertyEntity::getIndex).reversed())
65+
.sorted(Comparator.comparingInt(PropertyEntity::getIndex))
6666
.collect(Collectors.toList());
6767
Object[] res = new Object[sortedList.size()];
6868
for (int i = 0; i < sortedList.size(); i++) {

src/main/java/org/github/azirzsk/fcp/parser/ToolParser.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import org.github.azirzsk.fcp.annotation.Function;
55
import org.github.azirzsk.fcp.entity.FunctionEntity;
66
import org.github.azirzsk.fcp.entity.ToolEntity;
7+
import org.github.azirzsk.fcp.utils.ParserUtils;
78

89
import java.lang.reflect.Method;
910
import java.util.ArrayList;
11+
import java.util.HashSet;
1012
import java.util.List;
13+
import java.util.Set;
1114

1215
/**
1316
* @author zhangshukun
@@ -25,14 +28,20 @@ public List<ToolEntity> parse(Class<?> clazz) {
2528
log.warn("'{}'中没有任何方法", clazz);
2629
throw new NullPointerException(clazz + "中没有任何方法");
2730
}
31+
Set<String> functionNameSet = new HashSet<>();
2832
List<ToolEntity> toolEntityList = new ArrayList<>();
2933
for (Method method : methods) {
3034
Function function = method.getAnnotation(Function.class);
3135
if (function == null) {
3236
continue;
3337
}
3438
FunctionEntity functionEntity = FUNCTION_PARSER.parse(method);
35-
39+
if (functionNameSet.contains(functionEntity.getName())) {
40+
log.warn("'{}'中存在重名的方法:{}", clazz, functionEntity.getName());
41+
String newFunctionName = ParserUtils.parseMethodName(method);
42+
functionEntity.setName(newFunctionName);
43+
}
44+
functionNameSet.add(functionEntity.getName());
3645
ToolEntity toolEntity = new ToolEntity();
3746
toolEntity.setFunction(functionEntity);
3847
toolEntityList.add(toolEntity);

src/main/java/org/github/azirzsk/fcp/utils/ParserUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import org.github.azirzsk.fcp.converter.Converter;
55
import org.github.azirzsk.fcp.enums.PropertyType;
66

7+
import java.lang.reflect.Method;
78
import java.util.ArrayList;
9+
import java.util.Arrays;
810
import java.util.List;
11+
import java.util.stream.Collectors;
912

1013
/**
1114
* @author zhangshukun
@@ -14,6 +17,23 @@
1417
@Slf4j
1518
public class ParserUtils {
1619

20+
/**
21+
* 解析方法名称
22+
* @param method 要解析的方法
23+
* @return 方法名称,例子:getUser(String, Integer)
24+
*/
25+
public static String parseMethodName(Method method) {
26+
if (method == null) {
27+
throw new NullPointerException("方法为空");
28+
}
29+
String methodName = method.getName();
30+
Class<?>[] parameterTypes = method.getParameterTypes();
31+
List<String> parameterTypeStrList = Arrays.stream(parameterTypes)
32+
.map(Class::getSimpleName)
33+
.collect(Collectors.toList());
34+
return methodName + "(" + String.join(", ", parameterTypeStrList) + ")";
35+
}
36+
1737
/**
1838
* 解析参数枚举值
1939
*

src/test/java/org/github/azirzsk/fcp/invoke/FunctionCallTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.github.azirzsk.fcp.invoke;
22

33
import org.github.azirzsk.fcp.FCP;
4+
import org.github.azirzsk.fcp.testclass.invoke.RepeatMethod;
45
import org.github.azirzsk.fcp.testclass.invoke.SingleNoArgMethod;
56
import org.github.azirzsk.fcp.testclass.invoke.SingleNoReturnMethod;
67
import org.github.azirzsk.fcp.testclass.invoke.SingleReturnMethod;
@@ -73,4 +74,15 @@ public void testNestCustomObject() {
7374
String invokeStr = "{\"home\":{\"address\":\"北京市朝阳区\",\"userInfo\":{\"name\":\"azirzsk\",\"age\":25}}}";
7475
Assertions.assertEquals("地址:北京市朝阳区,用户信息:姓名:azirzsk,年龄:25", fcp.functionCall("printHome", invokeStr));
7576
}
77+
78+
@Test
79+
public void testRepeatMethod() {
80+
RepeatMethod repeatMethod = new RepeatMethod();
81+
FCP fcp = FCP.create();
82+
fcp.functionCall(repeatMethod);
83+
84+
String invokeStr = "{\"str\":\"hello world\",\"repeat\":3}";
85+
Assertions.assertEquals("hello worldhello worldhello world", fcp.functionCall("print(String, int)", invokeStr));
86+
87+
}
7688
}

src/test/java/org/github/azirzsk/fcp/parser/ToolParserTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.github.azirzsk.fcp.parser;
22

33
import org.github.azirzsk.fcp.entity.ToolEntity;
4+
import org.github.azirzsk.fcp.testclass.invoke.RepeatMethod;
45
import org.github.azirzsk.fcp.testclass.invoke.SingleReturnMethod;
56
import org.github.azirzsk.fcp.utils.FileUtils;
67
import org.github.azirzsk.fcp.utils.JsonUtils;
@@ -23,4 +24,12 @@ public void normalParser() {
2324
Assertions.assertEquals(JsonUtils.toJsonObject(result), jsonObject);
2425
}
2526

27+
@Test
28+
public void testRepeatMethodNameParser() {
29+
ToolParser toolParser = new ToolParser();
30+
List<ToolEntity> result = toolParser.parse(RepeatMethod.class);
31+
Assertions.assertEquals("print", result.get(0).getFunction().getName());
32+
Assertions.assertEquals("print(String, int)", result.get(1).getFunction().getName());
33+
}
34+
2635
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.github.azirzsk.fcp.testclass.invoke;
2+
3+
import org.github.azirzsk.fcp.annotation.Function;
4+
import org.github.azirzsk.fcp.annotation.Property;
5+
6+
/**
7+
* @author zhangshukun
8+
* @since 2025/1/1
9+
*/
10+
public class RepeatMethod {
11+
12+
13+
@Function(desc = "打印字符串")
14+
public String print(String str) {
15+
return str;
16+
}
17+
18+
@Function(desc = "打印字符串")
19+
public String print(@Property(desc = "重复的字符串") String str, @Property(desc = "重复次数") int repeat) {
20+
StringBuilder sb = new StringBuilder();
21+
for (int i = 0; i < repeat; i++) {
22+
sb.append(str);
23+
}
24+
return sb.toString();
25+
}
26+
}
27+
28+

src/test/resources/data.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@
1818
"additionalProperties": false
1919
},
2020
"SingleReturnMethod": [
21+
{
22+
"type": "function",
23+
"function": {
24+
"name": "print",
25+
"description": "打印用户信息",
26+
"parameters": {
27+
"type": "object",
28+
"properties": {
29+
"user": {
30+
"type": "object",
31+
"description": "用户信息",
32+
"properties": {
33+
"name": {
34+
"type": "string",
35+
"description": "姓名"
36+
},
37+
"age": {
38+
"type": "int",
39+
"description": "年龄"
40+
}
41+
},
42+
"required": [
43+
"name",
44+
"age"
45+
]
46+
}
47+
},
48+
"required": [
49+
"user"
50+
],
51+
"additionalProperties": false
52+
},
53+
"strict": true
54+
}
55+
},
2156
{
2257
"type": "function",
2358
"function": {
@@ -43,6 +78,55 @@
4378
},
4479
"strict": true
4580
}
81+
},
82+
{
83+
"type": "function",
84+
"function": {
85+
"name": "printHome",
86+
"description": "打印家庭信息",
87+
"parameters": {
88+
"type": "object",
89+
"properties": {
90+
"home": {
91+
"type": "object",
92+
"description": "家庭信息",
93+
"properties": {
94+
"userInfo": {
95+
"type": "object",
96+
"description": "用户信息",
97+
"properties": {
98+
"name": {
99+
"type": "string",
100+
"description": "姓名"
101+
},
102+
"age": {
103+
"type": "int",
104+
"description": "年龄"
105+
}
106+
},
107+
"required": [
108+
"name",
109+
"age"
110+
]
111+
},
112+
"address": {
113+
"type": "string",
114+
"description": "地址"
115+
}
116+
},
117+
"required": [
118+
"userInfo",
119+
"address"
120+
]
121+
}
122+
},
123+
"required": [
124+
"home"
125+
],
126+
"additionalProperties": false
127+
},
128+
"strict": true
129+
}
46130
}
47131
],
48132
"FieldTest": {

0 commit comments

Comments
 (0)