private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException { RuleParam param = rule.createParameter(); String keyAttribute = ruleC.getAttrValue("key"); if (StringUtils.isNotBlank(keyAttribute)) { /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */ param.setKey(StringUtils.trim(keyAttribute)); } String typeAttribute = ruleC.getAttrValue("type"); if (StringUtils.isNotBlank(typeAttribute)) { /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */ param.setType(type(StringUtils.trim(typeAttribute))); } SMInputCursor paramC = ruleC.childElementCursor(); while (paramC.getNext() != null) { String propNodeName = paramC.getLocalName(); String propText = StringUtils.trim(paramC.collectDescendantText(false)); if (StringUtils.equalsIgnoreCase("key", propNodeName)) { param.setKey(propText); } else if (StringUtils.equalsIgnoreCase("description", propNodeName)) { param.setDescription(propText); } else if (StringUtils.equalsIgnoreCase("type", propNodeName)) { param.setType(type(propText)); } else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) { param.setDefaultValue(propText); } } if (StringUtils.isEmpty(param.getKey())) { throw new SonarException("Node <key> is missing in <param>"); } }
@Test public void rule_with_property() { List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("foo"); assertThat(rule.getName()).isEqualTo("bar"); assertThat(rule.getDescription()).isEqualTo("Foo Bar"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER); assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY); assertThat(rule.getParams()).hasSize(1); RuleParam prop = rule.getParam("property"); assertThat(prop.getKey()).isEqualTo("property"); assertThat(prop.getDescription()).isEqualTo("Ignore ?"); assertThat(prop.getDefaultValue()).isEqualTo("false"); assertThat(prop.getType()).isEqualTo(PropertyType.STRING.name()); }
@CheckForNull private String paramDescription(String repositoryKey, String ruleKey, RuleParam param) { String desc = StringUtils.defaultIfEmpty( i18n.getParamDescription(repositoryKey, ruleKey, param.getKey()), param.getDescription()); return StringUtils.defaultIfBlank(desc, null); }
public String getKey() { return ruleParam.getKey(); }
private static void addRuleProperty(Rule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RuleParam param = rule.createParameter(fieldKey); param.setDescription(propertyAnnotation.description()); param.setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name()); } catch (IllegalArgumentException e) { throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType()).name()); } } }
newRule.setTags(rule.getTags()); for (RuleParam param : rule.getParams()) { RulesDefinition.NewParam newParam = newRule.createParam(param.getKey()); newParam.setDefaultValue(param.getDefaultValue()); newParam.setDescription(paramDescription(repository.getKey(), rule.getKey(), param)); newParam.setType(RuleParamType.parse(param.getType()));
apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()) .setDefaultValue(param.getDefaultValue()));
@Test public void rule_with_integer_property() { List<Rule> rules = parseAnnotatedClass(RuleWithIntegerProperty.class); RuleParam prop = rules.get(0).getParam("property"); assertThat(prop.getDescription()).isEqualTo("Max"); assertThat(prop.getDefaultValue()).isEqualTo("12"); assertThat(prop.getType()).isEqualTo(PropertyType.INTEGER.name()); }
private static Rule toRuleNotNull(RulesDefinition.Rule ruleDef) { Rule rule = Rule.create(ruleDef.repository().key(), ruleDef.key()) .setName(ruleDef.name()) .setSeverity(RulePriority.valueOf(ruleDef.severity())) .setLanguage(ruleDef.repository().language()) .setIsTemplate(ruleDef.template()) .setConfigKey(ruleDef.internalKey()); for (Param param : ruleDef.params()) { rule.createParameter(param.key()).setDefaultValue(param.defaultValue()).setDescription(param.description()); } return rule; }
DefaultCommonRulesRepository enableRule(String ruleKey, Map<String, String> params) { Rule rule = supportedRulesByKey.get(ruleKey); if (rule == null) { throw new IllegalStateException("Unknown rule: " + ruleKey); } for (Map.Entry<String, String> entry : params.entrySet()) { String paramKey = entry.getKey(); RuleParam param = rule.getParam(paramKey); if (param == null) { throw new IllegalStateException(String.format("Rule '%s' has no parameter named '%s'", ruleKey, paramKey)); } param.setDefaultValue(entry.getValue()); } rules.add(rule); return this; }
@Override public RulesProfile createProfile(ValidationMessages validation) { RulesProfile profile1 = RulesProfile.create("Profile 1", "xoo"); profile1.activateRule(Rule.create("repo1", "defaultSeverity"), null); profile1.activateRule(Rule.create("repo1", "overrideSeverity"), RulePriority.CRITICAL); Rule ruleWithParam = Rule.create("repo1", "overrideParam"); ruleWithParam.setParams(Arrays.asList(new RuleParam(ruleWithParam, "param", "", ""))); ActiveRule arWithParam = profile1.activateRule(ruleWithParam, null); arWithParam.setParameter("param", "value"); return profile1; } }
private void appendRule(ActiveRule activeRule) throws IOException { String ruleKey = activeRule.getRuleKey(); // SONARGROOV-40 : key of rule having null parameters have been suffixed with ".fixed" if (ruleKey.endsWith(".fixed")) { ruleKey = ruleKey.substring(0, ruleKey.length() - ".fixed".length()); } writer.append("<rule class=\"").append(ruleKey); if (activeRule.getActiveRuleParams().isEmpty()) { writer.append(AUTO_CLOSING_TAG); } else { writer.append("\">\n"); for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { String value = activeRuleParam.getValue(); String defaultValue = activeRuleParam.getRuleParam().getDefaultValue(); if (StringUtils.isNotBlank(value) && !value.equals(defaultValue)) { writer.append("<property name=\"") .append(activeRuleParam.getKey()) .append("\" value=\"") .append(StringEscapeUtils.escapeXml(value)) .append(AUTO_CLOSING_TAG); } } writer.append("</rule>\n"); } }
/** * @deprecated visibility should be decreased to protected or package */ @Deprecated public ActiveRuleParam(ActiveRule activeRule, RuleParam ruleParam, String value) { this.activeRule = activeRule; this.ruleParam = ruleParam; this.value = value; this.paramKey = ruleParam.getKey(); }
@Override public List<Rule> createRules() { Rule rule = Rule.create("checkstyle", "ConstantName", "Constant Name"); rule.setDescription("Checks that constant names conform to the specified format"); rule.setConfigKey("Checker/TreeWalker/ConstantName"); rule.setSeverity(RulePriority.BLOCKER); rule.setStatus(Rule.STATUS_BETA); rule.setTags(new String[] {"style", "clumsy"}); rule.createParameter("format").setDescription("Regular expression").setDefaultValue("A-Z").setType("REGULAR_EXPRESSION"); return Arrays.asList(rule); } }
newRule.setTags(rule.getTags()); for (RuleParam param : rule.getParams()) { RulesDefinition.NewParam newParam = newRule.createParam(param.getKey()); newParam.setDefaultValue(param.getDefaultValue()); newParam.setDescription(paramDescription(repository.getKey(), rule.getKey(), param)); newParam.setType(RuleParamType.parse(param.getType()));
apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()) .setDefaultValue(param.getDefaultValue()));
@CheckForNull private String paramDescription(String repositoryKey, String ruleKey, RuleParam param) { String desc = StringUtils.defaultIfEmpty( i18n.getParamDescription(repositoryKey, ruleKey, param.getKey()), param.getDescription()); return StringUtils.defaultIfBlank(desc, null); }
@Test public void rule_with_text_property() { List<Rule> rules = parseAnnotatedClass(RuleWithTextProperty.class); RuleParam prop = rules.get(0).getParam("property"); assertThat(prop.getDescription()).isEqualTo("text"); assertThat(prop.getDefaultValue()).isEqualTo("Long text"); assertThat(prop.getType()).isEqualTo(PropertyType.TEXT.name()); }