1 package de.example.plugins.helloworld;
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.check.Cardinality;
16 import org.sonar.plugins.java.Java;
17 import org.sonar.squidbridge.annotations.RuleTemplate;
18 import org.sonar.squidbridge.rules.ExternalDescriptionLoader;
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Charsets;
22 import com.google.common.collect.Iterables;
23 import com.google.common.io.Resources;
24 import com.google.gson.Gson;
26 import de.example.helloworld.checks.CheckList;
29 * Definition of rules.
31 public class HelloWorldRulesDefinition implements RulesDefinition {
32 private static final String RESOURCE_BASE_PATH = "/de/example/l10n/helloworld/rules/gushelloworld";
33 private final Gson gson = new Gson();
36 public void define(Context context) {
37 NewRepository repository = context.createRepository(CheckList.REPOSITORY_KEY, Java.KEY).setName("Gus HelloWorld Definition");
38 List<Class> checks = CheckList.getChecks();
39 new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class));
40 for (Class ruleClass : checks) {
41 newRule(ruleClass, repository);
47 protected void newRule(Class<?> ruleClass, NewRepository repository) {
49 org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
50 if (ruleAnnotation == null) {
51 throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
53 String ruleKey = ruleAnnotation.key();
54 if (StringUtils.isEmpty(ruleKey)) {
55 throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
57 NewRule rule = repository.rule(ruleKey);
59 throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
61 rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
62 if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) {
63 throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass);
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 = ExternalDescriptionLoader.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 = HelloWorldRulesDefinition.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"));