- SonarQube Community Build v25.10.0.114319
- Java Code
- Rule java:S6816 Nullable injected fields and parameters should provide a default value
S6816 reports an issue for the following code
package org.example;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class S6816SpringValueAnnotationWithoutDefault {
@Bean
public String bean(@Value("${x}") @NonNull String x) {
return x;
}
}
It reports an issue that I should provide a default in the expression for the @Value annotation because the String parameter would be nullable. But it is annotated as @NonNull. A default value is not needed here. The application startup is expected to fail at runtime if the property x is not set.
I’ve attached the above source as minimal maven project.
test.zip (2.0 KB)
Cheers
Andreas