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

View File

@ -1,7 +1,7 @@
package org.bdware.analysis.dynamic; package org.bdware.analysis.dynamic;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import org.bdware.sc.util.JsonUtil;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@ -13,12 +13,12 @@ public class TracedFile {
Scanner sc = new Scanner(in); Scanner sc = new Scanner(in);
trans = new ArrayList<>(); trans = new ArrayList<>();
Transaction transaction = new Transaction(); Transaction transaction = new Transaction();
transaction.tmToVal = new HashMap<Integer, List<Integer>>(); transaction.tmToVal = new HashMap<>();
// TODO ignore handle transaction start // TODO ignore handle transaction start
// TODO ignore handle contractID/method.... // TODO ignore handle contractID/method....
// TODO ignore handle transaction end // TODO ignore handle transaction end
while (sc.hasNextLine()) { 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("traceMark")) {
if (null != jo.get("lval")) { if (null != jo.get("lval")) {
transaction.insert(jo.get("traceMark").getAsInt(), jo.get("lval").getAsInt()); 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.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.sc.py.bean.*; import org.bdware.sc.py.bean.*;
import org.bdware.sc.py.utils.HttpClient; import org.bdware.sc.py.utils.HttpClient;
import org.bdware.sc.py.utils.HttpExt; import org.bdware.sc.py.utils.HttpExt;
@ -55,14 +54,13 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams); pyYjsRequest.setYjsParams(pyYjsParams);
response = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest)); 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(); return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
} }
public String createBuiltinsObj(String moduleClassName, PYMethodParams PYMethodParams) { public String createBuiltinsObj(String moduleClassName, PYMethodParams PYMethodParams) {
String urlPath = "/api/createBuiltinsObj/"; String urlPath = "/api/createBuiltinsObj/";
String httpResponse = null; String httpResponse;
PYYjsRequest pyYjsRequest = new PYYjsRequest(); PYYjsRequest pyYjsRequest = new PYYjsRequest();
PYYjsParams pyYjsParams = new PYYjsParams(); PYYjsParams pyYjsParams = new PYYjsParams();
@ -74,7 +72,7 @@ public class PYEntry {
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest)); 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(); return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
} }
@ -82,7 +80,7 @@ public class PYEntry {
String urlPath = "/api/availablePackages/"; String urlPath = "/api/availablePackages/";
String response; String response;
response = HttpClient.get(ip, port, urlPath); response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonParser.parseString(response).getAsJsonObject(); JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject availablePackages = JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availablePackages"); pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availablePackages");
JsonArray array = availablePackages.getAsJsonArray("packages"); JsonArray array = availablePackages.getAsJsonArray("packages");
@ -100,7 +98,7 @@ public class PYEntry {
String urlPath = "/api/availableModules/"; String urlPath = "/api/availableModules/";
String response; String response;
response = HttpClient.get(ip, port, urlPath); response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonParser.parseString(response).getAsJsonObject(); JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject availablePackages = JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availableModules"); pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availableModules");
JsonArray array = availablePackages.getAsJsonArray("modules"); JsonArray array = availablePackages.getAsJsonArray("modules");
@ -127,7 +125,7 @@ public class PYEntry {
// "yjsResult": {"packageTotalInfo": {"package_name": "yjsexample", // "yjsResult": {"packageTotalInfo": {"package_name": "yjsexample",
// "module_names": ["yjsexample.sample"]}} // "module_names": ["yjsexample.sample"]}}
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject(); JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject packageTotalInfo = JsonObject packageTotalInfo =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("packageTotalInfo"); pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("packageTotalInfo");
PackageInfo packageInfo = JsonUtil.fromJson(packageTotalInfo, PackageInfo.class); PackageInfo packageInfo = JsonUtil.fromJson(packageTotalInfo, PackageInfo.class);
@ -163,7 +161,7 @@ public class PYEntry {
// "sayHello"}, {"func_sign": "(self, name)", "func_name": "sayHello"}], // "sayHello"}, {"func_sign": "(self, name)", "func_name": "sayHello"}],
// "class_name": "A"} // "class_name": "A"}
ModuleInfo moduleInfo; ModuleInfo moduleInfo;
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject(); JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject moduleInfoObj = JsonObject moduleInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("moduleInfo"); pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("moduleInfo");
moduleInfo = JsonUtil.fromJson(moduleInfoObj, ModuleInfo.class); moduleInfo = JsonUtil.fromJson(moduleInfoObj, ModuleInfo.class);
@ -227,12 +225,12 @@ public class PYEntry {
System.out.println(JsonUtil.toJson(pyYjsRequest)); System.out.println(JsonUtil.toJson(pyYjsRequest));
httpResponse = HttpClient.post(ip, port, urlPath, 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(); PYClass pyClass = new PYClass();
ClassCollection classInfo; ClassCollection classInfo;
JsonObject pyResponse = JsonParser.parseString(httpResponse).getAsJsonObject(); JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject classInfoObj = JsonObject classInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("classInfo"); pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("classInfo");
classInfo = JsonUtil.fromJson(classInfoObj, ClassCollection.class); classInfo = JsonUtil.fromJson(classInfoObj, ClassCollection.class);
@ -291,7 +289,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams); pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams); pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, 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"); JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString(); return ysjResult.toString();
} }
@ -310,7 +308,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams); pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams); pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, 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"); JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString(); return ysjResult.toString();
} }
@ -331,7 +329,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams); pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams); pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, 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"); JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString(); return ysjResult.toString();
} }
@ -354,7 +352,7 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams); pyYjsRequest.setYjsParams(pyYjsParams);
System.out.println(JsonUtil.toJson(pyYjsRequest)); System.out.println(JsonUtil.toJson(pyYjsRequest));
httpResponse = HttpClient.post(ip, port, urlPath, 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"); JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString(); return ysjResult.toString();
} }

View File

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