Skip to content

Commit 42a60be

Browse files
committed
Added fromJson methods JSON deserialization
1 parent 8fb3e4a commit 42a60be

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
lines changed

src/main/java/it/unipr/utils/PaperStatisticsObject.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,35 @@ public static PaperStatisticsObject newStatisticsObject() {
115115
return new PaperStatisticsObject();
116116
}
117117

118+
/**
119+
* Constructs a PaperStatisticsObject from a JSON representation. Missing
120+
* fields are treated as zero/defaults.
121+
*
122+
* @param json the JSONObject containing statistics
123+
*
124+
* @return a built PaperStatisticsObject
125+
*/
126+
public static PaperStatisticsObject fromJson(org.json.JSONObject json) {
127+
if (json == null)
128+
return null;
129+
return PaperStatisticsObject.newStatisticsObject()
130+
.address(json.optString("address", ""))
131+
.totalOpcodes(json.optInt("total_opcodes", 0))
132+
.totalJumps(json.optInt("total_jumps", 0))
133+
.totalEdges(json.optInt("total_edges", 0))
134+
.resolved(json.optInt("resolved_jumps", 0))
135+
.unreachable(json.optInt("unreachable_jumps", 0))
136+
.erroneous(json.optInt("erroneous_jumps", 0))
137+
.unknown(json.optInt("unknown_jumps", 0))
138+
.topState(json.optInt("top_stack_head_jumps", 0))
139+
.build();
140+
}
141+
118142
/**
119143
* Sets the number of resolved jumps.
120144
*
121145
* @param resolved the resolved jumps
122-
*
146+
*
123147
* @return the updated {@code PaperStatisticsObject} instance
124148
*/
125149
public PaperStatisticsObject resolved(int resolved) {
@@ -131,7 +155,7 @@ public PaperStatisticsObject resolved(int resolved) {
131155
* Sets the number of unreachable jumps.
132156
*
133157
* @param unreachable the unreachable jumps
134-
*
158+
*
135159
* @return the updated {@code PaperStatisticsObject} instance
136160
*/
137161
public PaperStatisticsObject unreachable(int unreachable) {
@@ -143,7 +167,7 @@ public PaperStatisticsObject unreachable(int unreachable) {
143167
* Sets the number of erroneous jumps.
144168
*
145169
* @param erroneous the erroneous jumps
146-
*
170+
*
147171
* @return the updated {@code PaperStatisticsObject} instance
148172
*/
149173
public PaperStatisticsObject erroneous(int erroneous) {
@@ -155,7 +179,7 @@ public PaperStatisticsObject erroneous(int erroneous) {
155179
* Sets the number of unknown jumps.
156180
*
157181
* @param unknown the unknown jumps
158-
*
182+
*
159183
* @return the updated {@code PaperStatisticsObject} instance
160184
*/
161185
public PaperStatisticsObject unknown(int unknown) {
@@ -167,7 +191,7 @@ public PaperStatisticsObject unknown(int unknown) {
167191
* Sets the number of unknown jumps where the whole state was top.
168192
*
169193
* @param topState the unknown jumps where the whole state was top
170-
*
194+
*
171195
* @return the updated {@code PaperStatisticsObject} instance
172196
*/
173197
public PaperStatisticsObject topState(int topState) {

src/main/java/it/unipr/utils/StandardStatisticsObject.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,35 @@ public static StandardStatisticsObject newStatisticsObject() {
116116
return new StandardStatisticsObject();
117117
}
118118

119+
/**
120+
* Constructs a StandardStatisticsObject from a JSON representation. Missing
121+
* fields are treated as zero/defaults.
122+
*
123+
* @param json the JSONObject containing statistics
124+
*
125+
* @return a built StandardStatisticsObject
126+
*/
127+
public static StandardStatisticsObject fromJson(org.json.JSONObject json) {
128+
if (json == null)
129+
return null;
130+
return StandardStatisticsObject.newStatisticsObject()
131+
.address(json.optString("address", ""))
132+
.totalOpcodes(json.optInt("total_opcodes", 0))
133+
.totalJumps(json.optInt("total_jumps", 0))
134+
.totalEdges(json.optInt("total_edges", 0))
135+
.resolvedJumps(json.optInt("resolved_jumps", 0))
136+
.definitelyUnreachableJumps(json.optInt("definitely_unreachable_jumps", 0))
137+
.maybeUnreachableJumps(json.optInt("maybe_unreachable_jumps", 0))
138+
.maybeUnsoundJumps(json.optInt("maybe_unsound_jumps", 0))
139+
.unsoundJumps(json.optInt("unsound_jumps", 0))
140+
.build();
141+
}
142+
119143
/**
120144
* Sets the number of resolved jumps.
121145
*
122146
* @param resolvedJumps the resolved jumps
123-
*
147+
*
124148
* @return the updated {@code StandardStatisticsObject} instance
125149
*/
126150
public StandardStatisticsObject resolvedJumps(int resolvedJumps) {
@@ -132,7 +156,7 @@ public StandardStatisticsObject resolvedJumps(int resolvedJumps) {
132156
* Sets the number of definitely unreachable jumps.
133157
*
134158
* @param definitelyUnreachableJumps the definitely unreachable jumps
135-
*
159+
*
136160
* @return the updated {@code StandardStatisticsObject} instance
137161
*/
138162
public StandardStatisticsObject definitelyUnreachableJumps(int definitelyUnreachableJumps) {
@@ -144,7 +168,7 @@ public StandardStatisticsObject definitelyUnreachableJumps(int definitelyUnreach
144168
* Sets the number of maybe unreachable jumps.
145169
*
146170
* @param maybeUnreachableJumps the maybe unreachable jumps
147-
*
171+
*
148172
* @return the updated {@code StandardStatisticsObject} instance
149173
*/
150174
public StandardStatisticsObject maybeUnreachableJumps(int maybeUnreachableJumps) {
@@ -156,7 +180,7 @@ public StandardStatisticsObject maybeUnreachableJumps(int maybeUnreachableJumps)
156180
* Sets the number of unsound jumps.
157181
*
158182
* @param unsoundJumps the unsound jumps
159-
*
183+
*
160184
* @return the updated {@code StandardStatisticsObject} instance
161185
*/
162186
public StandardStatisticsObject unsoundJumps(int unsoundJumps) {
@@ -168,7 +192,7 @@ public StandardStatisticsObject unsoundJumps(int unsoundJumps) {
168192
* Sets the number of maybe unsound jumps.
169193
*
170194
* @param maybeUnsoundJumps the maybe unsound jumps
171-
*
195+
*
172196
* @return the updated {@code StandardStatisticsObject} instance
173197
*/
174198
public StandardStatisticsObject maybeUnsoundJumps(int maybeUnsoundJumps) {

src/main/java/it/unipr/utils/VulnerabilitiesObject.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,31 @@ public static VulnerabilitiesObject newVulnerabilitiesObject() {
6262
return new VulnerabilitiesObject();
6363
}
6464

65+
/**
66+
* Constructs a VulnerabilitiesObject from a JSON representation. Missing
67+
* fields are treated as zero/defaults.
68+
*
69+
* @param json the JSONObject containing vulnerabilities
70+
*
71+
* @return a built VulnerabilitiesObject
72+
*/
73+
public static VulnerabilitiesObject fromJson(org.json.JSONObject json) {
74+
if (json == null)
75+
return null;
76+
return VulnerabilitiesObject.newVulnerabilitiesObject()
77+
.reentrancy(json.optInt("reentrancy", 0))
78+
.randomness(json.optInt("randomness_dependency", 0))
79+
.possibleRandomness(json.optInt("randomness_dependency_possible", 0))
80+
.txOrigin(json.optInt("tx_origin", 0))
81+
.possibleTxOrigin(json.optInt("tx_origin_possible", 0))
82+
.build();
83+
}
84+
6585
/**
6686
* Sets the reentrancy vulnerability score.
6787
*
6888
* @param reentrancy the reentrancy score
69-
*
89+
*
7090
* @return the updated {@code VulnerabilitiesObject} instance
7191
*/
7292
public VulnerabilitiesObject reentrancy(int reentrancy) {
@@ -78,7 +98,7 @@ public VulnerabilitiesObject reentrancy(int reentrancy) {
7898
* Sets the randomness dependency vulnerability score.
7999
*
80100
* @param randomness the randomness dependency score
81-
*
101+
*
82102
* @return the updated {@code VulnerabilitiesObject} instance
83103
*/
84104
public VulnerabilitiesObject randomness(int randomness) {
@@ -90,7 +110,7 @@ public VulnerabilitiesObject randomness(int randomness) {
90110
* Sets the possible randomness dependency vulnerability score.
91111
*
92112
* @param possibleRandomness the possible randomness dependency score
93-
*
113+
*
94114
* @return the updated {@code VulnerabilitiesObject} instance
95115
*/
96116
public VulnerabilitiesObject possibleRandomness(int possibleRandomness) {
@@ -102,7 +122,7 @@ public VulnerabilitiesObject possibleRandomness(int possibleRandomness) {
102122
* Sets the tx. origin vulnerability score.
103123
*
104124
* @param txOrigin the tx. origin score
105-
*
125+
*
106126
* @return the updated {@code VulnerabilitiesObject} instance
107127
*/
108128
public VulnerabilitiesObject txOrigin(int txOrigin) {
@@ -130,7 +150,7 @@ public VulnerabilitiesObject possibleTxOrigin(int possibleTxOrigin) {
130150
*
131151
* @param cfg the EVM control-flow graph from which to extract vulnerability
132152
* data
133-
*
153+
*
134154
* @return a {@link VulnerabilitiesObject} containing detected
135155
* vulnerabilities
136156
*/

0 commit comments

Comments
 (0)