diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-07-24 10:31:07 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-07-24 10:31:07 +0200 |
commit | f04357ba06cc0c2b6e694017567186b9e76d4bea (patch) | |
tree | dc69cfa38ea8283cc6f566df96eefa79fa2890dc /sonar-plugin-api | |
parent | c099bc484b891ecd80e4bf10d8a863320015eeba (diff) | |
parent | 4b31edf8e292a4d43c15a9faee9ee6ee5e855d84 (diff) | |
download | sonarqube-f04357ba06cc0c2b6e694017567186b9e76d4bea.tar.gz sonarqube-f04357ba06cc0c2b6e694017567186b9e76d4bea.zip |
Merge branch 'master' into SONAR-4898
Diffstat (limited to 'sonar-plugin-api')
14 files changed, 76 insertions, 41 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRule.java index 3e3d18651e9..35447556df9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRule.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.rule; import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; + import java.util.Map; /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java index 5eeea77687d..08d34525d69 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java @@ -24,16 +24,19 @@ import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.rule.RuleKey; import javax.annotation.concurrent.Immutable; + import java.util.Map; @Immutable -class DefaultActiveRule implements ActiveRule { +public class DefaultActiveRule implements ActiveRule { private final RuleKey ruleKey; + private final String name; private final String severity, internalKey, language; private final Map<String, String> params; DefaultActiveRule(NewActiveRule newActiveRule) { this.severity = newActiveRule.severity; + this.name = newActiveRule.name; this.internalKey = newActiveRule.internalKey; this.ruleKey = newActiveRule.ruleKey; this.params = ImmutableMap.copyOf(newActiveRule.params); @@ -45,6 +48,10 @@ class DefaultActiveRule implements ActiveRule { return ruleKey; } + public String name() { + return name; + } + @Override public String severity() { return severity; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java index dd4b53d5953..3f20d89bf3f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java @@ -33,6 +33,7 @@ import java.util.Map; */ public class NewActiveRule { final RuleKey ruleKey; + String name; String severity = Severity.defaultSeverity(); Map<String, String> params = new HashMap<String, String>(); String internalKey, language; @@ -43,6 +44,11 @@ public class NewActiveRule { this.ruleKey = ruleKey; } + public NewActiveRule setName(String name) { + this.name = name; + return this; + } + public NewActiveRule setSeverity(@Nullable String severity) { this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity()); return this; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 873fed63243..234b3865ca3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -19,16 +19,15 @@ */ package org.sonar.api.batch.sensor; -import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.issue.IssueBuilder; -import org.sonar.api.batch.sensor.measure.Measure; -import org.sonar.api.batch.sensor.measure.MeasureBuilder; - import com.google.common.annotations.Beta; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; import org.sonar.api.batch.rule.ActiveRules; +import org.sonar.api.batch.sensor.issue.Issue; +import org.sonar.api.batch.sensor.issue.IssueBuilder; +import org.sonar.api.batch.sensor.measure.Measure; +import org.sonar.api.batch.sensor.measure.MeasureBuilder; import org.sonar.api.config.Settings; import javax.annotation.CheckForNull; @@ -101,7 +100,11 @@ public interface SensorContext { /** * Add an issue. Use {@link #issueBuilder()} to create the new issue. - * @return true if the new issue is registered, false if the related rule does not exist or is disabled in the Quality profile. + * @return <code>true</code> if the new issue is registered, <code>false</code> if: + * <ul> + * <li>the rule does not exist</li> + * <li>the rule is disabled in the Quality profile</li> + * </ul> */ boolean addIssue(Issue issue); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java index c56e623056c..a8d2512542a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/Issue.java @@ -48,6 +48,7 @@ public interface Issue { /** * Message of the issue. */ + @CheckForNull String message(); /** @@ -65,7 +66,7 @@ public interface Issue { /** * See constants in {@link org.sonar.api.rule.Severity}. - * Can be null before issue is saved to tell to use severity configured in quality profile. + * Can be null before issue is saved. Means to use severity configured in quality profile. */ @CheckForNull String severity(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java index 5480103b1e7..f6b4fa85aaa 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/IssueBuilder.java @@ -56,7 +56,8 @@ public interface IssueBuilder { IssueBuilder onProject(); /** - * Line of the issue. Only available for {@link #onFile(InputFile)} issues. If no line is specified then issue is supposed to be global to the file. + * Line of the issue. Only available for {@link #onFile(InputFile)} issues. + * If no line is specified it means that issue is global to the file. */ IssueBuilder atLine(int line); @@ -77,7 +78,7 @@ public interface IssueBuilder { IssueBuilder severity(@Nullable String severity); /** - * Build the issue. + * Build the issue. After call of this method the builder is cleaned and can be used to build another issue. */ Issue build(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java index 9ca0fdd4f82..f3b56f73b6c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java @@ -70,6 +70,7 @@ public class DefaultIssue implements Issue, Serializable { return ruleKey; } + @CheckForNull @Override public String message() { return message; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java index 62e89813f1c..fefbfb1e4c8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueBuilder.java @@ -23,7 +23,6 @@ import com.google.common.base.Preconditions; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; -import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.issue.IssueBuilder; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; @@ -108,8 +107,21 @@ public class DefaultIssueBuilder implements IssueBuilder { } @Override - public Issue build() { - return new DefaultIssue(this); + public DefaultIssue build() { + DefaultIssue result = new DefaultIssue(this); + reset(); + return result; + } + + private void reset() { + key = null; + onProject = false; + path = null; + ruleKey = null; + message = null; + line = null; + effortToFix = null; + severity = null; } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java index ea606d45bd8..e7805affe65 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/Measure.java @@ -19,13 +19,12 @@ */ package org.sonar.api.batch.sensor.measure; -import org.sonar.api.batch.sensor.Sensor; - import com.google.common.annotations.Beta; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; +import org.sonar.api.batch.sensor.Sensor; -import javax.annotation.Nullable; +import javax.annotation.CheckForNull; import java.io.Serializable; @@ -39,7 +38,7 @@ public interface Measure<G extends Serializable> { /** * The {@link InputFile} this measure belongs to. Returns null if measure is global to the project. */ - @Nullable + @CheckForNull InputFile inputFile(); Metric<G> metric(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/MeasureBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/MeasureBuilder.java index 05b16bee932..8dcfd9e37dd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/MeasureBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/MeasureBuilder.java @@ -19,10 +19,9 @@ */ package org.sonar.api.batch.sensor.measure; -import org.sonar.api.batch.measure.Metric; - import com.google.common.annotations.Beta; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.measure.Metric; import java.io.Serializable; @@ -54,7 +53,7 @@ public interface MeasureBuilder<G extends Serializable> { MeasureBuilder<G> withValue(G value); /** - * Build the measure. + * Build the measure. After call of this method the builder is cleaned and can be used to build another measure. */ Measure<G> build(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java index 6664a055b8a..e51f548d529 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasure.java @@ -19,8 +19,6 @@ */ package org.sonar.api.batch.sensor.measure.internal; -import org.sonar.api.batch.sensor.measure.Measure; - import com.google.common.base.Preconditions; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; @@ -28,8 +26,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; +import org.sonar.api.batch.sensor.measure.Measure; -import javax.annotation.Nullable; +import javax.annotation.CheckForNull; import java.io.Serializable; @@ -48,7 +47,7 @@ public class DefaultMeasure<G extends Serializable> implements Measure<G>, Seria this.value = builder.value; } - @Nullable + @CheckForNull @Override public InputFile inputFile() { return inputFile; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureBuilder.java index a1ef2e508d9..31fddee7652 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureBuilder.java @@ -19,41 +19,37 @@ */ package org.sonar.api.batch.sensor.measure.internal; -import org.sonar.api.batch.sensor.measure.MeasureBuilder; - import com.google.common.base.Preconditions; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.Metric; +import org.sonar.api.batch.sensor.measure.MeasureBuilder; import java.io.Serializable; public class DefaultMeasureBuilder<G extends Serializable> implements MeasureBuilder<G> { - Boolean onProject = null; + boolean onProject = false; InputFile file; Metric<G> metric; G value; @Override public DefaultMeasureBuilder<G> onFile(InputFile inputFile) { - onProject(false); - Preconditions.checkNotNull(inputFile, "inputFile should be non null"); + Preconditions.checkState(!this.onProject, "onProject already called"); + Preconditions.checkState(this.file == null, "onFile already called"); + Preconditions.checkNotNull(inputFile, "InputFile should be non null"); this.file = inputFile; return this; } @Override public DefaultMeasureBuilder<G> onProject() { - onProject(true); - this.file = null; + Preconditions.checkState(!this.onProject, "onProject already called"); + Preconditions.checkState(this.file == null, "onFile already called"); + this.onProject = true; return this; } - private void onProject(boolean isOnProject) { - Preconditions.checkState(this.onProject == null, "onFile or onProject can be called only once"); - this.onProject = isOnProject; - } - @Override public DefaultMeasureBuilder<G> forMetric(Metric<G> metric) { Preconditions.checkState(metric != null, "Metric already defined"); @@ -72,6 +68,15 @@ public class DefaultMeasureBuilder<G extends Serializable> implements MeasureBui @Override public DefaultMeasure<G> build() { - return new DefaultMeasure<G>(this); + DefaultMeasure<G> result = new DefaultMeasure<G>(this); + reset(); + return result; + } + + private void reset() { + onProject = false; + file = null; + metric = null; + value = null; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java index 584b1ea7663..448880b9e4b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java @@ -40,6 +40,7 @@ public class ActiveRulesBuilderTest { public void build_rules() throws Exception { ActiveRules activeRules = new ActiveRulesBuilder() .create(RuleKey.of("squid", "S0001")) + .setName("My Rule") .setSeverity(Severity.CRITICAL) .setInternalKey("__S0001__") .setParam("min", "20") @@ -58,6 +59,7 @@ public class ActiveRulesBuilderTest { ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001")); assertThat(squid1.ruleKey().repository()).isEqualTo("squid"); assertThat(squid1.ruleKey().rule()).isEqualTo("S0001"); + assertThat(((DefaultActiveRule) squid1).name()).isEqualTo("My Rule"); assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL); assertThat(squid1.internalKey()).isEqualTo("__S0001__"); assertThat(squid1.params()).hasSize(1); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java index c75a31679b2..b894ffec092 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java @@ -19,14 +19,13 @@ */ package org.sonar.api.batch.sensor.measure.internal; -import org.sonar.api.batch.sensor.measure.Measure; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.measures.CoreMetrics; + import static org.fest.assertions.Assertions.assertThat; public class DefaultMeasureTest { @@ -63,7 +62,7 @@ public class DefaultMeasureTest { @Test public void not_allowed_to_call_onFile_and_onProject() { thrown.expect(IllegalStateException.class); - thrown.expectMessage("onFile or onProject can be called only once"); + thrown.expectMessage("onProject already called"); new DefaultMeasureBuilder<Integer>() .onProject() .onFile(new DefaultInputFile("src/Foo.php")) |