@RuleProperty in a custom language

Hi all, I have implemented a custom language with a bunch of custom rules. In one of these rules, I wanted to have a limit configurable. So I added this to my rule:

    private static final String DEFAULT_MAX_ALLOWED_STR = "25";

    @RuleProperty(
            key = "limit",
            description = "maximum number of allowed steps within a scenario",
            defaultValue = DEFAULT_MAX_ALLOWED_STR)
    public String maxAllowedStr = DEFAULT_MAX_ALLOWED_STR;

However, that parameter is not shown in the frontend, and it cannot be configured in a quality profile. What else do I need to do?

I’ve done exactly as you have and the rule parameter shows up when I browse the rule in my SQ instance.

What do you see? What version of SonarQube are you using?

After clicking on the “Change” button:

I don’t see anything of the rule:

The version of my SonarQube server is

  • Community Build
  • v25.7.0.110598
  • MQR Mode

I eventually found out after some inspection of the Sonar code. For some other reasons, I had to set up the repositoy myself and fill the rules. In that, I had to add these methods:


    private void loadField(Field field, NewRule newRule) {
        RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class);
        if (ruleProperty != null) {
            String fieldKey = StringUtils.defaultIfEmpty(ruleProperty.key(), field.getName());
            String defaultValue = ruleProperty.defaultValue();
            NewParam param = newRule.createParam(fieldKey)
                    .setDescription(ruleProperty.description())
                    .setDefaultValue(defaultValue);
            String propertyType = ruleProperty.type();
            RuleParamType ruleParamType = ruleParamType(propertyType);
            param.setType(ruleParamType);
            logger.info("added rule property \"{}\" default-value \"{}\" type {}", fieldKey, defaultValue, ruleParamType);
        }
    }

    private RuleParamType ruleParamType(String source) {
        RuleParamType result;
        if (!StringUtils.isBlank(source)) {
            result = RuleParamType.parse(source);
        } else {
            result = RuleParamType.STRING;
        }
        return result;
    }
1 Like