Skip to content

Commit

Permalink
Allow loading of Spotbugs properties from external file (#234)
Browse files Browse the repository at this point in the history
Some detector can be configured with these properties, also debugging
can be turned on if needed.

The option name is the same as the one that is used in Spotbugs
itself ('findbugs.loadPropertiesFrom')

Co-authored-by: Gabor Garancsi <[email protected]>
  • Loading branch information
corebonts and Gabor Garancsi authored Mar 7, 2021
1 parent a14d3a2 commit cb77fc5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public enum GeneralOption implements ConfigurationOption {
PMD_SHOW_VIOLATION_DETAILS("pmd.showViolationDetails", "Show violation details and URL", "false"),

FINDBUGS_ENABLED("findbugs.enabled", "FindBugs enabled", "false"),
FINDBUGS_LOAD_PROPERTIES_FROM("findbugs.loadPropertiesFrom", "FindBugs properties file", ""),
FINDBUGS_INCLUDE_FILTER("findbugs.includeFilter", "FindBugs include filter file", ""),
FINDBUGS_EXCLUDE_FILTER("findbugs.excludeFilter", "FindBugs exclude filter file", ""),

SPOTBUGS_ENABLED("spotbugs.enabled", "SpotBugs enabled", "false"),
SPOTBUGS_LOAD_PROPERTIES_FROM("spotbugs.loadPropertiesFrom", "SpotBugs properties file", ""),
SPOTBUGS_INCLUDE_FILTER("spotbugs.includeFilter", "SpotBugs include filter file", ""),
SPOTBUGS_EXCLUDE_FILTER("spotbugs.excludeFilter", "SpotBugs exclude filter file", ""),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package pl.touk.sputnik.processor.spotbugs;

import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.ClassScreener;
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.FindBugs2;
import edu.umd.cs.findbugs.IClassScreener;
import edu.umd.cs.findbugs.Priorities;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.SystemProperties;
import edu.umd.cs.findbugs.config.UserPreferences;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -36,8 +42,8 @@ public SpotBugsProcessor(@NotNull Configuration configuration) {
@Nullable
@Override
public ReviewResult process(@NotNull Review review) {
FindBugs2 spotBugs = createFindBugs2(review);
try {
loadSystemProperties();
try (FindBugs2 spotBugs = createFindBugs2(review)) {
spotBugs.execute();
} catch (Exception e) {
log.error("SpotBugs processing error", e);
Expand All @@ -46,6 +52,17 @@ public ReviewResult process(@NotNull Review review) {
return collectorBugReporter.getReviewResult();
}

private void loadSystemProperties() {
getPropertiesFileLocation().ifPresent(propertiesFileLocation -> {
try {
SystemProperties.loadPropertiesFromURL(Paths.get(propertiesFileLocation).toUri().toURL());
log.info("Using SpotBugs properties file {}", propertiesFileLocation);
} catch (MalformedURLException e) {
log.error("Invalid location for properties file: {}", propertiesFileLocation);
}
});
}

@NotNull
@Override
public String getName() {
Expand Down Expand Up @@ -102,6 +119,13 @@ private IClassScreener createClassScreener(@NotNull Review review) {
return classScreener;
}

private Optional<String> getPropertiesFileLocation() {
return Stream.of(GeneralOption.SPOTBUGS_LOAD_PROPERTIES_FROM, GeneralOption.FINDBUGS_LOAD_PROPERTIES_FROM)
.map(config::getProperty)
.filter(StringUtils::isNotBlank)
.findFirst();
}

@Nullable
private String getIncludeFilterFilename() {
String includeFilterFilename = config.getProperty(GeneralOption.SPOTBUGS_INCLUDE_FILTER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.umd.cs.findbugs.SystemProperties;
import pl.touk.sputnik.TestEnvironment;
import pl.touk.sputnik.configuration.ConfigurationSetup;
import pl.touk.sputnik.configuration.GeneralOption;
Expand All @@ -24,7 +26,10 @@ class SpotBugsProcessorTest extends TestEnvironment {

@BeforeEach
void setUp() {
config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.BUILD_TOOL.getKey(), GRADLE));
config = new ConfigurationSetup().setUp(ImmutableMap.of(
GeneralOption.BUILD_TOOL.getKey(), GRADLE,
GeneralOption.SPOTBUGS_LOAD_PROPERTIES_FROM.getKey(), "src/test/resources/spotbugs/spotbugs-config.properties"
));
spotBugsProcessor = new SpotBugsProcessor(config);
}

Expand Down Expand Up @@ -54,4 +59,10 @@ void shouldReturnEmptyWhenNoFilesToReview() {
assertThat(reviewResult.getViolations()).isEmpty();
}

@Test
void shouldLoadPropertiesFromExternalLocation() {
ReviewResult reviewResult = spotBugsProcessor.process(nonExistentReview());

assertThat(SystemProperties.getBoolean("findbugs.de.comment")).isTrue();
}
}
2 changes: 2 additions & 0 deletions src/test/resources/spotbugs/spotbugs-config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Don't report empty catch blocks if a source comment is found in the block.
findbugs.de.comment = true

0 comments on commit cb77fc5

Please sign in to comment.