Skip to content

Commit a2a01ce

Browse files
committed
mining: pass compiler options to hint guards
1 parent b7194ca commit a2a01ce

1 file changed

Lines changed: 54 additions & 31 deletions

File tree

sandbox_mining_cli/src/main/java/org/sandbox/mining/scanner/SourceScanner.java

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.attribute.BasicFileAttributes;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Map;
2526

2627
import org.eclipse.jdt.core.dom.CompilationUnit;
2728
import org.sandbox.jdt.triggerpattern.api.BatchTransformationProcessor;
@@ -35,6 +36,8 @@
3536
*/
3637
public class SourceScanner {
3738

39+
private static final Map<String, String> NO_COMPILER_OPTIONS = Map.of();
40+
3841
private final StandaloneAstParser parser;
3942
private final int maxFiles;
4043

@@ -81,7 +84,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
8184
if (javaFiles.size() >= maxFiles) {
8285
return FileVisitResult.TERMINATE;
8386
}
84-
if (file.toString().endsWith(".java")) {
87+
if (file.toString().endsWith(".java")) { //$NON-NLS-1$
8588
javaFiles.add(file);
8689
}
8790
return FileVisitResult.CONTINUE;
@@ -98,7 +101,7 @@ public FileVisitResult visitFileFailed(Path file, IOException exc) {
98101
}
99102

100103
/**
101-
* Scans all Java files using the given hint file and produces a mining report.
104+
* Scans all Java files using the given hint files and produces a mining report.
102105
*
103106
* @param repoName name of the repository being scanned
104107
* @param rootDir the root directory of the cloned repository
@@ -109,37 +112,57 @@ public FileVisitResult visitFileFailed(Path file, IOException exc) {
109112
*/
110113
public MiningReport scan(String repoName, Path rootDir, List<String> subPaths, List<HintFile> hintFiles)
111114
throws IOException {
112-
MiningReport report = new MiningReport();
113-
List<Path> javaFiles = findJavaFiles(rootDir, subPaths);
114-
report.addFileCount(repoName, javaFiles.size());
115-
116-
for (HintFile hintFile : hintFiles) {
117-
BatchTransformationProcessor processor = new BatchTransformationProcessor(hintFile);
118-
119-
for (Path javaFile : javaFiles) {
120-
String source = Files.readString(javaFile, StandardCharsets.UTF_8);
121-
CompilationUnit cu = parser.parse(source);
122-
List<TransformationResult> results = processor.process(cu);
123-
124-
for (TransformationResult result : results) {
125-
String relativePath = rootDir.relativize(javaFile).toString();
126-
int line = cu.getLineNumber(result.match().getOffset());
127-
String hintFileName = hintFile.getId() != null ? hintFile.getId() : "unknown";
128-
TransformationRule rule = result.rule();
129-
String ruleName = rule.getDescription();
130-
if (ruleName == null) {
131-
ruleName = rule.getRuleId();
132-
}
133-
if (ruleName == null) {
134-
int ruleIndex = hintFile.getRules().indexOf(rule);
135-
ruleName = ruleIndex >= 0 ? hintFileName + (ruleIndex + 1) : hintFileName;
136-
}
137-
report.addMatch(repoName, hintFileName, ruleName, relativePath, line, result.matchedText(),
138-
result.hasReplacement() ? result.replacement() : null);
115+
return scan(repoName, rootDir, subPaths, hintFiles, NO_COMPILER_OPTIONS);
116+
}
117+
118+
/**
119+
* Scans all Java files using the given hint files and compiler options, producing
120+
* a mining report. Compiler options are forwarded to guard evaluation so rules
121+
* such as {@code sourceVersionGE(11)} behave like they do in Eclipse cleanup
122+
* execution.
123+
*
124+
* @param repoName name of the repository being scanned
125+
* @param rootDir the root directory of the cloned repository
126+
* @param subPaths optional sub-paths to restrict scanning to
127+
* @param hintFiles list of parsed hint files to apply
128+
* @param compilerOptions compiler options for guard evaluation
129+
* @return the mining report with all matches
130+
* @throws IOException if file reading fails
131+
*/
132+
public MiningReport scan(String repoName, Path rootDir, List<String> subPaths, List<HintFile> hintFiles,
133+
Map<String, String> compilerOptions) throws IOException {
134+
MiningReport report = new MiningReport();
135+
Map<String, String> effectiveCompilerOptions = compilerOptions != null ? compilerOptions : NO_COMPILER_OPTIONS;
136+
List<Path> javaFiles = findJavaFiles(rootDir, subPaths);
137+
report.addFileCount(repoName, javaFiles.size());
138+
139+
for (HintFile hintFile : hintFiles) {
140+
BatchTransformationProcessor processor = new BatchTransformationProcessor(hintFile);
141+
142+
for (Path javaFile : javaFiles) {
143+
String source = Files.readString(javaFile, StandardCharsets.UTF_8);
144+
CompilationUnit cu = parser.parse(source);
145+
List<TransformationResult> results = processor.process(cu, effectiveCompilerOptions);
146+
147+
for (TransformationResult result : results) {
148+
String relativePath = rootDir.relativize(javaFile).toString();
149+
int line = cu.getLineNumber(result.match().getOffset());
150+
String hintFileName = hintFile.getId() != null ? hintFile.getId() : "unknown"; //$NON-NLS-1$
151+
TransformationRule rule = result.rule();
152+
String ruleName = rule.getRuleId();
153+
if (ruleName == null || ruleName.isBlank()) {
154+
ruleName = rule.getDescription();
139155
}
156+
if (ruleName == null || ruleName.isBlank()) {
157+
int ruleIndex = hintFile.getRules().indexOf(rule);
158+
ruleName = ruleIndex >= 0 ? hintFileName + (ruleIndex + 1) : hintFileName;
159+
}
160+
report.addMatch(repoName, hintFileName, ruleName, relativePath, line, result.matchedText(),
161+
result.hasReplacement() ? result.replacement() : null);
140162
}
141163
}
164+
}
142165

143-
return report;
166+
return report;
144167
}
145-
}
168+
}

0 commit comments

Comments
 (0)