diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-09 17:17:00 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-09 17:51:19 +0100 |
commit | a2fa3e4651b30a3cb9284a34656ed8ecfc13d274 (patch) | |
tree | 3625b03443dccc3d3ae39f83b9d28b4bef6688c8 /sonar-plugin-api/src | |
parent | b49f4a0e2dc098099f2172e24a48768a983f96f5 (diff) | |
download | sonarqube-a2fa3e4651b30a3cb9284a34656ed8ecfc13d274.tar.gz sonarqube-a2fa3e4651b30a3cb9284a34656ed8ecfc13d274.zip |
SONAR-6162 Fix NPE when searching for Check of custom rules
Diffstat (limited to 'sonar-plugin-api/src')
5 files changed, 29 insertions, 8 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 35447556df9..06091f37297 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 @@ -63,4 +63,11 @@ public interface ActiveRule { */ @CheckForNull String internalKey(); + + /** + * Optional rule key of the template rule. + * @since 4.5.3 + */ + @CheckForNull + String templateRuleKey(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java index ab8ff4fbdec..5cdc2aba2b3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java @@ -28,6 +28,7 @@ import org.sonar.api.utils.SonarException; import org.sonar.check.RuleProperty; import javax.annotation.CheckForNull; + import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; @@ -122,10 +123,12 @@ public class Checks<C> { } for (ActiveRule activeRule : activeRules.findByRepository(repository)) { - String engineKey = StringUtils.defaultIfBlank(activeRule.internalKey(), activeRule.ruleKey().rule()); + String engineKey = StringUtils.defaultIfBlank(activeRule.templateRuleKey(), activeRule.ruleKey().rule()); Object checkClassesOrObject = checksByEngineKey.get(engineKey); - Object obj = instantiate(activeRule, checkClassesOrObject); - add(activeRule.ruleKey(), (C) obj); + if (checkClassesOrObject != null) { + Object obj = instantiate(activeRule, checkClassesOrObject); + add(activeRule.ruleKey(), (C) obj); + } } return this; } 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 08d34525d69..bfe7b620828 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 @@ -31,13 +31,14 @@ import java.util.Map; public class DefaultActiveRule implements ActiveRule { private final RuleKey ruleKey; private final String name; - private final String severity, internalKey, language; + private final String severity, internalKey, language, templateRuleKey; private final Map<String, String> params; DefaultActiveRule(NewActiveRule newActiveRule) { this.severity = newActiveRule.severity; this.name = newActiveRule.name; this.internalKey = newActiveRule.internalKey; + this.templateRuleKey = newActiveRule.templateRuleKey; this.ruleKey = newActiveRule.ruleKey; this.params = ImmutableMap.copyOf(newActiveRule.params); this.language = newActiveRule.language; @@ -77,4 +78,9 @@ public class DefaultActiveRule implements ActiveRule { public String internalKey() { return internalKey; } + + @Override + public String templateRuleKey() { + return templateRuleKey; + } } 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 3f20d89bf3f..07e85dc9a76 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 @@ -36,7 +36,7 @@ public class NewActiveRule { String name; String severity = Severity.defaultSeverity(); Map<String, String> params = new HashMap<String, String>(); - String internalKey, language; + String internalKey, language, templateRuleKey; private final ActiveRulesBuilder builder; NewActiveRule(ActiveRulesBuilder builder, RuleKey ruleKey) { @@ -59,6 +59,11 @@ public class NewActiveRule { return this; } + public NewActiveRule setTemplateRuleKey(@Nullable String templateRuleKey) { + this.templateRuleKey = templateRuleKey; + return this; + } + public NewActiveRule setLanguage(@Nullable String language) { this.language = language; return this; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java index 42f8d7f6654..d802900f71b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java @@ -114,9 +114,9 @@ public class CheckFactoryTest { } @Test - public void use_engine_key() { - RuleKey ruleKey = RuleKey.of("squid", "One"); - builder.create(ruleKey).setInternalKey("S0001").activate(); + public void use_template_rule_key() { + RuleKey ruleKey = RuleKey.of("squid", "S0001_123"); + builder.create(ruleKey).setTemplateRuleKey("S0001").activate(); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class); |