Skip to content

Commit 14f3689

Browse files
authored
处理初始化远程函数不通过引起的远程函数加载不全的问题,感谢 calmcc 的贡献 #28
#28
1 parent ec87dff commit 14f3689

File tree

1 file changed

+80
-85
lines changed

1 file changed

+80
-85
lines changed

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 80 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -140,60 +140,60 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
140140
//if (Log.DEBUG == false) {
141141
// function.put(APIJSONConstant.KEY_DEBUG, 0);
142142
//}
143-
//
143+
//
144144
//JSONRequest functionItem = new JSONRequest();
145145
//functionItem.put(FUNCTION_, function);
146-
//
147-
//JSONObject script = new JSONRequest(); // isAll ? new JSONRequest() : table;
148-
//script.put("simple", 0);
149-
//if (Log.DEBUG == false) {
150-
// script.put(APIJSONConstant.KEY_DEBUG, 0);
151-
//}
152-
// 不能用这个来优化,因为可能配置了不校验远程函数是否存在
153-
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
154-
//JSONRequest nameInAt = new JSONRequest();
155-
//nameInAt.put("from", "Function");
156-
//{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
157-
// JSONRequest fun = new JSONRequest();
158-
// fun.setColumn("name");
159-
// nameInAt.put("Function", fun);
160-
//} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
161-
162-
//script.put("name{}@", nameInAt);
163-
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
146+
//
147+
//JSONObject script = new JSONRequest(); // isAll ? new JSONRequest() : table;
148+
//script.put("simple", 0);
149+
//if (Log.DEBUG == false) {
150+
// script.put(APIJSONConstant.KEY_DEBUG, 0);
151+
//}
152+
// 不能用这个来优化,因为可能配置了不校验远程函数是否存在
153+
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
154+
//JSONRequest nameInAt = new JSONRequest();
155+
//nameInAt.put("from", "Function");
156+
//{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
157+
// JSONRequest fun = new JSONRequest();
158+
// fun.setColumn("name");
159+
// nameInAt.put("Function", fun);
160+
//} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
161+
162+
//script.put("name{}@", nameInAt);
163+
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
164164

165165
//JSONRequest scriptItem = new JSONRequest();
166-
//scriptItem.put(SCRIPT_, script);
166+
//scriptItem.put(SCRIPT_, script);
167167

168168
JSONObject request = new JSONObject();
169169
//request.putAll(functionItem.toArray(0, 0, FUNCTION_));
170170
//request.putAll(scriptItem.toArray(0, 0, SCRIPT_));
171171

