Skip to content

Commit

Permalink
refactor: convert constructor args to method parameter and store `Son…
Browse files Browse the repository at this point in the history
…arLintEngine` as a local variable (#757)
  • Loading branch information
MartinWitt authored Mar 23, 2022
1 parent 339abaa commit fc7af13
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/java/sorald/rule/StaticAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public interface StaticAnalyzer {
/**
* Scan files for violations of some rules.
*
* @param projectRoot the root folder of the project.
* @param files The files to analyze.
* @param rule The rules to use.
* @param classpath Classpath that includes any dependencies.
* @return All violations of the rules found in the files.
*/
Collection<RuleViolation> findViolations(
List<File> files, List<Rule> rule, List<String> classpath);
File projectRoot, List<File> files, List<Rule> rule, List<String> classpath);
}
2 changes: 1 addition & 1 deletion src/main/java/sorald/sonar/ProjectScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static Set<RuleViolation> scanProject(

// TODO generalize to not directly use the SonarStaticAnalyzer
var violations =
new SonarStaticAnalyzer(baseDir).findViolations(filesToScan, rules, classpath);
new SonarStaticAnalyzer().findViolations(baseDir, filesToScan, rules, classpath);
return new HashSet<>(violations);
}
}
14 changes: 4 additions & 10 deletions src/main/java/sorald/sonar/SonarStaticAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@
import sorald.rule.StaticAnalyzer;

public class SonarStaticAnalyzer implements StaticAnalyzer {
private final File projectRoot;
private final SonarLintEngine sonarLint;

public SonarStaticAnalyzer(File projectRoot) {
this.projectRoot = projectRoot;
this.sonarLint = SonarLintEngine.getInstance();
}

@Override
public Collection<RuleViolation> findViolations(
List<File> files, List<Rule> rules, List<String> classpath) {
return analyze(files, rules, classpath);
File projectRoot, List<File> files, List<Rule> rules, List<String> classpath) {
return analyze(projectRoot, files, rules, classpath);
}

private Collection<RuleViolation> analyze(
List<File> files, List<Rule> rules, List<String> classpath) {
File projectRoot, List<File> files, List<Rule> rules, List<String> classpath) {

List<JavaInputFile> inputFiles =
files.stream()
Expand All @@ -52,6 +45,7 @@ private Collection<RuleViolation> analyze(
.addInputFiles(inputFiles)
.build();

SonarLintEngine sonarLint = SonarLintEngine.getInstance();
var issueHandler = new IssueHandler();
sonarLint.analyze(config, issueHandler, null, null);
sonarLint.stop();
Expand Down

0 comments on commit fc7af13

Please sign in to comment.