Skip to content

Commit c1fca65

Browse files
committed
Class for paper-like statistics
1 parent 69de106 commit c1fca65

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package it.unipr.utils;
2+
3+
import java.util.Objects;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.json.JSONObject;
7+
8+
/**
9+
* Collects statistical data related to CFG analysis.
10+
*/
11+
public class PaperStatisticsObject extends StatisticsObject<PaperStatisticsObject> {
12+
13+
private static final Logger log = LogManager.getLogger(PaperStatisticsObject.class);
14+
15+
private int resolved;
16+
private int unreachable;
17+
private int erroneous;
18+
private int unknown;
19+
20+
/**
21+
* Creates a new {@code PaperStatisticsObject} with default values.
22+
*/
23+
private PaperStatisticsObject() {
24+
super();
25+
this.resolved = 0;
26+
this.unreachable = 0;
27+
this.erroneous = 0;
28+
this.unknown = 0;
29+
}
30+
31+
/**
32+
* Creates a new {@code PaperStatisticsObject} with specified values.
33+
*
34+
* @param address the contract address
35+
* @param totalOpcodes the total number of opcodes
36+
* @param totalJumps the total number of jumps
37+
* @param totalEdges the total number of edges
38+
* @param resolved the number of resolved jumps
39+
* @param unreachable the number of definitely unreachable jumps
40+
* @param erroneous the number of maybe unreachable jumps
41+
* @param unknown the number of unsound jumps
42+
* @param json the JSON representation of the object
43+
*/
44+
private PaperStatisticsObject(String address, int totalOpcodes, int totalJumps, int totalEdges,
45+
int resolved, int unreachable, int erroneous, int unknown, JSONObject json) {
46+
super(address, totalOpcodes, totalJumps, totalEdges, json);
47+
this.resolved = resolved;
48+
this.unreachable = unreachable;
49+
this.erroneous = erroneous;
50+
this.unknown = unknown;
51+
52+
this.json.put("resolved_jumps", this.resolved);
53+
this.json.put("unreachable_jumps", this.unreachable);
54+
this.json.put("erroneous_jumps", this.erroneous);
55+
this.json.put("unknown_jumps", this.unknown);
56+
}
57+
58+
/**
59+
* Returns the number of resolved jumps.
60+
*
61+
* @return the resolved jumps
62+
*/
63+
public int getResolved() {
64+
return resolved;
65+
}
66+
67+
/**
68+
* Returns the number of unreachable jumps.
69+
*
70+
* @return the unreachable jumps
71+
*/
72+
public int getUnreachable() {
73+
return unreachable;
74+
}
75+
76+
/**
77+
* Returns the number of erroneous jumps.
78+
*
79+
* @return the erroneous jumps
80+
*/
81+
public int getErroneous() {
82+
return erroneous;
83+
}
84+
85+
/**
86+
* Returns the number of unknown jumps.
87+
*
88+
* @return the unknown jumps
89+
*/
90+
public int getUnknown() {
91+
return unknown;
92+
}
93+
94+
/**
95+
* Creates a new {@code PaperStatisticsObject} with default values.
96+
*
97+
* @return a new instance of {@code PaperStatisticsObject}
98+
*/
99+
public static PaperStatisticsObject newStatisticsObject() {
100+
return new PaperStatisticsObject();
101+
}
102+
103+
/**
104+
* Sets the number of resolved jumps.
105+
*
106+
* @param resolved the resolved jumps
107+
*
108+
* @return the updated {@code PaperStatisticsObject} instance
109+
*/
110+
public PaperStatisticsObject resolved(int resolved) {
111+
this.resolved = resolved;
112+
return this;
113+
}
114+
115+
/**
116+
* Sets the number of unreachable jumps.
117+
*
118+
* @param unreachable the unreachable jumps
119+
*
120+
* @return the updated {@code PaperStatisticsObject} instance
121+
*/
122+
public PaperStatisticsObject unreachable(int unreachable) {
123+
this.unreachable = unreachable;
124+
return this;
125+
}
126+
127+
/**
128+
* Sets the number of erroneous jumps.
129+
*
130+
* @param erroneous the erroneous jumps
131+
*
132+
* @return the updated {@code PaperStatisticsObject} instance
133+
*/
134+
public PaperStatisticsObject erroneous(int erroneous) {
135+
this.erroneous = erroneous;
136+
return this;
137+
}
138+
139+
/**
140+
* Sets the number of unknown jumps.
141+
*
142+
* @param unknown the unknown jumps
143+
*
144+
* @return the updated {@code PaperStatisticsObject} instance
145+
*/
146+
public PaperStatisticsObject unknown(int unknown) {
147+
this.unknown = unknown;
148+
return this;
149+
}
150+
151+
/**
152+
* Builds a new {@code PaperStatisticsObject} with the specified values.
153+
*
154+
* @return a new {@code PaperStatisticsObject} instance
155+
*/
156+
@Override
157+
public PaperStatisticsObject build() {
158+
return new PaperStatisticsObject(address, totalOpcodes, totalJumps, totalEdges, resolved, unreachable,
159+
erroneous, unknown, json);
160+
}
161+
162+
@Override
163+
public boolean equals(Object o) {
164+
if (this == o)
165+
return true;
166+
if (o == null || getClass() != o.getClass())
167+
return false;
168+
PaperStatisticsObject that = (PaperStatisticsObject) o;
169+
return super.equals(o)
170+
&& resolved == that.resolved
171+
&& unreachable == that.unreachable
172+
&& erroneous == that.erroneous
173+
&& unknown == that.unknown;
174+
}
175+
176+
@Override
177+
public int hashCode() {
178+
return super.hashCode() ^ Objects.hash(resolved, unreachable, erroneous, unknown);
179+
}
180+
181+
/**
182+
* Logs the statistics of the given {@code PaperStatisticsObject}.
183+
*
184+
* @param statistics the statistics object to log
185+
*/
186+
@Override
187+
public void printStatistics() {
188+
log.info("Total opcodes: {}", getTotalOpcodes());
189+
log.info("Total jumps: {}", getTotalJumps());
190+
log.info("Total edges: {}", getTotalEdges());
191+
log.info("Resolved jumps: {}", getResolved());
192+
log.info("Unreachable jumps: {}", getUnreachable());
193+
log.info("Erroneous jumps: {}", getErroneous());
194+
log.info("Unknown jumps: {}", getUnknown());
195+
}
196+
}

0 commit comments

Comments
 (0)