diff options
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar')
5 files changed, 414 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DecoratorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DecoratorContext.java index ae7f9dd3069..018d22cc0a0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DecoratorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DecoratorContext.java @@ -25,6 +25,7 @@ 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; @@ -96,4 +97,23 @@ public interface DecoratorContext { Collection<Dependency> getOutgoingDependencies(); + // 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); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java index 28004e4b239..f5b73a25722 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java @@ -26,6 +26,7 @@ import org.sonar.api.measures.Measure; 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; @@ -162,6 +163,32 @@ public interface SensorContext extends org.sonar.api.batch.sensor.SensorContext */ 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 -------------- Dependency saveDependency(Dependency dependency); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java index 6384ef65edc..5032c68afcc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java @@ -25,6 +25,7 @@ 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 org.sonar.graph.DirectedGraphAccessor; import javax.annotation.CheckForNull; @@ -119,6 +120,21 @@ public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Depe 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. */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java index 7bfdea2c835..7690bd56c94 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java @@ -20,7 +20,6 @@ package org.sonar.api.batch.rule; import org.sonar.api.BatchComponent; -import org.sonar.api.batch.RequiresDB; import org.sonar.api.rule.RuleKey; import javax.annotation.CheckForNull; @@ -32,10 +31,7 @@ import java.util.Collection; * be extended by plugins. * * @since 4.2 - * @deprecated since 5.2 no more possible to query any rule on batch side. Use {@link ActiveRules} to get active rules. */ -@Deprecated -@RequiresDB public interface Rules extends BatchComponent { /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java new file mode 100644 index 00000000000..7f19e8214cd --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java @@ -0,0 +1,351 @@ +/* + * 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.codehaus.org/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.codehaus.org/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.codehaus.org/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); + } + +} |