Skip to content

Commit c674916

Browse files
committed
远程函数:新增校验 Function 表中配置的 returnType
1 parent 1b2b65b commit c674916

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package apijson.orm;
77

88
import java.io.FileReader;
9+
import java.lang.invoke.WrongMethodTypeException;
910
import java.lang.reflect.InvocationTargetException;
1011
import java.lang.reflect.Method;
1112
import java.util.Arrays;
@@ -31,7 +32,7 @@
3132
* @author Lemon
3233
*/
3334
public class AbstractFunctionParser implements FunctionParser {
34-
// private static final String TAG = "AbstractFunctionParser";
35+
private static final String TAG = "AbstractFunctionParser";
3536

3637
/**开启支持远程函数
3738
*/
@@ -205,7 +206,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
205206
}
206207

207208
try {
208-
return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues(), currentObject, type);
209+
return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues(), row.getString("returnType"), currentObject, type);
209210
} catch (Exception e) {
210211
if (e instanceof NoSuchMethodException) {
211212
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 不在后端工程的DemoFunction内!"
@@ -236,19 +237,31 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
236237
*/
237238
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
238239
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
239-
return invoke(parser, methodName, parameterTypes, args, null, TYPE_REMOTE_FUNCTION);
240+
return invoke(parser, methodName, parameterTypes, args, null, null, TYPE_REMOTE_FUNCTION);
240241
}
241242
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
242-
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, JSONObject currentObject, int type) throws Exception {
243+
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType, JSONObject currentObject, int type) throws Exception {
243244
if (type == TYPE_SCRIPT_FUNCTION) {
244-
return invokeScript(parser, methodName, parameterTypes, args, currentObject);
245+
return invokeScript(parser, methodName, parameterTypes, args, returnType, currentObject);
246+
}
247+
248+
Method m = parser.getClass().getMethod(methodName, parameterTypes); // 不用判空,拿不到就会抛异常
249+
250+
if (Log.DEBUG) {
251+
String rt = Log.DEBUG && m.getReturnType() != null ? m.getReturnType().getSimpleName() : null;
252+
253+
if ("void".equals(rt)) {
254+
rt = null;
255+
}
256+
if ("void".equals(returnType)) {
257+
returnType = null;
258+
}
259+
260+
if (rt != returnType && (rt == null || rt.equals(returnType) == false)) {
261+
throw new WrongMethodTypeException("远程函数 " + methodName + " 的实际返回值类型 " + rt + " 与 Function 表中的配置的 " + returnType + " 不匹配!");
262+
}
245263
}
246264

247-
Method m = parser.getClass().getMethod(methodName, parameterTypes);
248-
//费性能,还是初始化时做更好
249-
//if (m.getReturnType().getSimpleName().equals(returnType) == false) {
250-
// throw new IllegalArgumentTypeException("");
251-
//}
252265
return m.invoke(parser, args);
253266
}
254267

@@ -269,7 +282,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
269282
}
270283

271284
public static Object invokeScript(@NotNull AbstractFunctionParser parser, @NotNull String methodName
272-
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, JSONObject currentObject) throws Exception {
285+
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType, JSONObject currentObject) throws Exception {
273286
JSONObject row = SCRIPT_MAP.get(methodName);
274287
if (row == null) {
275288
throw new UnsupportedOperationException("调用远程函数脚本 " + methodName + " 不存在!");
@@ -336,7 +349,34 @@ public static Object invokeScript(@NotNull AbstractFunctionParser parser, @NotNu
336349
//}
337350
}
338351

339-
System.out.println("invokeScript " + methodName + "(..) >> result = " + result);
352+
353+
if (Log.DEBUG && result != null) {
354+
Class<?> rt = result.getClass(); // 作为远程函数的 js 类型应该只有 JSON 的几种类型
355+
String fullReturnType = (StringUtil.isSmallName(returnType)
356+
? returnType : (returnType.startsWith("JSON") ? "com.alibaba.fastjson." : "java.lang.") + returnType);
357+
358+
if ((rt == null && returnType != null) || (rt != null && returnType == null)) {
359+
throw new WrongMethodTypeException("远程函数 " + methodName + " 的实际返回值类型 "
360+
+ (rt == null ? null : rt.getName()) + " 与 Function 表中的配置的 " + fullReturnType + " 不匹配!");
361+
}
362+
363+
Class<?> cls;
364+
try {
365+
cls = Class.forName(fullReturnType);
366+
}
367+
catch (Exception e) {
368+
throw new WrongMethodTypeException("远程函数 " + methodName + " 在 Function 表中的配置的类型 "
369+
+ returnType + " 对应的 " + fullReturnType + " 错误!在 Java 中 Class.forName 找不到这个类型!");
370+
}
371+
372+
if (cls.isAssignableFrom(rt) == false) {
373+
throw new WrongMethodTypeException("远程函数 " + methodName + " 的实际返回值类型 "
374+
+ (rt == null ? null : rt.getName()) + " 与 Function 表中的配置的 "
375+
+ returnType + " 对应的 " + fullReturnType + " 不匹配!");
376+
}
377+
}
378+
379+
Log.d(TAG, "invokeScript " + methodName + "(..) >> result = " + result);
340380
return result;
341381
}
342382

0 commit comments

Comments
 (0)