resess / Slicer4J

Slicer4J is an accurate, low-overhead dynamic slicer for Java programs.
MIT License
38 stars 17 forks source link

toString() methods and data classes. #11

Closed ArtemUntila closed 3 years ago

ArtemUntila commented 3 years ago

Main.java:

1.  import java.util.Arrays;
2.  import java.util.Collection;
3.  import java.util.HashSet;
4.  import java.util.Set;
5.  
6.  public class Main {
7.  
8.      public static void main(String[] args) {
9.          PairSet<Integer, String> pairSet = new PairSet<>(Arrays.asList(new Pair<>(1, "A"), new Pair<>(2, "B")));
10.         System.out.println(pairSet);
11.     }
12. }
13. 
14. class PairSet<T, G> {
15. 
16.     Set<Pair<T, G>> set;
17. 
18.     public PairSet() {
19.         set = new HashSet<>();
20.     }
21. 
22.     public PairSet(Collection<Pair<T, G>> collection) {
23.         this();
24.         set.addAll(collection);
25.     }
26. 
27.     @Override
28.     public String toString() {
29.         StringBuilder sb = new StringBuilder();
30.         for (Pair<T, G> pair : set) {
31.             sb.append(pair).append('\n');
32.         }
33.         return sb.toString();
34.     }
35. }
36. 
37. class Pair<T, G> {
38. 
39.     T first;
40.     G second;
41. 
42.     public Pair(T first, G second) {
43.         this.first = first;
44.         this.second = second;
45.     }
46. 
47.     @Override
48.     public String toString() {
49.         return String.format("%s %s %s", first, (char) 8212, second);
50.     }
51. }

If we run the program, it outputs:

2 — B
1 — A

Running Slicer4J w.r.t. Main:10 line:

cd scripts
python3 slicer4j.py -j .../issue.jar -o .../issue -b Main:10 -m "Main"

slice.log:

Main:10
Main:9
PairSet:23
PairSet:18
PairSet:19

Class Pair and both toString() methods were not included at all, despite Pair constructor was definitely executed.

Not included class Pair is relevant to my previous issue #10, which means that the same thing will happen with any data classes. Lines 16 and 24 are directly related to the issue #10.

khaled-e-a commented 3 years ago

fixed in latest DynamicSlicingCore and Slicer4J and added tests.