|
16 | 16 |
|
17 | 17 | import static apijson.framework.APIJSONConstant.FUNCTION_;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.rmi.ServerException;
|
20 | 21 | import java.util.ArrayList;
|
21 | 22 | import java.util.Arrays;
|
|
33 | 34 | import apijson.RequestMethod;
|
34 | 35 | import apijson.RequestRole;
|
35 | 36 | import apijson.StringUtil;
|
| 37 | +import apijson.framework.MethodUtil.Argument; |
36 | 38 | import apijson.orm.AbstractFunctionParser;
|
37 | 39 | import apijson.orm.JSONRequest;
|
38 | 40 | import apijson.orm.ParserCreator;
|
@@ -292,6 +294,149 @@ public Object verifyAccess(@NotNull JSONObject request) throws Exception {
|
292 | 294 |
|
293 | 295 |
|
294 | 296 |
|
| 297 | + /**获取方法参数的定义 |
| 298 | + * @param request |
| 299 | + * @return |
| 300 | + * @throws IOException |
| 301 | + * @throws ClassNotFoundException |
| 302 | + * @throws IllegalArgumentException |
| 303 | + */ |
| 304 | + public String getMethodArguments(@NotNull JSONObject request) throws IllegalArgumentException, ClassNotFoundException, IOException { |
| 305 | + return getMethodArguments(request, "methodArgs"); |
| 306 | + } |
| 307 | + /**获取方法参数的定义 |
| 308 | + * @param request |
| 309 | + * @param requestKey |
| 310 | + * @param methodArgs |
| 311 | + * @return |
| 312 | + * @throws IllegalArgumentException |
| 313 | + * @throws ClassNotFoundException |
| 314 | + * @throws IOException |
| 315 | + */ |
| 316 | + public String getMethodArguments(@NotNull JSONObject request, String methodArgsKey) throws IllegalArgumentException, ClassNotFoundException, IOException { |
| 317 | + String argsStr = request.getString(methodArgsKey); |
| 318 | + if (StringUtil.isEmpty(argsStr, true)) { |
| 319 | + JSONObject obj = request.getJSONObject("request"); |
| 320 | + argsStr = obj == null ? null : obj.getString(methodArgsKey); |
| 321 | + } |
| 322 | + List<Argument> methodArgs = JSON.parseArray(removeComment(argsStr), Argument.class); |
| 323 | + if (methodArgs == null || methodArgs.isEmpty()) { |
| 324 | + return ""; |
| 325 | + } |
| 326 | + |
| 327 | + Class<?>[] types = new Class<?>[methodArgs.size()]; |
| 328 | + Object[] args = new Object[methodArgs.size()]; |
| 329 | + MethodUtil.initTypesAndValues(methodArgs, types, args, true); |
| 330 | + |
| 331 | + String s = ""; |
| 332 | + if (types != null) { |
| 333 | + String sn; |
| 334 | + for (int i = 0; i < types.length; i++) { |
| 335 | + sn = types[i] == null ? null : types[i].getSimpleName(); |
| 336 | + if (sn == null) { |
| 337 | + sn = Object.class.getSimpleName(); |
| 338 | + } |
| 339 | + |
| 340 | + if (i > 0) { |
| 341 | + s += ","; |
| 342 | + } |
| 343 | + |
| 344 | + if (MethodUtil.CLASS_MAP.containsKey(sn)) { |
| 345 | + s += sn; |
| 346 | + } |
| 347 | + else { |
| 348 | + s += types[i].getName(); |
| 349 | + } |
| 350 | + } |
| 351 | + } |
| 352 | + return s; |
| 353 | + } |
| 354 | + |
| 355 | + |
| 356 | + /**获取方法的定义 |
| 357 | + * @param request |
| 358 | + * @return |
| 359 | + * @throws IOException |
| 360 | + * @throws ClassNotFoundException |
| 361 | + * @throws IllegalArgumentException |
| 362 | + */ |
| 363 | + public String getMethodDefination(@NotNull JSONObject request) |
| 364 | + throws IllegalArgumentException, ClassNotFoundException, IOException { |
| 365 | +// request.put("arguments", removeComment(request.getString("methodArgs"))); |
| 366 | + return getMethodDefination(request, "method", "arguments", "type", "exceptions", "Java"); |
| 367 | + } |
| 368 | + /**获取方法的定义 |
| 369 | + * @param request |
| 370 | + * @param method |
| 371 | + * @param arguments |
| 372 | + * @param type |
| 373 | + * @return method(argType0,argType1...): returnType |
| 374 | + * @throws IOException |
| 375 | + * @throws ClassNotFoundException |
| 376 | + * @throws IllegalArgumentException |
| 377 | + */ |
| 378 | + public String getMethodDefination(@NotNull JSONObject request, String method, String arguments, String type, String exceptions, String language) |
| 379 | + throws IllegalArgumentException, ClassNotFoundException, IOException { |
| 380 | + String n = request.getString(method); |
| 381 | + if (StringUtil.isEmpty(n, true)) { |
| 382 | + throw new NullPointerException("getMethodDefination StringUtil.isEmpty(methodArgs, true) !"); |
| 383 | + } |
| 384 | + String a = request.getString(arguments); |
| 385 | + String t = request.getString(type); |
| 386 | + String e = request.getString(exceptions); |
| 387 | + |
| 388 | + if (language == null) { |
| 389 | + language = ""; |
| 390 | + } |
| 391 | + switch (language) { |
| 392 | + case "TypeScript": |
| 393 | + return n + "(" + (StringUtil.isEmpty(a, true) ? "" : a) + ")" + (StringUtil.isEmpty(t, true) ? "" : ": " + t) + (StringUtil.isEmpty(e, true) ? "" : " throws " + e); |
| 394 | + case "Go": |
| 395 | + return n + "(" + (StringUtil.isEmpty(a, true) ? "" : a ) + ")" + (StringUtil.isEmpty(t, true) ? "" : " " + t) + (StringUtil.isEmpty(e, true) ? "" : " throws " + e); |
| 396 | + default: |
| 397 | + //类型可能很长,Eclipse, Idea 代码提示都是类型放后面 return (StringUtil.isEmpty(t, true) ? "" : t + " ") + n + "(" + (StringUtil.isEmpty(a, true) ? "" : a) + ")"; |
| 398 | + return n + "(" + (StringUtil.isEmpty(a, true) ? "" : a) + ")" + (StringUtil.isEmpty(t, true) ? "" : ": " + t) + (StringUtil.isEmpty(e, true) ? "" : " throws " + e); |
| 399 | + } |
| 400 | + } |
| 401 | + |
| 402 | + /** |
| 403 | + * methodArgs 和 classArgs 都可以带注释 |
| 404 | + */ |
| 405 | + public String getMethodRequest(@NotNull JSONObject request) { |
| 406 | + String req = request.getString("request"); |
| 407 | + if (StringUtil.isEmpty(req, true) == false) { |
| 408 | + return req; |
| 409 | + } |
| 410 | + |
| 411 | + req = "{"; |
| 412 | + Boolean isStatic = request.getBoolean("static"); |
| 413 | + String methodArgs = request.getString("methodArgs"); |
| 414 | + String classArgs = request.getString("classArgs"); |
| 415 | + |
| 416 | + boolean comma = false; |
| 417 | + if (isStatic != null && isStatic) { |
| 418 | + req += "\n \"static\": " + true; |
| 419 | + comma = true; |
| 420 | + } |
| 421 | + if (StringUtil.isEmpty(methodArgs, true) == false) { |
| 422 | + req += (comma ? "," : "") + "\n \"methodArgs\": " + methodArgs; |
| 423 | + comma = true; |
| 424 | + } |
| 425 | + if (StringUtil.isEmpty(classArgs, true) == false) { |
| 426 | + req += (comma ? "," : "") + "\n \"classArgs\": " + classArgs; |
| 427 | + } |
| 428 | + req += "\n}"; |
| 429 | + return req; |
| 430 | + } |
| 431 | + |
| 432 | + // public static JSONObject removeComment(String json) { |
| 433 | + // return JSON.parseObject(removeComment(json)); |
| 434 | + // } |
| 435 | + public static String removeComment(String json) { |
| 436 | + return json == null ? null: json.replaceAll("(//.*)|(/\\*[\\s\\S]*?\\*/)", ""); |
| 437 | + } |
| 438 | + |
| 439 | + |
295 | 440 |
|
296 | 441 | public double plus(@NotNull JSONObject request, String i0, String i1) {
|
297 | 442 | return request.getDoubleValue(i0) + request.getDoubleValue(i1);
|
|
0 commit comments