Skip to content

Commit 5ef8a5e

Browse files
committed
Server:引入 UnitAuto 自动化单元测试工具 MethodUtil 及相关接口和远程函数
1 parent 1eebd7b commit 5ef8a5e

File tree

3 files changed

+1016
-0
lines changed

3 files changed

+1016
-0
lines changed

APIJSON-Java-Server/APIJSONFramework/src/main/java/apijson/framework/APIJSONController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import javax.servlet.http.HttpSession;
3636

37+
import com.alibaba.fastjson.JSON;
3738
import com.alibaba.fastjson.JSONObject;
3839

3940
import apijson.Log;
@@ -223,4 +224,12 @@ public Object logout(@NotNull HttpSession session) {
223224
}
224225

225226

227+
public JSONObject invokeMethod(String request) {
228+
return MethodUtil.invokeMethod(request);
229+
}
230+
231+
public JSONObject listMethod(String request) {
232+
return MethodUtil.listMethod(request);
233+
}
234+
226235
}

APIJSON-Java-Server/APIJSONFramework/src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static apijson.framework.APIJSONConstant.FUNCTION_;
1818

19+
import java.io.IOException;
1920
import java.rmi.ServerException;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
@@ -33,6 +34,7 @@
3334
import apijson.RequestMethod;
3435
import apijson.RequestRole;
3536
import apijson.StringUtil;
37+
import apijson.framework.MethodUtil.Argument;
3638
import apijson.orm.AbstractFunctionParser;
3739
import apijson.orm.JSONRequest;
3840
import apijson.orm.ParserCreator;
@@ -292,6 +294,149 @@ public Object verifyAccess(@NotNull JSONObject request) throws Exception {
292294

293295

294296

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+
295440

296441
public double plus(@NotNull JSONObject request, String i0, String i1) {
297442
return request.getDoubleValue(i0) + request.getDoubleValue(i1);

0 commit comments

Comments
 (0)