172-
// 可以用它,因为 Function 表必须存在,没有绕过校验的配置 // 不能用这个来优化,因为可能配置了不校验远程函数是否存在
173-
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
174-
JSONRequest item = new JSONRequest();
175-
176-
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
177-
JSONObject function = isAll ? new JSONRequest() : table;
178-
if (!Log.DEBUG) {
179-
function.put(APIJSONConstant.KEY_DEBUG, 0);
180-
}
181-
item.put(FUNCTION_, function);
182-
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
183-
184-
if (ENABLE_SCRIPT_FUNCTION) { // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
185-
JSONRequest script = new JSONRequest();
186-
script.put("name@", "/Function/name");
187-
script.put("simple", 0);
188-
item.put(SCRIPT_, script);
189-
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
190-
191-
request.putAll(item.toArray(0, 0));
192-
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
172+
// 可以用它,因为 Function 表必须存在,没有绕过校验的配置 // 不能用这个来优化,因为可能配置了不校验远程函数是否存在
173+
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
174+
JSONRequest item = new JSONRequest();
175+
176+
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
177+
JSONObject function = isAll ? new JSONRequest() : table;
178+
if (! Log.DEBUG) {
179+
function.put(APIJSONConstant.KEY_DEBUG, 0);
180+
}
181+
item.put(FUNCTION_, function);
182+
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
183+
184+
if (ENABLE_SCRIPT_FUNCTION) { // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
185+
JSONRequest script = new JSONRequest();
186+
script.put("name@", "/Function/name");
187+
script.put("simple", 0);
188+
item.put(SCRIPT_, script);
189+
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
190+
191+
request.putAll(item.toArray(0, 0));
192+
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
193193

194194

195195
JSONObject response = creator.createParser().setMethod(GET).setNeedVerify(true).parseResponse(request);
196-
if (!JSONResponse.isSuccess(response)) {
196+
if (! JSONResponse.isSuccess(response)) {
197197
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
198198
}
199199

@@ -207,38 +207,38 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
207207
}
208208

209209
Map<String, JSONObject> scriptMap = new HashMap<>();
210-
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
211-
if (scriptList != null && !scriptList.isEmpty()) {
212-
//if (isAll) {
213-
// SCRIPT_MAP = new LinkedHashMap<>();
214-
//}
215-
Map<String, JSONObject> newMap = new LinkedHashMap<>();
216-
217-
for (int i = 0; i < scriptList.size(); i++) {
218-
JSONObject item = scriptList.getJSONObject(i);
219-
item = item == null ? null : item.getJSONObject(SCRIPT_);
220-
if (item == null) { // 关联查不到很正常
221-
continue;
222-
}
223-
224-
String n = item.getString("name");
225-
if (!StringUtil.isName(n)) {
226-
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
227-
}
228-
229-
String s = item.getString("script");
230-
if (StringUtil.isEmpty(s, true)) {
231-
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
232-
}
233-
newMap.put(n, item);
234-
}
235-
236-
scriptMap = newMap;
237-
}
210+
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
211+
if (scriptList != null && ! scriptList.isEmpty()) {
212+
//if (isAll) {
213+
// SCRIPT_MAP = new LinkedHashMap<>();
214+
//}
215+
Map<String, JSONObject> newMap = new LinkedHashMap<>();
216+
217+
for (int i = 0; i < scriptList.size(); i++) {
218+
JSONObject item = scriptList.getJSONObject(i);
219+
item = item == null ? null : item.getJSONObject(SCRIPT_);
220+
if (item == null) { // 关联查不到很正常
221+
continue;
222+
}
223+
224+
String n = item.getString("name");
225+
if (! StringUtil.isName(n)) {
226+
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
227+
}
228+
229+
String s = item.getString("script");
230+
if (StringUtil.isEmpty(s, true)) {
231+
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
232+
}
233+
newMap.put(n, item);
234+
}
235+
236+
scriptMap = newMap;
237+
}
238238

239239
JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
240240
int size = list == null ? 0 : list.size();
241-
if (isAll && size == 0) {
241+
if (isAll && size <= 0) {
242242
Log.w(TAG, "init isAll && size <= 0,,没有可用的远程函数");
243243
return response;
244244
}
@@ -251,7 +251,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
251251

252252
for (int i = 0; i < size; i++) {
253253
JSONObject item = list.getJSONObject(i);
254-
item = item == null ? null : item.getJSONObject(FUNCTION_);
254+
item = item == null ? null : item.getJSONObject(FUNCTION_);
255255
if (item == null) {
256256
continue;
257257
}
@@ -265,7 +265,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
265265
}
266266
}
267267
String name = item.getString("name");
268-
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
268+
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
269269
// demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));
270270
//加载脚本
271271
if (item.get("language") != null) {
@@ -283,12 +283,12 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
283283

284284
String[] methods = StringUtil.split(item.getString("methods"));
285285

286-
if (methods == null || methods.length == 0) {
286+
if (methods == null || methods.length <= 0) {
287287
methods = ALL_METHODS;
288288
}
289289

290290
if (demo != null){
291-
if (!demo.containsKey("result()")) {
291+
if (! demo.containsKey("result()")) {
292292
demo.put("result()", getFunctionCall(name, item.getString("arguments")));
293293
}
294294
demo.put(JSONRequest.KEY_TAG, item.get(JSONRequest.KEY_TAG));
@@ -301,7 +301,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
301301
.setNeedVerify(false)
302302
.parseResponse(demo);
303303

304-
if (!JSONResponse.isSuccess(r)) {
304+
if (! JSONResponse.isSuccess(r)) {
305305
try {
306306
onServerError(JSONResponse.getMsg(r), shutdownWhenServerError);
307307
} catch (Exception e) {
@@ -385,11 +385,6 @@ public static <T extends Object> void test(APIJSONFunctionParser<T> function) th
385385
}
386386

387387

388-
389-
390-
391-
392-
393388
/**获取远程函数的demo,如果没有就自动补全
394389
* @param curObj
395390
* @return
@@ -400,7 +395,7 @@ public JSONObject getFunctionDemo(@NotNull JSONObject curObj) {
400395
if (demo == null) {
401396
demo = new JSONObject();
402397
}
403-
if (!demo.containsKey("result()")) {
398+
if (! demo.containsKey("result()")) {
404399
demo.put("result()", getFunctionCall(curObj.getString("name"), curObj.getString("arguments")));
405400
}
406401
return demo;
@@ -774,11 +769,11 @@ public String getMethodRequest(@NotNull JSONObject curObj) {
774769
req += "\n \"static\": " + true;
775770
comma = true;
776771
}
777-
if (!StringUtil.isEmpty(methodArgs, true)) {
772+
if (! StringUtil.isEmpty(methodArgs, true)) {
778773
req += (comma ? "," : "") + "\n \"methodArgs\": " + methodArgs;
779774
comma = true;
780775
}
781-
if (!StringUtil.isEmpty(classArgs, true)) {
776+
if (! StringUtil.isEmpty(classArgs, true)) {
782777
req += (comma ? "," : "") + "\n \"classArgs\": " + classArgs;
783778
}
784779
req += "\n}";
@@ -792,4 +787,4 @@ public static String removeComment(String json) {
792787
return json == null ? null: json.replaceAll("(//.*)|(/\\*[\\s\\S]*?\\*/)", "");
793788
}
794789

795-
}
790+
}

0 commit comments

Comments
 (0)