Skip to content

Commit ba48ab2

Browse files
SONARJAVA-5866 S6816 should not raise on fields and parameters annotated as NonNull
1 parent 68cca1f commit ba48ab2

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S6816",
33
"hasTruePositives": false,
4-
"falseNegatives": 14,
4+
"falseNegatives": 12,
55
"falsePositives": 0
66
}

java-checks-test-sources/default/src/main/java/checks/spring/NullableInjectedFieldsHaveDefaultValueSample.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.annotation.CheckForNull;
44
import javax.annotation.Nullable;
55
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.lang.NonNull;
67

78
public class NullableInjectedFieldsHaveDefaultValueSample {
89
@Nullable
@@ -39,7 +40,7 @@ public class NullableInjectedFieldsHaveDefaultValueSample {
3940

4041
private static final String NON_COMPLIANT_IN_A_CONSTANT = "${non.compliant.constant}";
4142
//fix@fixStaticConstant {{Set null as default value}}
42-
//edit@fixStaticConstant [[sl=40;el=40;sc=61;ec=88]] {{"${non.compliant.constant:#{null}}"}}
43+
//edit@fixStaticConstant [[sl=-4;el=-4;sc=61;ec=88]] {{"${non.compliant.constant:#{null}}"}}
4344
@Nullable
4445
@Value(value = NON_COMPLIANT_IN_A_CONSTANT) // Noncompliant [[sc=3;ec=46;secondary=-1;quickfixes=fixStaticConstant,localFixOnStaticConstant]]
4546
//fix@localFixOnStaticConstant {{Set null as default value locally}}
@@ -48,7 +49,7 @@ public class NullableInjectedFieldsHaveDefaultValueSample {
4849

4950
private final String finalButNotStatic = "${non.compliant.constant}";
5051
//fix@fixConstant {{Set null as default value}}
51-
//edit@fixConstant [[sl=49;el=49;sc=44;ec=71]] {{"${non.compliant.constant:#{null}}"}}
52+
//edit@fixConstant [[sl=-4;el=-4;sc=44;ec=71]] {{"${non.compliant.constant:#{null}}"}}
5253
@Nullable
5354
@Value(finalButNotStatic) // Noncompliant [[sc=3;ec=28;secondary=-1;quickfixes=fixConstant,localFixOnConstant]]
5455
// fix@localFixOnConstant {{Set null as default value locally}}
@@ -118,4 +119,16 @@ private String setNothingWithTwoParameters(@Nullable String a, String b) {
118119
@org.jspecify.annotations.Nullable
119120
@Value("${my.property_jspecify}") // Noncompliant {{Provide a default null value for this field.}} [[sc=3;ec=27;secondary=-1]]
120121
private String myProperty_jspecify;
122+
123+
@Value("${notNull}")
124+
@NonNull
125+
private String notNull; // Compliant, fields marked as non-null should be ignored
126+
127+
private String nonNullArgument1(@Value("${x}") @org.jspecify.annotations.NonNull String x) { // Compliant, parameters marked as non-null should be ignored
128+
return x;
129+
}
130+
131+
private String nonNullArgument2(@Value("${x}") @NonNull String x) { // Compliant, parameters marked as non-null should be ignored
132+
return x;
133+
}
121134
}

java-checks/src/main/java/org/sonar/java/checks/spring/NullableInjectedFieldsHaveDefaultValueCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static AnnotationTree extractValueAnnotationOnSetter(MethodTree method)
170170
private static Optional<AnnotationTree> getNullableAnnotation(VariableTree field) {
171171
SymbolMetadata.NullabilityData nullabilityData = field.symbol().metadata().nullabilityData(SymbolMetadata.NullabilityTarget.FIELD);
172172
SymbolMetadata.AnnotationInstance instance = nullabilityData.annotation();
173-
if (instance == null) {
173+
if (instance == null || nullabilityData.isNonNull(SymbolMetadata.NullabilityLevel.CLASS, false, false)) {
174174
return Optional.empty();
175175
}
176176
return Optional.ofNullable(field.symbol().metadata().findAnnotationTree(instance));

0 commit comments

Comments
 (0)