-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
48 lines (40 loc) · 1.79 KB
/
Parser.java
File metadata and controls
48 lines (40 loc) · 1.79 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
package main.java.parser;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import main.java.config.Configuration;
import main.java.analysis.AnalysisResult;
import main.java.analysis.Analyzer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
public class Parser {
public AnalysisResult parseAndAnalyze(String source, Configuration config) throws IOException {
new JavaParser();
AnalysisResult result = new AnalysisResult();
Files.walk(Paths.get(source))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".java"))
.forEach(path -> {
try {
// Parse the file and get the ParseResult
ParseResult<CompilationUnit> parseResult = JavaParser.parse(new File(path.toString()));
// Extract the CompilationUnit from the ParseResult
Optional<CompilationUnit> cuOptional = parseResult.getResult();
if (cuOptional.isPresent()) {
CompilationUnit cu = cuOptional.get();
// Perform analysis
Analyzer analyzer = new Analyzer(config);
result.merge(analyzer.analyze(cu));
} else {
System.err.println("Could not parse: " + path);
}
} catch (IOException e) {
e.printStackTrace();
}
});
return result;
}
}