Skip to content

Commit

Permalink
feat: use SPI for analyzer loading (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Mar 24, 2022
1 parent fc7af13 commit 19edf5a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>11</maven.compiler.target>
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/sorald/sonar/ProjectScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import sorald.Constants;
import sorald.FileUtils;
import sorald.rule.Rule;
import sorald.rule.RuleViolation;
import sorald.rule.StaticAnalyzer;

/** Helper class that uses Sonar to scan projects for rule violations. */
public class ProjectScanner {
Expand Down Expand Up @@ -61,10 +63,11 @@ public static Set<RuleViolation> scanProject(
e.printStackTrace();
}
}

// TODO generalize to not directly use the SonarStaticAnalyzer
var violations =
new SonarStaticAnalyzer().findViolations(baseDir, filesToScan, rules, classpath);
ServiceLoader<StaticAnalyzer> analyzers = ServiceLoader.load(StaticAnalyzer.class);
Set<RuleViolation> violations = new HashSet<>();
for (StaticAnalyzer analyzer : analyzers) {
violations.addAll(analyzer.findViolations(baseDir, filesToScan, rules, classpath));
}
return new HashSet<>(violations);
}
}
2 changes: 2 additions & 0 deletions src/main/java/sorald/sonar/SonarStaticAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sorald.sonar;

import com.google.auto.service.AutoService;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -14,6 +15,7 @@
import sorald.rule.RuleViolation;
import sorald.rule.StaticAnalyzer;

@AutoService(StaticAnalyzer.class)
public class SonarStaticAnalyzer implements StaticAnalyzer {

@Override
Expand Down

0 comments on commit 19edf5a

Please sign in to comment.