fix: 修复JavaScriptEngine日志输出args格式错误,并提取方法

V0.5.x
zak 2023-08-23 11:53:56 +08:00
parent 9772801a3c
commit ceca2379b1
1 changed files with 13 additions and 8 deletions

View File

@ -52,19 +52,13 @@ public class JavaScriptEngine implements IScriptEngine {
public <T> T invokeMethod(TypeReference<T> type, String methodName, Object... args) {
Value member = jsScript.getMember("invoke");
StringBuilder sbArgs = new StringBuilder("[");
//将入参转成json
for (int i = 0; i < args.length; i++) {
args[i] = JsonUtils.toJsonString(args[i]);
sbArgs.append(i == args.length - 1 ? "," : "").append(args[i]);
}
sbArgs.append("]");
StringBuilder sbArgs = formatArgs(args);
//通过调用invoke方法将目标方法返回结果转成json
Value rst = member.execute(methodName, args);
String json = rst.asString();
log.info("invoke script {},args:{}, result:{}", methodName, sbArgs, json);
log.info("invoke script={}, args={}, result={}", methodName, sbArgs, json);
//没有返回值
if (json == null || "null".equals(json)) {
@ -74,4 +68,15 @@ public class JavaScriptEngine implements IScriptEngine {
return JsonUtils.parseObject(json, type);
}
private static StringBuilder formatArgs(Object[] args) {
StringBuilder sbArgs = new StringBuilder("[");
//将入参转成json
for (int i = 0; i < args.length; i++) {
args[i] = JsonUtils.toJsonString(args[i]);
sbArgs.append(args[i]).append(i != args.length - 1 ? "," : "");
}
sbArgs.append("]");
return sbArgs;
}
}