1 package de.example.plugins.custom.javascript;
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.javascript.JavaScriptLanguage;
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.custom.javascript.checks.CheckList;
28 public class CustomRulesDefinition implements RulesDefinition {
29 private static final String RESOURCE_BASE_PATH = "/de/example/l10n/javascript/rules/custom";
31 private final Gson gson = new Gson();
34 public void define(Context context) {
35 NewRepository repository = context
36 .createRepository(CheckList.REPOSITORY_KEY, JavaScriptLanguage.KEY)
37 .setName(CheckList.REPOSITORY_NAME);
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 addHtmlDescription(rule, metadataKey);
71 addMetadata(rule, metadataKey);
75 private void addMetadata(NewRule rule, String metadataKey) {
76 URL resource = ExternalDescriptionLoader.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 static 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 static 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"));