77
88import java .io .FileReader ;
99import java .lang .reflect .InvocationTargetException ;
10+ import java .lang .reflect .Method ;
1011import java .util .Arrays ;
1112import java .util .HashMap ;
1213import java .util .List ;
3233public class AbstractFunctionParser implements FunctionParser {
3334 // private static final String TAG = "AbstractFunctionParser";
3435
36+ /**开启支持远程函数
37+ */
38+ public static boolean ENABLE_REMOTE_FUNCTION = true ;
39+ /**开启支持远程函数中的 JavaScript 脚本形式
40+ */
41+ public static boolean ENABLE_SCRIPT_FUNCTION = true ;
42+
3543 public static final int TYPE_REMOTE_FUNCTION = 0 ;
3644 //public static final int TYPE_SQL_FUNCTION = 1;
3745 public static final int TYPE_SCRIPT_FUNCTION = 1 ;
@@ -160,6 +168,10 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
160168 * @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
161169 */
162170 public static Object invoke (@ NotNull AbstractFunctionParser parser , @ NotNull String function , @ NotNull JSONObject currentObject ) throws Exception {
171+ if (ENABLE_REMOTE_FUNCTION == false ) {
172+ throw new UnsupportedOperationException ("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
173+ " == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !" );
174+ }
163175
164176 FunctionBean fb = parseFunction (function , currentObject , false );
165177
@@ -170,15 +182,19 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
170182
171183 int type = row .getIntValue ("type" );
172184 if (type < TYPE_REMOTE_FUNCTION || type > TYPE_SCRIPT_FUNCTION ) {
173- throw new UnsupportedOperationException ("type = " + type + " 不合法!必须是 [0, 1, 2] 中的一个 !" );
185+ throw new UnsupportedOperationException ("type = " + type + " 不合法!必须是 [0, 1] 中的一个 !" );
186+ }
187+ if (ENABLE_SCRIPT_FUNCTION == false && type == TYPE_SCRIPT_FUNCTION ) {
188+ throw new UnsupportedOperationException ("type = " + type + " 不合法!AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION" +
189+ " == false 时不支持远程函数中的脚本形式!如需支持则设置 AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION = true !" );
174190 }
175191
176192
177193 int version = row .getIntValue ("version" );
178194 if (parser .getVersion () < version ) {
179195 throw new UnsupportedOperationException ("不允许 version = " + parser .getVersion () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 version >= " + version + " !" );
180196 }
181- String tag = row .getString ("tag" );
197+ String tag = row .getString ("tag" ); // TODO 改为 tags,类似 methods 支持多个 tag。或者干脆不要?因为目前非开放请求全都只能后端指定
182198 if (tag != null && tag .equals (parser .getTag ()) == false ) {
183199 throw new UnsupportedOperationException ("不允许 tag = " + parser .getTag () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 tag = " + tag + " !" );
184200 }
@@ -227,7 +243,13 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
227243 if (type == TYPE_SCRIPT_FUNCTION ) {
228244 return invokeScript (parser , methodName , parameterTypes , args , currentObject );
229245 }
230- return parser .getClass ().getMethod (methodName , parameterTypes ).invoke (parser , args );
246+
247+ Method m = parser .getClass ().getMethod (methodName , parameterTypes );
248+ //费性能,还是初始化时做更好
249+ //if (m.getReturnType().getSimpleName().equals(returnType) == false) {
250+ // throw new IllegalArgumentTypeException("");
251+ //}
252+ return m .invoke (parser , args );
231253 }
232254
233255 public static Invocable INVOCABLE ;
0 commit comments