public Rule answer(InvocationOnMock iom) throws Throwable { Rule rule = Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]); rule.createParameter("format"); rule.createParameter("message"); return rule; } });
@Override public List<Rule> createRules() { Rule rule = Rule.create("checkstyle", "ConstantName"); rule.createParameter("format"); return Arrays.asList(rule); } }
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>"); } }
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()); } } }
@Test public void exportRuleParameters() throws IOException, SAXException { Writer writer = new StringWriter(); RulesProfile profile = RulesProfile.create("sonar way", "java"); Rule rule = Rule.create("checkstyle", "IllegalRegexp", "illegal regexp"); rule.createParameter("format"); rule.createParameter("message"); rule.createParameter("tokens"); ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER); activeRule.setParameter("format", "foo"); activeRule.setParameter("message", "with special characters < > &"); // the tokens parameter is not set new XMLProfileSerializer().write(profile, writer); assertSimilarXml("exportRuleParameters.xml", writer.toString()); }
@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); } }
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 (Strings.isNullOrEmpty(param.getKey())) { throw new SonarException("Node <key> is missing in <param>"); } }
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()); } } }
private 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()); } } }
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()); } } }
private RulesProfile select(QProfile qProfile, ActiveRules activeRules) { RulesProfile deprecatedProfile = new RulesProfile(); // TODO deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.getName()); deprecatedProfile.setLanguage(qProfile.getLanguage()); for (org.sonar.api.batch.rule.ActiveRule activeRule : ((DefaultActiveRules) activeRules).findByLanguage(qProfile.getLanguage())) { Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); rule.setConfigKey(activeRule.internalKey()); ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity())); for (Map.Entry<String, String> param : activeRule.params().entrySet()) { rule.createParameter(param.getKey()); deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); } } return deprecatedProfile; }
private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) { RulesProfile deprecatedProfile = new RulesProfile(); // TODO deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.getName()); deprecatedProfile.setLanguage(qProfile.getLanguage()); for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) { Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); rule.setConfigKey(activeRule.internalKey()); // SONAR-6706 if (activeRule.templateRuleKey() != null) { rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey())); } ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity())); for (Map.Entry<String, String> param : activeRule.params().entrySet()) { rule.createParameter(param.getKey()); deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); } } return deprecatedProfile; }
private static Rule createRepositoryRule(DelphiRule fRule) { RulePriority priority = severityFromLevel(fRule.getPriority()); Rule rule = Rule.create(DelphiPmdConstants.REPOSITORY_KEY, fRule.getName(), fRule.getMessage()).setSeverity( priority); rule.setDescription(fRule.getDescription()); rule.setTags(fRule.getTags()); rule.setConfigKey(fRule.getClazz()); List<RuleParam> ruleParams = new ArrayList<RuleParam>(); if (fRule.getProperties() != null) { for (Property property : fRule.getProperties()) { RuleParam param = rule.createParameter() .setKey(property.getName()) .setDescription(property.getName()) .setType("s"); if (NumberUtils.isNumber(property.getValue())) { param.setType("i"); } param.setDefaultValue(property.getValue()); ruleParams.add(param); } } rule.setParams(ruleParams); return rule; }
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; }
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>"); } }