Skip to content

Commit 92d571c

Browse files
committed
feat:入口类增加新方法及增加注释
1 parent 8c5d059 commit 92d571c

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

src/main/java/org/github/azirzsk/fcp/FCP.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import org.github.azirzsk.fcp.invoke.FunctionCall;
77
import org.github.azirzsk.fcp.parser.ToolParser;
88

9+
import java.util.ArrayList;
910
import java.util.List;
1011

1112
/**
13+
* FunctionCall入口类
14+
* 生成JSON格式的工具调用以及回调对应方法
15+
*
1216
* @author zhangshukun
1317
* @since 2024/11/25
1418
*/
@@ -19,6 +23,8 @@ public class FCP {
1923

2024
private final FunctionCall functionCall = new FunctionCall();
2125

26+
private final List<ToolEntity> toolEntityList = new ArrayList<>();
27+
2228
public static FCP create() {
2329
return new FCP();
2430
}
@@ -33,20 +39,38 @@ private FCP() {
3339
* @param function 对象
3440
* @return FunctionCall
3541
*/
36-
public String functionCall(Object function) {
37-
List<ToolEntity> toolEntityList = toolParser.parse(function.getClass());
38-
if (toolEntityList.isEmpty()) {
39-
throw new NullPointerException("传入的对应没有Function注解");
42+
public String parse(Object function) {
43+
List<ToolEntity> parseResult = toolParser.parse(function.getClass());
44+
if (parseResult.isEmpty()) {
45+
log.warn("传入的对象中没有标注@Function注解的方法");
46+
throw new NullPointerException("传入的对象中没有标注@Function注解的方法");
4047
}
4148
// 构造函数名和实际方法的映射关系
42-
for (ToolEntity toolEntity : toolEntityList) {
49+
for (ToolEntity toolEntity : parseResult) {
4350
String name = toolEntity.getFunction().getName();
4451
functionCall.getMethodMap().put(name, toolEntity.getFunction());
4552
}
4653
functionCall.getObjectMap().put(function.getClass(), function);
54+
this.toolEntityList.addAll(parseResult);
55+
return JSON.toJSONString(parseResult);
56+
}
57+
58+
/**
59+
* 获取当前已解析的FunctionCall
60+
*
61+
* @return FunctionCall
62+
*/
63+
public String getAllFunctionCall() {
4764
return JSON.toJSONString(toolEntityList);
4865
}
4966

67+
/**
68+
* 回调对应方法
69+
*
70+
* @param name 方法名称
71+
* @param argumentsStr 调用参数
72+
* @return 调用结果
73+
*/
5074
public Object functionCall(String name, String argumentsStr) {
5175
return functionCall.functionCall(name, argumentsStr);
5276
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FunctionCallTest {
1818
public void testInvoke() {
1919
SingleReturnMethod toolTestClass = new SingleReturnMethod();
2020
FCP fcp = FCP.create();
21-
fcp.functionCall(toolTestClass);
21+
fcp.parse(toolTestClass);
2222

2323
String invokeStr1 = "{\"a\":25,\"b\":230}";
2424
Assertions.assertEquals(5, fcp.functionCall("gcd", invokeStr1));
@@ -30,7 +30,7 @@ public void testInvoke() {
3030
public void testInvokeException() {
3131
SingleReturnMethod toolTestClass = new SingleReturnMethod();
3232
FCP fcp = FCP.create();
33-
fcp.functionCall(toolTestClass);
33+
fcp.parse(toolTestClass);
3434
String invokeStr = "{\"b\":25,\"b\":230}";
3535
Assertions.assertThrows(NullPointerException.class, () -> fcp.functionCall("gcd", invokeStr), "没有找到对应的方法:gcd.a");
3636
}
@@ -39,7 +39,7 @@ public void testInvokeException() {
3939
public void testVoidReturn() {
4040
SingleNoReturnMethod singleNoReturnMethod = new SingleNoReturnMethod();
4141
FCP fcp = FCP.create();
42-
fcp.functionCall(singleNoReturnMethod);
42+
fcp.parse(singleNoReturnMethod);
4343

4444
String invokeStr = "{\"message\":\"hello world\"}";
4545
Assertions.assertNull(fcp.functionCall("printArg", invokeStr));
@@ -49,7 +49,7 @@ public void testVoidReturn() {
4949
public void testNoArg() {
5050
SingleNoArgMethod singleNoArgMethod = new SingleNoArgMethod();
5151
FCP fcp = FCP.create();
52-
fcp.functionCall(singleNoArgMethod);
52+
fcp.parse(singleNoArgMethod);
5353

5454
String invokeStr = "{}";
5555
Assertions.assertEquals(123, fcp.functionCall("noArg", invokeStr));
@@ -59,7 +59,7 @@ public void testNoArg() {
5959
public void testCustomObject() {
6060
SingleReturnMethod singleReturnMethod = new SingleReturnMethod();
6161
FCP fcp = FCP.create();
62-
fcp.functionCall(singleReturnMethod);
62+
fcp.parse(singleReturnMethod);
6363

6464
String invokeStr = "{\"user\":{\"name\":\"azirzsk\",\"age\":25}}";
6565
Assertions.assertEquals("姓名:azirzsk,年龄:25", fcp.functionCall("print", invokeStr));
@@ -69,7 +69,7 @@ public void testCustomObject() {
6969
public void testNestCustomObject() {
7070
SingleReturnMethod singleReturnMethod = new SingleReturnMethod();
7171
FCP fcp = FCP.create();
72-
fcp.functionCall(singleReturnMethod);
72+
fcp.parse(singleReturnMethod);
7373

7474
String invokeStr = "{\"home\":{\"address\":\"北京市朝阳区\",\"userInfo\":{\"name\":\"azirzsk\",\"age\":25}}}";
7575
Assertions.assertEquals("地址:北京市朝阳区,用户信息:姓名:azirzsk,年龄:25", fcp.functionCall("printHome", invokeStr));
@@ -79,7 +79,7 @@ public void testNestCustomObject() {
7979
public void testRepeatMethod() {
8080
RepeatMethod repeatMethod = new RepeatMethod();
8181
FCP fcp = FCP.create();
82-
fcp.functionCall(repeatMethod);
82+
fcp.parse(repeatMethod);
8383

8484
String invokeStr = "{\"str\":\"hello world\",\"repeat\":3}";
8585
Assertions.assertEquals("hello worldhello worldhello world", fcp.functionCall("print(String, int)", invokeStr));

0 commit comments

Comments
 (0)