66package apijson .orm ;
77
88import java .io .FileReader ;
9+ import java .lang .invoke .WrongMethodTypeException ;
910import java .lang .reflect .InvocationTargetException ;
1011import java .lang .reflect .Method ;
1112import java .util .Arrays ;
3132 * @author Lemon
3233 */
3334public 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