@Test public void wrong_annotation() { context = new RulesDefinition.Context(); repository = context.createRepository(REPOSITORY, Language.KOTLIN.toString()); new RulesDefinitionAnnotationLoader().load(repository, WrongAnnotationUsage.class); assertThatThrownBy( () -> RulesDefinitionUtils.setDefaultValuesForParameters(repository, Collections.singletonList(WrongAnnotationUsage.class), Language.RUBY)) .isInstanceOf(IllegalStateException.class) .hasMessage("Invalid @PropertyDefaultValue on WrongAnnotationUsage for language RUBY"); assertThatThrownBy( () -> RulesDefinitionUtils.setDefaultValuesForParameters(repository, Collections.singletonList(WrongAnnotationUsage.class), Language.KOTLIN)) .isInstanceOf(IllegalStateException.class) .hasMessage("Invalid @PropertyDefaultValue on WrongAnnotationUsage for language KOTLIN"); }
public static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language) { for (Class check : checks) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class); String ruleKey = ruleAnnotation.key(); for (Field field : check.getDeclaredFields()) { RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class); PropertyDefaultValues defaultValues = field.getAnnotation(PropertyDefaultValues.class); if (ruleProperty == null || defaultValues == null) { continue; } String paramKey = ruleProperty.key(); List<PropertyDefaultValue> valueForLanguage = Arrays.stream(defaultValues.value()) .filter(defaultValue -> defaultValue.language() == language) .collect(Collectors.toList()); if (valueForLanguage.size() != 1) { throw new IllegalStateException("Invalid @PropertyDefaultValue on " + check.getSimpleName() + " for language " + language); } valueForLanguage .forEach(defaultValue -> repository.rule(ruleKey).param(paramKey).setDefaultValue(defaultValue.defaultValue())); } } }
RulesDefinitionUtils { public static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language) { for (Class check : checks) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class); String ruleKey = ruleAnnotation.key(); for (Field field : check.getDeclaredFields()) { RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class); PropertyDefaultValues defaultValues = field.getAnnotation(PropertyDefaultValues.class); if (ruleProperty == null || defaultValues == null) { continue; } String paramKey = ruleProperty.key(); List<PropertyDefaultValue> valueForLanguage = Arrays.stream(defaultValues.value()) .filter(defaultValue -> defaultValue.language() == language) .collect(Collectors.toList()); if (valueForLanguage.size() != 1) { throw new IllegalStateException("Invalid @PropertyDefaultValue on " + check.getSimpleName() + " for language " + language); } valueForLanguage .forEach(defaultValue -> repository.rule(ruleKey).param(paramKey).setDefaultValue(defaultValue.defaultValue())); } } } }
RulesDefinitionUtils { public static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language) { for (Class check : checks) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class); String ruleKey = ruleAnnotation.key(); for (Field field : check.getDeclaredFields()) { RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class); PropertyDefaultValues defaultValues = field.getAnnotation(PropertyDefaultValues.class); if (ruleProperty == null || defaultValues == null) { continue; } String paramKey = ruleProperty.key(); List<PropertyDefaultValue> valueForLanguage = Arrays.stream(defaultValues.value()) .filter(defaultValue -> defaultValue.language() == language) .collect(Collectors.toList()); if (valueForLanguage.size() != 1) { throw new IllegalStateException("Invalid @PropertyDefaultValue on " + check.getSimpleName() + " for language " + language); } valueForLanguage .forEach(defaultValue -> repository.rule(ruleKey).param(paramKey).setDefaultValue(defaultValue.defaultValue())); } } } private  RulesDefinitionUtils(); }
RulesDefinitionUtils { public static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language) { for (Class check : checks) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class); String ruleKey = ruleAnnotation.key(); for (Field field : check.getDeclaredFields()) { RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class); PropertyDefaultValues defaultValues = field.getAnnotation(PropertyDefaultValues.class); if (ruleProperty == null || defaultValues == null) { continue; } String paramKey = ruleProperty.key(); List<PropertyDefaultValue> valueForLanguage = Arrays.stream(defaultValues.value()) .filter(defaultValue -> defaultValue.language() == language) .collect(Collectors.toList()); if (valueForLanguage.size() != 1) { throw new IllegalStateException("Invalid @PropertyDefaultValue on " + check.getSimpleName() + " for language " + language); } valueForLanguage .forEach(defaultValue -> repository.rule(ruleKey).param(paramKey).setDefaultValue(defaultValue.defaultValue())); } } } private  RulesDefinitionUtils(); static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language); }
RulesDefinitionUtils { public static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language) { for (Class check : checks) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(check, org.sonar.check.Rule.class); String ruleKey = ruleAnnotation.key(); for (Field field : check.getDeclaredFields()) { RuleProperty ruleProperty = field.getAnnotation(RuleProperty.class); PropertyDefaultValues defaultValues = field.getAnnotation(PropertyDefaultValues.class); if (ruleProperty == null || defaultValues == null) { continue; } String paramKey = ruleProperty.key(); List<PropertyDefaultValue> valueForLanguage = Arrays.stream(defaultValues.value()) .filter(defaultValue -> defaultValue.language() == language) .collect(Collectors.toList()); if (valueForLanguage.size() != 1) { throw new IllegalStateException("Invalid @PropertyDefaultValue on " + check.getSimpleName() + " for language " + language); } valueForLanguage .forEach(defaultValue -> repository.rule(ruleKey).param(paramKey).setDefaultValue(defaultValue.defaultValue())); } } } private  RulesDefinitionUtils(); static void setDefaultValuesForParameters(RulesDefinition.NewRepository repository, List<Class> checks, Language language);  }