Skip to content

SONARJAVA-4473 Update exception for S2384 to correspond to update in implementation #5063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions rules/S2384/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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();
}
}
Expand Down
Loading