private static void loadParameters(RulesDefinition.NewRule 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()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
@Test public void override_annotation_programmatically() { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository newRepository = context.createRepository("squid", "java"); NewRule newRule = annotationLoader.loadRule(newRepository, RuleWithProperty.class); newRule.setName("Overridden name"); newRule.param("property").setDefaultValue("true"); newRule.param("property").setDescription("Overridden"); newRepository.done(); RulesDefinition.Repository repository = context.repository("squid"); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("foo"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.name()).isEqualTo("Overridden name"); assertThat(rule.htmlDescription()).isEqualTo("Foo Bar"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.params()).hasSize(1); RulesDefinition.Param prop = rule.param("property"); assertThat(prop.key()).isEqualTo("property"); assertThat(prop.description()).isEqualTo("Overridden"); assertThat(prop.defaultValue()).isEqualTo("true"); assertThat(prop.type()).isEqualTo(RuleParamType.STRING); }
@Test public void define_rule_parameters() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); RulesDefinition.NewRule newNpe = newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newNpe.createParam("level").setDefaultValue("LOW").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER); newNpe.createParam("effort"); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.params()).hasSize(2); RulesDefinition.Param level = rule.param("level"); assertThat(level.key()).isEqualTo("level"); assertThat(level.name()).isEqualTo("Level"); assertThat(level.description()).isEqualTo("The level"); assertThat(level.defaultValue()).isEqualTo("LOW"); assertThat(level.type()).isEqualTo(RuleParamType.INTEGER); RulesDefinition.Param effort = rule.param("effort"); assertThat(effort.key()).isEqualTo("effort").isEqualTo(effort.name()); assertThat(effort.description()).isNull(); assertThat(effort.defaultValue()).isNull(); assertThat(effort.type()).isEqualTo(RuleParamType.STRING); // test equals() and hashCode() assertThat(level).isEqualTo(level).isNotEqualTo(effort).isNotEqualTo("level").isNotEqualTo(null); assertThat(level.hashCode()).isEqualTo(level.hashCode()); }
private void setupExternalNames(Collection<NewRule> rules) { ResourceBundle bundle; try { bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, locale); } catch (MissingResourceException e) { return; } for (NewRule rule : rules) { String baseKey = "rule." + repository.key() + "." + rule.key(); String nameKey = baseKey + ".name"; if (bundle.containsKey(nameKey)) { rule.setName(bundle.getString(nameKey)); } for (NewParam param : rule.params()) { String paramDescriptionKey = baseKey + ".param." + param.key(); if (bundle.containsKey(paramDescriptionKey)) { param.setDescription(bundle.getString(paramDescriptionKey)); } } } }
private void setupExternalNames(Collection<NewRule> rules) { URL resource = AnnotationBasedRulesDefinition.class.getResource("/org/sonar/l10n/" + languageKey + ".properties"); if (resource == null) { return; } ResourceBundle bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, Locale.ENGLISH); for (NewRule rule : rules) { String baseKey = "rule." + repository.key() + "." + rule.key(); String nameKey = baseKey + ".name"; if (bundle.containsKey(nameKey)) { rule.setName(bundle.getString(nameKey)); } for (NewParam param : rule.params()) { String paramDescriptionKey = baseKey + ".param." + param.key(); if (bundle.containsKey(paramDescriptionKey)) { param.setDescription(bundle.getString(paramDescriptionKey)); } } } }
/** * Loads descriptions of rules and related rule parameters. Existing descriptions * are overridden if English labels exist in bundles. */ public void load(RulesDefinition.NewRepository repo) { for (RulesDefinition.NewRule rule : repo.rules()) { String name = i18n.getName(repo.key(), rule.key()); if (StringUtils.isNotBlank(name)) { rule.setName(name); } String desc = i18n.getDescription(repo.key(), rule.key()); if (StringUtils.isNotBlank(desc)) { rule.setHtmlDescription(desc); } for (RulesDefinition.NewParam param : rule.params()) { String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key()); if (StringUtils.isNotBlank(paramDesc)) { param.setDescription(paramDesc); } } } } }
private void setupExternalNames(Collection<NewRule> rules) { URL resource = AnnotationBasedRulesDefinition.class.getResource("/org/sonar/l10n/" + languageKey + ".properties"); if (resource == null) { return; } ResourceBundle bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, Locale.ENGLISH); for (NewRule rule : rules) { String baseKey = "rule." + repository.key() + "." + rule.key(); String nameKey = baseKey + ".name"; if (bundle.containsKey(nameKey)) { rule.setName(bundle.getString(nameKey)); } for (NewParam param : rule.params()) { String paramDescriptionKey = baseKey + ".param." + param.key(); if (bundle.containsKey(paramDescriptionKey)) { param.setDescription(bundle.getString(paramDescriptionKey)); } } } }
private void setupExternalNames(Collection<NewRule> rules) { URL resource = AnnotationBasedRulesDefinition.class.getResource("/org/sonar/l10n/" + languageKey + ".properties"); if (resource == null) { return; } ResourceBundle bundle = ResourceBundle.getBundle("org.sonar.l10n." + languageKey, Locale.ENGLISH); for (NewRule rule : rules) { String baseKey = "rule." + repository.key() + "." + rule.key(); String nameKey = baseKey + ".name"; if (bundle.containsKey(nameKey)) { rule.setName(bundle.getString(nameKey)); } for (NewParam param : rule.params()) { String paramDescriptionKey = baseKey + ".param." + param.key(); if (bundle.containsKey(paramDescriptionKey)) { param.setDescription(bundle.getString(paramDescriptionKey)); } } } }
/** * Loads descriptions of rules and related rule parameters. Existing descriptions * are overridden if English labels exist in bundles. */ public void load(RulesDefinition.NewRepository repo) { for (RulesDefinition.NewRule rule : repo.rules()) { String name = i18n.getName(repo.key(), rule.key()); if (StringUtils.isNotBlank(name)) { rule.setName(name); } String desc = i18n.getDescription(repo.key(), rule.key()); if (StringUtils.isNotBlank(desc)) { rule.setHtmlDescription(desc); } for (RulesDefinition.NewParam param : rule.params()) { String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key()); if (StringUtils.isNotBlank(paramDesc)) { param.setDescription(paramDesc); } } } } }
private void loadParameters(RulesDefinition.NewRule rule, Field field) { RuleProperty propertyAnnotation = field.getAnnotation(RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private static void loadParameters(RulesDefinition.NewRule 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()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private void loadParameters(RulesDefinition.NewRule 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()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private static void fillParams(RulesDefinition.NewRule rule, List<ParamStruct> params) { for (ParamStruct param : params) { rule.createParam(param.key) .setDefaultValue(param.defaultValue) .setType(param.type) .setDescription(param.description); } }
hasTag.createParam("tag") .setDefaultValue("xoo") .setDescription("The tag to search for");
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setScope(RuleScope.ALL) .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA) .setGapDescription("squid.S115.effortToFix"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("5d", "10h")); rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default1"); rule1.createParam("param2").setDescription("parameter two").setDefaultValue("default2"); repo.createRule(HOTSPOT_RULE_KEY.rule()) .setName("Hotspot") .setHtmlDescription("Minimal hotspot") .setType(RuleType.SECURITY_HOTSPOT) .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3) .addCwe(1, 123, 863); repo.createRule(RULE_KEY2.rule()) .setName("Two") .setHtmlDescription("Minimal rule"); repo.done(); } }
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()));
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); // almost all the attributes of rule1 are changed NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One v2") .setHtmlDescription("Description of One v2") .setSeverity(INFO) .setInternalKey("config1 v2") // tag2 and tag3 removed, tag4 added .setTags("tag1", "tag4") .setType(RuleType.BUG) .setStatus(READY) .setGapDescription("squid.S115.effortToFix.v2"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("6d", "2h")); rule1.createParam("param1").setDescription("parameter one v2").setDefaultValue("default1 v2"); rule1.createParam("param2").setDescription("parameter two v2").setDefaultValue("default2 v2"); // rule2 is dropped, rule3 is new repo.createRule(RULE_KEY3.rule()) .setName("Three") .setHtmlDescription("Rule Three"); repo.done(); } }
/** * Loads descriptions of rules and related rule parameters. Existing descriptions * are overridden if English labels exist in bundles. */ public void load(RulesDefinition.NewRepository repo) { for (RulesDefinition.NewRule rule : repo.rules()) { String name = i18n.getName(repo.key(), rule.key()); if (StringUtils.isNotBlank(name)) { rule.setName(name); } String desc = i18n.getDescription(repo.key(), rule.key()); if (StringUtils.isNotBlank(desc)) { rule.setHtmlDescription(desc); } for (RulesDefinition.NewParam param : rule.params()) { String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key()); if (StringUtils.isNotBlank(paramDesc)) { param.setDescription(paramDesc); } } } } }
@Test public void define_rule_parameter_with_empty_default_value() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); RulesDefinition.NewRule newNpe = newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newNpe.createParam("level").setDefaultValue("").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.params()).hasSize(1); RulesDefinition.Param level = rule.param("level"); assertThat(level.key()).isEqualTo("level"); assertThat(level.name()).isEqualTo("Level"); assertThat(level.description()).isEqualTo("The level"); // Empty value is converted in null value assertThat(level.defaultValue()).isNull(); assertThat(level.type()).isEqualTo(RuleParamType.INTEGER); }
private static void fillParams(RulesDefinition.NewRule rule, List<ParamStruct> params) { for (ParamStruct param : params) { rule.createParam(param.key) .setDefaultValue(param.defaultValue) .setType(param.type) .setDescription(param.description); } }