/
JParse.java
100 lines (96 loc) · 4 KB
/
JParse.java
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.IOException;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import java.nio.file.Path;
import org.parsers.java.JavaParser;
import org.parsers.java.Node;
/**
* A test harness for parsing Java files from the command line.
*/
public class JParse {
static List<Node> roots = new ArrayList<>();
static private List<Path> paths = new ArrayList<Path>(),
failures = new ArrayList<Path>(),
successes = new ArrayList<Path>();
static private boolean tolerantParsing, parallelParsing, retainInMemory;
static private FileSystem fileSystem = FileSystems.getDefault();
static public void main(String args[]) throws IOException {
for (String arg : args) {
if (arg.equals("-t")) {
tolerantParsing = true;
continue;
}
if (arg.equals("-p")) {
parallelParsing = true;
roots = Collections.synchronizedList(roots);
failures = Collections.synchronizedList(failures);
successes = Collections.synchronizedList(successes);
continue;
}
if (arg.equals("-r")) {
retainInMemory = true;
continue;
}
Path path = fileSystem.getPath(arg);
if (!Files.exists(path)) {
System.err.println("File " + path + " does not exist.");
continue;
}
Files.walk(path).forEach(p->{
if (!Files.isDirectory(p) && p.toString().endsWith(".java")) {
paths.add(p);
}
});
}
if (paths.isEmpty()) usage();
long startTime = System.currentTimeMillis();
Stream<Path> stream = parallelParsing
? paths.parallelStream()
: paths.stream();
stream.forEach(path -> parseFile(path));
for (Path path : failures) {
System.out.println("Parse failed on: " + path);
}
System.out.println("\nParsed " + successes.size() + " files successfully");
System.out.println("Failed on " + failures.size() + " files.");
if (!failures.isEmpty()) System.exit(-1);
System.out.println("\nDuration: " + (System.currentTimeMillis() - startTime) + " milliseconds");
}
static public void parseFile(Path path) {
try {
JavaParser parser = new JavaParser(path);
parser.setParserTolerant(tolerantParsing);
//Node root = parser.CompilationUnit();
Node root = parser.Root();
if (retainInMemory) roots.add(root);
if (paths.size()==1) {
root.dump("");
}
System.out.println(path.getFileName().toString() + " parsed successfully.");
successes.add(path);
if (successes.size() % 1000 == 0) {
System.out.println("-----------------------------------------------");
System.out.println("Successfully parsed " + successes.size() + " files.");
System.out.println("-----------------------------------------------");
}
}
catch (Exception e) {
System.err.println("Error processing file: " + path);
failures.add(path);
e.printStackTrace();
}
}
static public void usage() {
System.out.println("Usage: java JParse <sourcefiles or directories>");
System.out.println("If you just pass it one java source file, it dumps the AST");
System.out.println("Use the -p flag to set whether to parse in multiple threads");
System.out.println("Use the -r flag to retain all the parsed AST's in memory");
System.out.println("Use the -t flag to set whether to parse in tolerant mode");
System.exit(-1);
}
}