aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-07-22 15:09:38 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-07-22 15:21:40 +0200
commitb436be2b5062426442dc76fac074c71062cc8da3 (patch)
tree8b2448853a7ceb07e7992578ecc995f008e79d97 /sonar-plugin-api
parent22a7b6006095b85a67483c5533e913105f560b42 (diff)
downloadsonarqube-b436be2b5062426442dc76fac074c71062cc8da3.tar.gz
sonarqube-b436be2b5062426442dc76fac074c71062cc8da3.zip
SONAR-5389 Allow to find an active rule by internal key
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRules.java11
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java33
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java1
3 files changed, 34 insertions, 11 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRules.java
index 17a885f0677..20a2fb19a4e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRules.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRules.java
@@ -23,6 +23,7 @@ import org.sonar.api.BatchComponent;
import org.sonar.api.rule.RuleKey;
import javax.annotation.CheckForNull;
+
import java.util.Collection;
/**
@@ -56,7 +57,17 @@ public interface ActiveRules extends BatchComponent {
/**
* The active rules for a given language, like <code>java</code>
+ * @deprecated since 4.5 Not sure rules will continue to be linked to a language
*/
+ @Deprecated
Collection<ActiveRule> findByLanguage(String language);
+ /**
+ * Find a {@link ActiveRule} by the associated internal key. <code>null</code>
+ * is returned if the rule does not exist or if the rule is not activated
+ * on any Quality profile associated with the module.
+ */
+ @CheckForNull
+ ActiveRule findByInternalKey(String repository, String internalKey);
+
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java
index 86807ff737f..91b5cafda55 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java
@@ -21,20 +21,23 @@ package org.sonar.api.batch.rule.internal;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
-import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.rule.RuleKey;
import javax.annotation.concurrent.Immutable;
+
import java.util.Collection;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
@Immutable
-class DefaultActiveRules implements ActiveRules {
+public class DefaultActiveRules implements ActiveRules {
// TODO use disk-backed cache (persistit) instead of full in-memory cache ?
private final ListMultimap<String, ActiveRule> activeRulesByRepository;
+ private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndKey = new HashMap<String, Map<String, ActiveRule>>();
+ private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<String, Map<String, ActiveRule>>();
private final ListMultimap<String, ActiveRule> activeRulesByLanguage;
public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
@@ -43,9 +46,18 @@ class DefaultActiveRules implements ActiveRules {
for (NewActiveRule newAR : newActiveRules) {
DefaultActiveRule ar = new DefaultActiveRule(newAR);
repoBuilder.put(ar.ruleKey().repository(), ar);
- if (ar.language()!=null) {
+ if (ar.language() != null) {
langBuilder.put(ar.language(), ar);
}
+ if (!activeRulesByRepositoryAndKey.containsKey(ar.ruleKey().repository())) {
+ activeRulesByRepositoryAndKey.put(ar.ruleKey().repository(), new HashMap<String, ActiveRule>());
+ activeRulesByRepositoryAndInternalKey.put(ar.ruleKey().repository(), new HashMap<String, ActiveRule>());
+ }
+ activeRulesByRepositoryAndKey.get(ar.ruleKey().repository()).put(ar.ruleKey().rule(), ar);
+ String internalKey = ar.internalKey();
+ if (internalKey != null) {
+ activeRulesByRepositoryAndInternalKey.get(ar.ruleKey().repository()).put(internalKey, ar);
+ }
}
activeRulesByRepository = repoBuilder.build();
activeRulesByLanguage = langBuilder.build();
@@ -53,13 +65,7 @@ class DefaultActiveRules implements ActiveRules {
@Override
public ActiveRule find(RuleKey ruleKey) {
- List<ActiveRule> rules = activeRulesByRepository.get(ruleKey.repository());
- for (ActiveRule rule : rules) {
- if (StringUtils.equals(rule.ruleKey().rule(), ruleKey.rule())) {
- return rule;
- }
- }
- return null;
+ return activeRulesByRepositoryAndKey.containsKey(ruleKey.repository()) ? activeRulesByRepositoryAndKey.get(ruleKey.repository()).get(ruleKey.rule()) : null;
}
@Override
@@ -76,4 +82,9 @@ class DefaultActiveRules implements ActiveRules {
public Collection<ActiveRule> findByLanguage(String language) {
return activeRulesByLanguage.get(language);
}
+
+ @Override
+ public ActiveRule findByInternalKey(String repository, String internalKey) {
+ return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : 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 c308024616f..584b1ea7663 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
@@ -52,6 +52,7 @@ public class ActiveRulesBuilderTest {
assertThat(activeRules.findAll()).hasSize(3);
assertThat(activeRules.findByRepository("squid")).hasSize(2);
assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
+ assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
assertThat(activeRules.findByRepository("unknown")).isEmpty();
ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));