作者:小青年 | 来源:互联网 | 2024-12-01 14:49
本文介绍了在SonarQube插件开发中如何利用`org.sonar.api.batch.rule.internal.NewRule.addParam()`方法为规则添加参数,并提供了多个实际应用的代码示例。
本文详细探讨了在Java编程语言中,特别是在SonarQube插件开发场景下,如何有效使用org.sonar.api.batch.rule.internal.NewRule.addParam()
方法来为自定义规则添加参数。该方法允许开发者向规则中添加必要的配置项,以增强规则的灵活性和适用性。以下是一些从不同源码库中提取的实际代码示例,旨在提供具体的实现参考。
方法概述
此方法属于NewRule
类,位于org.sonar.api.batch.rule.internal
包下,主要用于向规则中添加参数。每个参数通常包含一个名称和可选的描述信息。如果尝试添加已存在的参数,则会抛出IllegalStateException
异常。
代码示例
以下代码示例展示了如何在一个单元测试中正确使用addParam()
方法:
@Test
public void testAddParams() {
RulesBuilder builder = new RulesBuilder();
NewRule newRule = builder.add(RuleKey.of("squirrel", "SQ0001"));
newRule.addParam("min").setDescription("Minimum value");
newRule.addParam("max").setDescription("Maximum value");
try {
newRule.addParam("min");
} catch (IllegalStateException e) {
assertEquals("Parameter 'min' already exists on rule 'squirrel:SQ0001'", e.getMessage());
}
}
另一个例子展示了如何在创建新的规则时设置参数:
newSquid1.setSeverity(Severity.CRITICAL);
newSquid1.setStatus(RuleStatus.BETA);
newSquid1.addParam("min").setDescription("Minimum threshold");
newSquid1.addParam("max").setDescription("Maximum threshold");
此外,还有一个更复杂的示例,展示了如何从数据库加载规则并为其添加参数:
private Rules loadRules(RuleDao ruleDao, DefaultDebtModel debtModel, Durations durations) {
RulesBuilder rulesBuilder = new RulesBuilder();
List ruleParams = ruleDao.selectParameters();
ListMultimap paramsByRuleId = ArrayListMultimap.create();
for (RuleParamDto dto : ruleParams) {
paramsByRuleId.put(dto.getRuleId(), dto);
}
for (RuleDto ruleDto : ruleDao.selectEnabledAndNonManual()) {
RuleKey ruleKey = RuleKey.of(ruleDto.getRepositoryKey(), ruleDto.getRuleKey());
NewRule newRule = rulesBuilder.add(ruleKey)
.setId(ruleDto.getId())
.setName(ruleDto.getName())
.setSeverity(ruleDto.getSeverityString())
.setDescription(ruleDto.getDescription())
.setStatus(ruleDto.getStatus())
.setInternalKey(ruleDto.getConfigKey());
if (hasCharacteristic(ruleDto)) {
newRule.setDebtSubCharacteristic(effectiveCharacteristic(ruleDto, ruleKey, debtModel).key());
newRule.setDebtRemediationFunction(effectiveFunction(ruleDto, ruleKey, durations));
}
for (RuleParamDto ruleParamDto : paramsByRuleId.get(ruleDto.getId())) {
newRule.addParam(ruleParamDto.getName())
.setDescription(ruleParamDto.getDescription());
}
}
return rulesBuilder.build();
}
最后,这里有一个来自SonarLint Core的例子,说明了如何在加载规则定义时动态地为规则添加参数:
private static Rules createRules(StandaloneRuleDefinitionsLoader loader) {
RulesBuilder builder = new RulesBuilder();
for (RulesDefinition.Repository repoDef : loader.getContext().repositories()) {
for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
if (ruleDef.type() == RuleType.SECURITY_HOTSPOT) {
continue;
}
NewRule newRule = builder.add(RuleKey.of(repoDef.key(), ruleDef.key()))
.setInternalKey(ruleDef.internalKey())
.setDescription(ruleDef.htmlDescription() != null ? ruleDef.htmlDescription() : Markdown.convertToHtml(ruleDef.markdownDescription()))
.setSeverity(ruleDef.severity())
.setType(ruleDef.type() != null ? ruleDef.type().toString() : null)
.setName(ruleDef.name());
for (Param p : ruleDef.params()) {
newRule.addParam(p.key())
.setDescription(p.description());
}
}
}
return builder.build();
}