Skip to content
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

filter mutations to defensive unmodifiable collection returns #1310

Merged
merged 2 commits into from
Feb 13, 2024

Conversation

hcoles
Copy link
Owner

@hcoles hcoles commented Feb 12, 2024

It's common practice in some teams to wrap mutable collections with unmodifiable wrappers before returning them from a class to prevent accidental modification of state.

class Foo {
   private final List<String> field;

   ...

   List<String> doStuff() {
     return Collections.unmodifiableList(field);
   }
}

Although the presence of the wrapper can be easily detected by a test that attempts to modify the state of the list, some teams do not wish to specify this behaviour.

This change provides the option to filter out mutations implementing this defensive coding pattern.

The filter is disabled by default, but can be enabled with the +funmodifiablecollection feature string.

@hcoles hcoles merged commit 7be0282 into master Feb 13, 2024
7 checks passed
@romani
Copy link
Contributor

romani commented Feb 14, 2024

@hcoles , is this a way to activate this filter https://pitest.org/quickstart/maven/#features ?

@hcoles
Copy link
Owner Author

hcoles commented Feb 14, 2024

Yes, the filtering is controlled by a feature named funmodifiablecollection. It can be activated by adding +funmodifiablecollection in that section of the maven config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants