import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.sensor.DefaultSensorContext;
import org.sonar.batch.sensor.coverage.CoverageExclusions;
}
}
- @Override
- public void saveViolation(Violation violation, boolean force) {
- if (violation.getResource() == null) {
- violation.setResource(resourceOrProject(violation.getResource()));
- }
- index.addViolation(violation, force);
- }
-
- @Override
- public void saveViolation(Violation violation) {
- saveViolation(violation, false);
- }
-
- @Override
- public void saveViolations(Collection<Violation> violations) {
- if (violations != null) {
- for (Violation violation : violations) {
- saveViolation(violation);
- }
- }
- }
-
@Override
public Dependency saveDependency(Dependency dependency) {
return null;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.InputDir;
-import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.measure.MetricFinder;
-import org.sonar.api.batch.sensor.issue.NewIssueLocation;
-import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.Violation;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.batch.DefaultProjectTree;
import org.sonar.batch.scan.measure.MeasureCache;
return buckets.keySet();
}
- //
- //
- //
- // VIOLATIONS
- //
- //
- //
-
- @Override
- public void addViolation(Violation violation, boolean force) {
- // Reload
- Resource resource = getResource(violation.getResource());
- if (resource == null) {
- LOG.warn("Resource is not indexed. Ignoring violation {}", violation);
- return;
- }
- BatchComponent component = componentCache.get(resource);
-
- Rule rule = violation.getRule();
- if (rule == null) {
- LOG.warn("Rule is null. Ignoring violation {}", violation);
- return;
- }
-
- // keep a limitation (bug?) of deprecated violations api : severity is always
- // set by sonar. The severity set by plugins is overridden.
- // This is not the case with issue api.
-
- DefaultIssue newIssue = new DefaultIssue(sensorStorage);
- NewIssueLocation newLocation = newIssue.newLocation();
- if (component.isProjectOrModule()) {
- newLocation.onProject();
- } else if (component.isDir()) {
- newLocation.onDir((InputDir) component.inputPath());
- } else if (component.isFile()) {
- InputFile inputFile = (InputFile) component.inputPath();
- newLocation.onFile(inputFile);
- if (violation.hasLineId()) {
- newLocation.at(inputFile.selectLine(violation.getLineId()));
- }
- }
- newLocation.message(violation.getMessage());
-
- newIssue.addLocation(newLocation)
- .forRule(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey()))
- .effortToFix(violation.getCost())
- .save();
- }
-
@Override
public String getSource(Resource reference) {
Resource resource = getResource(reference);
*/
package org.sonar.api.batch;
+import java.util.Collection;
+import java.util.List;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Violation;
-
-import java.util.Collection;
-import java.util.List;
/**
* @since 1.10
@Deprecated
Dependency saveDependency(Dependency dependency);
- // RULES
- /**
- * Save a coding rule violation. The decorator which calls this method must be depended upon BatchBarriers.END_OF_VIOLATIONS_GENERATION.
- *
- * @since 2.5
- * @param force allows to force creation of violation even if it was suppressed by {@link org.sonar.api.rules.ViolationFilter}
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- DecoratorContext saveViolation(Violation violation, boolean force);
-
- /**
- * Save a coding rule violation. The decorator which calls this method must be depended upon BatchBarriers.END_OF_VIOLATIONS_GENERATION.
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- DecoratorContext saveViolation(Violation violation);
-
}
*/
package org.sonar.api.batch;
+import java.io.Serializable;
+import java.util.Collection;
+import javax.annotation.CheckForNull;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Violation;
-
-import javax.annotation.CheckForNull;
-
-import java.io.Serializable;
-import java.util.Collection;
/**
* @since 1.10
*/
Measure saveMeasure(Resource resource, Measure measure);
- // ----------- RULE VIOLATIONS --------------
-
- /**
- * Save a coding rule violation.
- *
- * @param force allows to force creation of violation even if it was supressed by {@link org.sonar.api.rules.ViolationFilter}
- * @since 2.5
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- void saveViolation(Violation violation, boolean force);
-
- /**
- * Save a coding rule violation.
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- void saveViolation(Violation violation);
-
- /**
- * Saves a list of violations.
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- void saveViolations(Collection<Violation> violations);
-
// ----------- DEPENDENCIES BETWEEN RESOURCES --------------
/**
*/
package org.sonar.api.batch;
+import java.util.Collection;
+import javax.annotation.CheckForNull;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Violation;
-
-import javax.annotation.CheckForNull;
-
-import java.util.Collection;
/**
* @deprecated since 4.5.2 should not be used by plugins. Everything should be accessed using {@link SensorContext}.
@CheckForNull
public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter);
- /**
- * @since 2.5
- * @deprecated in 3.6
- */
- @Deprecated
- public abstract void addViolation(Violation violation, boolean force);
-
- /**
- * @deprecated in 3.6
- */
- @Deprecated
- public final void addViolation(Violation violation) {
- addViolation(violation, false);
- }
-
/**
* Warning: the resource is automatically indexed for backward-compatibility, but it should be explictly
* indexed before. Next versions will deactivate this automatic indexation.
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.rules;
-
-import org.apache.commons.lang.builder.ReflectionToStringBuilder;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.utils.log.Loggers;
-
-import java.util.Date;
-
-/**
- * A class that represents a violation. A violation happens when a resource does not respect a defined rule.
- * @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.Issue}.
- */
-@Deprecated
-public class Violation {
-
- private Resource resource;
- private Rule rule;
- private String message;
- private RulePriority severity;
- private Integer lineId;
- private Double cost;
- private Date createdAt;
- private boolean switchedOff = false;
- private String checksum;
- private boolean isNew = false;
- private boolean isManual = false;
- private Integer permanentId;
- private Integer personId;
-
- /**
- * Creates of a violation from a rule. Will need to define the resource later on
- *
- * @deprecated since 2.3. Use the factory method {@link #create(ActiveRule, Resource)}
- */
- @Deprecated
- public Violation(Rule rule) {
- this.rule = rule;
- }
-
- /**
- * Creates a fully qualified violation
- *
- * @param rule the rule that has been violated
- * @param resource the resource the violation should be attached to
- * @deprecated since 2.3. Use the factory method create()
- */
- @Deprecated
- public Violation(Rule rule, Resource resource) {
- this.resource = resource;
- this.rule = rule;
- }
-
- public Resource getResource() {
- return resource;
- }
-
- /**
- * Sets the resource the violation applies to
- *
- * @return the current object
- */
- public Violation setResource(Resource resource) {
- this.resource = resource;
- return this;
- }
-
- public Rule getRule() {
- return rule;
- }
-
- /**
- * Sets the rule violated
- *
- * @return the current object
- */
- public Violation setRule(Rule rule) {
- this.rule = rule;
- return this;
- }
-
- public String getMessage() {
- return message;
- }
-
- /**
- * Sets the violation message
- *
- * @return the current object
- */
- public Violation setMessage(String message) {
- this.message = message;
- return this;
- }
-
- /**
- * @return line number (numeration starts from 1), or <code>null</code> if violation doesn't belong to concrete line
- * @see #hasLineId()
- */
- public Integer getLineId() {
- return lineId;
- }
-
- /**
- * Sets the violation line.
- *
- * @param lineId line number (numeration starts from 1), or <code>null</code> if violation doesn't belong to concrete line
- * @return the current object
- */
- public Violation setLineId(Integer lineId) {
- if (lineId != null && lineId < 1) {
- // TODO this normalization was added in 2.8, throw exception in future versions - see http://jira.sonarsource.com/browse/SONAR-2386
- Loggers.get(getClass()).warn("line must not be less than 1 - in future versions this will cause IllegalArgumentException");
- this.lineId = null;
- } else {
- this.lineId = lineId;
- }
- return this;
- }
-
- /**
- * @return <code>true<code> if violation belongs to concrete line
- * @since 2.8
- */
- public boolean hasLineId() {
- return lineId != null;
- }
-
- /**
- * @since 2.5
- */
- public RulePriority getSeverity() {
- return severity;
- }
-
- /**
- * For internal use only.
- *
- * @since 2.5
- */
- public Violation setSeverity(RulePriority severity) {
- this.severity = severity;
- return this;
- }
-
- /**
- * @deprecated since 2.5 use {@link #getSeverity()} instead. See http://jira.sonarsource.com/browse/SONAR-1829
- */
- @Deprecated
- public RulePriority getPriority() {
- return severity;
- }
-
- /**
- * For internal use only
- *
- * @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.sonarsource.com/browse/SONAR-1829
- */
- @Deprecated
- public Violation setPriority(RulePriority priority) {
- this.severity = priority;
- return this;
- }
-
- /**
- * @see #setCost(Double)
- * @since 2.4
- */
- public Double getCost() {
- return cost;
- }
-
- /**
- * The cost to fix a violation can't be precisely computed without this information. Let's take the following example : a rule forbids to
- * have methods whose complexity is greater than 10. Without this field "cost", the same violation is created with a method whose
- * complexity is 15 and a method whose complexity is 100. If the cost to fix one point of complexity is 0.05h, then 15mn is necessary to
- * fix the method whose complexity is 15, and 3h5mn is required to fix the method whose complexity is 100.
- *
- * @since 2.4
- */
- public Violation setCost(Double d) {
- if (d == null || d >= 0) {
- this.cost = d;
- return this;
- } else {
- throw new IllegalArgumentException("Cost to fix violation can't be negative or NaN");
- }
- }
-
- /**
- * @since 2.5
- */
- public Date getCreatedAt() {
- return createdAt;
- }
-
- /**
- * For internal use only
- *
- * @since 2.5
- */
- public Violation setCreatedAt(Date createdAt) {
- this.createdAt = createdAt;
- return this;
- }
-
- /**
- * Switches off the current violation. This is a kind of "mute", which means the violation exists but won't be counted as an active
- * violation (and thus, won't be counted in the total number of violations). It's usually used for false-positives.
- * <p/>
- * The extensions which call this method must be executed
- *
- * @param b if true, the violation is considered OFF
- * @since 2.8
- */
- public Violation setSwitchedOff(boolean b) {
- this.switchedOff = b;
- return this;
- }
-
- /**
- * Tells whether this violation is ON or OFF.
- *
- * @since 2.8
- */
- public boolean isSwitchedOff() {
- return switchedOff;
- }
-
- /**
- * Checksum is available in decorators executed after the barrier {@link org.sonar.api.batch.DecoratorBarriers#END_OF_VIOLATION_TRACKING}
- */
- public String getChecksum() {
- return checksum;
- }
-
- /**
- * For internal use only. Checksum is automatically set by Sonar. Plugins must not call this method.
- */
- public Violation setChecksum(String s) {
- this.checksum = s;
- return this;
- }
-
- /**
- * A violation is considered as "new" if it has been created after the reference analysis
- * (the "previous" analysis).
- * This method must be used only by post-jobs and decorators depending on the barrier
- * {@link org.sonar.api.batch.DecoratorBarriers#END_OF_VIOLATION_TRACKING}
- *
- * @since 2.9
- */
- public boolean isNew() {
- return isNew;
- }
-
- /**
- * For internal use only. MUST NOT BE SET FROM PLUGINS.
- *
- * @since 2.9
- */
- public Violation setNew(boolean b) {
- isNew = b;
- return this;
- }
-
- /**
- * @since 2.13
- */
- public boolean isManual() {
- return isManual;
- }
-
- /**
- * For internal use only. MUST NOT BE SET FROM PLUGINS.
- *
- * @since 2.13
- */
- public Violation setManual(boolean b) {
- isManual = b;
- return this;
- }
-
- /**
- * For internal use only. MUST NOT BE SET FROM PLUGINS.
- *
- * @since 2.13
- */
- public Integer getPermanentId() {
- return permanentId;
- }
-
- /**
- * For internal use only. MUST NOT BE SET FROM PLUGINS.
- *
- * @since 2.13
- */
- public Violation setPermanentId(Integer i) {
- this.permanentId = i;
- return this;
- }
-
- /**
- * @since 2.13
- */
- public Integer getPersonId() {
- return personId;
- }
-
- /**
- * For internal use only.
- *
- * @since 2.13
- */
- public Violation setPersonId(Integer i) {
- this.personId = i;
- return this;
- }
-
- @Override
- public String toString() {
- return ReflectionToStringBuilder.toString(this);
- }
-
- public static Violation create(ActiveRule activeRule, Resource resource) {
- return new Violation(activeRule.getRule()).setResource(resource);
- }
-
- public static Violation create(Rule rule, Resource resource) {
- return new Violation(rule).setResource(resource);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.rules;
-
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-
-import org.junit.Before;
-import org.junit.Test;
-
-public class ViolationTest {
- private Violation violation;
-
- @Before
- public void setUp() {
- violation = Violation.create((Rule) null, null);
- }
-
- /**
- * See http://jira.sonarsource.com/browse/SONAR-2386
- */
- @Test
- public void testLineIdContract() {
- violation.setLineId(null);
- assertThat(violation.hasLineId(), is(false));
- assertThat(violation.getLineId(), nullValue());
-
- violation.setLineId(0);
- assertThat(violation.hasLineId(), is(false));
- assertThat(violation.getLineId(), nullValue());
-
- violation.setLineId(1);
- assertThat(violation.hasLineId(), is(true));
- assertThat(violation.getLineId(), is(1));
- }
-
- @Test
- public void testCostContract() {
- violation.setCost(null);
- assertThat(violation.getCost(), nullValue());
-
- violation.setCost(1.0);
- assertThat(violation.getCost(), is(1.0));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testCostContract_NaN() {
- violation.setCost(Double.NaN);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testCostContract_Negative() {
- violation.setCost(-1.0);
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.test;
-
-import org.mockito.ArgumentMatcher;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.Violation;
-
-public class IsViolation extends ArgumentMatcher<Violation> {
-
- private Rule rule;
- private String message;
- private Resource resource;
- private Integer lineId;
-
- public IsViolation(Violation wanted) {
- this.lineId = wanted.getLineId();
- this.message = wanted.getMessage();
- this.resource = wanted.getResource();
- this.rule = wanted.getRule();
- }
-
- public IsViolation(Rule rule, String message, Resource resource, Integer lineId) {
- this.rule = rule;
- this.message = message;
- this.resource = resource;
- this.lineId = lineId;
- }
-
- @Override
- public boolean matches(Object o) {
- Violation violation = (Violation) o;
- if (lineId != null && !lineId.equals(violation.getLineId())) {
- return false;
- }
-
- if (message != null && !message.equals(violation.getMessage())) {
- return false;
- }
-
- if (resource != null && !resource.equals(violation.getResource())) {
- return false;
- }
-
- if (rule != null && !rule.equals(violation.getRule())) {
- return false;
- }
-
- return true;
- }
-}