1 package de.example.plugins.custom.java;
3 import java.io.IOException;
7 import javax.annotation.Nullable;
9 import org.sonar.api.rule.RuleStatus;
10 import org.sonar.api.server.debt.DebtRemediationFunction;
11 import org.sonar.api.server.rule.RulesDefinition;
12 import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
13 import org.sonar.api.utils.AnnotationUtils;
14 import org.sonar.plugins.java.Java;
15 import org.sonar.squidbridge.annotations.RuleTemplate;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Charsets;
19 import com.google.common.base.Strings;
20 import com.google.common.collect.Iterables;
21 import com.google.common.io.Resources;
22 import com.google.gson.Gson;
24 import de.example.custom.java.checks.CheckList;
27 * Definition of rules.
29 public class CustomRulesDefinition implements RulesDefinition {
30 private static final String RESOURCE_BASE_PATH = "/de/example/l10n/java/rules/custom";
32 private final Gson gson = new Gson();
35 public void define(Context context) {
36 NewRepository repository = context
37 .createRepository(CheckList.REPOSITORY_KEY, Java.KEY)
38 .setName(CheckList.REPOSITORY_NAME);
39 List<Class> checks = CheckList.getChecks();
40 new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class));
41 for (Class ruleClass : checks) {
42 newRule(ruleClass, repository);
48 protected void newRule(Class<?> ruleClass, NewRepository repository) {
50 org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
51 if (ruleAnnotation == null) {
52 throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
54 String ruleKey = ruleAnnotation.key();
55 if (Strings.isNullOrEmpty(ruleKey)) {
56 throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
58 NewRule rule = repository.rule(ruleKey);
60 throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
63 // Check whether it is a Rule Template.
64 rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
65 ruleMetadata(ruleClass, rule);
68 private void ruleMetadata(Class<?> ruleClass, NewRule rule) {
69 String metadataKey = rule.key();
70 addHtmlDescription(rule, metadataKey);
71 addMetadata(rule, metadataKey);
75 private void addMetadata(NewRule rule, String metadataKey) {
76 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json");
77 if (resource != null) {
78 RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class);
79 rule.setSeverity(metatada.defaultSeverity.toUpperCase());
80 rule.setName(metatada.title);
81 rule.addTags(metatada.tags);
82 rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase()));
83 if(metatada.remediation != null) {
84 rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions()));
85 rule.setGapDescription(metatada.remediation.linearDesc);
90 private void addHtmlDescription(NewRule rule, String metadataKey) {
91 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html");
92 if (resource != null) {
93 rule.setHtmlDescription(readResource(resource));
97 private String readResource(URL resource) {
99 return Resources.toString(resource, Charsets.UTF_8);
100 } catch (IOException e) {
101 throw new IllegalStateException("Failed to read: " + resource, e);
105 private static class RuleMetatada {
109 Remediation remediation;
112 String defaultSeverity;
115 private static class Remediation {
122 public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
123 if(func.startsWith("Constant")) {
124 return drf.constantPerIssue(constantCost.replace("mn", "min"));
126 if("Linear".equals(func)) {
127 return drf.linear(linearFactor.replace("mn", "min"));
129 return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));