forked from MathieuNls/clever-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
100 lines (82 loc) · 2.95 KB
/
Main.java
File metadata and controls
100 lines (82 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.File;
import java.lang.StringBuffer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Iterator;
import org.json.*;
public class Main {
//main is the entry point of our go program. It defers
//the execution of timeTrack so we can know how long it
//took for the main to complete.
//It also calls the compute and output the returned struct
//to stdout.
public static void main(String[] args) {
computeTime(System.currentTimeMillis(), computeDiff().toString(), "compute diff");
computeTime(System.currentTimeMillis(), computeAST().toString(), "compute AST");
}
public static void computeTime(long StartTime, String s, String text) {
long EndTime = System.currentTimeMillis();
System.out.println(s);
System.out.println(String.format("%s took %d ms", text, EndTime - StartTime));
}
//compute parses the git diffs in ./diffs and returns
//a diffResult struct that contains all the relevant informations
//about these diffs
// list of files in the diffs
// number of regions
// number of line added
// number of line deleted
// list of function calls seen in the diffs and their number of calls
public static DiffResult computeDiff() {
DiffResult diff = new DiffResult();
String PATH = "./diffs/";
File folder = new File(PATH);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length ; i++) { // for each diff in the folder "diffs"
String filename = PATH + listOfFiles[i].getName();
diff.parseFile(filename);
}
return diff;
}
//computeAST go through the AST and returns
//a astResult struct that contains all the variable declarations
public static AstResult computeAST() {
AstResult AST = new AstResult();
BufferedReader br = null;
StringBuffer bs = new StringBuffer();
String filename = "ast/astChallenge.json";
try {
br = new BufferedReader(new FileReader(filename));
String line;
while((line = br.readLine()) != null) { // read all the lines of the file
bs.append(line);
}
JSONObject jo = new JSONObject(bs.toString()); // create a JSONObject from the text in the file
AST.propagate(jo.getJSONObject("Root")); // travels through the JSONObject through the Children (see function below)
}
catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
catch(JSONException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
finally {
try {
if (br != null) {
br.close();
}
}
catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
return AST;
}
}