Interface RulesDefinition


  • @ServerSide
    @ComputeEngineSide
    @SonarLintSide
    @ExtensionPoint
    public interface RulesDefinition
    Defines some coding rules of the same repository. For example the Java Findbugs plugin provides an implementation of this extension point in order to define the rules that it supports.
    This interface replaces the deprecated class org.sonar.api.rules.RuleRepository.

    How to use

     public class MyJsRulesDefinition implements RulesDefinition {
       @Override
       public void define(Context context) {
         NewRepository repository = context.createRepository("my_js", "js").setName("My Javascript Analyzer");
         // define a rule programmatically. Note that rules
         // could be loaded from files (JSON, XML, ...)
         NewRule x1Rule = repository.createRule("x1")
          .setName("No empty line")
          .setHtmlDescription("Generate an issue on empty lines")
          // optional tags
          .setTags("style", "stupid")
         // optional status. Default value is READY.
         .setStatus(RuleStatus.BETA)
         // default severity when the rule is activated on a Quality profile. Default value is MAJOR.
         .setSeverity(Severity.MINOR);
         // optional type for SonarQube Quality Model. Default is RulesDefinition.Type.CODE_SMELL.
         .setType(RulesDefinition.Type.BUG)
         x1Rule
           .setDebtRemediationFunction(x1Rule.debtRemediationFunctions().linearWithOffset("1h", "30min"));
         x1Rule.createParam("acceptWhitespace")
           .setDefaultValue("false")
           .setType(RuleParamType.BOOLEAN)
           .setDescription("Accept whitespaces on the line");
         // don't forget to call done() to finalize the definition
         repository.done();
       }
     }
     

    If rules are declared in a XML file with the standard SonarQube format (see RulesDefinitionXmlLoader), then it can be loaded by using :
     public class MyJsRulesDefinition implements RulesDefinition {
       private final RulesDefinitionXmlLoader xmlLoader;
       public MyJsRulesDefinition(RulesDefinitionXmlLoader xmlLoader) {
         this.xmlLoader = xmlLoader;
       }
       @Override
       public void define(Context context) {
         NewRepository repository = context.createRepository("my_js", "js").setName("My Javascript Analyzer");
         // see javadoc of RulesDefinitionXmlLoader for the format
         xmlLoader.load(repository, getClass().getResourceAsStream("/path/to/rules.xml"));
         repository.done();
       }
     }
     

    In the above example, XML file must contain name and description of each rule. If it's not the case, then the (deprecated) English bundles can be used :
     public class MyJsRulesDefinition implements RulesDefinition {
       private final RulesDefinitionXmlLoader xmlLoader;
       private final RulesDefinitionI18nLoader i18nLoader;
       public MyJsRulesDefinition(RulesDefinitionXmlLoader xmlLoader, RulesDefinitionI18nLoader i18nLoader) {
         this.xmlLoader = xmlLoader;
         this.i18nLoader = i18nLoader;
       }
       @Override
       public void define(Context context) {
         NewRepository repository = context.createRepository("my_js", "js").setName("My Javascript Analyzer");
         xmlLoader.load(repository, getClass().getResourceAsStream("/path/to/rules.xml"), "UTF-8");
         i18nLoader.load(repository);
         repository.done();
       }
     }
     
    Since:
    4.3