1 package de.example.plugins.custom.java;
3 import java.io.IOException;
7 import javax.annotation.Nullable;
9 import org.apache.commons.lang.StringUtils;
10 import org.sonar.api.rule.RuleStatus;
11 import org.sonar.api.server.debt.DebtRemediationFunction;
12 import org.sonar.api.server.rule.RulesDefinition;
13 import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
14 import org.sonar.api.utils.AnnotationUtils;
15 import org.sonar.plugins.java.Java;
16 import org.sonar.squidbridge.annotations.RuleTemplate;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Charsets;
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 (StringUtils.isEmpty(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 org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class);
71 if (rspecKeyAnnotation != null) {
72 metadataKey = rspecKeyAnnotation.value();
73 rule.setInternalKey(metadataKey);
75 addHtmlDescription(rule, metadataKey);
76 addMetadata(rule, metadataKey);
80 private void addMetadata(NewRule rule, String metadataKey) {
81 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json");
82 if (resource != null) {
83 RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class);
84 rule.setSeverity(metatada.defaultSeverity.toUpperCase());
85 rule.setName(metatada.title);
86 rule.addTags(metatada.tags);
87 rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase()));
88 if(metatada.remediation != null) {
89 rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions()));
90 rule.setGapDescription(metatada.remediation.linearDesc);
95 private static void addHtmlDescription(NewRule rule, String metadataKey) {
96 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html");
97 if (resource != null) {
98 rule.setHtmlDescription(readResource(resource));
102 private static String readResource(URL resource) {
104 return Resources.toString(resource, Charsets.UTF_8);
105 } catch (IOException e) {
106 throw new IllegalStateException("Failed to read: " + resource, e);
110 private static class RuleMetatada {
114 Remediation remediation;
117 String defaultSeverity;
120 private static class Remediation {
127 public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
128 if(func.startsWith("Constant")) {
129 return drf.constantPerIssue(constantCost.replace("mn", "min"));
131 if("Linear".equals(func)) {
132 return drf.linear(linearFactor.replace("mn", "min"));
134 return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));