@@ -72,6 +72,7 @@ public final class AnnotationRuleParser implements ServerComponent { | |||
rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); | |||
rule.setCardinality(ruleAnnotation.cardinality()); | |||
rule.setStatus(ruleAnnotation.status()); | |||
rule.setTags(ruleAnnotation.tags()); | |||
List<Field> fields = FieldUtils2.getFields(clazz, true); | |||
for (Field field : fields) { |
@@ -21,6 +21,7 @@ package org.sonar.api.rules; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import com.google.common.base.Strings; | |||
import com.google.common.collect.Lists; | |||
import com.google.common.collect.Maps; | |||
import com.google.common.io.Closeables; | |||
import org.apache.commons.io.FileUtils; | |||
@@ -126,6 +127,7 @@ public final class XMLRuleParser implements ServerComponent { | |||
rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute))); | |||
} | |||
List<String> tags = Lists.newArrayList(); | |||
SMInputCursor cursor = ruleC.childElementCursor(); | |||
while (cursor.getNext() != null) { | |||
@@ -154,11 +156,15 @@ public final class XMLRuleParser implements ServerComponent { | |||
} else if (StringUtils.equalsIgnoreCase("param", nodeName)) { | |||
processParameter(rule, cursor); | |||
} else if (StringUtils.equalsIgnoreCase("tag", nodeName)) { | |||
tags.add(StringUtils.trim(cursor.collectDescendantText(false))); | |||
} | |||
} | |||
if (Strings.isNullOrEmpty(rule.getKey())) { | |||
throw new SonarException("Node <key> is missing in <rule>"); | |||
} | |||
rule.setTags(tags.toArray(new String[tags.size()])); | |||
} | |||
private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException { |
@@ -36,10 +36,7 @@ import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import javax.persistence.*; | |||
import java.util.ArrayList; | |||
import java.util.Date; | |||
import java.util.List; | |||
import java.util.Set; | |||
import java.util.*; | |||
@Entity | |||
@Table(name = "rules") | |||
@@ -72,6 +69,11 @@ public class Rule { | |||
*/ | |||
private static final Set<String> STATUS_LIST = ImmutableSet.of(STATUS_READY, STATUS_BETA, STATUS_DEPRECATED, STATUS_REMOVED); | |||
/** | |||
* @since 4.2 | |||
*/ | |||
private static final String[] DEFAULT_TAGS = new String[0]; | |||
@Id | |||
@Column(name = "id") | |||
@GeneratedValue | |||
@@ -127,12 +129,13 @@ public class Rule { | |||
@Column(name = "updated_at", updatable = true, nullable = true) | |||
private Date updatedAt; | |||
private transient String[] tags = DEFAULT_TAGS; | |||
/** | |||
* @deprecated since 2.3. Use the factory method {@link #create()} | |||
*/ | |||
@Deprecated | |||
public Rule() { | |||
// TODO reduce visibility to packaete | |||
} | |||
/** | |||
@@ -422,6 +425,20 @@ public class Rule { | |||
return this; | |||
} | |||
/** | |||
* For definition of rule only | |||
*/ | |||
public String[] getTags() { | |||
return tags; | |||
} | |||
/** | |||
* For definition of rule only | |||
*/ | |||
public void setTags(String[] tags) { | |||
this.tags = tags; | |||
} | |||
@Override | |||
public boolean equals(Object obj) { | |||
if (!(obj instanceof Rule)) { |
@@ -67,6 +67,7 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { | |||
newRule.setTemplate(Cardinality.MULTIPLE.equals(rule.getCardinality())); | |||
newRule.setSeverity(rule.getSeverity().toString()); | |||
newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus())); | |||
newRule.setTags(rule.getTags()); | |||
for (RuleParam param : rule.getParams()) { | |||
NewParam newParam = newRule.newParam(param.getKey()); | |||
newParam.setDefaultValue(param.getDefaultValue()); |
@@ -50,6 +50,7 @@ public class DeprecatedRuleDefinitionsTest { | |||
rule.setConfigKey("Checker/TreeWalker/ConstantName"); | |||
rule.setSeverity(RulePriority.BLOCKER); | |||
rule.setStatus(Rule.STATUS_BETA); | |||
rule.setTags(new String[]{"style", "security"}); | |||
rule.createParameter("format").setDescription("Regular expression").setDefaultValue("A-Z").setType("REGULAR_EXPRESSION"); | |||
return Arrays.asList(rule); | |||
} | |||
@@ -77,7 +78,7 @@ public class DeprecatedRuleDefinitionsTest { | |||
assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); | |||
assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/ConstantName"); | |||
assertThat(rule.status()).isEqualTo(RuleStatus.BETA); | |||
assertThat(rule.tags()).isEmpty(); | |||
assertThat(rule.tags()).containsOnly("style", "security"); | |||
assertThat(rule.params()).hasSize(1); | |||
RuleDefinitions.Param param = rule.param("format"); | |||
assertThat(param).isNotNull(); |