feat: use tools

use JsonUtil to replace JsonParser
This commit is contained in:
Frank.R.Wu 2021-11-10 16:06:45 +08:00
parent 6adcf235fb
commit b6377ac906
4 changed files with 26 additions and 28 deletions

View File

@ -1,9 +1,9 @@
package org.bdware.analysis.dynamic;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.sc.util.JsonUtil;
import java.io.InputStream;
import java.util.*;
@ -11,16 +11,16 @@ import java.util.*;
public class ProgramPoint {
private static final Logger LOGGER = LogManager.getLogger(ProgramPoint.class);
List<Transaction> trans;
List<FunctionTran> functionTrans;
List<FunctionTx> functionTrans;
public ProgramPoint(InputStream in) {
Scanner sc = new Scanner(in);
trans = new ArrayList<>();
functionTrans = new ArrayList<>();
Transaction transaction = new Transaction();
FunctionTran functionTran = new FunctionTran();
FunctionTx funcTx = new FunctionTx();
transaction.tmToVal = new HashMap<>();
functionTran.ppToVal = new HashMap<>();
funcTx.ppToVal = new HashMap<>();
// TODO ignore handle transaction start
// TODO ignore handle contractID/method....
// TODO ignore handle transaction end
@ -31,7 +31,7 @@ public class ProgramPoint {
String[] strings = string.split("_");
strings = strings[1].split("[(]");
System.out.println("[stringsss: ]" + strings[0]);
functionTran.insert(strings[0], "ENTER");
funcTx.insert(strings[0], "ENTER");
}
// if (string.contains("EXIT")) {
// String[] strings = string.split("_");
@ -40,7 +40,7 @@ public class ProgramPoint {
// functionMap.put(strings[1], "EXIT");
// }
if (string.contains("traceMark")) {
JsonObject jo = JsonParser.parseString(string).getAsJsonObject();
JsonObject jo = JsonUtil.parseString(string);
if (jo.get("traceMark") != null) {
if (jo.get("lval") != null) {
transaction.insert(
@ -54,7 +54,7 @@ public class ProgramPoint {
}
}
}
LOGGER.info(functionTran.ppToVal);
LOGGER.info(funcTx.ppToVal);
LOGGER.info(transaction.tmToVal);
sc.close();
}
@ -74,7 +74,7 @@ public class ProgramPoint {
}
}
static class FunctionTran {
static class FunctionTx {
Map<String, List<String>> ppToVal;
public void insert(String functionID, String val) {

View File

@ -1,7 +1,7 @@
package org.bdware.analysis.dynamic;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.sc.util.JsonUtil;
import java.io.InputStream;
import java.util.*;
@ -13,12 +13,12 @@ public class TracedFile {
Scanner sc = new Scanner(in);
trans = new ArrayList<>();
Transaction transaction = new Transaction();
transaction.tmToVal = new HashMap<Integer, List<Integer>>();
transaction.tmToVal = new HashMap<>();
// TODO ignore handle transaction start
// TODO ignore handle contractID/method....
// TODO ignore handle transaction end
while (sc.hasNextLine()) {
JsonObject jo = JsonParser.parseString(sc.nextLine()).getAsJsonObject();
JsonObject jo = JsonUtil.parseString(sc.nextLine());
if (null != jo.get("traceMark")) {
if (null != jo.get("lval")) {
transaction.insert(jo.get("traceMark").getAsInt(), jo.get("lval").getAsInt());

View File

@ -2,7 +2,6 @@ package org.bdware.sc.py;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.sc.py.bean.*;
import org.bdware.sc.py.utils.HttpClient;
import org.bdware.sc.py.utils.HttpExt;
@ -55,14 +54,13 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams);
response = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(response).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(response);
return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
}
public String createBuiltinsObj(String moduleClassName, PYMethodParams PYMethodParams) {
String urlPath = "/api/createBuiltinsObj/";
String httpResponse = null;
String httpResponse;
PYYjsRequest pyYjsRequest = new PYYjsRequest();
PYYjsParams pyYjsParams = new PYYjsParams();
@ -74,7 +72,7 @@ public class PYEntry {
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
}
@ -82,7 +80,7 @@ public class PYEntry {
String urlPath = "/api/availablePackages/";
String response;
response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonParser.parseString(response).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availablePackages");
JsonArray array = availablePackages.getAsJsonArray("packages");
@ -100,7 +98,7 @@ public class PYEntry {
String urlPath = "/api/availableModules/";
String response;
response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonParser.parseString(response).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availableModules");
JsonArray array = availablePackages.getAsJsonArray("modules");
@ -127,7 +125,7 @@ public class PYEntry {
// "yjsResult": {"packageTotalInfo": {"package_name": "yjsexample",
// "module_names": ["yjsexample.sample"]}}
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject packageTotalInfo =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("packageTotalInfo");
PackageInfo packageInfo = JsonUtil.fromJson(packageTotalInfo, PackageInfo.class);
@ -163,7 +161,7 @@ public class PYEntry {
// "sayHello"}, {"func_sign": "(self, name)", "func_name": "sayHello"}],
// "class_name": "A"}
ModuleInfo moduleInfo;
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject moduleInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("moduleInfo");
moduleInfo = JsonUtil.fromJson(moduleInfoObj, ModuleInfo.class);
@ -227,12 +225,12 @@ public class PYEntry {
System.out.println(JsonUtil.toJson(pyYjsRequest));
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
PYYjsResponse pyYjsResponse = JsonUtil.fromJson(httpResponse, PYYjsResponse.class);
// PYYjsResponse pyYjsResponse = JsonUtil.fromJson(httpResponse, PYYjsResponse.class);
PYClass pyClass = new PYClass();
ClassCollection classInfo;
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject classInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("classInfo");
classInfo = JsonUtil.fromJson(classInfoObj, ClassCollection.class);
@ -291,7 +289,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -310,7 +308,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -331,7 +329,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -354,7 +352,7 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams);
System.out.println(JsonUtil.toJson(pyYjsRequest));
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject();
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}

View File

@ -8,9 +8,9 @@ import java.io.InputStream;
public class ProgramPointTest {
public static void main(String[] args) throws FileNotFoundException {
String tracefile =
String traceFile =
"/Users/hulingxuan/git/SmartContract/contractExamples/traceTest/mainTrace.trace";
InputStream in = new FileInputStream(tracefile);
InputStream in = new FileInputStream(traceFile);
ProgramPoint tf = new ProgramPoint(in);
// tf.trans.add(Transaction);
}