1414
1515package zuo .biao .apijson .server ;
1616
17+ import java .lang .reflect .InvocationTargetException ;
18+
1719import com .alibaba .fastjson .JSONObject ;
1820
1921import zuo .biao .apijson .StringUtil ;
2224 * @author Lemon
2325 */
2426public 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 + "\n function必须符合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