diff options
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 33 insertions, 6 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java index 5239c783363..b819281357f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java @@ -19,7 +19,9 @@ */ package org.sonar.api.batch.sensor; +import java.util.function.Predicate; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.config.Configuration; /** * Describe what a {@link Sensor} is doing. Information may be used by the platform @@ -70,13 +72,17 @@ public interface SensorDescriptor { /** * Property this {@link Sensor} depends on. Used by the platform to skip execution of the {@link Sensor} when * property is not set. + * @deprecated since 6.5 use {@link #onlyWhenConfiguration(Predicate)} */ + @Deprecated SensorDescriptor requireProperty(String... propertyKey); /** * List properties this {@link Sensor} depends on. Used by the platform to skip execution of the {@link Sensor} when * property is not set. + * @deprecated since 6.5 use {@link #onlyWhenConfiguration(Predicate)} */ + @Deprecated SensorDescriptor requireProperties(String... propertyKeys); /** @@ -84,4 +90,10 @@ public interface SensorDescriptor { * @since 6.4 */ SensorDescriptor global(); + + /** + * Predicate that will be evaluated on current module/project {@link Configuration} by the platform to decide if execution of the {@link Sensor} should be skipped. + * @since 6.5 + */ + SensorDescriptor onlyWhenConfiguration(Predicate<Configuration> predicate); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java index 59cb196c2c0..78f024e6851 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java @@ -21,9 +21,13 @@ package org.sonar.api.batch.sensor.internal; import java.util.Arrays; import java.util.Collection; +import java.util.function.Predicate; import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.api.config.Configuration; + +import static java.util.Arrays.asList; public class DefaultSensorDescriptor implements SensorDescriptor { @@ -31,8 +35,8 @@ public class DefaultSensorDescriptor implements SensorDescriptor { private String[] languages = new String[0]; private InputFile.Type type = null; private String[] ruleRepositories = new String[0]; - private String[] properties = new String[0]; private boolean global = false; + private Predicate<Configuration> configurationPredicate; public String name() { return name; @@ -51,8 +55,8 @@ public class DefaultSensorDescriptor implements SensorDescriptor { return Arrays.asList(ruleRepositories); } - public Collection<String> properties() { - return Arrays.asList(properties); + public Predicate<Configuration> configurationPredicate() { + return configurationPredicate; } public boolean isGlobal() { @@ -100,7 +104,7 @@ public class DefaultSensorDescriptor implements SensorDescriptor { @Override public DefaultSensorDescriptor requireProperties(String... propertyKeys) { - this.properties = propertyKeys; + this.configurationPredicate = config -> asList(propertyKeys).stream().allMatch(config::hasKey); return this; } @@ -110,4 +114,10 @@ public class DefaultSensorDescriptor implements SensorDescriptor { return this; } + @Override + public SensorDescriptor onlyWhenConfiguration(Predicate<Configuration> configurationPredicate) { + this.configurationPredicate = configurationPredicate; + return this; + } + } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java index f058c90a940..7494d3660b0 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptorTest.java @@ -21,6 +21,7 @@ package org.sonar.api.batch.sensor.internal; import org.junit.Test; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.config.internal.MapSettings; import static org.assertj.core.api.Assertions.assertThat; @@ -33,13 +34,17 @@ public class DefaultSensorDescriptorTest { .name("Foo") .onlyOnLanguage("java") .onlyOnFileType(InputFile.Type.MAIN) - .requireProperty("sonar.foo.reportPath") + .requireProperty("sonar.foo.reportPath", "sonar.foo.reportPath2") .createIssuesForRuleRepository("squid-java"); assertThat(descriptor.name()).isEqualTo("Foo"); assertThat(descriptor.languages()).containsOnly("java"); assertThat(descriptor.type()).isEqualTo(InputFile.Type.MAIN); - assertThat(descriptor.properties()).containsOnly("sonar.foo.reportPath"); + MapSettings settings = new MapSettings(); + settings.setProperty("sonar.foo.reportPath", "foo"); + assertThat(descriptor.configurationPredicate().test(settings.asConfig())).isFalse(); + settings.setProperty("sonar.foo.reportPath2", "foo"); + assertThat(descriptor.configurationPredicate().test(settings.asConfig())).isTrue(); assertThat(descriptor.ruleRepositories()).containsOnly("squid-java"); } |