diff --git a/rules/S2384/java/rule.adoc b/rules/S2384/java/rule.adoc index 5b142204088..26de2782853 100644 --- a/rules/S2384/java/rule.adoc +++ b/rules/S2384/java/rule.adoc @@ -12,22 +12,25 @@ This rule checks that private arrays, collections and Dates are not stored or re The rule violation is not reported for mutable values stored in private methods if no non-private methods directly passes a mutable parameter to them. +Similarly, the rule violation is not reported for mutable values returned by a private getter, when the getter's value is not returned directly by a non-proviate method. + + === Noncompliant code example [source,java] ---- class A { - private String [] strings; + private String[] strings; public A () { strings = new String[]{"first", "second"}; } - public String [] getStrings() { + public String[] getStrings() { return strings; // Noncompliant } - public void setStrings(String [] strings) { + public void setStrings(String[] strings) { this.strings = strings; // Noncompliant } } @@ -54,15 +57,19 @@ class A { strings = new String[]{"first", "second"}; } - public String [] getStrings() { - return strings.clone(); + public String[] getStrings() { + return getStringsInternal().clone(); + } + + private String[] getStringsInternal() { + return strings; } - private void setStringsInternal(String [] strings) { + private void setStringsInternal(String[] strings) { this.strings = strings; } - public void setStrings(String [] strings) { + public void setStrings(String[] strings) { this.strings = strings.clone(); } }