org.sonar.api.server.rule
Interface RulesDefinition

All Superinterfaces:
Extension, ServerComponent, ServerExtension
All Known Implementing Classes:
XooRulesDefinition

public interface RulesDefinition
extends ServerExtension

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);

     x1Rule
       .setDebtSubCharacteristic("INTEGRATION_TESTABILITY")
       .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

Nested Class Summary
static class RulesDefinition.Context
          Instantiated by core but not by plugins
static interface RulesDefinition.DebtRemediationFunctions
          Factory of DebtRemediationFunction.
static interface RulesDefinition.ExtendedRepository
           
static interface RulesDefinition.NewExtendedRepository
           
static class RulesDefinition.NewParam
           
static interface RulesDefinition.NewRepository
           
static class RulesDefinition.NewRepositoryImpl
           
static class RulesDefinition.NewRule
           
static class RulesDefinition.Param
           
static interface RulesDefinition.Repository
           
static class RulesDefinition.RepositoryImpl
           
static class RulesDefinition.Rule
           
static class RulesDefinition.SubCharacteristics
          Default sub-characteristics of technical debt model.
 
Method Summary
 void define(RulesDefinition.Context context)
          This method is executed when server is started.
 

Method Detail

define

void define(RulesDefinition.Context context)
This method is executed when server is started.



Copyright © 2009–2015 SonarSource. All rights reserved.