@Test(expected=IllegalStateException.class) public void shouldThrowIllegalStateExceptionWhenNoKeyIsFoundInRuleAnnotation() { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME); newRepository.createRule("myCardinality"); newRepository.createRule("correctRule"); CustomRulesDefinition rulesDefinition = new CustomRulesDefinition(); rulesDefinition.newRule(UnregisteredRule.class, newRepository); }
protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); ruleMetadata(ruleClass, rule); }
CustomRulesDefinition implements RulesDefinition { protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); ruleMetadata(ruleClass, rule); } }
CustomRulesDefinition implements RulesDefinition { protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); ruleMetadata(ruleClass, rule); }  }
CustomRulesDefinition implements RulesDefinition { protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); ruleMetadata(ruleClass, rule); }  @Override void define(Context context); }
CustomRulesDefinition implements RulesDefinition { protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); ruleMetadata(ruleClass, rule); }  @Override void define(Context context);  }