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/helloworld";
34 private final Gson gson = new Gson();
37 public void define(Context context) {
38 NewRepository repository = context
39 .createRepository(CheckList.REPOSITORY_KEY, Java.KEY)
40 .setName(CheckList.REPOSITORY_NAME);
41 List<Class> checks = CheckList.getChecks();
42 new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class));
43 for (Class ruleClass : checks) {
44 newRule(ruleClass, repository);
50 protected void newRule(Class<?> ruleClass, NewRepository repository) {
52 org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
53 if (ruleAnnotation == null) {
54 throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
56 String ruleKey = ruleAnnotation.key();
57 if (StringUtils.isEmpty(ruleKey)) {
58 throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
60 NewRule rule = repository.rule(ruleKey);
62 throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
64 rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
65 if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) {
66 throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass);
68 ruleMetadata(ruleClass, rule);
71 private void ruleMetadata(Class<?> ruleClass, NewRule rule) {
72 String metadataKey = rule.key();
73 org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class);
74 if (rspecKeyAnnotation != null) {
75 metadataKey = rspecKeyAnnotation.value();
76 rule.setInternalKey(metadataKey);
78 addHtmlDescription(rule, metadataKey);
79 addMetadata(rule, metadataKey);
83 private void addMetadata(NewRule rule, String metadataKey) {
84 URL resource = ExternalDescriptionLoader.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json");
85 if (resource != null) {
86 RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class);
87 rule.setSeverity(metatada.defaultSeverity.toUpperCase());
88 rule.setName(metatada.title);
89 rule.addTags(metatada.tags);
90 rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase()));
91 if(metatada.remediation != null) {
92 rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions()));
93 rule.setGapDescription(metatada.remediation.linearDesc);
98 private static void addHtmlDescription(NewRule rule, String metadataKey) {
99 URL resource = HelloWorldRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html");
100 if (resource != null) {
101 rule.setHtmlDescription(readResource(resource));
105 private static String readResource(URL resource) {
107 return Resources.toString(resource, Charsets.UTF_8);
108 } catch (IOException e) {
109 throw new IllegalStateException("Failed to read: " + resource, e);
113 private static class RuleMetatada {
117 Remediation remediation;
120 String defaultSeverity;
123 private static class Remediation {
130 public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
131 if(func.startsWith("Constant")) {
132 return drf.constantPerIssue(constantCost.replace("mn", "min"));
134 if("Linear".equals(func)) {
135 return drf.linear(linearFactor.replace("mn", "min"));
137 return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));