|
| 1 | +package checks.spring; |
| 2 | + |
| 3 | +import javax.annotation.CheckForNull; |
| 4 | +import javax.annotation.Nullable; |
| 5 | +import org.springframework.beans.factory.annotation.Value; |
| 6 | + |
| 7 | +public class NullableInjectedFieldsHaveDefaultValueSample { |
| 8 | + @Nullable |
| 9 | + @Value("${my.property}") // Noncompliant [[sc=3;ec=27;secondary=-1]] {{Provide a default null value for this field.}} |
| 10 | + private String myProperty; |
| 11 | + |
| 12 | + @Value("${my.property}") // Noncompliant [[sc=3;ec=27;secondary=+1]] {{Provide a default null value for this field.}} |
| 13 | + @Nullable |
| 14 | + private String myPropertyWithReversedAnnotations; |
| 15 | + |
| 16 | + @Nullable |
| 17 | + @Value(value = "${my.property}") // Noncompliant [[sc=3;ec=35]] |
| 18 | + private String myOtherProperty; |
| 19 | + |
| 20 | + @Nullable |
| 21 | + @Value("${my.property}") // Noncompliant [[sc=3;ec=27;quickfixes=literalfix]] |
| 22 | + private String myPropertyToFix; |
| 23 | + // fix@literalfix {{Set null as default value}} |
| 24 | + // edit@literalfix [[sc=10;ec=26]] {{"${my.property:#{null}}"}} |
| 25 | + |
| 26 | + @Nullable |
| 27 | + @Value(value = "${my.property}") // Noncompliant [[sc=3;ec=35;quickfixes=argumentfix]] |
| 28 | + private String myPropertyToFixOnNamedArgument; |
| 29 | + // fix@argumentfix {{Set null as default value}} |
| 30 | + // edit@argumentfix [[sc=18;ec=34]] {{"${my.property:#{null}}"}} |
| 31 | + |
| 32 | + public void foo(@Nullable @Value("${my.property}") String argument) { // Noncompliant [[sc=29;ec=53;secondary=+0]] {{Provide a default null value for this parameter.}} |
| 33 | + /* Do something */ |
| 34 | + } |
| 35 | + |
| 36 | + public void bar(@Value(value = "${my.property}") @Nullable String argument) { // Noncompliant [[sc=19;ec=51;secondary=+0]] {{Provide a default null value for this parameter.}} |
| 37 | + /* Do something */ |
| 38 | + } |
| 39 | + |
| 40 | + private static final String NON_COMPLIANT_IN_A_CONSTANT = "${non.compliant.constant}"; |
| 41 | + //fix@fixStaticConstant {{Set null as default value}} |
| 42 | + //edit@fixStaticConstant [[sl=40;el=40;sc=61;ec=88]] {{"${non.compliant.constant:#{null}}"}} |
| 43 | + @Nullable |
| 44 | + @Value(value = NON_COMPLIANT_IN_A_CONSTANT) // Noncompliant [[sc=3;ec=46;secondary=-1;quickfixes=fixStaticConstant,localFixOnStaticConstant]] |
| 45 | + //fix@localFixOnStaticConstant {{Set null as default value locally}} |
| 46 | + //edit@localFixOnStaticConstant [[sc=18;ec=45]] {{"${non.compliant.constant:#{null}}"}} |
| 47 | + private String myNoncompliantIndirectProperty; |
| 48 | + |
| 49 | + private final String finalButNotStatic = "${non.compliant.constant}"; |
| 50 | + //fix@fixConstant {{Set null as default value}} |
| 51 | + //edit@fixConstant [[sl=49;el=49;sc=44;ec=71]] {{"${non.compliant.constant:#{null}}"}} |
| 52 | + @Nullable |
| 53 | + @Value(finalButNotStatic) // Noncompliant [[sc=3;ec=28;secondary=-1;quickfixes=fixConstant,localFixOnConstant]] |
| 54 | + // fix@localFixOnConstant {{Set null as default value locally}} |
| 55 | + //edit@localFixOnConstant [[sc=10;ec=27]] {{"${non.compliant.constant:#{null}}"}} |
| 56 | + private String myOtherNoncompliantIndirectProperty; |
| 57 | + |
| 58 | + @Nullable |
| 59 | + @Value(Constants.NON_COMPLIANT_PROPERTY_EXPRESSION_WITHOUT_NULLABLE_DEFAULT) // Noncompliant [[sc=3;ec=79;quickfixes=fixEternal]] |
| 60 | + // fix@fixEternal {{Set null as default value locally}} |
| 61 | + // edit@fixEternal [[sc=10;ec=78]] {{"${myPropertyWithoutDefault:#{null}}"}} |
| 62 | + private String resolvableButForeignConstant; |
| 63 | + |
| 64 | + private static final String DEFINED_IN_A_CONSTANT = "${my.constant:#{null}}"; |
| 65 | + @Nullable |
| 66 | + @Value(value = DEFINED_IN_A_CONSTANT) // Compliant |
| 67 | + private String myCompliantIndirectProperty; |
| 68 | + |
| 69 | + private String myNonCompliantAttribute; |
| 70 | + |
| 71 | + @Value("${my.property}") // Noncompliant [[sc=3;ec=27;secondary=+1]] |
| 72 | + public void setMyNonCompliantAttribute(@Nullable String myNonCompliantAttribute) { |
| 73 | + this.myNonCompliantAttribute = myNonCompliantAttribute; |
| 74 | + } |
| 75 | + |
| 76 | + @Nullable |
| 77 | + @Value(value = DEFINED_IN_A_CONSTANT) // Compliant |
| 78 | + private String myOtherCompliantIndirectProperty; |
| 79 | + |
| 80 | + @Nullable |
| 81 | + @Value("myProperty") // Compliant for S6816, even if wrong by other rules |
| 82 | + private String myPropertyNotUsingSpEL; |
| 83 | + |
| 84 | + @Nullable |
| 85 | + @Value("${myProperty") // Compliant for S6816, even if wrong by other rules |
| 86 | + private String myPropertyWithBrokenSpEL; |
| 87 | + |
| 88 | + @Nullable |
| 89 | + @Value("${my.property:#{null}}") // Compliant, a default value is provided |
| 90 | + private String myCompliantProperty; |
| 91 | + |
| 92 | + @Value(value = "${my.property}") // Compliant, the field is not nullable |
| 93 | + private String myNonNullProperty; |
| 94 | + |
| 95 | + @Value("#{null}") |
| 96 | + @Nullable |
| 97 | + private String nullAnyway; |
| 98 | + |
| 99 | + |
| 100 | + private String myCompliantAttribute; |
| 101 | + |
| 102 | + @Value("${my.property:#{null}}") // Compliant |
| 103 | + public void setMyCompliantAttribute(@Nullable String myCompliantAttribute) { |
| 104 | + this.myCompliantAttribute = myCompliantAttribute; |
| 105 | + } |
| 106 | + |
| 107 | + @CheckForNull |
| 108 | + private String setNothing(@Nullable String useless) { |
| 109 | + return useless; |
| 110 | + } |
| 111 | + |
| 112 | + @Value("${my.property}") // Compliant not a setter |
| 113 | + private String setNothingWithTwoParameters(@Nullable String a, String b) { |
| 114 | + a = b; |
| 115 | + return a; |
| 116 | + } |
| 117 | +} |
0 commit comments