From b6377ac9063bda2049208fc5532070c9dbd82051 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Wed, 10 Nov 2021 16:06:45 +0800 Subject: [PATCH] feat: use tools use JsonUtil to replace JsonParser --- .../bdware/analysis/dynamic/ProgramPoint.java | 16 +++++------ .../bdware/analysis/dynamic/TracedFile.java | 6 ++-- src/main/base/org/bdware/sc/py/PYEntry.java | 28 +++++++++---------- .../dynamic/test/ProgramPointTest.java | 4 +-- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/main/analysis/org/bdware/analysis/dynamic/ProgramPoint.java b/src/main/analysis/org/bdware/analysis/dynamic/ProgramPoint.java index f468d71..0b95ef3 100644 --- a/src/main/analysis/org/bdware/analysis/dynamic/ProgramPoint.java +++ b/src/main/analysis/org/bdware/analysis/dynamic/ProgramPoint.java @@ -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 trans; - List functionTrans; + List 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> ppToVal; public void insert(String functionID, String val) { diff --git a/src/main/analysis/org/bdware/analysis/dynamic/TracedFile.java b/src/main/analysis/org/bdware/analysis/dynamic/TracedFile.java index 52b3d02..40efc87 100644 --- a/src/main/analysis/org/bdware/analysis/dynamic/TracedFile.java +++ b/src/main/analysis/org/bdware/analysis/dynamic/TracedFile.java @@ -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>(); + 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()); diff --git a/src/main/base/org/bdware/sc/py/PYEntry.java b/src/main/base/org/bdware/sc/py/PYEntry.java index effb87f..e06f86c 100644 --- a/src/main/base/org/bdware/sc/py/PYEntry.java +++ b/src/main/base/org/bdware/sc/py/PYEntry.java @@ -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(); } diff --git a/src/test/java/org/bdware/analysis/dynamic/test/ProgramPointTest.java b/src/test/java/org/bdware/analysis/dynamic/test/ProgramPointTest.java index 0e72867..439a5df 100644 --- a/src/test/java/org/bdware/analysis/dynamic/test/ProgramPointTest.java +++ b/src/test/java/org/bdware/analysis/dynamic/test/ProgramPointTest.java @@ -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); }