Package org.sonar.api.server.rule
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 (seeRulesDefinitionXmlLoader
), 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
Nested Classes Modifier and Type Interface Description static class
RulesDefinition.AbstractContext
Instantiated by core but not by plugins, except for their tests.static class
RulesDefinition.Context
This implementation will be removed as soon as analyzers stop instantiating it.static interface
RulesDefinition.DebtRemediationFunctions
Factory ofDebtRemediationFunction
.static interface
RulesDefinition.ExtendedRepository
static interface
RulesDefinition.NewExtendedRepository
static class
RulesDefinition.NewParam
static interface
RulesDefinition.NewRepository
static class
RulesDefinition.NewRule
static class
RulesDefinition.OwaspTop10
static interface
RulesDefinition.Param
static interface
RulesDefinition.Repository
static class
RulesDefinition.Rule
static class
RulesDefinition.SubCharacteristics
Deprecated.in 5.5.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description 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.
-
-