Skip to content

Commit 2784973

Browse files
committed
Server:完善远程函数中取值类型错误的异常处理
1 parent 9a4e4ed commit 2784973

File tree

1 file changed

+30
-9
lines changed
  • APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server

1 file changed

+30
-9
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Function.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package zuo.biao.apijson.server;
1616

17+
import java.lang.reflect.InvocationTargetException;
18+
1719
import com.alibaba.fastjson.JSONObject;
1820

1921
import zuo.biao.apijson.StringUtil;
@@ -22,7 +24,7 @@
2224
* @author Lemon
2325
*/
2426
public class Function {
25-
// private static final String TAG = "Function";
27+
// private static final String TAG = "Function";
2628

2729
/**反射调用
2830
* @param fun
@@ -86,22 +88,22 @@ public static Object invoke(@NotNull Function fun, @NotNull JSONObject request,
8688
return invoke(fun, method, types, values);
8789
} catch (Exception e) {
8890
if (e instanceof NoSuchMethodException) {
89-
String f = method + "(JSONObject request";
90-
for (int i = 0; i < length; i++) {
91-
f += (", String " + keys[i]);
92-
}
93-
f += ")";
94-
95-
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + f + " 不在后端工程的DemoFunction内!"
91+
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(method, keys) + " 不在后端工程的DemoFunction内!"
9692
+ "\n请检查函数名和参数数量是否与已定义的函数一致!"
9793
+ "\n且必须为 function(key0,key1,...) 这种单函数格式!"
9894
+ "\nfunction必须符合Java函数命名,key是用于在request内取值的键!"
9995
+ "\n调用时不要有空格!");
10096
}
97+
if (e instanceof InvocationTargetException) {
98+
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数传参类型错误!"
99+
+ "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(method, keys) + " 的要求!");
100+
}
101101
throw e;
102102
}
103-
103+
104104
}
105+
106+
105107
/**反射调用
106108
* @param methodName
107109
* @param parameterTypes
@@ -112,4 +114,23 @@ public static Object invoke(@NotNull Function fun, @NotNull String methodName, @
112114
return fun.getClass().getDeclaredMethod(methodName, parameterTypes).invoke(fun, args);
113115
}
114116

117+
/**
118+
* @param method
119+
* @param keys
120+
* @return
121+
*/
122+
private static String getFunction(String method, String[] keys) {
123+
String f = method + "(JSONObject request";
124+
125+
if (keys != null) {
126+
for (int i = 0; i < keys.length; i++) {
127+
f += (", String " + keys[i]);
128+
}
129+
}
130+
131+
f += ")";
132+
133+
return f;
134+
}
135+
115136
}

0 commit comments

Comments
 (0)