@Test public void should_recognize_type() { assertThat(AnnotationRuleParser.guessType(Integer.class)).isEqualTo(PropertyType.INTEGER); assertThat(AnnotationRuleParser.guessType(int.class)).isEqualTo(PropertyType.INTEGER); assertThat(AnnotationRuleParser.guessType(Float.class)).isEqualTo(PropertyType.FLOAT); assertThat(AnnotationRuleParser.guessType(float.class)).isEqualTo(PropertyType.FLOAT); assertThat(AnnotationRuleParser.guessType(Boolean.class)).isEqualTo(PropertyType.BOOLEAN); assertThat(AnnotationRuleParser.guessType(boolean.class)).isEqualTo(PropertyType.BOOLEAN); assertThat(AnnotationRuleParser.guessType(String.class)).isEqualTo(PropertyType.STRING); assertThat(AnnotationRuleParser.guessType(Object.class)).isEqualTo(PropertyType.STRING); }
private List<Rule> parseAnnotatedClass(Class annotatedClass) { return new AnnotationRuleParser().parse("repo", Collections.singleton(annotatedClass)); }
public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) { List<Rule> rules = new ArrayList<>(); for (Class annotatedClass : annotatedClasses) { rules.add(create(repositoryKey, annotatedClass)); } return rules; }
@Override public List<Rule> createRules() { return annotationRuleParser.parse(CheckRepository.REPOSITORY_KEY, CheckRepository.getCheckClasses()); } }
private static Rule create(String repositoryKey, Class annotatedClass) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class); if (ruleAnnotation != null) { return toRule(repositoryKey, annotatedClass, ruleAnnotation); } LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class); return null; }
private static Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); Rule rule = Rule.create(repositoryKey, ruleKey, ruleName); rule.setDescription(description); rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); rule.setCardinality(ruleAnnotation.cardinality()); rule.setStatus(ruleAnnotation.status()); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { addRuleProperty(rule, field); } return rule; }
private Rule create(String repositoryKey, Class annotatedClass) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class); if (ruleAnnotation != null) { return toRule(repositoryKey, annotatedClass, ruleAnnotation); } LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class); return null; }
private Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); Rule rule = Rule.create(repositoryKey, ruleKey, ruleName); rule.setDescription(description); rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); rule.setCardinality(ruleAnnotation.cardinality()); rule.setStatus(ruleAnnotation.status()); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { addRuleProperty(rule, field); } return rule; }
@Override public List<Rule> createRules() { return new AnnotationRuleParser().parse(getKey(), getChecks()); }
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 static Rule create(String repositoryKey, Class annotatedClass) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class); if (ruleAnnotation != null) { return toRule(repositoryKey, annotatedClass, ruleAnnotation); } LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class); return null; }
public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) { List<Rule> rules = Lists.newArrayList(); for (Class annotatedClass : annotatedClasses) { rules.add(create(repositoryKey, annotatedClass)); } return rules; }
private static Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) { String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName()); String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null); String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null); Rule rule = Rule.create(repositoryKey, ruleKey, ruleName); rule.setDescription(description); rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); rule.setCardinality(ruleAnnotation.cardinality()); rule.setStatus(ruleAnnotation.status()); rule.setTags(ruleAnnotation.tags()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { addRuleProperty(rule, field); } return rule; }
public DefaultCommonRulesRepository(String language) { super(keyForLanguage(language), language); setName("Common SonarQube"); supportedRulesByKey = new HashMap<String, Rule>(); List<Rule> supportedRules = new AnnotationRuleParser().parse(keyForLanguage(CommonRulesConstants.REPO_KEY_PREFIX), CommonRulesConstants.CLASSES); for (Rule supportedRule : supportedRules) { supportedRulesByKey.put(supportedRule.getKey(), supportedRule); } }
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()); } } }
public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) { List<Rule> rules = new ArrayList<>(); for (Class annotatedClass : annotatedClasses) { rules.add(create(repositoryKey, annotatedClass)); } return rules; }
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()); } } }